/* ----------------Month & Day-------------*/

/* 
   	fnPageLoadTasks - Day and Month views
*/
function fnPageLoad() {
	foForm=document.foFlightDetails;
	if (PortName=='') {
    //var ary = (aryValidDestinations[foForm.selDepartPort.item(foForm.selDepartPort.selectedIndex).value]).split(",");
	fnChangePortOpts((foForm.selDepartPort),aryAllAirports);
	document.foFlightDetails.selDepartPort.selectedIndex=70;
	fnChangeArrivePort(document.foFlightDetails.selArrivePort,document.foFlightDetails.selDepartPort);
	foForm.selArrivePort.focus();
	} else {
/* szaman 
    var ary = (aryValidDestinations[foForm.selDepartPort.item(foForm.selDepartPort.selectedIndex).value]).split(",");
	fnChangePortOpts((foForm.selArrivePort),ary);
	foForm.selDepartPort.focus();
szaman */
    //alert('pn: '+PortName+' ['+PortValue+']');
    //var vs = 'AMS,BRS,Brussels,BRU,CPH,FAO,GVA,Gothenburg,GOT,LPL,London,LGW,LHR,LTN,STN,Milan,LIN,NCE,Rome,FCO,Stockholm,ARN';
    //var ary=(vs).split(",");
    for (var PortId in aryAirports) {
        if (aryAirports[PortId].toLowerCase().indexOf(PortName.toLowerCase(),0)>-1) {
           PortValue=PortId; break;
        }
    }
    PortName = aryAirports[PortValue];

    var i=0;
    for (var PortId in aryAirports) {
      document.foFlightDetails.selArrivePort.options[i]= new Option(aryAirports[PortId], PortId)
      if (PortId==PortValue) {
        document.foFlightDetails.selArrivePort.selectedIndex=i;
      }
      i++;
    }
    document.foFlightDetails.selArrivePort.options.length=i;

    if (!aryValidSources[PortValue]) {
	    var ary = ["London"];
    } else {
	    var ary = (aryValidSources[PortValue]).split(",");
    }
	fnChangePortOpts((foForm.selDepartPort),ary);
	foForm.selDepartPort.focus();
	}
}

/* 
   	fnChangeDays - Day and Month views - change the days list after month changed
*/
function fnChangeDays(selDays, strDate)
{
	var dteDate;
	//Get the current day selected
	intCurrentDay = selDays.selectedIndex+1;

	//Turn strDate (in dd/mm/yyyy format) into a date
	if (typeof(strDate) == "string"){
		dteDate = new Date();
		dteDate.setYear(strDate.substr(6,4));
		dteDate.setMonth(strDate.substr(3,2)-1);
		dteDate.setDate(strDate.substr(0,2));}
	else {
		dteDate = strDate;};
	
	var intMaxDays, dteTemp;
	//Set dteTempt to the first day
	dteTemp = new Date(dteDate);
	dteTemp.setDate(1);
	
	//Write out the new day selections
	intMaxDays=dteDate.getMaxDays();
	for (i=0; i<=intMaxDays; i++)
	{
		dteTemp.setDate(i+1);
		selDays.options[i]= new Option((i+1)+" "+dteTemp.getWeekDay(),(i+1));
	}
	//set selected day to dteDate
	selDays.length=intMaxDays;
	intCurrentDay = ((intCurrentDay>intMaxDays)?intMaxDays:intCurrentDay);
	selDays.selectedIndex = intCurrentDay-1; //dteDate.getDate()-1;
	fnSaveCookieValue( selDays.name, intCurrentDay );

}

/* 
   	fnCatchUpReturnDate - Day and Month views
*/

function fnCatchUpReturnDate()
{
	//set the return date to be the same as depart date if not set before
	//remember that selectedIndex is zero based
	var dteDepart, intDepDay, intDepMonth, intDepMonthIndex;
	intDepDay = foFlightDetails.selDepartDay.selectedIndex+1;
	intDepMonthIndex = foFlightDetails.selDepartMonthYear.selectedIndex;
	intDepMonth = foFlightDetails.selDepartMonthYear.item(intDepMonthIndex).value;
	intDepYear = intDepMonth.substring(6,10);
	intDepMonth = intDepMonth.substring(3,5);
	//Get the month without trailing '0'
	if( intDepMonth.substring(0,1)=="0" ){
		intDepMonth = intDepMonth.substring(1,2);
	}else{
		intDepMonth = intDepMonth.substring(0,2);
	};
	dteDepart = new Date();
	dteDepart.setMonth(intDepMonth-1);
	dteDepart.setYear(intDepYear);
	dteDepart.setDate(foFlightDetails.selReturnDay.selectedIndex+1);
	if (foFlightDetails.selReturnDay.disabled != true)
	{ // only if return is earlier than depart and enabled
		if (foFlightDetails.selReturnMonthYear.selectedIndex < intDepMonthIndex)
		{
			foFlightDetails.selReturnMonthYear.selectedIndex = intDepMonthIndex;
			fnSaveCookieValue("selReturnMonthYear", foFlightDetails.selReturnMonthYear.item(foFlightDetails.selReturnMonthYear.selectedIndex).value);
			//alert(dteDepart.getUKDateString());
			fnChangeDays(foFlightDetails.selReturnDay, dteDepart) //update the days in the month
		}
		if ((foFlightDetails.selReturnDay.selectedIndex < intDepDay) && (foFlightDetails.selReturnMonthYear.selectedIndex <= intDepMonthIndex))
		{ 
			foFlightDetails.selReturnDay.selectedIndex = intDepDay-1;
			fnSaveCookieValue("selReturnDay", foFlightDetails.selReturnDay.item(foFlightDetails.selReturnDay.selectedIndex).value);
		}
	}
}


