var today = new Date(new Date().valueOf());

function setDates()
{
  // This will populate the date dropdowns with today and tomorrow's values.
  theForm = document.mainForm;
  yearOffset = parseInt(theForm.inyear.options[0].value,10);
  // getDate
  var tomorrow = new Date(new Date().valueOf() + (24*60*60*1000));
  var inMonth=tomorrow.getMonth();
  var inDay=tomorrow.getDate();
  var inYear=y2k(tomorrow.getYear());

  if(isLeapYear(inYear)) { var days = new Array(31,29,31,30,31,30,31,31,30,31,30,31); }
  else { var days = new Array(31,28,31,30,31,30,31,31,30,31,30,31); }

  outMonth=inMonth;
  outDay = inDay%days[inMonth];
  outYear=inYear;
  if(outDay == 0) { outMonth = (inMonth + 1) % 12; }
  if(outDay == 0 && inMonth == 11) { outYear++; }

  // Now set the select boxes to the appropriate dates:
  theForm.inmonth.options[inMonth].selected=true;
  theForm.inday.options[(inDay-1)].selected=true;
  theForm.inyear.options[(inYear-yearOffset)].selected=true;
  theForm.outmonth.options[outMonth].selected=true;
  theForm.outday.options[outDay].selected=true;
  theForm.outyear.options[(outYear-yearOffset)].selected=true;
}
// quadYear
function y2k(number){return (number < 1000) ? number + 1900 : number;}

function isLeapYear(yr)
{
  if (((yr % 4 == 0) && (yr % 100 != 0)) || (yr % 400 == 0)) { return true; }
  else { return false; }
}
  
function changeDates()
{
  theForm = document.mainForm;
  yearOffset = parseInt(theForm.inyear.options[0].value,10);
  var inMonth=parseInt(theForm.inmonth.options[theForm.inmonth.selectedIndex].value,10)-1;
  var inYear=parseInt(theForm.inyear.options[theForm.inyear.selectedIndex].value,10);
  var baseMonth=today.getMonth();
  var baseDay=today.getDate();
  var baseYear=y2k(today.getYear());

  if(isLeapYear(inYear)) { var days = new Array(31,29,31,30,31,30,31,31,30,31,30,31); }
  else { var days = new Array(31,28,31,30,31,30,31,31,30,31,30,31); }

  var inDay=parseInt(theForm.inday.options[theForm.inday.selectedIndex].value,10);

  if(inDay >= days[inMonth]) { inDay = days[inMonth]; }
  else { inDay = inDay%days[inMonth]; }
  theForm.inday.options[inDay-1].selected=true;

  if((inMonth < baseMonth) || (inMonth == baseMonth && inDay < baseDay))
  {
    theForm.inyear.options[((baseYear-yearOffset)+1)].selected=true;
    inYear = baseYear+1;
  }

  outMonth=inMonth;
  outDay = inDay%days[inMonth];
  outYear=inYear;
  if(outDay == 0) { outMonth = (inMonth + 1) % 12; }
  if(outDay == 0 && inMonth == 11) { outYear++; }

  theForm.outmonth.options[outMonth].selected=true;
  theForm.outday.options[outDay].selected=true;
  theForm.outyear.options[(outYear-yearOffset)].selected=true;

//new added
theForm.m1.value = theForm.inmonth.value - 1;
theForm.m2.value = theForm.outmonth.value -1;
theForm.y1.value = theForm.inyear.value;
theForm.y2.value = theForm.outyear.value;


theForm.idate.value = ( theForm.inmonth.value + "/" + theForm.inday.value + "/" + theForm.inyear.value);
theForm.odate.value = ( theForm.outmonth.value + "/" + theForm.outday.value + "/" + theForm.outyear.value);

//theForm.idate.value = "";
//theForm.odate.value = "";

}

function checkOutDate()
{
  theForm = document.mainForm;
  yearOffset = parseInt(theForm.inyear.options[0].value,10);
  var outMonth=parseInt(theForm.outmonth.options[theForm.outmonth.selectedIndex].value,10)-1;
  var outYear=parseInt(theForm.outyear.options[theForm.outyear.selectedIndex].value,10);

  if(isLeapYear(outYear)) { var days = new Array(31,29,31,30,31,30,31,31,30,31,30,31); }
  else { var days = new Array(31,28,31,30,31,30,31,31,30,31,30,31); }

  var outDay=parseInt(theForm.outday.options[theForm.outday.selectedIndex].value,10);

  if(outDay >= days[outMonth]) { outDay = days[outMonth]; }
  else { outDay = outDay%days[outMonth]; }
  theForm.outday.options[outDay-1].selected=true;

// added new
theForm.m2.value = theForm.outmonth.value - 1;
theForm.y2.value = theForm.outyear.value;


theForm.odate.value = ( theForm.outmonth.value + "/" + theForm.outday.value + "/" + theForm.outyear.value);




//theForm.idate.value = "";
//theForm.odate.value = "";

}


