/* 
	THE FOLLOWING FUNCTIONS ARE USED TO ALTER THE DOM FOR USE WITH THE FORM AT THE TOP
*/

/*
	Disable the 'trip separator' field on the 'all flights between dropdown
*/
function checkTripLength()
{

	if (document.flights.dates.selectedIndex==0) document.flights.trip_length.disabled = false;
	else document.flights.trip_length.disabled = true;

}


/*
	If the all destination field is checked then the following occurs:
		- set the desination drop down to disabled
		- makes sure that all departures is NOT toggled
		- sets the baseline default price to 5 (changeable!)
	if the all dest is unchecked:
		- sets drop down to enabled
*/
function disableDestination()
{

	if (document.flights.alldest.checked) {
		document.flights.dest.disabled = true;
		//if (document.flights.dep.disabled) document.flights.dep.disabled=false; document.flights.alldep.checked=false;
		setPrice(20);
	}
	else {
		enableDestination();
	}
}


/*
	If the all departures field is checked then the following occurs:
		- set the desination drop down to disabled
		- makes sure that all departures is NOT toggled
		- sets the baseline default price to 5 (changeable!)
	if the all dest is unchecked:
		- sets drop down to enabled
		
	Once this has run it then calls the changeDestination script to ensure that the drop downs are properly populated
*/
function disableDeparture()
{

	if (document.flights.alldep.checked) {
		document.flights.dep.disabled = true;
		if (document.flights.dest.disabled) {document.flights.dest.disabled=false; document.flights.alldest.checked=false;}
		setPrice(20);
	}
	else {
		enableDeparture();
	}
//Refresh destination list
	changeDestinationList();
}


function checkAllSelected(){
	if(	
		(document.flights.dep.options[document.flights.dep.selectedIndex].value=="NAp1") ||
		((document.flights.dest.options[document.flights.dest.selectedIndex].value=="NAp1") &&  (document.flights.alldest.checked==false) )||
		(document.flights.from.selectedIndex == "")||
		(document.flights.to.selectedIndex == "")||
		(
			document.flights.to[document.flights.to.selectedIndex].value-document.flights.from[document.flights.from.selectedIndex].value < 0
		)
		)
	{
	
		document.getElementById('errormessage').innerHTML='Please check all search fields have been filled in correctly.<br/><br/>';
		self.location.href = '#top'; 
		return false;
	}
	else{
		document.getElementById('errormessage').innerHTML='';
		return true;
	}	
	
}



function updateTravelType(type){
	if (type==1){
			betweenDates();
		
	}
	else{
		exactDates()
	}

}
/*
	Alters what is visible / enabled when the toggles at the top are changed.
*/
function updateFlightType(flightType) {
	
	if (flightType==2) {
	
		oneWayFlight();
		
		

	}
	else if (flightType==1){
		document.flights.trip_length.disabled = true;
		//setPrice(10);
	}
	else
	{
		returnFlight();
		
	}
}

function resetForm(){
	document.flights.reset();
	returnFlight();
	betweenDates();
	
	document.flights.dep.selectedIndex="";
	document.flights.dest.selectedIndex="";
	document.flights.to.selectedIndex="";
	document.flights.from.selectedIndex="";
}




function exactDates(){
		document.getElementById('exactdatesinfo').style.display='block';
		document.getElementById('betweeninfo').style.display='none';
		document.flights.from.options[0].text="---- Departure Date----";
		document.flights.to.options[0].text="---- Return Date----";
		document.flights.tripextension[0].checked=true;

}
function betweenDates(){
	document.getElementById('exactdatesinfo').style.display='none';
	document.getElementById('betweeninfo').style.display='block';	
	document.flights.from.options[0].text="---- Earliest Departure Date----";
	document.flights.to.options[0].text="---- Latest  Return Date ----";


}