/* ----------------Month-------------------*/

/* 
   	Month View Form Submission Validation
*/
function fnMCheckTravelDetails (foForm) {
    //strIsReturn = foForm.radSingleReturn.checked;
    strIsReturn = true;
	
	strDateDepart=foForm.selDepartMonthYear.value;
	strDayDepart=foForm.selDepartDay.value;
	dteDepart = new Date();
	dteDepart.setYear(strDateDepart.substr(6,4));
	dteDepart.setMonth(strDateDepart.substr(3,2)-1);
	//dteDepart.setDate(strDayDepart);
	
	strDateReturn=foForm.selReturnMonthYear.value;
	strDayReturn=foForm.selReturnDay.value;
	dteReturn = new Date();
	dteReturn.setYear(strDateReturn.substr(6,4));
	dteReturn.setMonth(strDateReturn.substr(3,2)-1);
	//dteReturn.setDate(strDayReturn);

	//dteReturn.setDate(foForm.selReturnDay.value)
	dteNow = new Date ();
	dteNow.setDate(1);
	dteNow.setHours(0);
	dteNow.setMinutes(0);
	dteNow.setSeconds(0);
	dteNow.setMilliseconds(0);//set to the beginning of day.

	strArrive = foForm.selArrivePort.value;
	strDepart = foForm.selDepartPort.value;

	//is Departure before today?
	if (dteDepart.valueOf() < dteNow.valueOf() ) {
		alert (aryErrors["depart_b4_now"]);
		return false;
	};

	//is Return before Departure?
	if ((dteReturn.valueOf() < dteDepart.valueOf() ) && (strIsReturn==true)){ 
		alert (aryErrors["return_b4_depart"]);
		return false;
	};

	//has a destination been chosen?
	if (strDepart == "") {alert (aryErrors["depart_not_chosen"]); return false;}

	//if everything is OK then set cookie to retain aiport choices
	//fnSetCookie(strDepart, strArrive, dteDepart, dteReturn, strIsReturn);
	
	foForm.departdate.value=dteDepart.getUKDateString();
	foForm.returndate.value=dteReturn.getUKDateString();
	return true;
}

function fnMSetFlightFromChart(myImage){
	imgTemp=new Image();
	imgTemp.src="images/flightmonthprice.gif";
	chartname=myImage.id;

	chartname=chartname.substr(0,chartname.indexOf("_"));

	objCurrentSelectedHolder = eval(chartname+"_selected");
	imgCurrentSelected = eval("document.all." + objCurrentSelectedHolder.value);
	imgCurrentSelected.src = "images/flightmonthprice.gif";
	myImage.src = "images/flightmonthprice_sel.gif";
	objCurrentSelectedHolder.value = myImage.id;

	tdResultsDetails=eval("document.all."+chartname+"details");
	tdResultsLink=eval("document.all."+chartname+"link");
	tdJourneyTotal=document.all.journeytotal;
	tdResultsDetails.innerText=myImage.title;
	tdResultsDetails.price=fnTo2DecPlace(myImage.price);
	tdResultsLink.innerHTML=" : <a href='"+myImage.flightlink+"' target='_new'>"+myImage.flightlinktitle+"</a>";
	sngOBPrice = document.all.outbounddetails.price;
	sngIBPrice = 0;
	if (typeof(document.all.inbounddetails)=="object"){ //is it single or return flight
		sngIBPrice = document.all.inbounddetails.price;
	}
	tdJourneyTotal.innerHTML="&pound;"+fnTo2DecPlace(sngIBPrice/1+sngOBPrice/1); //note the /1 forces int rather than char
}


/* ----------------Day-------------*/

/* 
   	fnToggleReturnDate - Day View
*/
function fnDToggleReturnDate(bChecked)
{
	(bChecked) ? foFlightDetails.selReturnMonthYear.disabled = false : foFlightDetails.selReturnMonthYear.disabled = true;
	(bChecked) ? foFlightDetails.selReturnDay.disabled = false : foFlightDetails.selReturnDay.disabled = true;
	//This is the single/return form variable that is used....
	foFlightDetails.bReturn.value = bChecked;
	fnSaveCookieValue('bReturn', bChecked);
}


function fnDToggleVis(table) {
	msgTable = (eval((table.id)+"_a"));
	if (table.style.display=="") {table.style.display = "none"; msgTable.style.display = "";}
	else {table.style.display= ""; msgTable.style.display = "none";}
}


