/*This function will test to make sure a valid

date has been entered.  If it is valid it will 

return "true".  If not, it will return "false".*/

function validateDate(month, day, year) {



   //if no year is given assume current year

   //unless date has passed, then assume next year

   if (year == null) {

      time = new Date();

      today_year = time.getYear(); 

      today_month = time.getMonth();

      today_day = time.getDay();

      if ((month > today_month) || ((month == today_month) && (day >= today_day)))

         year = today_year + 1900;

      else  year = today_year + 1901;

   }



   if ((month == null) || (day == null) || (year == null))

     return false;

   

   if (navigator.appName.indexOf("WebTV")!=-1) {

      ;//do nothing for now

   }

   else {

      var numDays;

      switch(month) {

	 case 1:

	 case 3:

	 case 5:

	 case 7:

	 case 8:

	 case 10:

	 case 12:

	    numDays = 31;

	    break;

	 case 4:

	 case 6:

	 case 9:

	 case 11:

	    numDays = 30;

	    break;

	 case 2:

	    numDays = 28;

	    break;

      }



      //Check for leap year

      if ((month == 2) && (day == 29)) {

        if (year % 100 == 0) {

          if (year % 400 != 0) 

            return false;

        }

        else {

          if (year % 4 != 0) 

            return false;

        }

      } else if (day > numDays) {

         //Compare days enter to maximum number of days in the given month

         return false;

      }



      return true;

   }

}



function validateFormDate(form_flds) {

   ix = form_flds.month.options.selectedIndex;

   ix2 = form_flds.day.options.selectedIndex;

   month = parseInt(form_flds.month.options[ix].value);

   day = parseInt(form_flds.day.options[ix2].value);



   dates_rqd = false;

   if (form_flds.dates_rqd)

      dates_rqd = form_flds.dates_rqd.value;



   if (navigator.appName.indexOf("WebTV")!=-1) {

      //do nothing for now

   }

   else {



      if (!datesCheck(month,day,dates_rqd))

        return false;



      return true;

   }



}



function validateFormDates(form_flds) {

   ix = form_flds.month.options.selectedIndex;

   ix2 = form_flds.day.options.selectedIndex;

   month = parseInt(form_flds.month.options[ix].value);

   day = parseInt(form_flds.day.options[ix2].value);

   ix = form_flds.depart_month.options.selectedIndex;

   ix2 = form_flds.depart_day.options.selectedIndex;

   depart_month = parseInt(form_flds.depart_month.options[ix].value);

   depart_day = parseInt(form_flds.depart_day.options[ix2].value);



   dates_rqd = false;

   if (form_flds.dates_rqd)

      dates_rqd = form_flds.dates_rqd.value;



   if (navigator.appName.indexOf("WebTV")!=-1) {

      //do nothing for now

   }

   else {



      if (!datesCheck(month,day,dates_rqd))

        return false;

      if (!datesCheck(depart_month,depart_day,dates_rqd))

        return false;

      if (month+day == depart_month+depart_day) {

        changeCheckOut();

      }



      return true;

   }



}



function datesCheck (month, day, dates_rqd) {



   if (dates_rqd) {

      if (day < 0 || month < 0)  {

         alert("Please enter both your Check-in AND Check-out dates");

	 return false;

      }

   }



   if ((day > 0) && (month < 0))  {

     alert("You have entered a partial date.\n"

	+ "Please enter a month or select 'Day' to clear the day.");

     return false;

   }



   if ((month > 0) && (day < 0))  {

     alert("You have entered a partial date.\n"

	+ "Please enter a day or select 'Month' to clear the month.");

     return false;

   }



   var today = new Date();

   var today_year = today.getFullYear();

   var today_month = today.getMonth();

   var today_day = today.getDay();



   var year = ((month > today_month) || ((month == today_month) 

      && (day >= today_day))) ? today_year : today_year + 1;

   

   if (!validateDate(month,day,year)) {

     alert("Invalid date:\nNot that many days in " + monthNames(month));

     return false;

   }



   return true;

}



function changeCheckOut() {

   if ((document.datesform.depart_month.selectedIndex == 0) ||

      (document.datesform.depart_month.selectedIndex < document.datesform.month.selectedIndex) ||

      ((document.datesform.depart_month.selectedIndex == document.datesform.month.selectedIndex) &&

      (document.datesform.depart_day.selectedIndex <= document.datesform.day.selectedIndex))) {

      

      if(document.datesform.month.selectedIndex != 0) {

	 if (document.datesform.month.selectedIndex == 2)  {

             var time = new Date();

             var today_year = time.getYear() + 1900; 

             var today_month = time.getMonth();

             //if the current month is past February, the following year must be assumed

             if(today_month > 2)

                today_year = today_year + 1;



             if (document.datesform.day.selectedIndex == 28 ) { 

	        if ( ( (today_year%4==0)&&(today_year%100 != 0) ) || (today_year%400==0) ) { // leap year

	           document.datesform.depart_day.selectedIndex = 29; 

                }

                else {

	          document.datesform.depart_day.selectedIndex = 1; 

	          document.datesform.depart_month.selectedIndex = 3; 

                } 

             }

             else if (document.datesform.day.selectedIndex > 28 ) { 

	       document.datesform.depart_day.selectedIndex = 1; 

	       document.datesform.depart_month.selectedIndex = 3; 

             }

             else { 

	        document.datesform.depart_day.selectedIndex = document.datesform.day.selectedIndex + 1;

	        document.datesform.depart_month.selectedIndex = document.datesform.month.selectedIndex;

             }

         }

	 else if ((document.datesform.day.selectedIndex == 30 ) &&

	    ((document.datesform.month.selectedIndex == 4) ||

	    (document.datesform.month.selectedIndex == 6)  ||

	    (document.datesform.month.selectedIndex == 9) ||

	    (document.datesform.month.selectedIndex == 11))) {

	       document.datesform.depart_day.selectedIndex = 1;

	       document.datesform.depart_month.selectedIndex = document.datesform.month.selectedIndex + 1;

	 }

         else if (document.datesform.day.selectedIndex > 30 ) {

	    document.datesform.depart_day.selectedIndex = 1;

	    if (document.datesform.month.selectedIndex == 12){ //move to January

	       document.datesform.depart_month.selectedIndex = 1;

            }

	    else  {

	       document.datesform.depart_month.selectedIndex = document.datesform.month.selectedIndex + 1;

            }

	 }

	 else {

	    document.datesform.depart_day.selectedIndex = document.datesform.day.selectedIndex + 1;

	    document.datesform.depart_month.selectedIndex = document.datesform.month.selectedIndex;

	 }

      }

   }



}



function monthNames(month) {

   var name;

   switch(month) {

      case 2:

         name = "February";

	 break;

      case 4:

         name = "April";

	 break;

      case 6:

         name = "June";

	 break;

      case 9:

         name = "September";

	 break;

      case 11:

         name = "November";

	 break;

   }

   return name;

}


