// ===================================================================
// Author: John Plaxton <john@availcheck.com>
// WWW: http://www.availcheck.com/
//
// NOTICE: You may NOT use this code for any purpose, commercial or
// private, without any permission from the author. 
//
// You may *NOT* re-distribute this code in any way. That means, you CANNOT
// include it in your product, or your web site, or any form.
// ===================================================================

//////////////////////////////////////////////////////////////////////////
function group_selector(grp, season){
// season H for high season between month 5 and 9
// group must be passed through as hidden field from form
// groups A - Z

// group object is (grp, c_name, c_image, days_1, days_2, days_3, days_4, days_5, days_6, days_7, days_8, extra_day)

 if (season == "H"){
    // put in your high season prices here
	if (grp == "A"){groupobject = new group("A", "FIAT SEICENTO","http://www.corfucarrental.com/images/seicento.jpg", 43.10, 86.20, 129.30, 147.60, 199.50, 239.40, 279.30, 319.20, 39.90);}
    if (grp == "AA"){groupobject = new group("AA", "DAEWOO MATIZ","http://www.corfucarrental.com/images/matiz.jpg", 43.10, 86.20, 129.30, 147.60, 199.50, 239.40, 279.30, 319.20, 39.90);}
	if (grp == "B"){groupobject = new group("B", "KIA PICANTO","http://www.corfucarrental.com/images/picanto.jpg", 43.65, 87.30, 130.95, 174.60, 204.40, 245.28, 286.16, 327.04, 40.88);}
    if (grp == "BB"){groupobject = new group("BB", "HYUNDAI ATOS","http://www.corfucarrental.com/images/atos.jpg", 43.65, 87.30, 130.95, 174.60, 204.40, 245.28, 286.16, 327.04, 40.88);}
	if (grp == "C"){groupobject = new group("C", "FIAT PUNTO","http://www.corfucarrental.com/images/punto.jpg", 48.15, 96.30, 144.45, 192.60, 226.90, 272.28, 317.66, 363.04, 45.38);}
	if (grp == "CC"){groupobject = new group("CC", "OPEL CORSA","http://www.corfucarrental.com/images/corsa.jpg", 48.15, 96.30, 144.45, 192.60, 226.90, 272.28, 317.66, 363.04, 45.38);}
	if (grp == "CCC"){groupobject = new group("CCC", "PEUGEOT 206","http://www.corfucarrental.com/images/peugeot.jpg", 48.15, 96.30, 144.45, 192.60, 226.90, 272.28, 317.66, 363.04, 45.38);}
	if (grp == "D"){groupobject = new group("D", "RENAULT THALIA","http://www.corfucarrental.com/images/thalia.jpg", 56.25, 112.50, 168.75, 225.00, 265.50, 318.60, 371.70, 424.80, 53.10);}
	if (grp == "DD"){groupobject = new group("DD", "HYUNDAI ACCENT","http://www.corfucarrental.com/images/accent.jpg", 56.25, 112.50, 168.75, 225.00, 265.50, 318.60, 371.70, 424.80, 53.10);}
	if (grp == "F"){groupobject = new group("F", "OPEL ZAFIRA","http://www.corfucarrental.com/images/zafira.jpg", 86.40, 172.80, 259.20, 345.60, 414.00, 496.80, 579.60, 662.40, 82.80);}
	if (grp == "FF"){groupobject = new group("FF", "FIAT DOBLO","http://www.corfucarrental.com/images/doblo.jpg", 86.40, 172.80, 259.20, 345.60, 414.00, 496.80, 579.60, 662.40, 82.80);}
	if (grp == "GG"){groupobject = new group("GG", "FIAT SCUDO","http://www.corfucarrental.com/images/scudo.jpg", 94.50, 189.00, 283.50, 378.00, 454.50, 545.40, 636.30, 727.20, 90.90);}
	if (grp == "G1"){groupobject = new group("G1", "HYUNDAI H1-9","http://www.corfucarrental.com/images/h1-9.jpg", 117.00, 234.00, 351.00, 468.00, 565.00, 678.00, 791.00, 904.00, 113.00);}
	if (grp == "J"){groupobject = new group("J", "DAEWOO MATIZ open","http://www.corfucarrental.com/images/matiz.jpg", 46.35, 92.70, 139.05, 185.40, 217.25, 260.70, 304.15, 347.60, 43.45);}
	if (grp == "K"){groupobject = new group("K", "RENAULT TWINGO open","http://www.corfucarrental.com/images/twingo.jpg", 49.05, 98.10, 147.15, 196.20, 231.40, 277.68, 323.96, 370.24, 46.28);}
	if (grp == "L"){groupobject = new group("L", "RENAULT MEGANE Cabrio 2.0","http://www.corfucarrental.com/images/megane1.jpg", 117.00, 234.00, 337.50, 450.00, 540.00, 648.00, 756.00, 864.00, 108.00);}
	if (grp == "S"){groupobject = new group("S", "SUZUKI JIMNY","http://www.corfucarrental.com/images/jimny.jpg", 59.85, 119.70, 179.50, 239.40, 283.50, 340.20, 396.90, 453.60, 56.70);}
	if (grp == "SS"){groupobject = new group("SS", "SUZUKI SAMURAI","http://www.corfucarrental.com/images/samurai.jpg", 59.85, 119.70, 179.50, 239.40, 283.50, 340.20, 396.90, 453.60, 56.70);}
	if (grp == "I"){groupobject = new group("I", "SUZUKI VITARA","http://www.corfucarrental.com/images/vitara.jpg", 70.65, 141.30, 211.95, 282.60, 334.90, 401.88, 468.86, 535.84, 66.98);}
    // etc etc 
  }
  else if (season == "L"){
    // put in your low season prices here 
	if (grp == "A"){groupobject = new group("A", "FIAT SEICENTO","http://www.corfucarrental.com/images/seicento.jpg", 36.90, 73.80, 110.70, 147.60, 168.75, 202.50, 236.25, 270.00, 33.75);}
    if (grp == "AA"){groupobject = new group("AA", "DAEWOO MATIZ","http://www.corfucarrental.com/images/matiz.jpg", 36.90, 73.80, 110.70, 147.60, 168.75, 202.50, 236.25, 270.00, 33.75);}
	if (grp == "B"){groupobject = new group("B", "KIA PICANTO","http://www.corfucarrental.com/images/picanto.jpg", 41.00, 82.00, 123.00, 164.00, 190.00, 228.00, 266.00, 304.00, 38.00);}
    if (grp == "BB"){groupobject = new group("BB", "HYUNDAI ATOS","http://www.corfucarrental.com/images/atos.jpg", 41.00, 82.00, 123.00, 164.00, 190.00, 228.00, 266.00, 304.00, 38.00);}
    if (grp == "C"){groupobject = new group("C", "FIAT PUNTO","http://www.corfucarrental.com/images/punto.jpg", 42.75, 85.50, 128.25, 171.00, 195.45, 234.52, 273.60, 312.70, 39.10);}
	if (grp == "CC"){groupobject = new group("CC", "OPEL CORSA","http://www.corfucarrental.com/images/corsa.jpg", 42.75, 85.50, 128.25, 171.00, 195.45, 234.52, 273.60, 312.70, 39.10);}
	if (grp == "CCC"){groupobject = new group("CCC", "PEUGEOT 206","http://www.corfucarrental.com/images/peugeot.jpg", 42.75, 85.50, 128.25, 171.00, 195.45, 234.52, 273.60, 312.70, 39.10);}
	if (grp == "D"){groupobject = new group("D", "RENAULT THALIA","http://www.corfucarrental.com/images/thalia.jpg", 49.05, 98.10, 147.15, 196.20, 229.50, 275.40, 321.30, 367.20, 45.90);}
	if (grp == "DD"){groupobject = new group("DD", "HYUNDAI ACCENT","http://www.corfucarrental.com/images/accent.jpg", 49.05, 98.10, 147.15, 196.20, 229.50, 275.40, 321.30, 367.20, 45.90);}
	if (grp == "F"){groupobject = new group("F", "OPEL ZAFIRA","http://www.corfucarrental.com/images/zafira.jpg", 75.60, 151.20, 226.80, 302.40, 360.00, 432.00, 504.00, 576.00, 72.00);}
	if (grp == "FF"){groupobject = new group("FF", "FIAT DOBLO","http://www.corfucarrental.com/images/doblo.jpg", 75.60, 151.20, 226.80, 302.40, 360.00, 432.00, 504.00, 576.00, 72.00);}
	if (grp == "GG"){groupobject = new group("GG", "FIAT SCUDO <br> 9 seats doors 5 <br> 2 big & 2 small suitcases<br> A/C ,Radio","http://www.corfucarrental.com/images/scudo.jpg", 83.70, 167.40, 251.10, 334.80, 400.50, 480.60, 560.70, 640.80, 80.10);}
	if (grp == "G1"){groupobject = new group("G1", "HYUNDAI H1-9","http://www.corfucarrental.com/images/h1-9.jpg", 105.00, 210.00, 315.00, 420.00, 505.00, 606.00, 707.00, 808.00, 101.00);}
	if (grp == "J"){groupobject = new group("J", "DAEWOO MATIZ open","http://www.corfucarrental.com/images/matiz.jpg", 40.95, 81.90, 122.85, 163.80, 190.25, 228.30, 266.40, 304.45, 38.05);}
	if (grp == "K"){groupobject = new group("K", "RENAULT TWINGO open","http://www.corfucarrental.com/images/twingo.jpg", 43.65, 87.30, 130.95, 174.60, 204.40, 245.28, 286.16, 327.04, 40.88);}
	if (grp == "L"){groupobject = new group("L", "RENAULT MEGANE Cabrio 2.0","http://www.corfucarrental.com/images/megane1.jpg", 108.00, 216.00, 315.00, 420.00, 472.50, 567.00, 661.50, 756.00, 94.50);}
	if (grp == "S"){groupobject = new group("S", "SUZUKI JIMNY","http://www.corfucarrental.com/images/jimny.jpg", 51.75, 103.50, 155.25, 207.00, 243.00, 291.60, 340.20, 388.80, 48.60);}
	if (grp == "SS"){groupobject = new group("SS", "SUZUKI SAMURAI","http://www.corfucarrental.com/images/samurai.jpg", 51.75, 103.50, 155.25, 207.00, 243.00, 291.60, 340.20, 388.80, 48.60);}
	if (grp == "I"){groupobject = new group("I", "SUZUKI VITARA","http://www.corfucarrental.com/images/vitara.jpg", 62.55, 125.10, 187.65, 250.20, 295.00, 354.00, 413.10, 472.10, 59.00);}
  }
  else {alert("wrong seasonal input. Check Coding");}
}

