// Functions for BargainWheel.com.au technology

function goBargainWheels() {

  SameAsPickup(); // IF NO RETURN WAS SELECTED. SET IT THE SAME
  if(Validate()==true){    
    if(BargainWheelsEnabled()){
        YAHOO.util.Dom.get('_countryLiveIn').value = YAHOO.util.Dom.get('countryListLiveIn').value;
        YAHOO.util.Dom.get('btnBargainWheelsSearch').value = "Please Wait...";

		heavyImage = new Image(); 
		heavyImage.src = "/SetSession.asp?bwUrl=" + escape(GenerateURL());
		
		var strURL = GenerateBargainWheelsURL();
		if(strURL!=""){
          top.location = strURL;
          //document.write(strURL);
        }
        else{
		  go();
          //SearchFailed();
        }
    }
    else{
		go();
      //SearchFailed();
    }                
  }
}


function SearchFailed(){
   alert("Oooops. This location isn't working at the moment. Sorry about that. Please use the bigger car rental companies for now.");
 }

function ConvertBargainWheelsLocation(strLocation){
  strLocationCode = "";
  
  try{
    strLocation = strLocation.split(",")[0];
    switch(strLocation){
		case "Adelaide":strLocationCode="ADE";break;
		case "Adelaide Airport":strLocationCode="AAP";break;
		//case "Alice Springs Airport":strLocationCode="ALS"; break;
		//case "Alice Springs":strLocationCode="ALC"; break;
		case "Bairnsdale":strLocationCode="BAR"; break;
		case "Ballina":strLocationCode="BAL"; break;
		case "Bankstown":strLocationCode="BKT"; break;
		case "Beenleigh":strLocationCode="BEE"; break;
		case "Bondi Junction":strLocationCode="BON"; break;
		case "Braybrook":strLocationCode="BBK"; break;
		case "Brisbane North":strLocationCode="BNS";break;    
		case "Brisbane South":strLocationCode="BSS";break;
		case "Brisbane":strLocationCode="BNE";break;
		case "Brisbane Domestic Airport":strLocationCode="BAP"; break;
		case "Brisbane International Airport":strLocationCode="BIA"; break;
		case "Broadbeach":strLocationCode="BBCH"; break;
		case "Broome":strLocationCode="BME"; break;
		case "Byron Bay":strLocationCode="BYR"; break;
		case "Caboolture":strLocationCode="CBT"; break;
		case "Cairns":strLocationCode="CNS";break;
		case "Cairns Airport":strLocationCode="CNA";break;
		case "Cairns - Northern Beaches":strLocationCode="CSB"; break;
		case "Caloundra":strLocationCode="CAL"; break;
		case "Camberwell":strLocationCode="CAM"; break;
		case "Canberra":strLocationCode="CANC";break;
		case "Canberra Airport":strLocationCode="CAN";break;
		case "Caulfield":strLocationCode="CLF"; break;
		case "Cheltenham":strLocationCode="CHE"; break;
		case "Clayton":strLocationCode="CLA"; break;
		case "Coburg":strLocationCode="COB"; break;
		case "Coolangatta":strLocationCode="OOL"; break;
		case "Coolangatta Airport":strLocationCode="CAA";break;  
		case "Coopers Plains":strLocationCode="CPS"; break;
		case "Cronulla":strLocationCode="CRO"; break;
		case "Dandenong":strLocationCode="DAN"; break;
		//case "Darwin":strLocationCode="DCC"; break;
		//case "Darwin Airport":strLocationCode="DAR"; break;
		case "Devonport":strLocationCode="DVC"; break;
		case "Devonport Ferry":strLocationCode="DVF"; break;
		case "Glenelg":strLocationCode="GLG"; break;
		case "Gold Coast - Northern Suburbs":strLocationCode="GCN"; break;
		case "Gold Coast Airport":strLocationCode="CAP"; break;
		//case "Hobart":strLocationCode="HOB"; break;
		//case "Hobart Airport":strLocationCode="HAP"; break;
		case "Hoppers Crossing":strLocationCode="HPP"; break;
		case "Ipswich":strLocationCode="IPW"; break;
		case "Kununurra":strLocationCode="KUN"; break;
		case "Logan":strLocationCode="LOG"; break;
		//case "Launceston":strLocationCode="LWC"; break;
		//case "Launceston Airport":strLocationCode="LAW"; break;
		case "Maidstone":strLocationCode="MAI";break;
		case "Maroochydore":strLocationCode="MAR";break;
		case "Maroochydore Airport":strLocationCode="SCAP";break;
		case "Melbourne":strLocationCode="MEL";break;
		case "Melbourne Airport":strLocationCode="MAP";break;
		case "Mooloolaba":strLocationCode="MOO"; break;
		case "Moorabbin":strLocationCode="MRB"; break;
		case "Nambour":strLocationCode="NBR"; break;
		case "Noosa Heads":strLocationCode="NSA"; break;
		case "Pakenham":strLocationCode="PKH"; break;
		case "Palm Cove":strLocationCode="PCV"; break;
		case "Parramatta":strLocationCode="PAR"; break;
		case "Perth":strLocationCode="PTH";break;
		case "Perth Domestic Airport":strLocationCode="PDA";break;
		case "Perth International Airport":strLocationCode="PIA"; break;
		case "Picton":strLocationCode="PIC"; break;
		case "Preston":strLocationCode="PRE";break;
		case "Richmond NSW":strLocationCode="RIC"; break;
		case "Sale":strLocationCode="SAL"; break;
		case "St Kilda":strLocationCode="STK"; break;
		case "Southport":strLocationCode="SPT"; break;
		case "Surfers Paradise":strLocationCode="SFS";break;
		case "Sydney Airport":strLocationCode="SAP"; break;
		case "Sydney":strLocationCode="SYD"; break;
		case "Tingalpa":strLocationCode="TIN"; break;
		case "Traralgon":strLocationCode="TLG"; break;
		case "Williamstown Vic":strLocationCode="WTN"; break;
		case "Yarram":strLocationCode="YRM"; break;
		case "Yarraville Vic":strLocationCode="YVE"; break;
		//case "Tullamarine":strLocationCode="NSA";break;
		
		case "Auckland":strLocationCode="AKC";break;
		case "Auckland Airport":strLocationCode="AKP";break;
		case "Christchurch Airport":strLocationCode="CHP"; break;
		case "Christchurch":strLocationCode="CHC"; break;
		case "Christchurch Airport":strLocationCode="CHP"; break;
		case "Christchurch":strLocationCode="CHC"; break;
		case "Dunedin":strLocationCode="DUN"; break;
		case "Wellington Airport":strLocationCode="WLA"; break;
		case "Wellington":strLocationCode="WLG"; break;		
        default:strLocationCode="";break;
    }
    return strLocationCode;
  }
  catch(err){
    return "";
  }
}


