/////////////////////////////////////////////// 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="http://www.vroomvroomvroom.com.au/";
  //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;
}
if(typeof(ADVANCED_MODE) == 'undefined') {
  var ADVANCED_MODE = "OFF";
}
if(typeof(DEBUG) == 'undefined') {
  var DEBUG = false;
}
if(typeof(DEPOT_SELECTION) == 'undefined'){
  var DEPOT_SELECTION = "COLLAPSED";                            
}

THEME = (typeof(THEME) == 'undefined') ? "" : THEME;
if (THEME == "") {
  if (getQueryVariable("theme") == "") {
    THEME = THEME;
  } else {
    THEME = getQueryVariable("theme");
  }
} else {
  if (getQueryVariable("theme") == "") {
    THEME = THEME;
  } else {
    THEME = getQueryVariable("theme")
  }
}

CURVED_CORNER = (typeof(CURVED_CORNER) == 'undefined') ? "" : CURVED_CORNER;
if (CURVED_CORNER == "") {
  if (getQueryVariable("curvebg") == "") {
    CURVED_CORNER = "";
  } else {
    CURVED_CORNER = getQueryVariable("curvebg");
  }
} else {
  if (getQueryVariable("curvebg") == "") {
    CURVED_CORNER = CURVED_CORNER;
  } else {
    CURVED_CORNER = getQueryVariable("curvebg")
  }
}

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 = "200px";
var ADVANCED_MODE_ON = false;

var _init;
var calFocused,bLocationsInitialised,vvvmap,bDowntown = false;
var indexPickupLocation,indexPickupLocation; 

// Location combobox variables
var oACpickup,oACreturn, oContentPickup, oContentReturn;
var bThresholdReached=false;
var bLocationsInitialised = false;
var activeLeg = "Pickup"; //"Pickup" or "Return"
var aryDepots;

YAHOO.namespace("vroom.calendar"); //Yahoo namespace used for the YUI!calendar
var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
if(isIE6) {OLD_SCHOOL_DROPDOWN=true;}

//////////////////////////////////////////////////////////////////////////////
// EXTRA HTML
//////////////////////////////////////////////////////////////////////////////
if(THEME!="") {document.write("<link type=\"text/css\" rel=\"stylesheet\" href=\"" + HTTPSERVER + "SearchFresh/styles/" + THEME + "super.css\" \/> ");}
document.write("<div id='vvvdialog'></div>");
document.write("<br clear=\"all\"/>");

if(CURVED_CORNER == "off") {
  document.getElementById("CornerTL").setAttribute("class", "corner cornerOFF TL");
  document.getElementById("CornerTR").setAttribute("class", "corner cornerOFF TR");
  document.getElementById("CornerBL").setAttribute("class", "corner cornerOFF BL");
  document.getElementById("CornerBR").setAttribute("class", "corner cornerOFF BR");
} else {
  document.getElementById("CornerTL").setAttribute("class", "corner " + CURVED_CORNER + " TL");
  document.getElementById("CornerTR").setAttribute("class", "corner " + CURVED_CORNER + " TR");
  document.getElementById("CornerBL").setAttribute("class", "corner " + CURVED_CORNER + " BL");
  document.getElementById("CornerBR").setAttribute("class", "corner " + CURVED_CORNER + " BR");
}

//////////////////////////////////////////////////////////////////////////////
// DEPOT AND LOCATION CLASSES
//////////////////////////////////////////////////////////////////////////////
var locations = new Object();
var depots = new Object();

function ClassDepot(aryNewDepot) {
  this.supplierid = aryNewDepot[0];
  this.depotid = aryNewDepot[1];
  this.address = aryNewDepot[2];
  this.displayname = aryNewDepot[3];
  this.country = aryNewDepot[4];
  this.postcode = aryNewDepot[5];
  this.airport = aryNewDepot[6];
  this.latitude = aryNewDepot[7];
  this.longitude = aryNewDepot[8];
  this.fulladdress = this.address + " " + this.displayname + ", " + this.country + ". " + this.postcode;
  this.selected = false;
}