function fnDCheckTravelDetails (foForm) {
	bIsReturn = new Boolean(foForm.bReturn.value);

/* civic
	strDateDepart=foForm.selDepartMonthYear.value;
	strDayDepart=foForm.selDepartDay.value;
	dteDepart = new Date();
	dteDepart.setYear(strDateDepart.substr(6,4));
	dteDepart.setMonth(strDateDepart.substr(3,2)-1);
	dteDepart.setDate(strDayDepart);
	
	strDateReturn=foForm.selReturnMonthYear.value;
	strDayReturn=foForm.selReturnDay.value;
	dteReturn = new Date();
	dteReturn.setYear(strDateReturn.substr(6,4));
	dteReturn.setMonth(strDateReturn.substr(3,2)-1);
	dteReturn.setDate(strDayReturn);
	
	dteNow = new Date ();
	dteNow.setHours(0);
	dteNow.setMinutes(0);
	dteNow.setSeconds(0);
	dteNow.setMilliseconds(0);//set to the beginning of day.

civic */
	strArrive = foForm.selArrivePort.value;
	strDepart = foForm.selDepartPort.value;
	

	//have departure and destinations been chosen?
	if (strArrive=="") { alert (aryErrors["arrival_not_chosen"]); return false; }
	if (strDepart=="") { alert (aryErrors["depart_not_chosen"]); return false; }
	
/* civic
	//is Departure before today
	if (dteDepart < dteNow ) { alert (aryErrors["depart_b4_now"]); return false; }
	//is Return before Departure
	bTest = new Boolean(((dteReturn < dteDepart ) && (bIsReturn==true)));
	if (bTest==true){ alert (aryErrors["return_b4_depart"]); return false; }

	//if everything is OK then set cookie to retain aiport choices
	//fnSetCookie(strDepart, strArrive, dteDepart, dteReturn, bIsReturn);
	
	foForm.departdate.value=dteDepart.getUKDateString();
	foForm.returndate.value=dteReturn.getUKDateString();
civic */

	return true;
}


/* ----------------All-------------*/

/* 
   	Custom error messages
*/
aryErrors = new Array ();
aryErrors["depart_b4_now"] = "Please select a departure date after todays date";
aryErrors["return_b4_depart"] = "Please select a departure date before your return date";
aryErrors["select_arrive"] = "Please choose a destination airport.";
aryErrors["arrival_not_chosen"] = "Please choose a destination airport.";
aryErrors["depart_not_chosen"] = "Please choose a departure airport.";

/*
	 fnSaveCookieValue - used by all views
*/
function fnSaveCookieValue (strName, strValue){
	//alert ("saving " + strName + "=" + strValue);

	switch (strName) {
	    case "departdate":
//alert("01/"+strValue.substring(2,10));
		{fnSaveCookieValue("selDepartMonthYear","01"+strValue.substring(2,10));
		//Get the day without trailing '0'
		if( strValue.substring(0,1)=="0" ){ strValue = strValue.substring(1,2) }else{ strValue = strValue.substring(0,2) };
		fnSaveCookieValue("selDepartDay",strValue);
		}
	    case "returndate":
		{fnSaveCookieValue("selDepartMonthYear","01"+strValue.substring(2,10));
		//Get the day without trailing '0'
		if( strValue.substring(0,1)=="0" ){ strValue = strValue.substring(1,2) }else{ strValue = strValue.substring(0,2) };
		fnSaveCookieValue("selDepartDay",strValue);
		}
	    default:
		{var expire = new Date(2010,1,1);
		strCookie = strName + "=" + escape(strValue) + "; expires=" + expire.toGMTString();
		//alert(strCookie);
		document.cookie = strCookie;
		}
	} 	
}


/*
	 PopUp - used by footer etc
*/
function PopUp(doc){
  width=600;
  height=450;
  scrll="yes";
  toolbar="no";
  //status="yes";
  if (arguments.length>1){
	width=arguments[1]
	height=arguments[2]
	scrll=arguments[3]
	toolbar=arguments[4]
  };
  properties="width="+width+",height="+height+",scrollbars="+scrll+",toolbar="+toolbar;

  doc=window.open (doc,"Aero48InfoPopup",properties);
  doc.focus();
}


function rollover(but){
	source=new String(but.src);
	if (source.match("_of."))
		{but.src=source.replace("_of.","_ov.");}
	else
		{but.src=source.replace("_ov.","_of.");}
}

function fnIsIE(){
    var ua = (window.navigator.userAgent);
    var msie = ua.indexOf ( "MSIE " );
    if ( msie > 0 ){
        return parseInt (ua.substring (msie+5, ua.indexOf (".",msie )));
	}
    else{
	    return 0;
	}
}


function fnTo2DecPlace(X) {
	var T, S=new String(Math.round(X*100));
	while (S.length<3) S='0'+S;
	return S.substr(0, T=(S.length-2)) + '.' + S.substr(T, 2);
}

//function fnExpireCookie (){
//	lastyear = new Date();
//	lastyear.setFullYear(lastyear.getFullYear() -1);
//	document.cookie = "; expires="+lastyear;
//
//}
