var today = new Date(new Date().valueOf());

function setDates()
{
  // This will populate the date dropdowns with today and tomarrow's values.
  theForm = document.mainForm;
  var yearOffset = parseInt(theForm.inyear.options[0].value,10);
  // getDate

  // First let's see if there is a value set in the user's cookie:
  var cookieIndate = getCookie("InDate");
  var cookieOutdate = getCookie("OutDate");

  var myInDate;
  var myOutDate;
  
  if(cookieIndate && cookieOutdate)
  {
    // Parse out the dates from the cookie.
    var inDatePieces = cookieIndate.split("-");
    myInDate = new Date();
    myInDate.setYear(inDatePieces[0]);
    myInDate.setMonth(inDatePieces[1]-1);
    myInDate.setDate(inDatePieces[2]);

    var outDatePieces = cookieOutdate.split("-");
    myOutDate = new Date();
    myOutDate.setYear(outDatePieces[0]);
    myOutDate.setMonth(outDatePieces[1]-1);
    myOutDate.setDate(outDatePieces[2]);

    // Now set the other parameters if available:
    setPrefs(theForm);
  }
  else
  {
    // Let's calculate the dates:
    var theDates = getDates();
    myInDate = theDates[0];
    myOutDate = theDates[1];
  }

  var inMonth=myInDate.getMonth();
  var inDay=myInDate.getDate();
  var inYear=y2k(myInDate.getYear());

  var outMonth=myOutDate.getMonth();
  var outDay=myOutDate.getDate();
  var outYear=y2k(myOutDate.getYear());

  // 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-1)].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;
  var 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(inMonth < baseMonth && inYear <= baseYear)
  {
    inYear++;
    theForm.inyear.options[inYear - yearOffset].selected = true;
  }
  
  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;
  

/* This is causing problems with some people
  if((inMonth < baseMonth) || (inMonth == baseMonth && inDay < baseDay))
  {
    theForm.inyear.options[((baseYear-yearOffset)+1)].selected=true;
    inYear = baseYear+1;
  }
*/
  var currOutMonth = parseInt(theForm.outmonth.options[theForm.outmonth.selectedIndex].value,10)-1;
  var currOutDay = parseInt(theForm.outday.options[theForm.outday.selectedIndex].value,10);
  var currOutYear = parseInt(theForm.outyear.options[theForm.outyear.selectedIndex].value,10);
  
  if((currOutYear < inYear) || (currOutYear == inYear && currOutMonth < inMonth) ||
    (currOutYear == inYear && currOutMonth == inMonth && currOutDay <= inDay))
  {
    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;
  }
}

function checkOutDate()
{
  theForm = document.mainForm;
  var 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;
}

function getDates()
{
  var targetInDay = 5;
  var lengthOfStay = 2;
  var interval = 86400000;

  var todayMS = today.getTime();
  var thisDow = today.getUTCDay();
  
  var diff = targetInDay - thisDow;
  var inDateModifyer = 0;

  if(diff > 0)
  {
    inDateModifyer = diff;
  }
  else if(diff < 0)
  {
    inDateModifyer = 7 + diff;
  }
  else
  {
    inDateModifyer = 1;
    lengthOfStay = 1;
  }

  var outDateModifyer = inDateModifyer + lengthOfStay;
  var nextInDate = new Date(todayMS + (inDateModifyer * interval));
  var nextOutDate = new Date(todayMS + ((inDateModifyer + lengthOfStay) * interval));

  var theDates = new Array(nextInDate,nextOutDate);

  return theDates;
}

function getCookie(name)
{
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i=0;
  while(i<clen)
  {
    var j = i + alen;
    if(document.cookie.substring(i,j) == arg)
    {
      return getCookieVal(j);
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if(i == 0) { break; }
  }
}

function getCookieVal(offset)
{
  var endstr = document.cookie.indexOf (";", offset);
  if(endstr == -1) { endstr = document.cookie.length; }
  return unescape(document.cookie.substring(offset, endstr));
}

function setPrefs(theForm)
{
  var numAdults = getCookie("NumberOfAdults");
  var numRooms = getCookie("NumberOfRooms");
  var numKids = getCookie("NumberOfChildren");

  if(theForm.numadults && numAdults) { theForm.numadults.options[(numAdults-1)].selected=true; }
  if(theForm.numrooms && numRooms) { theForm.numrooms.options[(numRooms-1)].selected=true; }
  if(theForm.numchildren && numKids) { theForm.numchildren.options[numKids].selected=true; }
}


