//////////////////////////// VARIOUS //////////////////////////////////////////////////
// This javascript is for running the vroom search box, including the autocomplete location picker.


//#############################  TODO ########################################
//# Make some locations shorter. This will require editing of the database. EG (BMI)
//# Iphone usability needs improving
//# Custom CSS. Eg... Green, Blue, Mobile, CarHire
//############################################################################



//////////////////////////////////////////////////////////////////////////////
// VARIABLES 
//////////////////////////////////////////////////////////////////////////////

var strReturnAddresses = "";
if(typeof(httpServer) == 'undefined'){
  httpServer="https://vroomvroomvroom.com/";
//  httpServer="http://localhost/vroom/";  
}
if(typeof(ALL_LOCATIONS_DROPDOWN) == 'undefined'){
  ALL_LOCATIONS_DROPDOWN = false;
}
if(typeof(PICKUP_PROMPT) == 'undefined'){
  PICKUP_PROMPT = "Were are you picking up from?";
}
if(typeof(RETURN_PROMPT) == 'undefined'){
  var RETURN_PROMPT = "Where are you returning the car?";
}
if(typeof(LIST_ALL_LOCATIONS_ONFOCUS) == 'undefined'){
  LIST_ALL_LOCATIONS_ONFOCUS = false;
}
if(typeof(SUGGESTION_MODE) == 'undefined'){
  SUGGESTION_MODE = "OFF";
}
if(typeof(OLD_SCHOOL_DROPDOWN) == 'undefined'){
  var OLD_SCHOOL_DROPDOWN = false;
}
if(typeof(BARGAIN_WHEELS) == 'undefined'){
  var BARGAIN_WHEELS = location.href.indexOf("bargainwheels.com.au")!=-1 ? true : false;
}
if(typeof(SHOW_FOOTER) == 'undefined'){
  var SHOW_FOOTER = false;
}

var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
if(isIE6)OLD_SCHOOL_DROPDOWN=true;


var ALPHABET_SOUP_THRESHOLD = 10; //how many locations in the autocomplete should there be before adding space before each new letter
var AUTO_HEIGHT_THRESHOLD = 20; //how many locations in the autocomplete should there be before limiting the height
var MAX_DROP_DOWN_HEIGHT = "300px";
var ADVANCED_MODE_ON = false;

/* Code for the Search Form */
//Yahoo namespace used for the YUI!calendar
YAHOO.namespace("vroom.calendar");

/** global variables **/
//string variables
var strMonth, strURL, strCountryID, strHost, strProtocol, pickupLoc, returnLoc, pickupTxt, returnTxt, age, affiliateID;
//dates
var dates, date, year, month, day;
//textbox fields
var txtPickupDate, txtReturnDate;
//dropdown lists
var countryListPickup, pickupList, returnList, pickupTime, returnTime, ageList;
                                                                                                           
//hidden fields
//var _returnDateObj, _pickupDateObj;
var _init,_pickupId, _returnId, _pickupDate, _returnDate, _countryPickupID, _countryLiveIn;
var calFocused = false;

var vvvmap = false; //The google map.
var newWin = false;
var bDowntown;
//var locationData="";
var bLocationsInitialised = false;
var strPickupAddresses, strReturnAddresses;

// Location combobox variables
var oDS;
var togglerPickup;
var togglerReturn;
var oACpickup;
var oACreturn;
var oContentPickup;
var oContentReturn;
var bThresholdReached=false;
var activeLeg = "Pickup"; //"Pickup" or "Return"
var aryDepots,aryPickup,aryReturn;





//////////////////////////////////////////////////////////////////////////////
// CONVERT NUMERICAL ARRAYS TO ASSOCIATIVE LIKE OBJECTS
//////////////////////////////////////////////////////////////////////////////
var locations = new Object();
var depots = new Object();

//aryDepots = [ dr("SupplierID") , dr("DepotID") , dr("Address") , dr("DisplayName") , dr("CountryName") , dr("PostCode") , 
//dr("Airport") , NullConvert(dr("Latitude"))  , NullConvert(dr("Longitude")) ]
depots.leg = "Pickup";
depots.supplierid =   function(i){return eval("ary" + depots.leg)[i][0];};
depots.depotid =      function(i){return eval("ary" + depots.leg)[i][1];};
depots.address =      function(i){return eval("ary" + depots.leg)[i][2];};
depots.displayname =  function(i){return eval("ary" + depots.leg)[i][3];};
depots.country =      function(i){return eval("ary" + depots.leg)[i][4];};
depots.postcode =     function(i){return eval("ary" + depots.leg)[i][5];};
depots.airport =      function(i){return eval("ary" + depots.leg)[i][6];};
depots.latitude =     function(i){return eval("ary" + depots.leg)[i][7];};
depots.longitude =    function(i){return eval("ary" + depots.leg)[i][8];};
depots.tostring =    function(i){return depots.address(i) + " " + depots.displayname(i) + ", " + depots.country(i) + ". " + depots.postcode(i);};








//////////////////////////////////////////////////////////////////////////////
// VARIOUS
//////////////////////////////////////////////////////////////////////////////

function includejs(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    
    //return false;
}

// SET SELECTED OPTION OF A SELECT BOX
function setSelectedValue(selectObject, value) {
  for(i = 0; i < selectObject.length; i++) {
    if(selectObject[i].value == value) {
      selectObject.selectedIndex = i;
      return true;
    }
  }
  return false;
}
// SET SELECTED OPTION OF A SELECT BOX
function setSelectedText(selectObject, text) {
  for(i = 0; i < selectObject.length; i++) {
    if(selectObject[i].text.indexOf(text)==0) {
      selectObject.selectedIndex = i;
      return true
    }
  }
  return false;
}

// ADD AN OPTION TO SELECT BOX
function AddSelection(ddl,strValue,strText){
  ddl.length = ddl.length + 1;
  ddl[ddl.length-1].text = strText;
  ddl[ddl.length-1].value = strValue;
}


function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1].replace(/%20/g," ");
    }
  } 
  return "";
}

function Trace(strTraceMe){
  YAHOO.util.Dom.get("divDebug").innerHTML = strTraceMe;
}


function GenerateURL() {
  //used to generate the URL for the vehicle search
  //typical search URL
  //http://www.vroomvroomvroom.com.au/book/default.aspx?pickupdate=2/4/2007&pickuptime=09:00:00&dropoffdate=2/7/2007&dropofftime=18:00:00&ddlPickUpLocation=1817&ddlDropOffLocation=1817&age=25&countryfrom=AU&dosearch=true#results
  strHost = window.location.hostname;
  
  //check if the page is hosted on a vroom site, if not, default to the AU site
  //can use the country selected in the country drop down list to redirect to the appropriate site
  countryListPickup = YAHOO.util.Dom.get('countryListPickup');
  strCountryPickupID = countryListPickup[countryListPickup.selectedIndex].value;
  
  switch (strCountryPickupID) {
    case "AU":
      if(window.location.hostname=="bargainwheels.com.au" || window.location.hostname=="www.bargainwheels.com.au") {
        strHost = "book.bargainwheels.com.au/book/default.aspx";
    		strProtocol = "http://";
    	}else if(window.location.hostname=="carhire.com.au" || window.location.hostname=="www.carhire.com.au") {
        strHost = "carhire.com.au/book/default.aspx";
    		strProtocol = "https://";
      } else {
        strHost = "vroomvroomvroom.com.au/book/default.aspx";
		strProtocol = "https://";
      }
      break;                              
    case "NZ":
      strHost = "www.vroomvroomvroom.co.nz/book/default.aspx";
	    strProtocol = "https://";
      break;
    case "US":
    case "CA":
    case "ZA":
      strHost = "vroomvroomvroom.com/book/default.aspx";
	  strProtocol = "https://";
      break;
    case "CY":
    case "DE":
    case "ES":
    case "FR":
    case "GB":
    case "GR":
    case "HR":
    case "IE":
    case "IT":
    case "MT":
    case "PT":
      strHost = "vroomvroomvroom.co.uk/book/default.aspx";
	  strProtocol = "https://";
      break;
    default:
      strHost = "vroomvroomvroom.com.au/book/default.aspx";
	  strProtocol = "https://";
      break;
  }

  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;
 
  //SaveDepotChoices();
 
  //SAVE SOME OF THE VARIABLES TO HIDDEN TEXT BOXES SO THEY CAN BE LOADED AFTER USING BACK BUTTON  
  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;


  strURL = strProtocol + strHost + "?" + 
  "pickupdate=" + YAHOO.util.Dom.get('_pickupDate').value + 
  "&pickuptime=" + pickupTime + ":00" +
  "&dropoffdate=" + YAHOO.util.Dom.get('_returnDate').value + 
  "&dropofftime=" + returnTime + ":00" + 
  "&ddlPickUpLocation=" + pickupId + 
  "&ddlDropOffLocation=" + returnId + 
  "&" + YAHOO.util.Dom.get('_depots').value + 
  "&age=" + age + 
  "&ddlCountry=" + strCountryPickupID + 
  "&countryfrom=" + countryLiveIn + 
  "&affiliate=" + affiliateID +
  "&dosearch=true" + 
  "&tick=" + DateTimeTicks();
  
  strURL += "#results";
  return strURL;
}