// function to update PU & DO dropdowns
function update_location_text(dd, tb){
  var d = dd;
  var t = tb;
   if (d.selectedIndex == 1){t.value = "Flight No. ?";}
  else if (d.selectedIndex == 2){t.value = "Hotel Name & Location ?";}
  else if (d.selectedIndex == 3){t.value = "Other. ?";}
  else {t.value = "Please select Pickup Type";}
  t.focus();
}

// validation of form before it is sent to secure.htm
function is_form_Ready(form){
  var f = document.fm;	
  var st_date = f.start_date;
  var end_date = f.end_date;  
  var secs_text = ":00";
  
  // get dates & time in correct format
  var s_date = f.start_date.value + " " + f.time_pickup1.value + ":" + f.time_pickup2.value + secs_text;
  var e_date = f.end_date.value + " " + f.time_return1.value + ":" + f.time_return2.value + secs_text;  
  
  // get no. hours from hire
  var hours_hire = suycDateDiff(s_date,e_date,"h","1");  
  // get days from hours
  var days_hire = parseInt(hours_hire / 24);
  //alert("days hire " + days_hire);
  // get remaining hours
  var hours_remaining = hours_hire % 24;
  //alert("xtr hours " + hours_remaining);

  // start date format
  if (! isDate(st_date.value,'dd MMM yyyy')){alert("Pickup Date is wrongy formatted. \nPlease use format e.g 09 July 2002.");st_date.focus();return false;}  
  // end date format
  else if (! isDate(end_date.value,'dd MMM yyyy')){alert("Drop off Date is wrongy formatted. \nPlease use format e.g 09 July 2002.");end_date.focus();return false;}
  // dropdown validated
  else if (form.time_pickup1.selectedIndex==0){alert("Please Select Your Pickup time");form.time_pickup1.focus();return false;}    
  else if (form.time_return1.selectedIndex==0){alert("Please Select Your Drop off time");form.time_return1.focus();return false;}    
  else if (form.pickup_location.selectedIndex==0){alert("Please Select Your Pickup Location");form.pickup_location.focus();return false;}    
  else if (form.Return_Location.selectedIndex==0){alert("Please Select Your Drop off location");form.Return_Location.focus();return false;}    

  // check for minus hours
  else if (hours_hire <= 0){alert("Pickup date/time cannot be after Drop off date or the same day! \nPlease reselect dates.");st_date.focus();return false;}
  // checks for times < 1 day and give warning message
  
  // removed after talk with Kjell 5.12.02
  //else if (days_hire == 0){alert("Any time from 1 - 24 hours hire is charged at the 1 day rate!");}

  // checks for 0 to 5 extra hours and warns of 20% extra charge
  else if (hours_remaining > 0 && hours_remaining < 1){
  	if (days_hire > 0){
  	  return confirm("Your hire is for " + days_hire + " days Plus " + hours_remaining + " hours.\nYou may be charged an additional 20% (of the daily rental fee) for these extra hours  Is this OK?");}
  	}
  // checks for 5+ hours extra and warns of extra day charge
  //else if (hours_remaining >= 5){return confirm("Your hire is for " + days_hire + " days " + hours_remaining + " hours. \nRenting for more than 5 hours over a day costs the same as an extra day.\nIs this OK?");}

}

