//########################################################
//########################################################
function fnSetPrefsFromCookie (foForm)
{
	var strArrivePort, strDepartPort, strIsReturn, dteReturn, dteDepart;
	ckiTravelData.load();
	strArrivePort = ckiTravelData.arrivePort;
	strDepartPort = ckiTravelData.departPort;
	strIsReturn = ckiTravelData.isreturn;
	dteReturn = new Date(ckiTravelData.returnDate);
	dteDepart = new Date(ckiTravelData.departDate);
	//only set the depart port if one hasnt been passed from server
	if (foForm.selDepartPort.selectedIndex == -1)
	{
		//set the departure port to cookie value
		for (i=0; i<foForm.selDepartPort.options.length; i++)
		{
			if (foForm.selDepartPort.options[i].value==strDepartPort)
			{
				foForm.selDepartPort.options[i].selected = true;
			}
		}
	}
	//populate the arrival ports to suit the departure value
	fnChangeArrivePort(foForm.selArrivePort,foForm.selDepartPort)

	//only set the arrival port if one hasnt been passed from server
	//if there is nothing set on server there will be one option 'undef' 'choose departure'
	if (foForm.selArrivePort.selectedIndex == -1)
	{
	//set the arrival port to cookie value
	foForm.selArrivePort.selectedIndex=0; //set the first one in case no cookie value set
		for (i=0; i<foForm.selArrivePort.options.length; i++)
		{
			if (foForm.selArrivePort.options[i].value==strArrivePort)
			{
				foForm.selArrivePort.options[i].selected = true;
			}
		}
	}
	if (strIsReturn=="false")
	{
		foForm.selReturnMonthYear.disabled=true;
		foForm.selReturnDay.disabled=true;
		//foForm.radSingleReturn[0].checked = true;
	}
	else
	{
		foForm.selReturnMonthYear.disabled=false;
		foForm.selReturnDay.disabled=false;
		fnCatchUpReturnDate()
		//foForm.radSingleReturn[1].checked = false;
	}
	//set the depart date
	fnSetDate (foForm.selDepartMonthYear, foForm.selDepartDay, dteDepart);
	//set the return date
	fnSetDate (foForm.selReturnMonthYear, foForm.selReturnDay, dteReturn);
}
//########################################################
function fnChangePortOpts(mySel,myArray)
{
	strOldValue = (mySel.value);
	var i=0;
	for (var element in myArray)
	{
		mySel.options[i]= new Option(aryAirports[myArray[element]], myArray[element])
		i++;
	}
	mySel.options.length=i;
	if (strOldValue != "")
	{
		fnSetComboValue (mySel, strOldValue )
	}
	return true;
}
//########################################################
function fnSetCookie (strDepartPort, strArrivePort, dteDepart, dteReturn, strIsReturn)
{
	var dteExpires, dteToday;
	dteToday = new Date ();
	dteExpires = dteToday;
	dteExpires.setMonth (dteToday.getMonth()+1);	
	ckiTravelData.departPort = strDepartPort;
	ckiTravelData.arrivePort = strArrivePort;
	ckiTravelData.returnDate = dteReturn;
	ckiTravelData.departDate = dteDepart;
	ckiTravelData.isreturn = strIsReturn;
	ckiTravelData.store();

}
//########################################################

function fnChangeArrivePort(selArrivePort,selDepartPort)
{
	var strPort, aryPorts;
	if  (selDepartPort.selectedIndex==-1)
	{
		return false; //no departure port is set
	}
	strPort = selDepartPort.options[selDepartPort.selectedIndex].value;
	aryPorts = aryValidDestinations[strPort].split(",");
	fnChangePortOpts (selArrivePort,aryPorts);
}

function fnChangeDepartPort(selDepartPort,selArrivePort)
{
	var strPort, aryPorts;
	if  (selArrivePort.selectedIndex==-1)
	{
		return false; //no departure port is set
	}
	strPort = selArrivePort.options[selArrivePort.selectedIndex].value;
    if (!aryValidSources[strPort]) {
	    var aryPorts = ["London"];
    } else {
	    var aryPorts = (aryValidSources[strPort]).split(",");
    }
	//aryPorts = aryValidSources[strPort].split(",");
	fnChangePortOpts (selDepartPort,aryPorts);
}
//#######################################################
function fnSetDate (selMonth, selDays, dteDate)
{
//set the date of the month and day drop downs to a date or default nearest fri or mon
//depart defaults to nearest fri
//return defaults to nearest mon
	var strSelName, dteDate, dteTemp;
	strSelName=selDays.name;
	strSelName=strSelName.toLowerCase();
	if (isNaN(dteDate))
	{//is date passed
		dteDate = new Date();
		dteDate.setNearestDay("fri");
		strSelName.indexOf("return")>0 ? dteDate.setNearestDay("mon") :  true; //this sets to mon AFTER the nearest fri not the nearest mon which could be beofre nearest fri!
	} 
	if (dteDate<new Date())
	{//is date passed older than now
		dteDate = new Date();
		//dteDate.setNearestDay(strDefaultDay);
		strSelName.indexOf("return")>0 ? dteDate.setNearestDay("mon") :  true;
	} 
	for (i=0; i<selMonth.options.length; i++)
	{
		dteTemp= new Date (selMonth.options[i].value);
		if ((dteTemp.getMonth()==dteDate.getMonth()) && (dteTemp.getYear()==dteDate.getYear()))	{selMonth.selectedIndex=i}
	}
	fnChangeDays(selDays,dteDate); //set the max days and default day to date
}
//#######################################################

//#######################################################
function fnChangeDateOpts(mySel,myArray)
{
	var i, dteDate;
	i=0;
	for (var element in myArray)
	{
		dteDate = new Date(element);
		mySel.options[i]= new Option(dteDate.getMonthName()+","+dteDate.getYear(), dteDate.getUSDateString());
		mySel.options[i].maxdays=(myArray[element][1]);
		i++;
	}
	mySel.options.length=i;
	return true;
}
//#######################################################
function fnPopulateSelect(mySel,myArray)
{
	var i;
	i=0;
	for (var element in myArray)
	{
		mySel.options[i]= new Option(myArray[element], element);
		i++;
	}
	mySel.options.length=i;
	return true;
}
//#######################################################
function fnFilterResorts(selArrivePort,selDepartPort)
{
	var strPort, aryPorts;
	if  (selDepartPort.selectedIndex==-1)
	{
		return false; //no departure port is set
	}
	strPort = selDepartPort.options[selDepartPort.selectedIndex].value;
	aryPorts = aryCountryResorts[strPort].split(",");
	fnChangePortOpts (selArrivePort,aryPorts);
}
//#######################################################
function fnSetComboValue (cmbCombo, strValue )
{
	//set the combo to a specified value
	for (i=0; i<cmbCombo.options.length; i++)
	{
		if (cmbCombo.options[i].value==strValue)
		{
			cmbCombo.options[i].selected = true;
			return true;
		}
	}
	//if cant find that value set to one that has same starting letter
	strValue = aryAirports[strValue];
	for (i=0; i<cmbCombo.options.length; i++)
	{
		strCombo = cmbCombo.options[i].text;
		if (strCombo.substr(0,1)==strValue.substr(0,1))
		{
			cmbCombo.options[i].selected = true;
			return true;
		}
	}
	//if cant find one with same letter set to first
	cmbCombo.options[0].selected = true;
}