function go() {
//LETS DO THIS!!! SEND CUSTOMER TO THE RESULTS PAGES!

  SameAsPickup(); // IF NO RETURN WAS SELECTED. SET IT THE SAME
  
  if(Validate()){
    _countryLiveIn.value = YAHOO.util.Dom.get('countryListLiveIn').value;
    YAHOO.util.Dom.get('btnSearch').value = "Please Wait...";
    top.location = GenerateURL();
  }
}

function Validate(){
  if(YAHOO.util.Dom.get('countryListPickup').selectedIndex == 0) {
    alert("Please select a country, then select your desired pickup and return locations to search.");
    return false;
  } 
  if(YAHOO.widget.DateMath.before(YAHOO.vroom.calendar.returncal.getSelectedDates()[0], YAHOO.vroom.calendar.pickupcal.getSelectedDates()[0])) {
    alert("You must return the car after you pick it up. Please change your dates!");
    return false;
  } 
  if(OLD_SCHOOL_DROPDOWN){
    if(YAHOO.util.Dom.get('ddlPickup').selectedIndex == 0){
      alert('Please choose a city! ;-)');
      return false;
    }
  }
  else if(YAHOO.util.Dom.get('myInputPickupId').value == "" || YAHOO.util.Dom.get('myInputPickup').value == "" || YAHOO.util.Dom.get('myInputPickup').value == PICKUP_PROMPT || YAHOO.util.Dom.get('myInputPickup').value == "Loading cities...") {
    alert('Please type in a pickup city. Thank you ;-)');
    return false;
  } 
  return true;
}

function DateTimeTicks () {
  var strReturn = "";
  var datenow = new Date();

  strReturn += datenow.getDate();
  strReturn += datenow.getMonth() + 1;
  strReturn += datenow.getFullYear();
  strReturn += datenow.getHours();
  strReturn += datenow.getMinutes();
  strReturn += datenow.getSeconds();
  strReturn += "." + datenow.getMilliseconds();
  return strReturn;
}

function CountryLiveInChanged(){
  countryListLiveIn = YAHOO.util.Dom.get("countryListLiveIn");
  _countryLiveIn.value = countryListLiveIn[countryListLiveIn.selectedIndex].value;
}









//////////////////////////////////////////////////////////////////////////////
// CUSTOMISED YUI CODE FOR LOCATIONS AUTOCOMPLETE 
//////////////////////////////////////////////////////////////////////////////
function InitLocationCombobox() {
  oDS = new YAHOO.util.FunctionDataSource(searchAllLocations); 
  oDS.responseSchema = {fields : ["location","airportcode","stateid","id"]}; 
  
  oACpickup = new YAHOO.widget.AutoComplete("myInputPickup", "myContainerPickup", oDS, oConfigs);
  oACreturn = new YAHOO.widget.AutoComplete("myInputReturn", "myContainerReturn", oDS, oConfigs);

  oContentPickup=YAHOO.util.Dom.getElementsByClassName("yui-ac-content")[0];
  oContentReturn=YAHOO.util.Dom.getElementsByClassName("yui-ac-content")[1];

  // Pickup combobox
  togglerPickup = YAHOO.util.Dom.get("toggleDropDownPickup");
  togglerReturn = YAHOO.util.Dom.get("toggleDropDownReturn");

  
  YAHOO.util.Event.addListener(togglerPickup,"click",togglePickup);
  YAHOO.util.Event.addListener(togglerReturn,"click",toggleReturn);


  oACpickup.itemSelectEvent.subscribe(onPickupSelection);     
  oACreturn.itemSelectEvent.subscribe(onReturnSelection);     

  oACpickup.textboxFocusEvent.subscribe(onPickupFocus);     
  oACreturn.textboxFocusEvent.subscribe(onReturnFocus);     
  YAHOO.util.Event.addListener(YAHOO.util.Dom.get("myInputPickup"),"click",onPickupFocus);
  YAHOO.util.Event.addListener(YAHOO.util.Dom.get("myInputReturn"),"click",onReturnFocus);

  oACpickup.textboxBlurEvent.subscribe(onPickupBlur);     
  oACreturn.textboxBlurEvent.subscribe(onReturnBlur);     

   
  oACpickup.resultTypeList = false;
  oACreturn.resultTypeList = false;

  oACreturn.formatResult = function(oResultData, sQuery, sResultMatch) {return FormatResults(oResultData, sQuery, sResultMatch, "r");};
  oACpickup.formatResult = function(oResultData, sQuery, sResultMatch) {return FormatResults(oResultData, sQuery, sResultMatch, "p");};
 
  return {fnSearch: searchAllLocations, oDS: oDS, oACpickup: oACpickup};
}


