//
// This code is provided by Kelkoo for the integration of their search forms into the Travel Channel
// Lines 68 and 92 updated to point at the correct content directory.
//

Locations = new Array();
Towns = new Array();
newLetter="";

function add_CityObj(region, iata_code, city, product)
{
	this.REGION = region;
	this.IATA_CODE = iata_code;
	this.CITY = city;
	this.PRODUCT = product;
}


function add_TownObj(town, type)
{
	this.town = town;
	this.type = type;
}

function initLocation(){Locations = new Array();}
function initTown(){Towns = new Array();}


function add_and_set(region, iata_code, city,  product){
	Locations[Locations.length] = new add_CityObj(region, iata_code, city,  product);
}

function optionObj(thevalue,theText){	this.value = thevalue;	this.text = theText;}

function addTown(town,air){
	Towns[Towns.length] = new add_TownObj(town, air);
}

function replaceIn(theString,toReplace,replaceBy)
{
	cL=new String(theString);
	lg=toReplace.length;
	i=cL.indexOf(toReplace);
	while ( i >0)
	{
		cL=cL.substring(0,i)+replaceBy+cL.substring(i+lg,cL.length);i=cL.indexOf(toReplace);
	}
	return(cL);
}

function getParam(theUrl,theParam)
{
	i=theUrl.indexOf("&"+theParam+"=");
	j=theUrl.indexOf("&",i+3);
	return( theUrl.substring(i+9, j));
}


function setDropDowns(theField)
{
	// Country field has been modified
	if (theField.name == "pickUpCountry")
	{

	  //alert("in .js : setDropDowns pickUpCountry");
	  //alert("setDropDowns pickUpCountry");
	  theCountry = theField.options[theField.selectedIndex].value;
	  theCountryUrl=replaceIn(theCountry," ","-");
	  window.document.getElementById("townList").src  ="./content/"+countryDir+"/travel/carHire/"+theCountryUrl+"/townList_"+theCountryUrl+"R6.html";

	  //alert("in .js : setDropDowns pickUpCountry : end");
	  newLetter = "";
	  citySelected = "";
	  setDropDowns(parent.document.getElementById("pickUpTown"));


	}
	// city field has been modified
	else if (theField.name == "pickUpTown" )
	{


	  //	  alert(citySelected);
	  if (theField.form.pickUpTown.options[theField.form.pickUpTown.selectedIndex].value!="")
	    {citySelected=theField.form.pickUpTown.options[theField.form.pickUpTown.selectedIndex].value;}
	  theCountry = theField.form.pickUpCountry.options[theField.form.pickUpCountry.selectedIndex].value;
	  oldLetter= newLetter;
	  newLetter= citySelected.charAt(0);
	  if (  newLetter.length > 0 && newLetter!=oldLetter  )
	    {
	      //reloading the location file
	      theField.form.classCity.value=newLetter;
	      //alert("reloading location file for newLetter "+newLetter);
	      theCountryUrl=replaceIn(theCountry," ","-");
              newLetter = escape(newLetter);
              theCountryUrl = escape(theCountryUrl);
	      window.document.getElementById("locationList").src  ="./content/"+countryDir+"/travel/carHire/"+theCountryUrl+"/locationList_"+theCountryUrl+"_"+newLetter+"R6.html";
            }
	  else if (newLetter.length < 1) // Change country : put empty dropdown
	    {
	      //alert("newLetter is empty!!");
              optionList = new Array();
	      reWriteDropDown(document.getElementById("pickUpAdress"),optionList,selectLocation);
	    }
	  else
	    {
	      //file is not reloaded, so we just need to filer the new location list
	      //alert("filtering locations");
	      optionList = getLocOptObjsAdress(document.getElementById("pickUpAdress").form);
      	      reWriteDropDown(document.getElementById("pickUpAdress"),optionList,selectLocation);
	      setLastValue(parent.document.getElementById("pickUpAdress"), "");
	      setDropDowns(parent.document.getElementById("pickUpAdress"));
	    }
	  //alert("in .js : setDropDowns pickUpTown : end");
  	}
	else if (theField.name == "pickUpAdress")
	{
	  //alert("in .js : setDropDowns pickAdress");
	  //alert("updating pickUpAdress");
          if (theField.form.pickUpTown.selectedIndex==0){
              optionList = new Array();
	      reWriteDropDown(document.getElementById("pickUpAdress"),optionList,selectLocation);
	      setLastValue(parent.document.getElementById("pickUpAdress"), "");
          }
	  citySplit = theField.form.pickUpAdress.value.split("#");
	  theField.form.pickUpAirportCode.value = citySplit[0];
	  // alert("in .js : setDropDowns pickAdress : end");
	}
}

function getEmptyList()
{
	itemList = new Array();
	itemList[itemList.length] = new optionObj("","");
	return itemList
}


function getLocOptObjsTown(theForm)
{
  if (theForm.airportSel[0].checked){airport="A";} else {airport="C";}
  itemList = new Array();
  for(i=0;i<Towns.length ;i++)
    {
      if ( (Towns[i].type).indexOf(airport)>=0)
	{
	  itemList[itemList.length] = new optionObj(Towns[i].town,Towns[i].town);
	}
    }
  return itemList;
}

function getLocOptObjsAdress(theForm)
{
  if (theForm.airportSel[0].checked){airport="A";} else {airport="C";}
  itemList = new Array();
  for(i=0;i<Locations.length ;i++)
    {
      if( ( Locations[i].PRODUCT==airport || theForm.pickUpTown.value.length < 1)
	  &&
	  (Locations[i].REGION == theForm.pickUpTown.value || theForm.pickUpTown.value.length < 1) )
	{
	  itemList[itemList.length] = new optionObj((Locations[i].IATA_CODE+"#"+Locations[i].CITY),Locations[i].CITY);
	}
    }
  return itemList;
}


function reWriteDropDown(theField,optionList,defaultText)
{
	theField.length = 0;
	theField[0] = new Option(defaultText,"");

	OptionIndex = 1;

	for(i=0;i<optionList.length;i++)
	{
		theField[OptionIndex] = new Option(optionList[i].text,optionList[i].value);
		OptionIndex++;
	}
	if (theField.selectedIndex < 0)
	{
		theField.options[0].selected = true;
	}
	if (theField.length == 2 && theField.options[0].value.length < 1)
	{
		theField.options[1].selected = true;
	}
 }


function defaultLocations(theForm) {
  theForm.pickUpCountry.selectedIndex = 0;
  setDropDowns(theForm.pickUpCountry);
  theForm.pickUpTown.selectedIndex = 0;
  setDropDowns(theForm.pickUpTown);
  theForm.pickUpAdress.selectedIndex = 0;
  setDropDowns(theForm.pickUpAdress);
}


function setLastValue(theField, theValue)
{
  // set the Index to 1 if there is only one value in the poplist
        if (theField.length == 2 && theField.options[0].value.length < 1)
	{
		theField.options[1].selected = true;
	}
        else
	{
		var hasBeenSet = false;
		for(i=0;i<theField.length;i++)
		{
			if (theField.options[i].value == theValue)
			{
				theField.selectedIndex = i;
				hasBeenSet = true;
				break;
			}
		}
		if (!hasBeenSet){theField.options[0].selected = true;}
	}
}