var aryPickupDepots = new Array();
var aryReturnDepots = new Array();

//////////////////////////////////////////////////////////////////////////////
// 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 "";
}

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) {
  if(DEBUG === true) {
    YAHOO.util.Dom.get("divTrace").innerHTML = strTraceMe + "<br />" + YAHOO.util.Dom.get("divTrace").innerHTML;
  }
}

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
  var strURL,strProtocol;
  var 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
  var 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;
  
  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;
  strCountryPickupID = countryListPickup[countryListPickup.selectedIndex].value;
  
  //SAVE SOME OF THE VARIABLES TO HIDDEN TEXT BOXES SO THEY CAN BE LOADED AFTER USING BACK BUTTON  
  SaveDepotChoices();
  YAHOO.util.Dom.get('_pickupTime').value = PICKUPTIME;   
  YAHOO.util.Dom.get('_returnTime').value = RETURNTIME;  

  //BUILD THE SEARCH URL
  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()) {
    YAHOO.util.Dom.get("_countryLiveIn").value = YAHOO.util.Dom.get('countryListLiveIn').value;
    YAHOO.util.Dom.get('btnSearch').value = "Please Wait...";
    top.location = GenerateURL();
	//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() {
  var countryListLiveIn = YAHOO.util.Dom.get("countryListLiveIn");
  YAHOO.util.Dom.get("_countryLiveIn").value = countryListLiveIn[countryListLiveIn.selectedIndex].value;
}

//////////////////////////////////////////////////////////////////////////////
// CUSTOMISED YUI CODE FOR LOCATIONS AUTOCOMPLETE 
//////////////////////////////////////////////////////////////////////////////
function InitLocationCombobox() {
  var 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];

  YAHOO.util.Event.addListener(YAHOO.util.Dom.get("toggleDropDownPickup"),"click",togglePickup);
  YAHOO.util.Event.addListener(YAHOO.util.Dom.get("toggleDropDownReturn"),"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()) {
        if(YAHOO.util.Dom.get("myInput" + activeLeg + "Id").value!=item[3]){
          YAHOO.util.Dom.get("_depots").value = "";
        }
        //Trace("exact match typed in and has changed. 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(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) {
  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(YAHOO.util.Dom.get("toggleDropDownPickup"), "open")) {
    YAHOO.util.Dom.addClass(YAHOO.util.Dom.get("toggleDropDownPickup"), "open");
  }

  // Is open
  if(oACpickup.isContainerOpen()) {
    oACpickup.collapseContainer();
  }
  // Is closed
  else {
    activeLeg = "Pickup";
    oACpickup.getInputEl().focus(); // Needed to keep widget active

    setTimeout(function() { // For IE
      oACpickup.getInputEl().focus(); // Needed to keep widget active
      },0);                      
  }
};