var searchAllLocations = function(sQuery) {
//Every keypress of the autocomplete, this gets run to filter results
    var allMatches = [],item, i, l;
    var strPreviousLetter="";
    var bNewAlphabet=false;

    sQuery = sQuery.replace(/%20/g," "); //convert URL coded spaces to a real space

    //match the first part of the location
    for(i=0, l=aryLocations.length; i<l; i++) {
        item = aryLocations[i];
        bNewAlphabet=false;
        if(item[0].toLowerCase().indexOf(sQuery.toLowerCase()) == 0) {
            if(item[0].toLowerCase()==sQuery.toLowerCase()){
              //Trace("exact match typed in. setting now");
              YAHOO.util.Dom.get("myInput" + activeLeg  + "Id").value = item[3];
            }

            if(strPreviousLetter != item[0].charAt(0)){
              if(allMatches.length > ALPHABET_SOUP_THRESHOLD){               
                bThresholdReached = true;
              }
              if(allMatches.length > 0){
                if(strPreviousLetter!=""){
                  bNewAlphabet=true; //to group by alphabet
                }
                strPreviousLetter = item[0].charAt(0);
              }                   
            }
            
            allMatches[allMatches.length] = [item[0], item[1], item[2], item[3], bNewAlphabet];      
        }
            
    }


    //match any part of the location or state. don't do this if there's already over AUTO_HEIGHT_THRESHOLD locations
    if(allMatches.length<AUTO_HEIGHT_THRESHOLD){
      for(i=0, l=aryLocations.length; i<l; i++) {
          item = aryLocations[i];
          bNewAlphabet=false;
          if(item[0].toLowerCase().indexOf(sQuery.toLowerCase()) != -1 || item[1].toLowerCase()== sQuery.toLowerCase()  || item[2].toLowerCase().indexOf(sQuery.toLowerCase()) == 0) {
              if(item[0].toLowerCase()==sQuery.toLowerCase()){
                //Trace("exact match typed in. setting now");
                YAHOO.util.Dom.get("myInput" + activeLeg  + "Id").value = item[3];
                //SameAsPickup();
              }
              
              if(strPreviousLetter != item[0].charAt(0)){
                if(allMatches.length > ALPHABET_SOUP_THRESHOLD){
                  bThresholdReached = true;
                }
                if(allMatches.length > 0){
                  if(strPreviousLetter!=""){
                    bNewAlphabet=true; //to group by alphabet
                  }
                  strPreviousLetter = item[0].charAt(0);
                }                   
              }
              if(item[0].toLowerCase().indexOf(sQuery.toLowerCase()) != 0 || allMatches.length>AUTO_HEIGHT_THRESHOLD){
                allMatches[allMatches.length] = [item[0], item[1], item[2], item[3],bNewAlphabet];
              }
          }
              
      }
    }
    //Help IE drop down list autoresize
    if(allMatches.length>AUTO_HEIGHT_THRESHOLD){     
      YAHOO.util.Dom.setStyle(oContentPickup,"height",MAX_DROP_DOWN_HEIGHT);
      YAHOO.util.Dom.setStyle(oContentReturn,"height",MAX_DROP_DOWN_HEIGHT);
    }else{
      YAHOO.util.Dom.setStyle(oContentPickup,"height","auto");
      YAHOO.util.Dom.setStyle(oContentReturn,"height","auto");
    }

    allMatches[allMatches.length] = [PICKUP_PROMPT,"","","",false];
    this.responseSchema = {fields: ["location","airportcode","stateid","id","newalphabet"]};
    return allMatches;
};    

	    var onPickupSelection = function(sType, aArgs,oData) {
          SetLocationId("Pickup",aArgs[2].id);            
    	    HideLocationSuggestion();
	    };

	    var onReturnSelection = function(sType, aArgs) { 
//	        var myAC = aArgs[0]; // reference back to the AC instance 
//	        var elLI = aArgs[1]; // reference to the selected LI element 
//	        var oData = aArgs[2]; // object literal of selected item's result data 

          SetLocationId("Return",aArgs[2].id);
	        UpdateReturnDepots(); //Update depot address onscreen
	    };

	    var onPickupFocus = function(sType, aArgs,oData) {
    	    activeLeg = "Pickup";
    	    if(YAHOO.util.Dom.get("myInputPickup").value==PICKUP_PROMPT){
    	      if(pickupFrom==""){
              YAHOO.util.Dom.get("myInputPickup").value="";
    	        if(LIST_ALL_LOCATIONS_ONFOCUS){
                togglePickup();  	        
                oACpickup.sendQuery("");
              }
            }
            else{
              if(SUGGESTION_MODE=="AUTOCOMPLETE"){
                YAHOO.util.Dom.get("myInputPickup").value=pickupFrom;
                YAHOO.util.Dom.get("myInputPickup").select();
                
                togglePickup();
                oACpickup.sendQuery(pickupFrom);
              }
              else{
                YAHOO.util.Dom.get("myInputPickup").value="";
              }
            }
          }else{
            YAHOO.util.Dom.get("myInputPickup").select();
          }
	    };

	    var onReturnFocus = function(sType, aArgs,oData) {
 	        //YAHOO.util.Dom.addClass(YAHOO.util.Dom.get("myInputReturn"), "RegularColorhover");
    	    HideLocationSuggestion();
    	    activeLeg = "Return";
    	    if(YAHOO.util.Dom.get("myInputReturn").value==RETURN_PROMPT){
            YAHOO.util.Dom.get("myInputReturn").value=returnTo;
    	      if(returnTo==""){
    	        if(LIST_ALL_LOCATIONS_ONFOCUS){
                toggleReturn();
                oACreturn.sendQuery("");    
              }
            }
            else{
              toggleReturn();
              oACreturn.sendQuery(returnTo);
            }

          }else{
            YAHOO.util.Dom.get("myInputReturn").select();
          }
	    };

	    var onPickupBlur = function(sType, aArgs,oData) {
	        //YAHOO.util.Dom.removeClass(YAHOO.util.Dom.get("myInputPickup"), "RegularColorhover");
    	    if(YAHOO.util.Dom.get("myInputPickup").value==""){
            YAHOO.util.Dom.get("myInputPickup").value=PICKUP_PROMPT;
            ResetDepotAddresses("Pickup");
          }
	    };
	    var onReturnBlur = function(sType, aArgs,oData) {
	        //YAHOO.util.Dom.removeClass(YAHOO.util.Dom.get("myInputReturn"), "RegularColorhover");
    	    if(YAHOO.util.Dom.get("myInputReturn").value==""){
            YAHOO.util.Dom.get("myInputReturn").value=RETURN_PROMPT;
            ResetDepotAddresses("Return");
          }
	    };
    
    // Instantiate AutoCompletes
    var oConfigs = {
        prehighlightClassName: "yui-ac-prehighlight",
        useShadow: false,
        queryDelay: 0,
        minQueryLength: 0,
        forceSelection: false,
        maxResultsDisplayed: 1000,
        autoHighlight: true,
        alwaysShowContainer:false,
        allowBrowserAutocomplete: false  
    };

    var togglePickup = function(e) {
        if(!YAHOO.util.Dom.hasClass(togglerPickup, "open")) {
            YAHOO.util.Dom.addClass(togglerPickup, "open")
        }
  
        // Is open
        if(oACpickup.isContainerOpen()) {
            oACpickup.collapseContainer();
        }
        // Is closed
        else {
            activeLeg = "Pickup";
            oACpickup.getInputEl().focus(); // Needed to keep widget active
            //oACpickup.sendQuery("");

            setTimeout(function() { // For IE
              //oACpickup["alwaysShowContainer"]=false;
              oACpickup.getInputEl().focus(); // Needed to keep widget active
              },0);                      
        }

    };


    var toggleReturn = function(e) {
        if(!YAHOO.util.Dom.hasClass(togglerReturn, "open")) {
            YAHOO.util.Dom.addClass(togglerReturn, "open")
        }
  
        // Is open
        if(oACreturn.isContainerOpen()) {
            oACreturn.collapseContainer();
        }
        // Is closed
        else {
            activeLeg = "Return";
            oACreturn.getInputEl().focus(); // Needed to keep widget active
                //oACreturn.sendQuery("");
    
                setTimeout(function() { // For IE
                  //oACreturn["alwaysShowContainer"]=false;
                  oACreturn.getInputEl().focus(); // Needed to keep widget active
                },0);
        }       
    };
/////////////////////////////////////////////////////////////////////////////
// LOCATIONS
/////////////////////////////////////////////////////////////////////////////


function FormatResults(oResultData, sQuery, sResultMatch, strLegID){
  return FormatLocation(oResultData.location, oResultData.stateid, oResultData.airportcode, oResultData.id, sQuery, sResultMatch, strLegID,oResultData.newalphabet);
}

function FormatLocation(strLocation, strStateID, strAirportCode, strLocationID, sQuery, sResultMatch, strLegID, bNewAlphabet){    
// FormatResults returns markup that bolds the original query, and also displays to additional pieces of supplemental data.
  var strIcon = "",strAirport = "",strState = "";
  var strClass = "";


  if(strLocation==PICKUP_PROMPT){
    return ("Need a different location? Type it in!");
  }
//Highlight currently selected location
  if(bNewAlphabet==true){
    strIcon = "<br>";
  }                                   
  strLocation = strLocation.replace(/<\^>/g,"");
  strLocation = strLocation.replace("International","Int.");

  if(sResultMatch.indexOf("Airport")!=-1){
    strIcon += "<span class='vvvacairport'></span>";
  }else{
    strIcon += "<span class='vvvaccity'></span>";
  }
  
  var nBoldFrom = strLocation.toLowerCase().indexOf(sQuery.toLowerCase());
  if(nBoldFrom > -1){
    strLocation = strLocation.substr(0,nBoldFrom) + "<b>" + strLocation.substr(nBoldFrom, sQuery.length) + "</b>" + strLocation.substr(sQuery.length + nBoldFrom,sResultMatch.length);
  }else{
 //   strLocation = strLocation
  }
  if(strStateID.length>0){
    strState = ",  " + strStateID;
    if(strStateID==sQuery){
      strState = "<b>" + strState + "</b>";
    }                                     
  }
  if(strAirportCode.length>0){
    strAirport = " <span class=ac>(" + strAirportCode + ")</span>";
    if(strAirportCode==sQuery){
      strAirport = "<b>" + strAirport + "</b>";
    }
  }      
  
  return "<span " + strClass + " id='" + strLegID + strLocationID  + "' >" + strIcon + strLocation + strState + strAirport + "</span>";
}

function InitOldSchoolLocationDropDown(){
  var item;
  var state="";
  var strOptions="";   
  var buf=[];
  for(i=0; i<aryLocations.length; i++) {
    item = aryLocations[i];
    state="";
    if(item[2]!=""){
      state=", "+item[2];
    }
    buf.push("<option value=\"" + item[3] + "\">" + item[0].substr(0,40)+state + "</option>");
  }
  buf.push("</select>&nbsp;</div>");
  strOptions = buf.join('');

  YAHOO.util.Dom.get('myAutoCompletePickup').innerHTML = "<div class=olddropdown><select id=ddlPickup tabindex=1 name=ddlPickup onChange=\"SetLocationId('Pickup',YAHOO.util.Dom.get('ddlPickup').value);\"><option value=\"\">Select a Pickup City</option>" + strOptions;
  YAHOO.util.Dom.get('myAutoCompleteReturn').innerHTML = "<div class=olddropdown><select id=ddlReturn tabindex=1 name=ddlReturn onChange=\"SetLocationId('Return',YAHOO.util.Dom.get('ddlReturn').value);\"><option value=\"\">Same as Pickup Location</option>" + strOptions;
}
                         