// function to setup secure.htm page 
function setup_group(){
  var gr = unescape(params["group"]);
  var secs_text = ":00";
  // create date time strings
  var s_date = removechar(unescape(params["start_date"]),"+") + " " + unescape(params["time_pickup1"]) + ":" + unescape(params["time_pickup2"]) + secs_text;
  var e_date = removechar(unescape(params["end_date"]),"+") + " " + unescape(params["time_return1"]) + ":" + unescape(params["time_return2"]) + secs_text;  
  
  // calculate high or low season
  var start_high_season = 5;
  var end_high_season   = 9;
  var d = new Date(s_date);
  var start_month = d.getMonth();
  if  (start_month > start_high_season && start_month < end_high_season){var season ="H";}else {var season = "L";}
  
  // get no. hours from hire
  var hours_hire = suycDateDiff(s_date,e_date,"h","1");  
  // get days from hours
  days_hire = parseInt(hours_hire / 24);
  // get remaining hours
  hours_remaining = hours_hire % 24;
  
  // allow for extra day charge for >= 5 hours
  if (days_hire < 1){tot_days_hire = days_hire + 1;}
  else if (hours_remaining >= 1){tot_days_hire = days_hire + 1;}
  else {tot_days_hire = days_hire;}
  
  // get prices and other info for this group
  group_selector(gr, season);
    
  // Calculate late night pickup extra charge
  extra_charge = 0;
  var late_charge = 15;
  var time_pickup1 = unescape(params["time_pickup1"]);
  var time_return1 = unescape(params["time_return1"]);
  if (time_pickup1 >=23 || time_pickup1 < 8){extra_charge = extra_charge + late_charge;}
  if (time_return1 >=23 || time_return1 < 8){extra_charge = extra_charge + late_charge;}

  // Calculate hire charge
  if (tot_days_hire == 1){net_total_charge = groupobject.days_1;}
  else if (tot_days_hire == 2){net_total_charge = groupobject.days_2;}
  else if (tot_days_hire == 3){net_total_charge = groupobject.days_3;}
  else if (tot_days_hire == 4){net_total_charge = groupobject.days_4;}
  else if (tot_days_hire == 5){net_total_charge = groupobject.days_5;}
  else if (tot_days_hire == 6){net_total_charge = groupobject.days_6;}
  else if (tot_days_hire == 7){net_total_charge = groupobject.days_7;}
  if (tot_days_hire > 7){
    var ex_days = tot_days_hire - 7;
    net_total_charge = (groupobject.days_7 + (ex_days * groupobject.extra_day));
  }
  net_total_charge = decimalFormat(net_total_charge,2);
  total_charge = parseFloat(net_total_charge) + parseFloat(extra_charge);
  total_charge = decimalFormat(total_charge,2);

  // Calculate Amount Payable to Kjell 
  percentage_to_pay = 20;
  net_payment = (total_charge * percentage_to_pay)/100;
  net_payment = decimalFormat(net_payment,2);
}
	