var toggleReturn = function(e) {
  if(!YAHOO.util.Dom.hasClass(YAHOO.util.Dom.get("toggleDropDownReturn"), "open")) {
    YAHOO.util.Dom.addClass(YAHOO.util.Dom.get("toggleDropDownReturn"), "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>";
  return strLocation;
}

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(PICKUPFROM != "" || RETURNTO != "") { //pickupFrom default location
      SetLocation("Pickup",PICKUPFROM,"partial");
    }
    if(YAHOO.util.Dom.get('myInputReturnId').value != "") { //BACK BUTTON USED!
      SetLocationId("Return",YAHOO.util.Dom.get('myInputReturnId').value);
    }
    else if(RETURNTO != "" || RETURNTO != "") { //return to default location
      SetLocation("Return",RETURNTO,"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++) {
      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.
  // Trace("Old Location ID:" + YAHOO.util.Dom.get("myInput" + strLeg + "Id").value);
  // Trace("New Location ID:" + strLocationId);

  if(YAHOO.util.Dom.get("myInput" + strLeg + "Id").value!=strLocationId) {
    YAHOO.util.Dom.get("_depots").value = "";
  }

  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) {
        eval("index" + strLeg + "Location=i");
        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() {
  var countryListPickup = YAHOO.util.Dom.get('countryListPickup');
  //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...";
  }
  
  strCountryPickupID = countryListPickup[countryListPickup.selectedIndex].value;
  if(countryListPickup.selectedIndex === 0) {
    //do nothing
  } else {
    //need to get the countryID from the country drop down
    if(countryListPickup.value == "" || strCountryPickupID == "Select Country") {
      alert("Please choose a country from the list.");
    } else {
      YAHOO.util.Dom.get("_countryPickupID").value = strCountryPickupID;
      var postData = "country=" + strCountryPickupID;
      includejs(HTTPSERVER + "vroomtools/locationJS.aspx?country=" + strCountryPickupID); //HTTPSERVER is set by the end user
    }
  }
  ResetDepotAddresses("Pickup");
  ResetDepotAddresses("Return");
}

function ResetDepotAddresses(strLeg) {
  if(ADVANCED_MODE == "MAPLINK") {
    YAHOO.util.Dom.get("divToggle" + strLeg + "Addresses").innerHTML = "<a href='#vvvmap' onclick='javascript:onclick=OpenVVVMap(\"" + strLeg + "\");'>" + ADVANCED_PICKER_PROMPT + "</a>";
    YAHOO.util.Dom.setStyle("divToggle" + strLeg + "Addresses", 'display', 'block');
  } else {
    YAHOO.util.Dom.setStyle("divToggle" + strLeg + "Addresses", 'display', 'none');
  }
}

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) {
  var dates, date, year, month, day;
	dates = args[0]; 
	date = dates[0];
	year = date[0];
	month = date[1];
	day = date[2];
	strMonth = getMonth(month);
	var txtPickupDate = YAHOO.util.Dom.get("txtPickupDate");
  txtPickupDate.value = day + '-' + strMonth + '-' + year;
	YAHOO.util.Dom.get("_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) {
  var strMonth, dates, date, year, month, day;
	dates = args[0]; 
	date = dates[0];
	year = date[0];
	month = date[1];
	day = date[2];
	strMonth = getMonth(month);
	var txtReturnDate = YAHOO.util.Dom.get("txtReturnDate");
  txtReturnDate.value = day + '-' + strMonth + '-' + year;
	YAHOO.util.Dom.get("_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) {
  var 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>";
  includejs(HTTPSERVER + "vroomtools/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";
}

function HideAddresses(strLeg) {
  var divAddresses = YAHOO.util.Dom.get("div" + strLeg + "AddressesContainer");
  YAHOO.util.Dom.replaceClass(divAddresses,"visible","hidden");
}

function ShowDialog(strContents) {
  var divDialog = YAHOO.util.Dom.get("vvvdialog");
  var divContainer = YAHOO.util.Dom.get("vvvcontainer");
  var nTotalPaddingHeight = parseInt(YAHOO.util.Dom.getStyle("vvvcontainer","borderBottomWidth")) + parseInt(YAHOO.util.Dom.getStyle("vvvcontainer","borderTopWidth")) + parseInt(YAHOO.util.Dom.getStyle("vvvcontainer","paddingBottom")) + parseInt(YAHOO.util.Dom.getStyle("vvvcontainer","paddingTop"));

  YAHOO.util.Dom.setStyle(divDialog, 'display', 'block');  
  YAHOO.util.Dom.setStyle(divDialog, 'left', divContainer.offsetLeft + "px");
  YAHOO.util.Dom.setStyle(divDialog, 'top', divContainer.offsetTop + "px");
  YAHOO.util.Dom.setStyle(divDialog, 'height', (divContainer.offsetHeight-nTotalPaddingHeight) + "px");
  divDialog.innerHTML = strContents;
}

function HideDialog() {
  var divDialog = YAHOO.util.Dom.get("vvvdialog");
  YAHOO.util.Dom.setStyle(divDialog, 'display', 'none');
  divDialog.innerHTML = "";
}

function ShowAddresses(strLeg) {
  //This generates the depot selection menu
  var depots = eval("ary" + strLeg + "Depots"); // this is just a shortcut for aryPickupDepots and aryReturnDepots
  var divToggleAddresses = YAHOO.util.Dom.get("divToggle" + strLeg + "Addresses");
  var strDepotDialog = "";
  var onClickAction = "";
  var checked = "";
  var liclass = "";
  var previoussupplierid = "";
  var nSelectionsAvailable=0;
  var strRadioLeg="";
  if(strLeg=="Return") {
    strRadioLeg="R";
  }
  for(i=0;i<depots.length;i++) {
    onClickAction = "onClick='OnDepotSelection(\"" + strLeg + "\"," + i + ");'";
    if(ADVANCED_MODE=="MAP" && depots[i].latitude!="") {
      //YAHOO.util.Dom.get("divMapAddresses").innerHTML="";
      //vvvmap.clearOverlays();
      onClickAction += "vvvmap.panTo(new GLatLng(" + depots[i].latitude + "," + depots[i].longitude + "));'";
      //Trace("plotting" + depots[i].latitude);
      vvvmap.setCenter(new GLatLng(depots[i].latitude,depots[i].longitude), 13);
      vvvmap.addOverlay(CreateMarker(i));
    }
    
    if(previoussupplierid != depots[i].supplierid) {
      liclass = " class=newli";
      if(i+1!=depots.length) {
        if(depots[i+1].supplierid!=depots[i].supplierid) {
          liclass = " class=sololi";
        }
      } else {
        liclass = " class=sololi";
      }
    } else {
      liclass = "";
      nSelectionsAvailable +=1;
    }
        
    //Build the depot radio button
    if(depots[i].selected) {
      checked="checked";
    } else {
      checked="";
    }
    strDepotDialog += "<li" + liclass + "><label " + onClickAction + "><input type=radio " + checked + " value='" + i + "' class='" + strLeg + "radio' name='" + depots[i].supplierid + strRadioLeg + "'><span class='supplier" + depots[i].supplierid + "16'> " + depots[i].address + "</span></label> <a href=\"http://maps.google.com/maps?q=" + depots[i].fulladdress + "\" target=_blank>(map)</a></li>";
    previoussupplierid = depots[i].supplierid;
  }

  if(nSelectionsAvailable>0) {
    strDepotDialog = "<b>Select preferred " + strLeg + " addresses in " + aryLocations[eval("index" + strLeg + "Location")][0] + ":</b><div class=depotaddresses>" + strDepotDialog;
  } else {
    strDepotDialog = "<b>" + strLeg + " addresses in " + aryLocations[eval("index" + strLeg + "Location")][0] + ":</b><div class=depotaddresses>" + strDepotDialog;
  }
  strDepotDialog += "</div>";
  strDepotDialog += "<center><input type=button value=ok onClick='SaveDepotChoices();HideDialog();'></br>";  
  //<input type=button value=cancel  onClick='HideDialog();'>

  ShowDialog(strDepotDialog);
}

//THIS FUNCTION IS RUN WHEN DEPOTS ARE LOADED
function DepotsAreLoaded(strLeg) {
  var divToggleAddresses = YAHOO.util.Dom.get("divToggle" + strLeg + "Addresses");
  var strCheckedAddresses = YAHOO.util.Dom.get("_depots").value;
  var depots = eval("ary" + strLeg + "Depots"); // this is just a shortcut for aryPickupDepots and aryReturnDepots
  depots.length = 0;
  var strPlural = (aryDepots.length == 1)? "":"s";
  var previoussupplierid = "";
  for(var i=0;i<aryDepots.length;i++) {
    depots[i] = new ClassDepot(aryDepots[i]);
    checked="";
    if(strLeg == "Pickup") {
      if(strCheckedAddresses.indexOf("=" + depots[i].depotid) != -1) {
        depots[i].selected=true;
      }else if (strCheckedAddresses == "" && previoussupplierid!=depots[i].supplierid) {
        depots[i].selected=true;
      } else {
        depots[i].selected=false;
      }
    } else {
      if(strCheckedAddresses.indexOf("," + depots[i].depotid) != -1) {
        depots[i].selected = true;
      } else if (previoussupplierid != depots[i].supplierid) {
        depots[i].selected = true;
      }
    }
    previoussupplierid = depots[i].supplierid;
  }
  
  if(aryLocations[eval("index" + strLeg + "Location")][0].indexOf("Airport")>-1) {
    var strAirportMapLink = "";
    divToggleAddresses.innerHTML = aryDepots.length + " companies operate at this airport. <a target=\"_blank\" href=\"http://maps.google.com?q=" + aryLocations[eval("index" + strLeg + "Location")][0] + ", " + depots[0].country + "\">map</a>";
  } else {
    divToggleAddresses.innerHTML = "<a href='javascript:onclick=ShowAddresses(\"" + strLeg + "\");'>Choose from " + aryDepots.length + " address option" + strPlural;
  }                                  
  //divToggleAddresses.innerHTML = "<a href='#vvvmap' onClick='javascript:onclick=OpenVVVMap(\"" + strLeg + "\");'>Show " + aryDepots.length + " addresses in map mode";
  
  if(DEPOT_SELECTION == "COLLAPSED") {
    YAHOO.util.Dom.setStyle(divToggleAddresses, 'display', 'block');
  }
}

function CreateMarker(i) {
  // Create our "tiny" marker icon
  var supplierIcon = new GIcon(G_DEFAULT_ICON);
  supplierIcon.image = "http://vroom.com.au/SearchFresh/gfx/marker" + depots.supplierid(i) + ".png"; // TODO: Use the URL from settings
  // Set up our GMarkerOptions object
  markerOptions = { icon:supplierIcon };
  marker = new GMarker(new GLatLng(depots.latitude(i),depots.longitude(i)),markerOptions);
   GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(depots.tostring(i));});
  return marker;
}

function SaveVVVMap() {
  SaveDepotChoices();
  CloseVVVMap();
}

function CloseVVVMap() {
  YAHOO.util.Dom.replaceClass(YAHOO.util.Dom.get("divMapContainer"),"visible","hidden");
  //GUnload();
}

function InitialiseGMap() {
/*
  if (GBrowserIsCompatible()) {
    vvvmap = new GMap2(document.getElementById("vvvmap"));
    vvvmap.setUIToDefault();
    vvvmap.setCenter(new GLatLng(37.4419, -122.1419), 13);
    // Add 10 markers to the map at random locations
    var bounds = vvvmap.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();
    var lngSpan = northEast.lng() - southWest.lng();
    var latSpan = northEast.lat() - southWest.lat();
    for (var i = 0; i < 10; i++) {
      var point = new GLatLng(southWest.lat() + latSpan * Math.random(), southWest.lng() + lngSpan * Math.random());
      vvvmap.addOverlay(new GMarker(point));
    }
  }
*/ 
}

function OnDepotSelection(strLeg,n) {
  var depots = eval("ary" + strLeg + "Depots"); // this is just a shortcut for aryPickupDepots and aryReturnDepots
  
  for(var i=0;i<depots.length;i++) {
    if(depots[n].supplierid==depots[i].supplierid) {
      if(n==i) {
        depots[i].selected = true;
      } else {
        depots[i].selected = false;
      }
    }
  }
}

function GetSupplierDepot(strSupplierID,strLeg) {
  //returns which depot the customer has selected
  var depots = eval("ary" + strLeg + "Depots"); // this is just a shortcut for aryPickupDepots and aryReturnDepots
  for(i=0;i<depots.length;i++) {
    if(depots[i].supplierid == strSupplierID && depots[i].selected === true) {
      return depots[i];
    }
  }
  return "";  
}

function SaveDepotChoices() {
  //saves AV=2323,32424&BG=2323,32323&HZ=31,231 in to a hidden text box so the browser will remember it.

  var strDepots = "";
  var depotReturn;

  for(var i=0;i<aryPickupDepots.length;i++) {
    if(aryPickupDepots[i].selected) {
      strReturnDepotID = "";
      depotReturn = GetSupplierDepot(aryPickupDepots[i].supplierid,"Return");
      
      if (!(depotReturn=="" && aryReturnDepots.length>0)){ //always save pickup depot unless it's one way and a return supplier depot doesn't exist
        if(strDepots!="") {
          strDepots += "&";
        }
        strDepots += aryPickupDepots[i].supplierid + "=" + aryPickupDepots[i].depotid + ",";
        
        if(depotReturn!="") {
          strDepots += depotReturn.depotid;
        }
        else {
          strDepots += aryPickupDepots[i].depotid;
        }
      }
    }
  }
  YAHOO.util.Dom.get('_depots').value = strDepots; //save depot selection to hidden text box
}

//////////////////////////////////////////////////////////////////////////////
// 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,"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,"23: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');
  var countryListPickup = YAHOO.util.Dom.get('countryListPickup');
      
  COUNTRYID = (typeof(COUNTRYID) == 'undefined') ? "" : COUNTRYID;
if (COUNTRYID == "") {
	if (getQueryVariable("countryid") == "") {
		COUNTRYID = "au";
	} else {
		COUNTRYID = getQueryVariable("countryid");
	}
} else {
	if (getQueryVariable("countryid") == "") {
		COUNTRYID = COUNTRYID;
	} else {
		COUNTRYID = getQueryVariable("countryid")
	}
}
  COUNTRYID = COUNTRYID.toUpperCase();
   
  PICKUPFROM = (typeof(PICKUPFROM) == 'undefined') ? "" : PICKUPFROM;
  PICKUPFROM = (PICKUPFROM == "") ? getQueryVariable("pickupfrom") : PICKUPFROM;
  
  AFFILIATEID = (typeof(AFFILIATEID) == 'undefined') ? "" : AFFILIATEID;
  AFFILIATEID = (AFFILIATEID == "") ? getQueryVariable("affiliateid") : AFFILIATEID;
  
  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 = (typeof(AGE) == 'undefined') ? "" : AGE;
  AGE = (AGE == "") ? getQueryVariable("AGE") : AGE;
  //bDowntown = (typeof(PICKUPFROM) == 'undefined') ? "false" : (PICKUPFROM.indexOf("City")>-1); //eg: if set to Chicago City, we set bDowntown=true
  
  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');
  _returnIndex = YAHOO.util.Dom.get("_returnIndex");
  
  //setSelectedValue(countryListPickup, YAHOO.util.Dom.get('_countryLiveIn').value);
  
  if(_init.value != "true") {
  //ONLY DO THIS IT'S THE FIRST TIME THE PAGE IS LOADED (not back button nor refresh)
  YAHOO.util.Dom.get("_countryPickupID").value = COUNTRYID;
  YAHOO.util.Dom.get("_countryLiveIn").value = COUNTRYID;
  
  if(COUNTRYID != "") {
    setSelectedValue(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);
  }
  else{    
    setSelectedValue(YAHOO.util.Dom.get("returnTime"), YAHOO.util.Dom.get('_returnTime').value);
    setSelectedValue(YAHOO.util.Dom.get("pickupTime"), YAHOO.util.Dom.get('_pickupTime').value);
    setSelectedValue(YAHOO.util.Dom.get("countryListLiveIn"), YAHOO.util.Dom.get('_countryLiveIn').value);
    setSelectedValue(countryListPickup, YAHOO.util.Dom.get('_countryPickupID').value);
  }
  
  YAHOO.util.Dom.get("checkSameAsPickup").checked=true;
  
  UpdateCalendar(YAHOO.vroom.calendar.pickupcal,YAHOO.util.Dom.get("_pickupDate").value);
  UpdateCalendar(YAHOO.vroom.calendar.returncal,YAHOO.util.Dom.get("_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);