// Begin - Dynamic drop down population
function PopulateLocations() {

  if(aryLocations.length > 1) {
                               
    if(OLD_SCHOOL_DROPDOWN){
      InitOldSchoolLocationDropDown();
    }
    else{
      if(bLocationsInitialised==false){
        InitLocationCombobox();
        bLocationsInitialised=true;
      }
    }
  
    //Initialise the autocomplete boxes
    if(YAHOO.util.Dom.get('myInputPickupId').value != "") { //BACK BUTTON USED!
      SetLocationId("Pickup",YAHOO.util.Dom.get('myInputPickupId').value);
    }
    else if(pickupLoc != "" || returnLoc != "") { //pickupFrom default location
      SetLocation("Pickup",pickupLoc,"partial");        
    }
    if(YAHOO.util.Dom.get('myInputReturnId').value != "") { //BACK BUTTON USED!
      SetLocationId("Return",YAHOO.util.Dom.get('myInputReturnId').value);
    }
    else if(returnLoc != "" || returnLoc != "") { //return to default location
      SetLocation("Return",returnLoc,"partial");  
    }
    
    if(!OLD_SCHOOL_DROPDOWN){  
      if(YAHOO.util.Dom.get('myInputPickup').value == "Loading cities..."){
          YAHOO.util.Dom.get('myInputPickup').value = PICKUP_PROMPT;
      }
      if(YAHOO.util.Dom.get('myInputReturn').value == "Loading cities..."){
        YAHOO.util.Dom.get('myInputReturn').value = RETURN_PROMPT;
      }
      if(YAHOO.util.Dom.get("myInputReturn").value!="" && YAHOO.util.Dom.get("myInputReturn").value!=RETURN_PROMPT){
        ShowPickup();
      }        
    }
    else if(YAHOO.util.Dom.get('myInputReturnId').value != ""){
       ShowPickup();
    }
    
    

	} 
}

function HideLocationSuggestion(){
    oACpickup.alwaysShowContainer  = false;
    YAHOO.util.Dom.get("divLocationSuggestions").className="hidden";
}

function SetLocation(strLeg, strCityName,strMode) {
  // strLeg Needs to be "Return" or "Pickup". 
  // strCityName is the city to set it selected to. 
  // strMode is "partial" match or "exact" match.
  // selectObject.value = strCityName;

  if(OLD_SCHOOL_DROPDOWN){
    if(setSelectedText(YAHOO.util.Dom.get('ddl'+strLeg),strCityName + " Airport")){
      YAHOO.util.Dom.get("myInput" + strLeg + "Id").value = YAHOO.util.Dom.get('ddl'+strLeg).value;
      return true;
    }
    else if (setSelectedText(YAHOO.util.Dom.get('ddl'+strLeg),strCityName)){
      YAHOO.util.Dom.get("myInput" + strLeg + "Id").value = YAHOO.util.Dom.get('ddl'+strLeg).value;
      return true;
    }
  }
  else{
    var bExact = strMode=="exact";
    for(i=0; i<aryLocations.length; i++) {
      //Trace(aryLocations[i][0]);
      if(aryLocations[i][0].indexOf(strCityName)>=0){
  
        if((bExact && (aryLocations[i][0]==strCityName))) {
          YAHOO.util.Dom.get("myInput" + strLeg).value = aryLocations[i][0];
          YAHOO.util.Dom.get("myInput" + strLeg + "Id").value = aryLocations[i][3];
          YAHOO.util.Dom.get("myInput" + strLeg).className = "RegularColor";
        }       
        else if(bExact==false){
          if(SUGGESTION_MODE=="LIST"){
            YAHOO.util.Dom.get('divLocationSuggestions').innerHTML = GetSuggestedLocations(strCityName);
            YAHOO.util.Dom.get('divLocationSuggestions').className = "visible";
          }
        }
        return true;
      }
    }
  }
  return false; //strCityName wasn't found
}                                                                      

function GetSuggestedLocations(strSearch){
  //strSearch = strSearch.replace(/%20/g," ")
  strSuggestedLocations = "";
  for(i=0; i<aryLocations.length; i++) {
    if(aryLocations[i][0].toLowerCase().indexOf(strSearch.toLowerCase())>-1){
       strSuggestedLocations += "<li onClick=\"HideLocationSuggestion();SetLocationId('Pickup'," + aryLocations[i][3] + ");\">" + LocationToString(aryLocations[i]) + "<br>";
    }
  }
  return '<b>Suggested ' + strSearch + ' Locations</b><br>' + strSuggestedLocations;

}

function SetLocationId(strLeg, strLocationId) {
  // strLeg Needs to be "Return" or "Pickup". 
  // strLocationId is the LocationId to set it selected to.
  var bSuccess=false; 
  if(OLD_SCHOOL_DROPDOWN){
    setSelectedValue(YAHOO.util.Dom.get('ddl'+strLeg),strLocationId);  
    YAHOO.util.Dom.get("myInput" + strLeg + "Id").value = strLocationId;
    bSuccess=true;    
  }                   
  else{  
    for(i=0; i<aryLocations.length; i++) {
      if(aryLocations[i][3] == strLocationId){
          YAHOO.util.Dom.get("myInput" + strLeg).value = LocationToString(aryLocations[i]);
          YAHOO.util.Dom.get("myInput" + strLeg + "Id").value = aryLocations[i][3];
          YAHOO.util.Dom.get("myInput" + strLeg).className = "RegularColor";
          //UpdateDepots(strLeg);
          bSuccess=true;
      }
    }
  }
  if(BARGAIN_WHEELS){
    setTimeout("BargainWheelsEnabled()",20); //spin that wheel. Enable the search button!
    
  }   
  return bSuccess; //location id was or wasn't found
}

function LocationToString(aryLocation){
    strAirportCode = aryLocation[1];
    if(strAirportCode.length > 0){
      strAirportCode = " (" + strAirportCode + ")";
    }
    
    strState = aryLocation[2];
    if(strState.length>0)
      strState = ", " + strState;
    
    return aryLocation[0] + strState + strAirportCode;
}

function RequestNewLocationsJS() {
  //clear the pickup and return drop downs
  if(OLD_SCHOOL_DROPDOWN){
    YAHOO.util.Dom.get('myAutoCompletePickup').innerHTML = "Loading...<br clear=all>";  
    YAHOO.util.Dom.get('myAutoCompleteReturn').innerHTML = "Loading...";
  }
  else{ 
    YAHOO.util.Dom.get('myInputPickup').value = "Loading cities...";
    YAHOO.util.Dom.get('myInputReturn').value = "Loading cities...";
    ResetDepotAddresses("Pickup");
    ResetDepotAddresses("Return");
  }
  
  strCountryPickupID = countryListPickup[countryListPickup.selectedIndex].value;
  if(YAHOO.util.Dom.get('countryListPickup').selectedIndex == 0) {
    //do nothing
  } else {
      
    //need to get the countryID from the country drop down
    countryListPickup = YAHOO.util.Dom.get('countryListPickup');
    if(countryListPickup.value == "" || strCountryPickupID == "Select Country") {
      alert("Please choose a country from the list.");
    } else {
      _countryPickupID.value = strCountryPickupID;
      var postData = "country=" + strCountryPickupID;
      includejs(httpServer + "locationJS.aspx?country=" + strCountryPickupID); //httpServer is set by the end user
    }
  }
}
// End - Dynamic drop down population
function ResetDepotAddresses(strLeg) {
  if(ADVANCED_MODE_ON){
  //YAHOO.util.Dom.get("divToggle" + strLeg + "Addresses").innerHTML="<a href='javascript:onclick=ToggleAddresses(\"" + strLeg + "\");'>" + ADVANCED_PICKER_PROMPT + "</a>";
  YAHOO.util.Dom.get("divToggle" + strLeg + "Addresses").innerHTML="<a href='#vvvmap' onclick='javascript:onclick=OpenVVVMap(\"" + strLeg + "\");'>" + ADVANCED_PICKER_PROMPT + "</a>";
  }
  else{
  YAHOO.util.Dom.get("divToggle" + strLeg + "Addresses").className = "hidden";
  }
}

function SameAsPickup(bForce) {
  if(OLD_SCHOOL_DROPDOWN){
    if(bForce || YAHOO.util.Dom.get('ddlReturn').selectedText == "Same as Pickup Location" || YAHOO.util.Dom.get('ddlReturn').selectedText == "" || YAHOO.util.Dom.get('ddlReturn').value == ""){
      setSelectedValue(YAHOO.util.Dom.get('ddlReturn'),YAHOO.util.Dom.get('ddlPickup').value);  
      YAHOO.util.Dom.get("myInputReturnId").value = YAHOO.util.Dom.get("myInputPickupId").value;
    }    
  }
  else{
    if((bForce || YAHOO.util.Dom.get("myInputReturn").value == "Loading cities..." || YAHOO.util.Dom.get("myInputReturn").value == RETURN_PROMPT || YAHOO.util.Dom.get("myInputReturn").value == "") && (YAHOO.util.Dom.get("myInputPickup").value!=PICKUP_PROMPT)){
      YAHOO.util.Dom.get("myInputReturnId").value = YAHOO.util.Dom.get("myInputPickupId").value;
      YAHOO.util.Dom.get("myInputReturn").value = YAHOO.util.Dom.get("myInputPickup").value;
      YAHOO.util.Dom.get("myInputReturn").className = "RegularColor";
    }
  }
}