//////////////////////////////////////////////////////////////////////////
// function to setup vars to pass through to paypal 
function setup_paypal_vars(){
  var fm = document.mainform;
  var fm_mal = document.mal_form;	
  
  // get the number of weeks in the hire
  var weeks_hire = parseInt(tot_days_hire / 7);  
  if ((tot_days_hire % 7)!= 0) {weeks_hire++;}
  
  // find our which radio button clicked and set insurance charge value
  var insurance_charge;   
  for(var i=0; i < fm.insurance.length; i++){
      if (fm.insurance[i].checked == true){
             insurance_charge = fm.insurance[i].value;
      }       
  }           
  // calculate insurance charge
  insurance_charge = (insurance_charge * weeks_hire);
  
  // add insurance charge to total
  total_charge = (total_charge * percentage_to_pay)/100;
  total_charge = decimalFormat(total_charge,2);
  total_charge = (parseFloat(total_charge) + parseFloat(insurance_charge));
  total_charge = decimalFormat(total_charge,2);
  
  //create date strings to pass to item_name var
  var s_text = removechar(unescape(params["start_date"]),"+") + " "+ unescape(params["time_pickup1"]) + ":" + unescape(params["time_pickup2"]);
  var e_text = removechar(unescape(params["end_date"]),"+") + " " + unescape(params["time_return1"]) + ":" + unescape(params["time_return2"]);
  
  // create pickup and dropoff strings passed through in custom var
  pickup_text = removechar(unescape(params["pickup_text"]),"+");
  pickup_details = unescape(params["pickup_location"]) + " " + pickup_text;
  return_text = removechar(unescape(params["return_text"]),"+")
  return_details = unescape(params["Return_Location"]) + " " + return_text;
  
  // create vars to pass to paypal
  fm.item_name.value = groupobject.c_name + ". " + days_hire + " days " + hours_remaining + " hours. Dates : " + s_text + " - " + e_text + "Pickup: " + pickup_details + "Dropoff: " + return_details;;
  fm.amount.value = total_charge;
  
  // create vars to pass to mal
  fm_mal.product.value = fm.item_name.value;
  fm_mal.price.value = fm.amount.value;
  fm_mal.qty.value = 1;
  
  
  // removed cos it did not work
  fm.custom.value = "Pickup : " + pickup_details + "Dropoff: " + return_details;
}	

