var rmonthi = -1;
var rdayi   = -1;

function toMonthString (i) {
   if (i == 0)       return ("January");
   else if (i == 1)  return ("February");
   else if (i == 2)  return ("March");
   else if (i == 3)  return ("April");
   else if (i == 4)  return ("May");
   else if (i == 5)  return ("June");
   else if (i == 6)  return ("July");
   else if (i == 7)  return ("August");
   else if (i == 8)  return ("September");
   else if (i == 9)  return ("October");
   else if (i == 10) return ("November");
   else              return ("December");
   }

function toYearMonthString (y, m) {
   if (m < 10)
      return (String(y) + "0" + String(m));
   else
      return (String(y) + String(m));
   }

function onLoad () {
   document.forms["abstravel"].CITY_NAME.value        = "";
  
   today    = new Date();
   
   depart   = new Date(today.valueOf() + (24 * 60 * 60 * 1000));
   depart_y = depart.getFullYear();
   depart_m = depart.getMonth();
   depart_d = depart.getDate();
   
   rtn      = new Date(today.valueOf() + (48 * 60 * 60 * 1000));
   rtn_y    = rtn.getFullYear();
   rtn_m    = rtn.getMonth();
   rtn_d    = rtn.getDate();
   
   for (i = 0; i < 10; ++i) {
      document.forms["abstravel"].IN_MONTH.options[i].text  = toMonthString((depart_m + i) % 12) + " " + Math.floor(depart_y + ((depart_m + i) / 12));
      document.forms["abstravel"].IN_MONTH.options[i].value = toYearMonthString(Math.floor(depart_y + ((depart_m + i) / 12)),(((depart_m + i) % 12) + 1));
      document.forms["abstravel"].OUT_MONTH.options[i].text  = toMonthString((rtn_m + i) % 12) + " " + Math.floor(rtn_y + ((rtn_m + i) / 12));
      document.forms["abstravel"].OUT_MONTH.options[i].value = toYearMonthString(Math.floor(rtn_y + ((rtn_m + i) / 12)),(((rtn_m + i) % 12) + 1));
      }
      
   document.forms["abstravel"].IN_MONTH.options[0].selected          = true;
   document.forms["abstravel"].IN_DAY.options[depart_d - 1].selected = true;
   document.forms["abstravel"].OUT_MONTH.options[0].selected          = true;
   document.forms["abstravel"].OUT_DAY.options[rtn_d - 1].selected    = true;
   
   rmonthi = 0;
   rdayi   = rtn_d - 1;
   
            
   document.forms["abstravel"].CITY_NAME.focus();
   }

function dateValidator (year, month, day) {
   leap_year = parseInt(year) % 4;
   comp1     = ((month == "02") || (month == "04") || (month == "06") || (month == "09") || (month == "11"));
   comp2     = (day == "31");

   if ((month == "02") && ((day == "30") || ((day == "29") && leap_year != 0)))
      return (false);
   
   if (comp1 && comp2)
      return (false);
      
   return (true);
   }

function isNum(num){
numlength = num.length
for(var i = 0;i < numlength;i ++){
cmp = "0123456789"
tst = num.substring(i, i + 1)
if(cmp.indexOf(tst) < 0)
return true
}
return false
}


function formValidator (theForm) {
 checkstr = "";

if (theForm.CITY_NAME.value.length == 0) {
      alert("Choose a city to proceed in the \"Destination\" field.");
      theForm.CITY_NAME.focus();
      return (false);
      }
      
   if (theForm.IN_MONTH.selectedIndex < 0) {
      alert("Please select a \"Check-In Month\"");
      theForm.IN_MONTH.focus();
      return (false);
      }
      
   if (theForm.IN_DAY.selectedIndex < 0) {
      alert("Please select a \"Check-In Day\"");
      theForm.IN_DAY.focus();
      return (false);
      }
      
       
   year  = (theForm.IN_MONTH.options[theForm.IN_MONTH.selectedIndex].value).substr(0,4);
   month = (theForm.IN_MONTH.options[theForm.IN_MONTH.selectedIndex].value).substr(4,2);
   day   = theForm.IN_DAY.options[theForm.IN_DAY.selectedIndex].value;
   if (!dateValidator(year,month, day)) {
      alert("Invalid \"Arrival Date\"");
      theForm.IN_DAY.focus();
      return (false);
      }
      
         
      if (theForm.OUT_DAY.selectedIndex < 0 || theForm.OUT_DAY.selectedIndex >= (theForm.OUT_DAY.options.length - 1)) {
         alert("Please select a \"Check-Out Day\"");
         theForm.OUT_DAY.focus();
         return (false);
         }
      
         
      year  = (theForm.OUT_MONTH.options[theForm.OUT_MONTH.selectedIndex].value).substr(0,4);
      month = (theForm.OUT_MONTH.options[theForm.OUT_MONTH.selectedIndex].value).substr(4,2);
     if (!dateValidator(year,month,day)) {
         alert("Invalid \"Check-Out Date\"");
         theForm.OUT_DAY.focus();
         return (false);
         }
         
      if ((theForm.IN_MONTH.options[theForm.IN_MONTH.selectedIndex].value +
           theForm.IN_DAY.options[theForm.IN_DAY.selectedIndex].value) >=
          (theForm.OUT_MONTH.options[theForm.OUT_MONTH.selectedIndex].value +
           theForm.OUT_DAY.options[theForm.OUT_DAY.selectedIndex].value)) {
         alert("\"Check-out date\" should be later than \"check-in date\" [" +
               (theForm.IN_MONTH.options[theForm.IN_MONTH.selectedIndex].value +
                theForm.IN_DAY.options[theForm.IN_DAY.selectedIndex].value) + "," +
               (theForm.OUT_MONTH.options[theForm.OUT_MONTH.selectedIndex].value +
                theForm.OUT_DAY.options[theForm.OUT_DAY.selectedIndex].value) + "]");
         theForm.OUT_MONTH.focus();
     return (false);
      }
}