//////////////////////////////////////////////////////////////////////////////
// DATE PICKER 
//////////////////////////////////////////////////////////////////////////////

function pickupDateSelect(type,args,obj) {
	dates = args[0]; 
	date = dates[0];
	year = date[0], month = date[1], day = date[2];
  
	strMonth = getMonth(month);
	
	txtPickupDate = YAHOO.util.Dom.get("txtPickupDate");
	_pickupDate = YAHOO.util.Dom.get("_pickupDate");
	
  txtPickupDate.value = day + '-' + strMonth + '-' + year;
	_pickupDate.value = month + '/' + day + '/' + year;  //US date for the search form paramater

  if(YAHOO.vroom.calendar.returncal.getSelectedDates() != "") {
    if(YAHOO.widget.DateMath.before(YAHOO.vroom.calendar.returncal.getSelectedDates()[0], YAHOO.vroom.calendar.pickupcal.getSelectedDates()[0])) {
	    YAHOO.vroom.calendar.returncal.select(YAHOO.widget.DateMath.add(YAHOO.vroom.calendar.pickupcal.getSelectedDates()[0], YAHOO.widget.DateMath.DAY, 3));
      YAHOO.vroom.calendar.returncal.cfg.setProperty("pagedate", month + "/" + year);
      YAHOO.vroom.calendar.returncal.render();
    }
  }

	YAHOO.vroom.calendar.pickupcal.hide();
}

function returnDateSelect(type,args,obj) {
	dates = args[0]; 
	date = dates[0];
	year = date[0], month = date[1], day = date[2];
  
	strMonth = getMonth(month);
  
	txtReturnDate = YAHOO.util.Dom.get("txtReturnDate");
	_returnDate = YAHOO.util.Dom.get("_returnDate");

  txtReturnDate.value = day + '-' + strMonth + '-' + year;
	_returnDate.value = month + '/' + day + '/' + year;  //US date for the search form paramater
	YAHOO.vroom.calendar.returncal.hide();
}

function UpdateCalendar(cal,strDate) {
	if (strDate.value != "") {
		cal.select(strDate);
		var selectedDates = cal.getSelectedDates();
		if (selectedDates.length > 0) {
			cal.cfg.setProperty("pagedate", (selectedDates[0].getMonth()+1) + "/" + selectedDates[0].getFullYear());
			cal.render();
		}
	}
}

function getMonth(month) {
  strMonth;
  switch (month) {
  case 1:
    strMonth = "Jan";
    break;
  case 2:
    strMonth = "Feb";
    break;
  case 3:
    strMonth = "Mar";
    break;
  case 4:
    strMonth = "Apr";
    break;
  case 5:
    strMonth = "May";
    break;
  case 6:
    strMonth = "Jun";
    break;
  case 7:
    strMonth = "Jul";
    break;
  case 8:
    strMonth = "Aug";
    break;
  case 9:
    strMonth = "Sep";
    break;
  case 10:
    strMonth = "Oct";
    break;
  case 11:
    strMonth = "Nov";
    break;
  case 12:
    strMonth = "Dec";
    break;
  }
  return strMonth;
}

function CloseCalendar(e){
  if(!calFocused)
      this.hide();
}



                                                                  





//////////////////////////////////////////////////////////////////////////////
// DEPOT ADDRESSES 
//////////////////////////////////////////////////////////////////////////////

//SERVER SIDE REQUEST FOR DEPOTS
function UpdateDepots(strLeg) {
    //YAHOO.util.Dom.get('divToggle' + strLeg + 'Addresses').innerHTML="<div class=vvvloading></div>";
    YAHOO.util.Dom.get('div' + strLeg + 'Addresses').innerHTML="<div class=vvvloading></div>";
    includejs(httpServer + "depotJS.aspx?format=Array&action=" + strLeg + "&locationid=" + YAHOO.util.Dom.get('myInput' + strLeg + 'Id').value);
}

function ShowPickup(){
  ShowReturn();
}

function ShowReturn(){
  YAHOO.util.Dom.get("divReturn").className="visible";
  YAHOO.util.Dom.get("divCheckSamePickup").className="hidden";
}







//////////////////////////////////////////////////////////////////////////////
// INITIALISATION. LETS GET THIS PARTY STARTED!!!
//////////////////////////////////////////////////////////////////////////////

function InitialisePickupCountries(){
  ddlCountryPickup = YAHOO.util.Dom.get('countryListPickup');
  AddSelection(ddlCountryPickup,"ZZ","Select a Country");
  AddSelection(ddlCountryPickup,"AU","Australia");
  AddSelection(ddlCountryPickup,"CA","Canada");
  AddSelection(ddlCountryPickup,"CY","Cyprus");
  AddSelection(ddlCountryPickup,"FJ","Fiji");
  AddSelection(ddlCountryPickup,"FR","France");
  AddSelection(ddlCountryPickup,"DE","Germany");
  AddSelection(ddlCountryPickup,"IE","Ireland");
  AddSelection(ddlCountryPickup,"IT","Italy");
  AddSelection(ddlCountryPickup,"MT","Malta");
  AddSelection(ddlCountryPickup,"NZ","New Zealand");
  AddSelection(ddlCountryPickup,"PT","Portugal");
  AddSelection(ddlCountryPickup,"ZA","South Africa");
  AddSelection(ddlCountryPickup,"ES","Spain");
  AddSelection(ddlCountryPickup,"GB","United Kingdom");
  AddSelection(ddlCountryPickup,"US","United States");
}