function changeDestinationList() {

	

	

	var oldDest = "";

	if (document.flights.dest.length>0)
		oldDest = document.flights.dest.options[document.flights.dest.selectedIndex].value;


	// Remove all existing items from select list
	var dep = "";
	document.flights.alldest.checked=false;
	document.flights.alldest.disabled = false;
	
	if (dep=="") dep = document.flights.dep.options[document.flights.dep.selectedIndex].value;
	
	if (dep =="NAp1"){
		return false;
	}
	
	
		depList = eval('d' + dep);
	
		selectedIndex = 0;
	
		var data;
		var op = new Array(countries.length);
		
	
	
		for (var i = 0; i < op.length; i++){
			op[i]="";
		}
		
		for (var i = 0; i < depList.length; i++){
			if(depList[i]!="1"){	
			if (depList[i] == oldDest ){
				op[eval('c' + depList[i])]=op[eval('c' + depList[i])]+'<option value="'+depList[i]+'" selected="selected">'+eval('' + depList[i])+' ('+depList[i]+')</option>';
				}
				else{
				op[eval('c' + depList[i])]=op[eval('c' + depList[i])]+'<option value="'+depList[i]+'">'+eval('' + depList[i])+' ('+depList[i]+')</option>';
				}
			}		
		}
			
		for (var i = 0; i < op.length; i++){
			if (op[i]==""){}else{
			if ( ('all_'+countries[i-1].replace(/ /,'_').toLowerCase()+'_airports')== oldDest ){
				data= data+'<optgroup label="'+(countries[i-1])+'">'+op[i]+'<option value="all_'+countries[i-1].replace(/ /,'_').toLowerCase()+'_airports" style="font-weight:bold; " selected="selected">'+(countries[i-1])+': All airports</option></optgroup>';}
			else{
					if(dep=='any'){
											data= data+'<optgroup label="'+(countries[i-1])+'">'+op[i]+'</optgroup>';
	
					}
					else{
						data= data+'<optgroup label="'+(countries[i-1])+'">'+op[i]+'<option value="all_'+countries[i-1].replace(/ /,'_').toLowerCase()+'_airports" style="font-weight:bold; ">'+(countries[i-1])+': All airports</option></optgroup>';
					}
			}}
		}
		
		var t5list="";
		for (var i = 0; i < top5dept.length; i++){
			if(top5dept[i]==dep){
				t5 = eval('t5'+dep)
				for (var q = 0; q < t5.length-1; q++){
				t5list = t5list + '<option value="'+t5[q]+'">'+eval('' + t5[q])+' ('+t5[q]+')</option>';
				}
				t5list =t5list + '<option value="NAp1">----------------------</option>';
			}
		}
		
		data = t5list + data;
		document.getElementById('destChangeable').innerHTML='<select name="dest" style="width:250px;"><option value="NAp1"> ---- Destination ----  </option>'+data+'</select>';
		
		//var alldest determines whether all destinations can be shown
		alldest= false;
		ukAirports = eval('uk_airports');
		for (var i = 0; i < ukAirports.length-1; i++){
			if (ukAirports[i]==dep)alldest=true;
			
		}
		irlAirports= eval('irl_airports');
		for (var i = 0; i < irlAirports.length-1; i++){
			if (irlAirports[i]==dep)alldest=true;
		}
		ukDeptAirport();
	
	if(dep=="any"){
		ukDeptAirport();
		document.getElementById('destinations_checkbox').style.background='url(http://flightchecker.moneysavingexpert.com/images2/FCR_GoAnywhere_Grey.gif) no-repeat center';
		document.flights.alldest.checked=false;
		document.flights.alldest.disabled = true;
	}
	else if(!alldest){
			nonUkDeptAirport()	;
	}
	
	
	

}

function ukDeptAirport(){
	document.flights.flighttype[0].disabled=false;
	document.flights.flighttype[1].disabled=false;
	
	document.getElementById('destinations_checkbox').style.background='url(http://flightchecker.moneysavingexpert.com/images2/leftcolumn_illgo.gif) no-repeat center';
	document.getElementById('return_label').style.textDecoration='none';

}


function nonUkDeptAirport(){
	document.flights.flighttype[0].disabled=true;
	document.flights.flighttype[1].checked=true;
			
	document.flights.alldest.checked=false;
	document.flights.alldest.disabled = true;

		
	document.getElementById('destinations_checkbox').style.background='url(http://flightchecker.moneysavingexpert.com/images2/FCR_GoAnywhere_Grey.gif) no-repeat center';
	
	document.getElementById('return_label').style.textDecoration='line-through';
	oneWayFlight();
	updateFlightType(2)
			

}

function returnFlight(){
	document.flights.flighttype[0].checked=true;	
	document.getElementById('onewayWarning').style.display='none';
	document.flights.to.options[0].text="---- Latest  Return Date----"
	document.flights.trip_length.disabled = false;
	document.flights.searchdatetype[0].disabled = false;

}

function oneWayFlight(){
		document.flights.flighttype[1].checked=true;
		document.getElementById('onewayWarning').style.display='block';
		document.flights.to.options[0].text="---- Latest  Departure Date----"
		document.flights.trip_length.disabled = true;
}

/* 
	Enables the destination drop down field
*/
function enableDestination()
{
	document.flights.dest.disabled = false;
}

/* 
	Enables the departures drop down field
*/