function insurance_popup(){
  var fm = document.mainform;	
  // get the number of weeks in the hire
  var weeks_hire = parseInt(tot_days_hire / 7);  
  if ((tot_days_hire % 7)!= 0) {weeks_hire++;}
  
  // find our which radio button clicked and set insurance charge value
  var insurance_charge;   
  for(var i=0; i < fm.insurance.length; i++){
      if (fm.insurance[i].checked == true){
             insurance_charge = fm.insurance[i].value;
      }       
  }           
  // calculate insurance charge
  insurance_charge = (insurance_charge * weeks_hire);
  alert('This will add ' + insurance_charge + ' Euros to the cost of the hire.'); 
  //alert('weeks hire' + weeks_hire + ' themod ' + themod + ' days_hire ' + tot_days_hire); 
}

//////////////////////////////////////////////////////////////////////////
// constructor for creating group object
function group(grp, c_name, c_image, days_1, days_2, days_3, days_4, days_5, days_6, days_7, days_8, extra_day){
  this.grp    = grp;
  this.c_name = c_name;
  this.c_image = c_image;
  this.days_1 = days_1;
  this.days_2 = days_2;
  this.days_3 = days_3;
  this.days_4 = days_4;
  this.days_5 = days_5;
  this.days_6 = days_6;
  this.days_7 = days_7;
  this.days_8 = days_8;
  this.extra_day  = extra_day;
}  