function InitialiseIliveInCountries(){
  ddlCountryLiveIn = YAHOO.util.Dom.get('countryListLiveIn');
  
  AddSelection(ddlCountryLiveIn,"AU","Australia");
  AddSelection(ddlCountryLiveIn,"GB","United Kingdom");
  AddSelection(ddlCountryLiveIn,"US","United States");
  AddSelection(ddlCountryLiveIn,"NZ","New Zealand");
  AddSelection(ddlCountryLiveIn,"AF","Afghanistan");
  AddSelection(ddlCountryLiveIn,"UE","Ajan");

  AddSelection(ddlCountryLiveIn,"AL","Albania");
  AddSelection(ddlCountryLiveIn,"DZ","Algeria");
  AddSelection(ddlCountryLiveIn,"AS","American Samoa");
  AddSelection(ddlCountryLiveIn,"AD","Andorra");
  AddSelection(ddlCountryLiveIn,"AO","Angola");
  AddSelection(ddlCountryLiveIn,"AI","Anguilla");

  AddSelection(ddlCountryLiveIn,"AG","Antigua");
  AddSelection(ddlCountryLiveIn,"AR","Argentina");
  AddSelection(ddlCountryLiveIn,"AB","Aruba");
  AddSelection(ddlCountryLiveIn,"AU","Australia");
  AddSelection(ddlCountryLiveIn,"AT","Austria");
  AddSelection(ddlCountryLiveIn,"BS","Bahamas");

  AddSelection(ddlCountryLiveIn,"BH","Bahrain");
  AddSelection(ddlCountryLiveIn,"BL","Bangladesh");
  AddSelection(ddlCountryLiveIn,"BB","Barbados");
  AddSelection(ddlCountryLiveIn,"BE","Belgium");
  AddSelection(ddlCountryLiveIn,"BZ","Belize");
  AddSelection(ddlCountryLiveIn,"BP","Benin");

  AddSelection(ddlCountryLiveIn,"BM","Bermuda");
  AddSelection(ddlCountryLiveIn,"BT","Bhutan");
  AddSelection(ddlCountryLiveIn,"BO","Bolivia");
  AddSelection(ddlCountryLiveIn,"YE","Bonaire");
  AddSelection(ddlCountryLiveIn,"BW","Botswana");
  AddSelection(ddlCountryLiveIn,"BR","Brazil");

  AddSelection(ddlCountryLiveIn,"BN","Brunei Darussalam");
  AddSelection(ddlCountryLiveIn,"BG","Bulgaria");
  AddSelection(ddlCountryLiveIn,"BU","Burma");
  AddSelection(ddlCountryLiveIn,"BI","Burundi");
  AddSelection(ddlCountryLiveIn,"CM","Cameroon");
  AddSelection(ddlCountryLiveIn,"CA","Canada");

  AddSelection(ddlCountryLiveIn,"CB","Cape Verde");
  AddSelection(ddlCountryLiveIn,"CE","Carolina Island");
  AddSelection(ddlCountryLiveIn,"CC","Cayman Island");
  AddSelection(ddlCountryLiveIn,"CF","Central African Rep.");
  AddSelection(ddlCountryLiveIn,"CD","Chad");
  AddSelection(ddlCountryLiveIn,"JY","Channel Islands");

  AddSelection(ddlCountryLiveIn,"CL","Chile");
  AddSelection(ddlCountryLiveIn,"CN","China");
  AddSelection(ddlCountryLiveIn,"CO","Colombia");
  AddSelection(ddlCountryLiveIn,"CJ","Comoros");
  AddSelection(ddlCountryLiveIn,"CG","Congo");
  AddSelection(ddlCountryLiveIn,"CK","Cook Islands");

  AddSelection(ddlCountryLiveIn,"CR","Costa Rica");
  AddSelection(ddlCountryLiveIn,"HR","Croatia");
  AddSelection(ddlCountryLiveIn,"CU","Cuba");
  AddSelection(ddlCountryLiveIn,"CY","Cyprus");
  AddSelection(ddlCountryLiveIn,"CS","Czechoslovakia");
  AddSelection(ddlCountryLiveIn,"DY","Dahomey");

  AddSelection(ddlCountryLiveIn,"DK","Denmark");
  AddSelection(ddlCountryLiveIn,"DR","Djibouti");
  AddSelection(ddlCountryLiveIn,"DO","Dominican Republic");
  AddSelection(ddlCountryLiveIn,"DM","Dominicia");
  AddSelection(ddlCountryLiveIn,"UE","Dubai");
  AddSelection(ddlCountryLiveIn,"EC","Ecuador");

  AddSelection(ddlCountryLiveIn,"EG","Egypt");
  AddSelection(ddlCountryLiveIn,"EL","El Salvador");
  AddSelection(ddlCountryLiveIn,"EI","Ellice Islands");
  AddSelection(ddlCountryLiveIn,"EQ","Equatorial Guinea");
  AddSelection(ddlCountryLiveIn,"ER","Eritrea");
  AddSelection(ddlCountryLiveIn,"EE","Estonia");

  AddSelection(ddlCountryLiveIn,"ET","Ethiopia");
  AddSelection(ddlCountryLiveIn,"FA","Falkland Islands");
  AddSelection(ddlCountryLiveIn,"FJ","Fiji");
  AddSelection(ddlCountryLiveIn,"FI","Finland");
  AddSelection(ddlCountryLiveIn,"FR","France");
  AddSelection(ddlCountryLiveIn,"FG","French Guiana");

  AddSelection(ddlCountryLiveIn,"UE","Fumairah");
  AddSelection(ddlCountryLiveIn,"GA","Gabon");
  AddSelection(ddlCountryLiveIn,"GM","Gambia");
  AddSelection(ddlCountryLiveIn,"DE","Germany");
  AddSelection(ddlCountryLiveIn,"GH","Ghana");
  AddSelection(ddlCountryLiveIn,"GI","Gibraltar");

  AddSelection(ddlCountryLiveIn,"GL","Gilbert Islands");
  AddSelection(ddlCountryLiveIn,"CT","Grand Cayman");
  AddSelection(ddlCountryLiveIn,"GR","Greece");
  AddSelection(ddlCountryLiveIn,"GG","Greenland");
  AddSelection(ddlCountryLiveIn,"GD","Grenada");
  AddSelection(ddlCountryLiveIn,"GP","Guadeloupe");

  AddSelection(ddlCountryLiveIn,"GU","Guam");
  AddSelection(ddlCountryLiveIn,"GT","Guatemala");
  AddSelection(ddlCountryLiveIn,"GN","Guinea");
  AddSelection(ddlCountryLiveIn,"GY","Guyana");
  AddSelection(ddlCountryLiveIn,"HT","Haiti");
  AddSelection(ddlCountryLiveIn,"HD","Honduras");

  AddSelection(ddlCountryLiveIn,"HK","Hong Kong");
  AddSelection(ddlCountryLiveIn,"HU","Hungary");
  AddSelection(ddlCountryLiveIn,"IS","Iceland");
  AddSelection(ddlCountryLiveIn,"IN","India");
  AddSelection(ddlCountryLiveIn,"ID","Indonesia");
  AddSelection(ddlCountryLiveIn,"IR","Iran");

  AddSelection(ddlCountryLiveIn,"IQ","Iraq");
  AddSelection(ddlCountryLiveIn,"IE","Ireland");
  AddSelection(ddlCountryLiveIn,"IM","Isle of Man");
  AddSelection(ddlCountryLiveIn,"IL","Israel");
  AddSelection(ddlCountryLiveIn,"IT","Italy");
  AddSelection(ddlCountryLiveIn,"JM","Jamaica");

  AddSelection(ddlCountryLiveIn,"JP","Japan");
  AddSelection(ddlCountryLiveIn,"JO","Jordan");
  AddSelection(ddlCountryLiveIn,"KH","Kampuchea");
  AddSelection(ddlCountryLiveIn,"KE","Kenya");
  AddSelection(ddlCountryLiveIn,"KI","Kinbati");
  AddSelection(ddlCountryLiveIn,"KD","Korea, North");

  AddSelection(ddlCountryLiveIn,"KP","Korea, South");
  AddSelection(ddlCountryLiveIn,"KW","Kuwait");
  AddSelection(ddlCountryLiveIn,"LA","Laos Peoples Rep");
  AddSelection(ddlCountryLiveIn,"LB","Lebanon");
  AddSelection(ddlCountryLiveIn,"LS","Lesotho");
  AddSelection(ddlCountryLiveIn,"LR","Liberia");

  AddSelection(ddlCountryLiveIn,"LY","Libya");
  AddSelection(ddlCountryLiveIn,"LI","Lichtenstein");
  AddSelection(ddlCountryLiveIn,"LE","Line Islands");
  AddSelection(ddlCountryLiveIn,"LT","Lithuania");
  AddSelection(ddlCountryLiveIn,"LU","Luxembourg");
  AddSelection(ddlCountryLiveIn,"MG","Madagascar");

  AddSelection(ddlCountryLiveIn,"MW","Malawi");
  AddSelection(ddlCountryLiveIn,"MY","Malaysia");
  AddSelection(ddlCountryLiveIn,"MI","Maldive Islands");
  AddSelection(ddlCountryLiveIn,"ML","Mali");
  AddSelection(ddlCountryLiveIn,"MT","Malta");
  AddSelection(ddlCountryLiveIn,"MR","Marianna Island");

  AddSelection(ddlCountryLiveIn,"MS","Marshall Islands");
  AddSelection(ddlCountryLiveIn,"MQ","Martinique");
  AddSelection(ddlCountryLiveIn,"MM","Mauritarua");
  AddSelection(ddlCountryLiveIn,"MU","Mauritius");
  AddSelection(ddlCountryLiveIn,"MX","Mexico");
  AddSelection(ddlCountryLiveIn,"MC","Monaco");

  AddSelection(ddlCountryLiveIn,"MO","Mongolia");
  AddSelection(ddlCountryLiveIn,"MN","Monserrat");
  AddSelection(ddlCountryLiveIn,"MA","Morocco");
  AddSelection(ddlCountryLiveIn,"MZ","Mozambique");
  AddSelection(ddlCountryLiveIn,"SW","Namibia");
  AddSelection(ddlCountryLiveIn,"NA","Nauru");

  AddSelection(ddlCountryLiveIn,"NE","Nepal");
  AddSelection(ddlCountryLiveIn,"AN","Netherland Antilles");
  AddSelection(ddlCountryLiveIn,"NL","Netherlands");
  AddSelection(ddlCountryLiveIn,"NC","New Caledonia");
  AddSelection(ddlCountryLiveIn,"NZ","New Zealand");
  AddSelection(ddlCountryLiveIn,"NI","Nicaragua");

  AddSelection(ddlCountryLiveIn,"NR","Niger");
  AddSelection(ddlCountryLiveIn,"NG","Nigeria");
  AddSelection(ddlCountryLiveIn,"NS","Norfolk Island");
  AddSelection(ddlCountryLiveIn,"NO","Norway");
  AddSelection(ddlCountryLiveIn,"OM","Oman");
  AddSelection(ddlCountryLiveIn,"PK","Pakistan");

  AddSelection(ddlCountryLiveIn,"PA","Panama");
  AddSelection(ddlCountryLiveIn,"NU","Papua New Guinea");
  AddSelection(ddlCountryLiveIn,"PY","Paraguay");
  AddSelection(ddlCountryLiveIn,"PE","Peru");
  AddSelection(ddlCountryLiveIn,"PH","Philippines");
  AddSelection(ddlCountryLiveIn,"PI","Phoenix Island");

  AddSelection(ddlCountryLiveIn,"PL","Poland");
  AddSelection(ddlCountryLiveIn,"PT","Portugal");
  AddSelection(ddlCountryLiveIn,"PR","Puerto Rico");
  AddSelection(ddlCountryLiveIn,"QA","Qatar");
  AddSelection(ddlCountryLiveIn,"UE","Ras Al Khaimah");
  AddSelection(ddlCountryLiveIn,"RK","Rep. of Kiribati");

  AddSelection(ddlCountryLiveIn,"RI","Reunion Islands");
  AddSelection(ddlCountryLiveIn,"RO","Romania");
  AddSelection(ddlCountryLiveIn,"RW","Rwanda");
  AddSelection(ddlCountryLiveIn,"SH","Saba");
  AddSelection(ddlCountryLiveIn,"SB","Sabah");
  AddSelection(ddlCountryLiveIn,"BY","Saint Bartholemew");

  AddSelection(ddlCountryLiveIn,"CX","Saint Croix");
  AddSelection(ddlCountryLiveIn,"EU","Saint Eustatius");
  AddSelection(ddlCountryLiveIn,"JN","Saint Johns");
  AddSelection(ddlCountryLiveIn,"SK","Saint Kitts");
  AddSelection(ddlCountryLiveIn,"LC","Saint Lucia");
  AddSelection(ddlCountryLiveIn,"GP","Saint Maarten");

  AddSelection(ddlCountryLiveIn,"CV","Saint Thomas");
  AddSelection(ddlCountryLiveIn,"VC","Saint Vincent");
  AddSelection(ddlCountryLiveIn,"SP","Saipan");
  AddSelection(ddlCountryLiveIn,"AS","Samoa, American");
  AddSelection(ddlCountryLiveIn,"WS","Samoa, Independent");

  AddSelection(ddlCountryLiveIn,"SM","San Marino");
  AddSelection(ddlCountryLiveIn,"ST","Sao Tome");
  AddSelection(ddlCountryLiveIn,"KT","Sarawak");
  AddSelection(ddlCountryLiveIn,"SA","Saudi Arabia");
  AddSelection(ddlCountryLiveIn,"SN","Senegal");
  AddSelection(ddlCountryLiveIn,"SC","Seychelles");

  AddSelection(ddlCountryLiveIn,"UE","Sharjah");
  AddSelection(ddlCountryLiveIn,"SL","Sierra Leone");
  AddSelection(ddlCountryLiveIn,"SX","Sikkim");
  AddSelection(ddlCountryLiveIn,"SG","Singapore");
  AddSelection(ddlCountryLiveIn,"SJ","SLOVENIA");
  AddSelection(ddlCountryLiveIn,"SO","Society Islands");

  AddSelection(ddlCountryLiveIn,"SI","Solomon Islands");
  AddSelection(ddlCountryLiveIn,"SQ","Somalia");
  AddSelection(ddlCountryLiveIn,"ZA","South Africa");
  AddSelection(ddlCountryLiveIn,"SU","Soviet Union");
  AddSelection(ddlCountryLiveIn,"ES","Spain");
  AddSelection(ddlCountryLiveIn,"LK","Sri Lanka");

  AddSelection(ddlCountryLiveIn,"SD","Sudan");
  AddSelection(ddlCountryLiveIn,"SR","Surinam");
  AddSelection(ddlCountryLiveIn,"SZ","Swaziland");
  AddSelection(ddlCountryLiveIn,"SE","Sweden");
  AddSelection(ddlCountryLiveIn,"CH","Switzerland");
  AddSelection(ddlCountryLiveIn,"SY","Syria");

  AddSelection(ddlCountryLiveIn,"TA","Tahiti");
  AddSelection(ddlCountryLiveIn,"TW","Taiwan");
  AddSelection(ddlCountryLiveIn,"TZ","Tanzania");
  AddSelection(ddlCountryLiveIn,"TH","Thailand");
  AddSelection(ddlCountryLiveIn,"TP","Timor");
  AddSelection(ddlCountryLiveIn,"TT","Tobago");

  AddSelection(ddlCountryLiveIn,"TG","Togo");
  AddSelection(ddlCountryLiveIn,"TO","Tonga");
  AddSelection(ddlCountryLiveIn,"TL","Tortola");
  AddSelection(ddlCountryLiveIn,"TT","Trinidad");
  AddSelection(ddlCountryLiveIn,"TN","Tunisia");
  AddSelection(ddlCountryLiveIn,"TR","Turkey");

  AddSelection(ddlCountryLiveIn,"TC","Turks Caicos");
  AddSelection(ddlCountryLiveIn,"UG","Uganda");
  AddSelection(ddlCountryLiveIn,"UA","Ukraine");
  AddSelection(ddlCountryLiveIn,"AE","United Arab Emirates");
  AddSelection(ddlCountryLiveIn,"GB","United Kingdom");
  AddSelection(ddlCountryLiveIn,"US","United States");

  AddSelection(ddlCountryLiveIn,"UV","Upper Volta");
  AddSelection(ddlCountryLiveIn,"UY","Uruguay");
  AddSelection(ddlCountryLiveIn,"NH","VANUATU");
  AddSelection(ddlCountryLiveIn,"VA","Vatican City");
  AddSelection(ddlCountryLiveIn,"VE","Venezuela");
  AddSelection(ddlCountryLiveIn,"VN","Vietnam");

  AddSelection(ddlCountryLiveIn,"VI","Virgin Islands, UK");
  AddSelection(ddlCountryLiveIn,"VI","Virgin Islands, US");
  AddSelection(ddlCountryLiveIn,"YD","Yemen Dem Republic");
  AddSelection(ddlCountryLiveIn,"YU","Yugoslavia");
  AddSelection(ddlCountryLiveIn,"ZM","Zaire");
  AddSelection(ddlCountryLiveIn,"ZB","Zambia"); 
  AddSelection(ddlCountryLiveIn,"KF","Zimbabwe");

}