function GenerateBargainWheelsURL() {
  var countryListPickup = YAHOO.util.Dom.get('countryListPickup');
  strCountryPickupID = countryListPickup[countryListPickup.selectedIndex].value;
  
  strHost = "www.bargainwheels.com.au/";

  
  var pickupDate = YAHOO.util.Dom.get('_pickupDate').value;
  var returnDate = YAHOO.util.Dom.get('_returnDate').value;
  aryPickupDate = pickupDate.split("/"); 
  pday = aryPickupDate[1];
  pmonth = aryPickupDate[0];
  pyear = aryPickupDate[2];
  aryReturnDate = returnDate.split("/"); 
  dday = aryReturnDate[1];
  dmonth = aryReturnDate[0];
  dyear = aryReturnDate[2];
  
  var vehiclecategory = YAHOO.util.Dom.get('CategoryID').value;
/*  pickupHour = YAHOO.util.Dom.get('pickupHour')[YAHOO.util.Dom.get('pickupHour').selectedIndex].value;
  pickupMins = YAHOO.util.Dom.get('pickupMins')[YAHOO.util.Dom.get('pickupMins').selectedIndex].value;
  returnHour = YAHOO.util.Dom.get('returnHour')[YAHOO.util.Dom.get('returnHour').selectedIndex].value;
  returnMins = YAHOO.util.Dom.get('returnMins')[YAHOO.util.Dom.get('returnMins').selectedIndex].value;
*/
//  pickupTime = YAHOO.util.Dom.get('pickupTime')[YAHOO.util.Dom.get('pickupTime').selectedIndex].value;
//  returnTime = YAHOO.util.Dom.get('returnTime')[YAHOO.util.Dom.get('returnTime').selectedIndex].value;
//  pickupId = YAHOO.util.Dom.get('myInputPickupId').value; //Location
//  returnId = YAHOO.util.Dom.get('myInputReturnId').value;
 
  
  //CONVERT BARGAIN WHEELS LOCATION CODES
  if(OLD_SCHOOL_DROPDOWN){
    var pickupLocation = ConvertBargainWheelsLocation(YAHOO.util.Dom.get('ddlPickup')[YAHOO.util.Dom.get('ddlPickup').selectedIndex].text);
    var returnLocation = ConvertBargainWheelsLocation(YAHOO.util.Dom.get('ddlReturn')[YAHOO.util.Dom.get('ddlReturn').selectedIndex].text);  
  }
  else{
    var pickupLocation = ConvertBargainWheelsLocation(YAHOO.util.Dom.get('myInputPickup').value);
    var returnLocation = ConvertBargainWheelsLocation(YAHOO.util.Dom.get('myInputReturn').value);
  }
  
  if(pickupLocation=="" || returnLocation==""){
    return "";
  } 
  

  //LOAD OTHER VARIABLES 
  var age = YAHOO.util.Dom.get('ageList')[YAHOO.util.Dom.get('ageList').selectedIndex].value;
  var countryLiveIn = YAHOO.util.Dom.get('countryListLiveIn')[YAHOO.util.Dom.get('countryListLiveIn').selectedIndex].value;
  //countryListPickup = YAHOO.util.Dom.get('countryListPickup');
  strCountryPickupID = countryListPickup[countryListPickup.selectedIndex].value;
//  strCountryID = countryList[countryList.selectedIndex].value;
                                      
  
  strURL = "http://" + strHost + 
  "car-rental-search-results.asp?strURL=bwhome" + 
  "&Country=Au" +
  "&LocationType=4" + 
  "&CategoryID=" + vehiclecategory + 
  "&PickupCode=" + pickupLocation + 
  "&DropoffCode=" + returnLocation + 
  "&PDay=" + pday + 
  "&PMonth=" + pmonth + 
  "&PYear=" + pyear + 
  "&DDay=" + dday + 
  "&DMonth=" + dmonth + 
  "&DYear=" + dyear + 
  "&PickupTime=" + YAHOO.util.Dom.get('pickupTime')[YAHOO.util.Dom.get('pickupTime').selectedIndex].value + 
  "&DropoffTime=" + YAHOO.util.Dom.get('returnTime')[YAHOO.util.Dom.get('returnTime').selectedIndex].value;  
  
/*
  strURL = "http://bargainwheels.com.au/car-rental-search-results.asp?strURL=bwhome     
  &Country=Au
  &LocationType=4
  &CategoryID=1
  &PickupCode=BNE
  &DropoffCode=BNE
  &PDay=02
  &PMonth=Dec
  &PYear=2009
  &DDay=07
  &DMonth=Dec
  &DYear=2009&PickupTime=09:00&DropoffTime=09:00
   //http://www.bargainwheels.com.au/rpmStep2.asp?strURL=bwhome&LocationType=4&CategoryID=1&PickupCode=AAP&DropoffCode=AAP&PDay=12&PMonth=Apr&PYear=2008&DDay=17&DMonth=Apr&DYear=2008&PickupTime=09:00&DropoffTime=09:00
*/

  return strURL;
}

