//
//   res_error.js
//
//   Written By Scott Walton   18/08/05
//   Modified by Scott Walton 31/12/2007 - adjust to cater correctly to leap year date check
//
//   Purpose : To contain the javascript validations for the 
//          reservation process
//
//

function dateOK(Y, M, D) { 
  return D>0 && (D<=[31,28,31,30,31,30,31,31,30,31,30,31][M] ||
    D==29 && M==1 && Y%4==0 && (Y%100>0 || Y%400==0) );
}


function checkstep1 () {
   v_errormsg="";
   v_errorstat = false;
   v_puloc="";
   v_dmonth="";

   for (var i = 0; i<document.step1.PULOC.options.length; i++) {
     if (document.step1.PULOC.options[i].selected==true) {
        v_puloc=document.step1.PULOC.options[i].value;
        break;
     }
   }    

   if (v_puloc=="") {
      v_errorstat = true;
      v_errormsg = "A pickup location must be selected";
   }

   for (var i = 0; i<document.step1.DODAY.options.length; i++) {
     if (document.step1.DODAY.options[i].selected==true) {
        v_dday=parseFloat(document.step1.DODAY.options[i].text);
        break;
     }
   }

   for (var i = 0; i<document.step1.DOMTHYR.options.length; i++) {
     if (document.step1.DOMTHYR.options[i].selected==true) {
        v_dmonth=document.step1.DOMTHYR.options[i].value;
        break;
     }
   }      
        var word=v_dmonth.split("-");
        var v_dyear=parseFloat(word[0]);
        var  v_dmonth=parseFloat(word[1]) -1; 
        
   //  Check that the entered fields make a valid date 
   if (!dateOK(v_dyear, v_dmonth, v_dday)) {
      v_errorstat = true;
      v_errormsg = "Dropoff date is not a valid date";
   }
   
      for (var i = 0; i<document.step1.PUDAY.options.length; i++) {
     if (document.step1.PUDAY.options[i].selected==true) {
        v_pday=parseFloat(document.step1.PUDAY.options[i].text);
        break;
     }
   }


   for (var i = 0; i<document.step1.PUMTHYR.options.length; i++) {
     if (document.step1.PUMTHYR.options[i].selected==true) {
        v_pmonth=document.step1.PUMTHYR.options[i].value;
        break;
     }
   }      
   var word2=v_pmonth.split("-");
   var v_pyear=parseFloat(word2[0]);
   var  v_pmonth=parseFloat(word2[1]) -1;     


   //  Check that the entered fields make a valid date 
   if (!dateOK(v_pyear, v_pmonth, v_pday)) {
      v_errorstat = true;
      v_errormsg = "Pickup date is not a valid date";
   }
   
   
   //  Dates Cannot be in the past
   
   var v_pdate= new Date(v_pyear, v_pmonth, v_pday);
   var v_ddate= new Date(v_dyear, v_dmonth, v_dday);

   var v_now = new Date();
   v_now.setDate(v_now.getDate());

   if (v_now>v_pdate) {
      v_errorstat = true;
      v_errormsg = "Cannot book a vehicle with a past date!";
   }

   if (v_pdate>v_ddate) {
      v_errorstat = true;
      v_errormsg = "Pickup date must be before dropoff date!";
   }

   //   return the message and status
   if (v_errorstat==true) {
      alert (v_errormsg);
      return false;
   } else {
     return true;
   }

} // END of checkstep1


//
//   Any online validation is done here for step3 input
//
function checkstep3 () {
   v_errmsg="";
   v_errorstat = false;

   if (document.step3.CCEXPIRY.value == "") {
       v_errorstat=true;
       v_errormsg="Credit Card Expiry Date cannot be left blank";
   } 
   if (document.step3.CCNUM.value == "") {
       v_errorstat=true;
       v_errormsg="Credit Card number cannot be left blank";
   } 
   if (document.step3.PHONENO.value == "") {
       v_errorstat=true;
       v_errormsg="Phone Number cannot be left blank";
   } 
   if (document.step3.EMAIL.value == "") {
       v_errorstat=true;
       v_errormsg="Email cannot be left blank";
   } 
   if (document.step3.LASTNAME.value == "") {
       v_errorstat=true;
       v_errormsg="Last Name cannot be left blank";
   } 
   if (document.step3.FIRSTNAME.value == "") {
       v_errorstat=true;
       v_errormsg="First Name cannot be left blank";
   } 

   //   return the message and status
   if (v_errorstat==true) {
      alert (v_errormsg);
      return false;
   } else {
     return true;
   }

} // END of step3 


//   END of res_error.js