function InitialiseTimes(ddl){
  if(ddl.length<1){
    AddSelection(ddl,"00:00","12:00 Midnight");
    AddSelection(ddl,"00:30","12:30 AM");
    AddSelection(ddl,"01:00","1:00 AM");
    AddSelection(ddl,"01:30","1:30 AM");
    AddSelection(ddl,"02:00","2:00 AM");
    AddSelection(ddl,"02:30","2:30 AM");
    AddSelection(ddl,"03:00","3:00 AM");
    AddSelection(ddl,"03:30","3:30 AM");
    AddSelection(ddl,"04:00","4:00 AM");
    AddSelection(ddl,"04:30","4:30 AM");
    AddSelection(ddl,"05:00","5:00 AM");
    AddSelection(ddl,"05:30","5:30 AM");
    AddSelection(ddl,"06:00","6:00 AM");
    AddSelection(ddl,"06:30","6:30 AM");
    AddSelection(ddl,"07:00","7:00 AM");
    AddSelection(ddl,"07:30","7:30 AM");
    AddSelection(ddl,"08:00","8:00 AM");
    AddSelection(ddl,"08:30","8:30 AM");
    AddSelection(ddl,"09:00","9:00 AM");
    AddSelection(ddl,"09:30","9:30 AM");
    AddSelection(ddl,"10:00","10:00 AM");
    AddSelection(ddl,"10:30","10:30 AM");
    AddSelection(ddl,"11:00","11:00 AM");
    AddSelection(ddl,"11:30","11:30 AM");
    AddSelection(ddl,"12:00","12:00 Midday");
    AddSelection(ddl,"12:30","12:30 PM");
    AddSelection(ddl,"13:00","1:00 PM");
    AddSelection(ddl,"13:30","1:30 PM");
    AddSelection(ddl,"14:00","2:00 PM");
    AddSelection(ddl,"14:30","2:30 PM");
    AddSelection(ddl,"15:00","3:00 PM");
    AddSelection(ddl,"15:30","3:30 PM");
    AddSelection(ddl,"16:00","4:00 PM");
    AddSelection(ddl,"16:30","4:30 PM");
    AddSelection(ddl,"17:00","5:00 PM");
    AddSelection(ddl,"17:30","5:30 PM");
    AddSelection(ddl,"18:00","6:00 PM");
    AddSelection(ddl,"18:30","6:30 PM");
    AddSelection(ddl,"19:00","7:00 PM");
    AddSelection(ddl,"19:30","7:30 PM");
    AddSelection(ddl,"20:00","8:00 PM");
    AddSelection(ddl,"20:30","8:30 PM");
    AddSelection(ddl,"21:00","9:00 PM");
    AddSelection(ddl,"21:30","9:30 PM");
    AddSelection(ddl,"22:00","10:00 PM");
    AddSelection(ddl,"22:30","10:30 PM");
    AddSelection(ddl,"23:00","11:00 PM");
    AddSelection(ddl,"24:30","11:30 PM");
    setSelectedValue(ddl,"10:00");
  } 
}
          
          
function init() {
  InitialisePickupCountries();
  InitialiseIliveInCountries();
  InitialiseTimes(YAHOO.util.Dom.get("returnTime"));
  InitialiseTimes(YAHOO.util.Dom.get("pickupTime"));
  _init = YAHOO.util.Dom.get('_init');

  countryID = (typeof(countryID) == 'undefined') ? "" : countryID;
  countryID = (countryID == "") ? getQueryVariable("countryID") : countryID;
     
  pickupFrom = (typeof(pickupFrom) == 'undefined') ? "" : pickupFrom;
  pickupFrom = (pickupFrom == "") ? getQueryVariable("pickupFrom") : pickupFrom;
  returnTo = (typeof(returnTo) == 'undefined') ? "" : returnTo;
  returnTo = (returnTo == "") ? getQueryVariable("returnTo") : returnTo;

  pickupDate = (typeof(pickupDate) == 'undefined') ? "" : pickupDate;
  pickupDate = (pickupDate == "") ? getQueryVariable("pickupDate") : pickupDate;
  pickupDate = (pickupDate == "") ? (YAHOO.widget.DateMath.add(new Date(), YAHOO.widget.DateMath.DAY, 1)) : new Date(pickupDate);
                             
  returnDate = (typeof(returnDate) == 'undefined') ? "" : returnDate;
  returnDate = (returnDate == "") ? getQueryVariable("returnDate") : returnDate;
  returnDate = (returnDate == "") ? (YAHOO.widget.DateMath.add(pickupDate, YAHOO.widget.DateMath.DAY, 4)) : new Date(returnDate);
  
  pickupTime = (typeof(pickupTime) == 'undefined') ? "" : pickupTime;
  pickupTime = (pickupTime == "") ? getQueryVariable("pickupTime") : pickupTime;
  
  returnTime = (typeof(returnTime) == 'undefined') ? "" : returnTime;
  returnTime = (returnTime == "") ? getQueryVariable("returnTime") : returnTime;

  age = (age == null) ? "" : age;
  bDowntown = (typeof(pickupFrom) == 'undefined') ? "false" : (pickupFrom.indexOf("City")>-1); //eg: if set to Chicago City, we set bDowntown=true 
  affiliateID = (typeof(affiliateID) == 'undefined') ? "" : affiliateID;
  affiliateID = (affiliateID == "") ? getQueryVariable("affiliateID") : affiliateID;
  pickupLoc = pickupFrom;
  returnLoc = returnTo;
  
  YAHOO.vroom.calendar.pickupcal = new YAHOO.widget.CalendarGroup("pickupcal","pickupcalContainer", { title:"Pickup date:", PAGES:1, close:true,HIDE_BLANK_WEEKS:true} );
	YAHOO.vroom.calendar.pickupcal.selectEvent.subscribe(pickupDateSelect, YAHOO.vroom.calendar.pickupcal, true);
  YAHOO.util.Event.addListener("pickupcalimg", "click", YAHOO.vroom.calendar.pickupcal.show, YAHOO.vroom.calendar.pickupcal, true);
  YAHOO.util.Event.addListener("txtPickupDate", "focus", YAHOO.vroom.calendar.pickupcal.show, YAHOO.vroom.calendar.pickupcal, true);
  YAHOO.util.Event.addListener("pickupcalimg", "blur", CloseCalendar, YAHOO.vroom.calendar.pickupcal, true);
  YAHOO.util.Event.addListener("txtPickupDate", "blur", CloseCalendar, YAHOO.vroom.calendar.pickupcal, true);
  YAHOO.util.Event.addListener("pickupcalContainer", "blur", CloseCalendar, YAHOO.vroom.calendar.pickupcal, true);
  YAHOO.util.Event.addListener("pickupcalContainer", "mouseover", function(){calFocused=true}, YAHOO.vroom.calendar.pickupcal, true);
  YAHOO.util.Event.addListener("pickupcalContainer", "mouseout", function(){calFocused=false}, YAHOO.vroom.calendar.pickupcal, true);

  YAHOO.vroom.calendar.returncal = new YAHOO.widget.CalendarGroup("returncal","returncalContainer", { title:"Return date:", PAGES:1, close:true, HIDE_BLANK_WEEKS:true} );
	YAHOO.vroom.calendar.returncal.selectEvent.subscribe(returnDateSelect, YAHOO.vroom.calendar.returncal, true);
  YAHOO.util.Event.addListener("returncalimg", "click", YAHOO.vroom.calendar.returncal.show, YAHOO.vroom.calendar.returncal, true);
  YAHOO.util.Event.addListener("txtReturnDate", "focus", YAHOO.vroom.calendar.returncal.show, YAHOO.vroom.calendar.returncal, true);
  YAHOO.util.Event.addListener("returncalimg", "blur", CloseCalendar, YAHOO.vroom.calendar.returncal, true);
  YAHOO.util.Event.addListener("txtReturnDate", "blur", CloseCalendar, YAHOO.vroom.calendar.returncal, true);
  YAHOO.util.Event.addListener("returncalContainer", "mouseover", function(){calFocused=true}, YAHOO.vroom.calendar.returncal, true);
  YAHOO.util.Event.addListener("returncalContainer", "mouseout", function(){calFocused=false}, YAHOO.vroom.calendar.returncal, true);

  
  
  //previously saved stuff                
  YAHOO.util.Dom.setStyle('Loading', 'display', 'none');
  _init = YAHOO.util.Dom.get('_init');
	txtPickupDate = YAHOO.util.Dom.get("txtPickupDate");
	_pickupDate = YAHOO.util.Dom.get("_pickupDate");
	txtReturnDate = YAHOO.util.Dom.get("txtReturnDate");
	_returnDate = YAHOO.util.Dom.get("_returnDate");
  countryListPickup = YAHOO.util.Dom.get('countryListPickup');
	_countryPickupID = YAHOO.util.Dom.get("_countryPickupID");
	_pickupId = YAHOO.util.Dom.get("_pickupId");
	_returnIndex = YAHOO.util.Dom.get("_returnIndex");
	_countryLiveIn = YAHOO.util.Dom.get("_countryLiveIn");
                                                 
  setSelectedValue(YAHOO.util.Dom.get("countryListPickup"), YAHOO.util.Dom.get('_countryPickupID').value);
  setSelectedValue(YAHOO.util.Dom.get("countryListLiveIn"), YAHOO.util.Dom.get('_countryLiveIn').value);
                                           
  if(!SHOW_FOOTER){
    YAHOO.util.Dom.get("vvvfooter").className="hidden";    
  }

  if(_init.value != "true") {
    //ONLY DO THIS IT'S THE FIRST TIME THE PAGE IS LOADED (not back button nor refresh)
    _countryPickupID.value = countryID;
    _countryLiveIn.value = countryID;

    if(countryID != "") {
      setSelectedValue(YAHOO.util.Dom.get("countryListPickup"), countryID);
      setSelectedValue(YAHOO.util.Dom.get("countryListLiveIn"), countryID);
    }
    if(pickupTime != "") {
      setSelectedValue(YAHOO.util.Dom.get("pickupTime"), pickupTime);
    }
    if(returnTime != "") {
      setSelectedValue(YAHOO.util.Dom.get("returnTime"), returnTime);
    }
    if(age != "") {
      setSelectedValue(YAHOO.util.Dom.get("ageList"), age);
    }
  	YAHOO.vroom.calendar.pickupcal.select(pickupDate);
    YAHOO.vroom.calendar.returncal.select(returnDate);
  }

  YAHOO.util.Dom.get("checkSameAsPickup").checked=true;                                        
                         
  UpdateCalendar(YAHOO.vroom.calendar.pickupcal,_pickupDate.value);
  UpdateCalendar(YAHOO.vroom.calendar.returncal,_returnDate.value);
  
  YAHOO.vroom.calendar.pickupcal.render();
  YAHOO.vroom.calendar.returncal.render();

  _init.value = "true";

  if(countryListPickup.selectedIndex > 0) {
    setTimeout("RequestNewLocationsJS()",1); //fix IE
  }

}

//////////////////////////////////////////////////////////////////////////////
// GET THE PARTY STARTED 
//////////////////////////////////////////////////////////////////////////////

setTimeout("init()",10);