function BargainWheelsEnabled(){
  var strPickup, strReturn;                                         
  if(OLD_SCHOOL_DROPDOWN){
    strCountryPickupID = countryListPickup[countryListPickup.selectedIndex].value;

    strPickup = YAHOO.util.Dom.get('ddlPickup')[YAHOO.util.Dom.get('ddlPickup').selectedIndex].text;
    strReturn = YAHOO.util.Dom.get('ddlReturn')[YAHOO.util.Dom.get('ddlReturn').selectedIndex].text;
    if(YAHOO.util.Dom.get('ddlReturn').selectedIndex==0){
      strReturn="";
    }
  }
  else{
    strPickup = YAHOO.util.Dom.get('myInputPickup').value;
    strReturn = YAHOO.util.Dom.get('myInputReturn').value;  
  }
  if(strReturn=="" || strReturn==RETURN_PROMPT){
    strReturn = strPickup;
  }
  
  if(CanBargainWheelsDoThis(strPickup, strReturn)){
	//WriteVehicleSizeDDL();
    YAHOO.util.Dom.get('btnBargainWheelsSearch').disabled=false;
    YAHOO.util.Dom.removeClass(YAHOO.util.Dom.get('btnBargainWheelsSearch'), "vvvsearchnobargain");
    YAHOO.util.Dom.addClass(YAHOO.util.Dom.get('btnBargainWheelsSearch'), "vvvsearchbargain");	
	return true;
  }
  else
  {
    //WriteVehicleAllSizeDDL();
    //YAHOO.util.Dom.get('btnBargainWheelsSearch').disabled=true; 
    //YAHOO.util.Dom.removeClass(YAHOO.util.Dom.get('btnBargainWheelsSearch'), "vvvsearchbargain");
    //YAHOO.util.Dom.addClass(YAHOO.util.Dom.get('btnBargainWheelsSearch'), "vvvsearchnobargain");
	return false;
  }
  
}