function enableDeparture()
{
	document.flights.dep.disabled = false;
}


/*
	Changes the price 
*/
function setPrice(price)
{
	document.flights.price.value = price;
}

function setDestination(dep)
{	

	
	if(dep == "alldest"){
			document.flights.dest.disabled=true;
			document.flights.alldest.checked=true;
			
			
	}
	else{
		for (var i = 0; i < document.flights.dest.options.length-1; i++)
    		{
    				if (document.flights.dest.options[i].value == dep ) {selectedIndex=i; }
			}

	

		document.flights.dest.selectedIndex=selectedIndex;
	}
}



/* 
	THE FOLLOWING FUNCTIONS ARE FOR GENERATING THE SEO URLS
*/


String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function getMonthName(number) {
var names = new Array("jan", "feb", "mar", 
"apr", "may", "jun", "jul", "aug", "sep", 
"oct", "nov", "dec");
return names[number];
}
  function secondsToDate(seconds)
  {
    var theDate = new Date(seconds * 1000);
    var day = theDate.getDate();
    var month = theDate.getMonth();
    month = getMonthName(month);
    var year = theDate.getFullYear();
    year = year.toString().substring(2)

    dateString = day+'-'+month+'-'+year;
    return dateString;
  }

function get_check_value(location)
{
var c_value = "";
for (var i=0; i < location.length; i++)
   {
   if (location[i].checked)
      {
      c_value = c_value + location[i].value + "\n";
      }
   }
return c_value;
}

function airportName(name)
{
	var name_return = "";
	if (name == "london - all") name_return="london";
	else {
		if(name.indexOf('(')>0) name_return=name.substring(0, name.indexOf('('));
		else name_return=name;
	}

	return name_return.trim();

}

function price(value)
{

	var value_return = "";

	value_return = parseInt((value ? value : 20));

	return value_return;
}



function formURL() //Upon form submission, go to full URL
{

	url = '/cheap-flights_';
	dep="";
	//if (document.flights.dep.selectedIndex==1){ url = url+'any';dep='ALL';}
    //else {	
	if( document.flights.dep.options[document.flights.dep.selectedIndex].value.toLowerCase().substring(0,4)=="any"){	
		url = url+'any';
	}
	else{
				url = url+airportName(document.flights.dep.options[document.flights.dep.selectedIndex].text.toLowerCase());

	}
		dep = document.flights.dep.options[document.flights.dep.selectedIndex].value;
		
	//}
       url = url+'-';
	if (document.flights.alldest.checked)url = url+'any';	
    else if( document.flights.dest.options[document.flights.dest.selectedIndex].value.toLowerCase().substring(0,4)=="all_"){
		url = url+airportName(document.flights.dest.options[document.flights.dest.selectedIndex].value.toLowerCase());
	}

	else {
	
	url = url+airportName(document.flights.dest.options[document.flights.dest.selectedIndex].text.toLowerCase());
}
	

	alldest= false;

	ukAirports = eval('uk_airports');
    for (var i = 0; i < ukAirports.length; i++){
		if (ukAirports[i]==dep)alldest=true;
		
	}
	irlAirports= eval('irl_airports');
	for (var i = 0; i < irlAirports.length; i++){
		if (irlAirports[i]==dep)alldest=true;
	}
	
	if(!alldest && dep!="any"){
			//Inbound
			url = url+'_coming-back';
		
	}
	else if (get_check_value(document.flights.flighttype)==3) {
		url = url+'_return';
	}
	else{
		url = url+'_going';
	}

	url = url+'/';

	url = url+secondsToDate(document.flights.from.options[document.flights.from.selectedIndex].value);
	url = url+'_';
	url = url+secondsToDate(document.flights.to.options[document.flights.to.selectedIndex].value);


	url = url+'/';

	if(document.flights.searchdatetype[0].checked){
		//between dates
		url = url+'between';
	}
	else{
		/*NEED TO CODE TOGGLES IN*/
		if(document.flights.tripextension[0].checked)url = url+'exact-dates';
		else if (document.flights.tripextension[1].checked) url = url+'1-day-flex';
		else if (document.flights.tripextension[2].checked) url = url+'3-day-flex';
	}


	url = url+'/';

		url = url+ price(document.flights.price.value);

		url = url+'-pounds';

		if (!document.flights.trip_length.disabled && document.flights.trip_length.options[document.flights.trip_length.selectedIndex].value!="any") {

			url = url+'/';	

			url = url+document.flights.trip_length.options[document.flights.trip_length.selectedIndex].value+'-day-trip';
		}

		location.href=escape(url)+'#search';

}
