﻿// JScript File

function imgPreload() {

  // Don't bother if there's no document.images
  if (document.images) {
    if (typeof document.WM == 'undefined'){
      document.WM = new Object();
    }
    document.WM.loadedImages = new Array();
    // Loop through all the arguments.
    var argLength = imgPreload.arguments.length;
    for(arg=0;arg<argLength;arg++) {
      // For each arg, create a new image.
      document.WM.loadedImages[arg] = new Image();
      // Then set the source of that image to the current argument.
      document.WM.loadedImages[arg].src = imgPreload.arguments[arg];
    }
  }
}

function imgSwap(dObj, dSrc){
  if (document.images) {
     if (dObj && dObj.src) {
        dObj.src = dSrc;
     }
  }
}

function doMainLoad() {
   //preload images for site-wide nav
   imgPreload('/images/SearchNowOver.gif');
   //in case of refresh, check panel mode
   checkCount();
}

function checkCount() {
   var iCount = document.pageForm.adults.value + document.pageForm.children.value + document.pageForm.seniors.value;
   var secondHalf = document.getElementById('extendedForm');
   if (iCount>0) {
      secondHalf.style.display = 'block';
   }
   else {
      secondHalf.style.display = 'none';
   }
}

function checkSwitch(from,val,to)
{
    if (from.value.length>=val) {
       to.focus();
    }
}

function checkDate(day,month,year) {
   if (day>30 && (month=='February' || month=='April' || month=='June' || month=='September' || month=='November')) {
      return(false);
   }
   else if (day>29 && month=='February') {
      return(false);
   }
   else if (day>28 && month=='February') {
      if (!(year%4)==0) {
         // no division by 4 means can't be a leap year
         return(false);
      }
      else if ((year%100)==0 && !(year%400)==0) {
         // every 100 years the leap year is canceled, but every 400 years the cancellation is canceled too
         return(false);
      }
   }
   return(true);
}

function doCheckForm() 
{
   var numericCheck = /^\d*$/;
   var emailCheck = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
   if (document.pageForm.departureMonth.value=="0") {
      alert("You must specify the month of your intended departure.");
      document.pageForm.departureMonth.focus();
      return(false);   
   }
   if (document.pageForm.departureDay.value=="0") {
      alert("You must specify the day of your intended departure.");
      document.pageForm.departureDay.focus();
      return(false);   
   }
   if (document.pageForm.departureYear.value=="0") {
      alert("You must specify the year of your intended departure.");
      document.pageForm.departureYear.focus();
      return(false);   
   }
   if (!checkDate(document.pageForm.departureDay.value,document.pageForm.departureMonth.value,document.pageForm.departureYear.value)) 
   {
      alert("Your chosen date of departure does not exist.");
      document.pageForm.departureDay.focus();
      return(false);   
   }
   if (document.pageForm.returnMonth.value=="0") {
      alert("You must specify the month of your intended return.");
      document.pageForm.departureMonth.focus();
      return(false);   
   }
   if (document.pageForm.returnDay.value=="0") {
      alert("You must specify the day of your intended return.");
      document.pageForm.departureDay.focus();
      return(false);   
   }
   if (document.pageForm.returnYear.value=="0") {
      alert("You must specify the year of your intended return.");
      document.pageForm.departureYear.focus();
      return(false);   
   }
   if (!checkDate(document.pageForm.returnDay.value,document.pageForm.returnMonth.value,document.pageForm.returnYear.value)) 
   {
      alert("Your chosen date of return does not exist.");
      document.pageForm.returnDay.focus();
      return(false);   
   }
   if (document.pageForm.departureState.value=="0") {
      alert("You must choose the state you intend to depart from.");
      document.pageForm.departureState.focus();
      return(false);   
   }
   if (document.pageForm.departureCity.value=="") {
      alert("You must enter the name of the city you intend to depart from.");
      document.pageForm.departureCity.focus();
      return(false);
   }
   if (document.pageForm.destination.value=="") {
      alert("You must enter the destination you intend to travel to.");
      document.pageForm.destination.focus();
      return(false);
   }
   // count total travelers
   var iCount = document.pageForm.adults.value + document.pageForm.children.value + document.pageForm.seniors.value;
   if (iCount<=0) {
      alert("You must let us know the number of adults, children and seniors who will be traveling.");
      document.pageForm.adults.focus();
      return(false);
   }   
   // count total non-child travelers
   var iCount = document.pageForm.adults.value + document.pageForm.seniors.value;
   if (iCount<=0) {
      alert("You must have at least one adult or senior traveling.");
      document.pageForm.adults.focus();
      return(false);
   }   
   if (document.pageForm.cabinType.value=="0") {
      alert("You must choose the type of cabin that you would like.");
      document.pageForm.cabinType.focus();
      return(false);
   }
   if (document.pageForm.cabinNumber.value=="") {
      alert("You must choose the number of cabins that you would like.");
      document.pageForm.cabinNumber.focus();
      return(false);
   }
   if (!document.pageForm.cabinNumber.value.match(numericCheck)) {
      alert("You must enter only numbers for the number of cabins.");
      document.pageForm.cabinNumber.focus();
      return(false);
   }
   if (document.pageForm.emailAddress.value=="") {
      alert("You must enter an email address.");
      document.pageForm.emailAddress.focus();
      return(false);
   }
   if (!document.pageForm.emailAddress.value.match(emailCheck)) {
      alert("Your email address format is invalid.");
      document.pageForm.emailAddress.focus();
      return(false);
   }   
   if (document.pageForm.firstName.value=="") {
      alert("You must enter your first name.");
      document.pageForm.firstName.focus();
      return(false);
   }
   if (document.pageForm.lastName.value=="") {
      alert("You must enter your last name.");
      document.pageForm.lastName.focus();
      return(false);
   }   
   if (document.pageForm.dayphone1.value=="" || document.pageForm.dayphone2.value=="" || document.pageForm.dayphone3.value=="") {
      alert("You must enter your daytime telephone number.");
      document.pageForm.dayphone1.focus();
      return(false);
   }
   if (document.pageForm.dayphone1.value.length!=3 || document.pageForm.dayphone2.value.length!=3 || document.pageForm.dayphone3.value.length!=4) {
      alert("Your daytime telephone number is invalid.");
      document.pageForm.dayphone1.focus();
      return(false);
   }
   if (!document.pageForm.dayphone1.value.match(numericCheck) || !document.pageForm.dayphone2.value.match(numericCheck) || !document.pageForm.dayphone3.value.match(numericCheck)) {
      alert("Please enter only numbers for the daytime telephone number.");
      document.pageForm.dayphone1.focus();
      return(false);
   }
   if (document.pageForm.eveningphone1.value!="" || document.pageForm.eveningphone2.value!="" || document.pageForm.eveningphone3.value!="") {
      // optional, validate if found
      if (document.pageForm.eveningphone1.value.length!=3 || document.pageForm.eveningphone2.value.length!=3 || document.pageForm.eveningphone3.value.length!=4) {
         alert("Your evening telephone number is invalid.");
         document.pageForm.eveningphone1.focus();
         return(false);
      }
      if (!document.pageForm.eveningphone1.value.match(numericCheck) || !document.pageForm.eveningphone2.value.match(numericCheck) || !document.pageForm.eveningphone3.value.match(numericCheck)) {
         alert("Please enter only numbers for the evening telephone number.");
         document.pageForm.eveningphone1.focus();
         return(false);
      }   
   }
   return(true);
}