function CanBargainWheelsDoThis(returnTxt, pickupTxt){
    //returns true if bargain wheels can handle the selected locations
    
    //fix difference with name of sunshine coast / maroochudore airport
    returnTxt = returnTxt.replace("Sunshine Coast Airport","Maroochydore Airport");
    pickupTxt = pickupTxt.replace("Sunshine Coast Airport","Maroochydore Airport");

    ////////////////// ONE WAY MATRIX ///////////////////////////
    if(pickupTxt.indexOf("Brisbane")>-1 && pickupTxt.indexOf("Brisbane East")==-1 && (returnTxt.indexOf("Brisbane") >-1 || returnTxt.indexOf("Broadbeach") >-1 || returnTxt.indexOf("Surfers Paradise") >-1 || returnTxt.indexOf("Coolangatta") >-1 || returnTxt.indexOf("Gold Coast") >-1)){
      return true;
    }
    else if(pickupTxt.indexOf("Broadbeach")>-1 && (returnTxt.indexOf("Broadbeach") >-1 || returnTxt.indexOf("Brisbane") >-1 || returnTxt.indexOf("Surfers Paradise") >-1 || returnTxt.indexOf("Gold Coast") >-1)){  
      return true;
    }
    else if(pickupTxt.indexOf("Surfers Paradise")>-1 && (returnTxt.indexOf("Surfers Paradise") >-1 || returnTxt.indexOf("Brisbane") >-1 || returnTxt.indexOf("Broadbeach") >-1 || returnTxt.indexOf("Gold Coast") >-1)){  
      return true;
    }
    else if(pickupTxt.indexOf("Gold Coast")>-1 && (returnTxt.indexOf("Gold Coast") >-1 || returnTxt.indexOf("Brisbane") >-1 || returnTxt.indexOf("Broadbeach") >-1 || returnTxt.indexOf("Surfers Paradise") >-1)){  
      return true;
    }
	else if(pickupTxt.indexOf("Coolangatta")>-1 && (returnTxt.indexOf("Coolangatta") >-1 || returnTxt.indexOf("Brisbane") >-1 || returnTxt.indexOf("Broadbeach") >-1 || returnTxt.indexOf("Surfers Paradise") >-1)){  
      return true;
    }
    else if(pickupTxt.indexOf("Mooloolaba")>-1 && (returnTxt.indexOf("Mooloolaba") >-1 || returnTxt.indexOf("Noosa") >-1  || returnTxt.indexOf("Maroochydore") >-1 || returnTxt.indexOf("Brisbane") >-1)){  
      return true;
    }
    else if(pickupTxt.indexOf("Maroochydore")>-1 && (returnTxt.indexOf("Maroochydore") || returnTxt.indexOf("Noosa") >-1  >-1 || returnTxt.indexOf("Mooloolaba") >-1 || returnTxt.indexOf("Brisbane") >-1)){  
      return true;
    }
    else if(pickupTxt.indexOf("Noosa")>-1 && (returnTxt.indexOf("Noosa") >-1 || returnTxt.indexOf("Maroochydore") >-1 || returnTxt.indexOf("Mooloolaba") >-1 || returnTxt.indexOf("Brisbane") >-1)){  
      return true;
    }
    else if(pickupTxt.indexOf("Brisbane")>-1 && pickupTxt.indexOf("Brisbane East")==-1 && (returnTxt.indexOf("Mooloolaba") >-1 || returnTxt.indexOf("Noosa") >-1 || returnTxt.indexOf("Maroochydore") >-1)){  
      return true;
    }
	else if(pickupTxt==returnTxt){  
	  return true;
    }
	else if(pickupTxt.indexOf(adjustLocation(returnTxt))>-1){  
	  return true;
    }
	else{
      return false;
    }    
}

