//
//   res_error.js
//
//   Written By Scott Walton   18/08/05
//
//   Purpose : To contain the javascript validations for the 
//          reservation process
//
//   Modified by Owen O Doherty 31/03/09 - Added popout changing styles
//

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=php_today;
   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

function checkstep1_small() {
   v_errormsg="";
   v_errorstat = false;
   v_puloc="";
   v_dmonth="";
   
   // check pick up date has a value
   if (document.step1_small.PUDATE && document.step1_small.PUDATE.value == "") {
      v_errorstat = true;
      v_errormsg = "Must enter a Pick Up Date";
   }
   
   // check pick up date has a value
   if (document.step1_small.DODATE && document.step1_small.DODATE.value == "") {
      v_errorstat = true;
      v_errormsg = "Must enter a Drop Off Date";
   }
   
   //Check that the PU date is not after the DO date
   var puDate = new Date();
   var pudate_str = document.getElementById('PUDATE').value;
   var pudate_array = pudate_str.split('-');
   
   puDate.setFullYear(pudate_array[2],pudate_array[1]-1, pudate_array[0])
   
   var doDate = new Date();
   var dodate_str = document.getElementById('DODATE').value;
   var dodate_array = dodate_str.split('-');
   doDate.setFullYear(dodate_array[2],dodate_array[1]-1, dodate_array[0])
    
   if (puDate>doDate) {
      v_errorstat = true;
      v_errormsg = "Pick Up Date cannot be after Drop Off Date";
   }
   
   if (document.step1_small.PULOC[document.step1_small.PULOC.selectedIndex].value == "") {
      v_errorstat = true;
      v_errormsg = "Please choose a Pick Up Location";
   }
   


   // return the message and status
   if (v_errorstat == true) {
      alert (v_errormsg);
      return false;
   } else {
      return true;
   }   



} // END of checkstep1_small

//
//   Any online validation is done here for step3 input
//
function checkstep3 () {
   v_errmsg="";
   v_errorstat = false;


   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 

//
//   Added these below functions from main_function.js to combine into one
//

 function popitup(url) {
    newwindow=window.open(url,'carwin','height=100,width=800, resizable=yes, scrollbars=yes');
    if (window.focus) {
         newwindow.focus()
    }
    return false; 
} 
 function popexcess(url) {
    newwindow=window.open(url,'excesswin','height=500,width=500,scrollbars=yes');
    if (window.focus) {
         newwindow.focus()
    }
    return false; 
 } 

 function poplarger(url) {
    newwindow=window.open(url,'info','height=500,width=600,scrollbars=yes');
    if (window.focus) {
         newwindow.focus()
    }
    return false; 
 }