//////////////////////////////////////////////////////////////////////////
// function to remove the + sign that come from URL string
function removechar(word,ch) {
  var str = word;
  var ch = ch;
  var newstr = "";
  var replacement = " ";
  for (var i = 0; i < str.length; i++)
    {if (str.charAt(i) == ch){newstr = newstr + replacement}
    else {newstr = newstr + str.charAt(i)}
    } // end for loop
return newstr;
}

//////////////////////////////////////////////////////////////////////////
// generic function to get pass through vars from URL
function getParams() {
var idx = document.URL.indexOf('?');
var params = new Array();
if (idx != -1) {
var pairs = document.URL.substring(idx+1, document.URL.length).split('&');
  for (var i=0; i<pairs.length; i++) {
    nameVal = pairs[i].split('=');
    params[nameVal[0]] = nameVal[1];
  }
}
return params;
}

//////////////////////////////////////////////////////////////////////////
// Function used to get number of days between 2 dates
function suycDateDiff( start, end, interval, rounding ) {
    var iOut = 0;
    // Create 2 error messages, 1 for each argument. 
    var startMsg = "Check the Start Date and End Date\n"
        startMsg += "must be a valid date format.\n\n"
        startMsg += "Please try again." ;
		
    var intervalMsg = "Sorry the dateAdd function only accepts\n"
        intervalMsg += "d, h, m OR s intervals.\n\n"
        intervalMsg += "Please try again." ;

    var bufferA = Date.parse( start ) ;
    var bufferB = Date.parse( end ) ;
    	
    // check that the start parameter is a valid Date. 
    if ( isNaN (bufferA) || isNaN (bufferB) ) {
        alert( startMsg ) ;
        return null ;
    }
	
    // check that an interval parameter was not numeric. 
    if ( interval.charAt == 'undefined' ) {
        // the user specified an incorrect interval, handle the error. 
        alert( intervalMsg ) ;
        return null ;
    }
    var number = bufferB-bufferA ;
    // what kind of add to do? 
    switch (interval.charAt(0))
    {
        case 'd': case 'D': 
            iOut = parseInt(number / 86400000) ;
            if(rounding) iOut += parseInt((number % 86400000)/43200001) ;
            break ;
        case 'h': case 'H':
            iOut = parseInt(number / 3600000 ) ;
            if(rounding) iOut += parseInt((number % 3600000)/1800001) ;
            break ;
        case 'm': case 'M':
            iOut = parseInt(number / 60000 ) ;
            if(rounding) iOut += parseInt((number % 60000)/30001) ;
            break ;
        case 's': case 'S':
            iOut = parseInt(number / 1000 ) ;
            if(rounding) iOut += parseInt((number % 1000)/501) ;
            break ;
        default:
        // If we get to here then the interval parameter
        // didn't meet the d,h,m,s criteria.  Handle
        // the error. 		
        alert(intervalMsg) ;
        return null ;
    }

    return iOut ;
}

//////////////////////////////////////////////////////////////////////////
// function used to format numbers to 2 decimal places
function decimalFormat(n,dec) {
  n += "";
  if(isNaN(parseFloat(n)))
    return "NaN";
  i = n.indexOf('.');
  if ( i!=-1 )
    if ( i+dec < n.length )
      n = n.substring(0,i+dec+1);
    else
      for ( i=dec-(n.length-i-1); i>0; i-- )
        n += '0';
  else {
    n += '.';
    for ( i=dec; i>0; i-- )
      n += '0';
  } return n;
}