function adjustLocation(strLocation) {

strLocation=strLocation.replace(/ Airport/gi, "");
strLocation=strLocation.replace(/ airport/gi, "");

return strLocation.split(",")[0];
}    
    
function WriteVehicleSizeDDL(){
  var strDivCarSize="";
  strDivCarSize += "    <span class=\"label\">Size:<\/span> ";
  strDivCarSize += "    <SELECT name=CategoryID id=CategoryID>";
  strDivCarSize += "    <option value='0' >Choose Vehicle Type<\/option>";
  strDivCarSize += "    <option value='1' selected>Small Car &nbsp;<\/option>";
  strDivCarSize += "    <option value='22' >Mid Size &nbsp;<\/option>";
  strDivCarSize += "    <option value='23' >Full Size &nbsp;<\/option>";
  strDivCarSize += "    <option value='24' >Prestige\/Sports Car &nbsp;<\/option>";
  strDivCarSize += "    <option value='25' >Station Wagons &nbsp;<\/option>";
  strDivCarSize += "    <option value='15' >People Movers &nbsp;<\/option>";
  strDivCarSize += "    <option value='17' >Commercials \/ Utes \/ Trucks &nbsp;<\/option>";
  strDivCarSize += "    <option value='27' >4wd's &nbsp;<\/option>";
  strDivCarSize += "    <option value='10' >Campers &nbsp;<\/option>";
  strDivCarSize += "    <option value='21' >Motorbikes &nbsp;<\/option>";
  strDivCarSize += "    <\/SELECT>";
  strDivCarSize += "";

  YAHOO.util.Dom.get("divExtra").innerHTML = strDivCarSize;
}

function WriteVehicleAllSizeDDL(){
  var strDivCarSize="";
  strDivCarSize += "    <span class=\"label\">Size:<\/span> ";
  strDivCarSize += "    <SELECT name=CategoryID id=CategoryID>";
  strDivCarSize += "    <option>Search All Sizes<\/option>";
  strDivCarSize += "    <\/SELECT>";
  strDivCarSize += "";

  YAHOO.util.Dom.get("divExtra").innerHTML = strDivCarSize;
}

function WriteBargainWheelsButton(){
  var strBargainButton="";
  strBargainButton += "    <input id=\"btnSearch\" style=\"display: none\" class=vvvsearchvroom  onclick=\"go();return false;\" value=\"Search Big Companies\" name=\"btnSearch\" tabindex=\"10\" type=\"button\">";
  strBargainButton += "    <span id=or>&nbsp;</span>";
  strBargainButton += "    <input id=\"btnBargainWheelsSearch\" class=vvvsearchbargain onclick=\"goBargainWheels();return false;\" value=\"Search Little Companies\" name=\"btnBargainWheelsSearch\" tabindex=\"10\" type=\"button\">"; 
  strBargainButton += "";
  strBargainButton += "";
  YAHOO.util.Dom.get("vvvsearch").innerHTML = strBargainButton;
}

function GetMonth(intMonth) {
    switch(intMonth) {
      case "1": return "Jan"; break;
      case "2": return "Feb"; break;
      case "3": return "Mar"; break;
      case "4": return "Apr"; break;
      case "5": return "May"; break;
      case "6": return "Jun"; break;
      case "7": return "Jul"; break;
      case "8": return "Aug"; break;
      case "9": return "Sep"; break;
      case "10": return "Oct"; break;
      case "11": return "Nov"; break;
      case "12": return "Dec"; break;
    }
}

function InitBargainWheels(){
  WriteVehicleSizeDDL();
  WriteBargainWheelsButton();   
}

if(BARGAIN_WHEELS){
  setTimeout("InitBargainWheels()",10);
}