function SplitDate1() {
var sd = document.mainForm.idate.value.split("\/");

// strip leading 0 from months 1 thru 9
if(sd[0]== "01")
{
document.mainForm.inmonth.value = "1";
document.mainForm.inmonth.options[0].selected=true;
}

if(sd[0]== "02")
{ 
document.mainForm.inmonth.value = "2";
document.mainForm.inmonth.options[1].selected=true;
}

if(sd[0]== "03")
{ 
document.mainForm.inmonth.value = "3";
document.mainForm.inmonth.options[2].selected=true;
}

if(sd[0]== "04")
{ 
document.mainForm.inmonth.value = "4";
document.mainForm.inmonth.options[3].selected=true;
}

if(sd[0]== "05")
{ 
document.mainForm.inmonth.value = "5";
document.mainForm.inmonth.options[4].selected=true;
}

if(sd[0]== "06")
{ 
document.mainForm.inmonth.value = "6";
document.mainForm.inmonth.options[5].selected=true;
}

if(sd[0]== "07")
{ 
document.mainForm.inmonth.value = "7";
document.mainForm.inmonth.options[6].selected=true;
}

if(sd[0]== "08")
{ 
document.mainForm.inmonth.value = "8";
document.mainForm.inmonth.options[7].selected=true;
}

if(sd[0]== "09")
{ 
document.mainForm.inmonth.value = "9";
document.mainForm.inmonth.options[8].selected=true;
}

if(sd[0]== "10")
{ 
document.mainForm.inmonth.value = "10";
document.mainForm.inmonth.options[9].selected=true;
}

if(sd[0]== "11")
{ 
document.mainForm.inmonth.value = "11";
document.mainForm.inmonth.options[10].selected=true;
}

if(sd[0]== "12")
{ 
document.mainForm.inmonth.value = "12";
document.mainForm.inmonth.options[11].selected=true;
}


// strip leading 0 from dates 1 thru 9

if(sd[1] == "01")
{
document.mainForm.inday.value = "1";
}
if(sd[1] == "02")
{
document.mainForm.inday.value = "2";
}
if(sd[1] == "03")
{
document.mainForm.inday.value = "3";
}
if(sd[1] == "04")
{
document.mainForm.inday.value = "4";
}
if(sd[1] == "05")
{
document.mainForm.inday.value = "5";
}
if(sd[1] == "06")
{
document.mainForm.inday.value = "6";
}
if(sd[1] == "07")
{
document.mainForm.inday.value = "7";
}
if(sd[1] == "08")
{
document.mainForm.inday.value = "8";
}
if(sd[1] == "09")
{
document.mainForm.inday.value = "9";
}
if(sd[1] >= "10")
{
document.mainForm.inday.value = sd[1];
}




// document.mainForm.inmonth.value = sd[0];
//document.mainForm.inday.value = sd[1];
document.mainForm.inyear.value = sd[2];

document.mainForm.y1.value = sd[2];

changeDates();

}



function SplitDate2() {
var sd = document.mainForm.odate.value.split("\/");

// strip leading 0 from months 1 thru 9
if(sd[0]== "01")
{
document.mainForm.outmonth.value = "1";
document.mainForm.outmonth.options[0].selected=true;
}

if(sd[0]== "02")
{ 
document.mainForm.outmonth.value = "2";
document.mainForm.outmonth.options[1].selected=true;
}

if(sd[0]== "03")
{ 
document.mainForm.outmonth.value = "3";
document.mainForm.outmonth.options[2].selected=true;
}

if(sd[0]== "04")
{ 
document.mainForm.outmonth.value = "4";
document.mainForm.outmonth.options[3].selected=true;
}

if(sd[0]== "05")
{ 
document.mainForm.outmonth.value = "5";
document.mainForm.outmonth.options[4].selected=true;
}

if(sd[0]== "06")
{ 
document.mainForm.outmonth.value = "6";
document.mainForm.outmonth.options[5].selected=true;
}

if(sd[0]== "07")
{ 
document.mainForm.outmonth.value = "7";
document.mainForm.outmonth.options[6].selected=true;
}

if(sd[0]== "08")
{ 
document.mainForm.outmonth.value = "8";
document.mainForm.outmonth.options[7].selected=true;
}

if(sd[0]== "09")
{ 
document.mainForm.outmonth.value = "9";
document.mainForm.outmonth.options[8].selected=true;
}

if(sd[0]== "10")
{ 
document.mainForm.outmonth.value = "10";
document.mainForm.outmonth.options[9].selected=true;
}

if(sd[0]== "11")
{ 
document.mainForm.outmonth.value = "11";
document.mainForm.outmonth.options[10].selected=true;
}

if(sd[0]== "12")
{ 
document.mainForm.outmonth.value = "12";
document.mainForm.outmonth.options[11].selected=true;
}


// strip leading 0 from dates 1 thru 9

if(sd[1] == "01")
{
document.mainForm.outday.value = "1";
}
if(sd[1] == "02")
{
document.mainForm.outday.value = "2";
}
if(sd[1] == "03")
{
document.mainForm.outday.value = "3";
}
if(sd[1] == "04")
{
document.mainForm.outday.value = "4";
}
if(sd[1] == "05")
{
document.mainForm.outday.value = "5";
}
if(sd[1] == "06")
{
document.mainForm.outday.value = "6";
}
if(sd[1] == "07")
{
document.mainForm.outday.value = "7";
}
if(sd[1] == "08")
{
document.mainForm.outday.value = "8";
}
if(sd[1] == "09")
{
document.mainForm.outday.value = "9";
}
if(sd[1] >= "10")
{
document.mainForm.outday.value = sd[1];
}




// document.mainForm.outmonth.value = sd[0];
//document.mainForm.outday.value = sd[1];
document.mainForm.outyear.value = sd[2];

document.mainForm.y2.value = sd[2];

checkOutDate();

}


