/*
// // $RCSfile: inc-search-flights.js,v $
// $Source: /cvs/cyplonweb-live/includes/inc-search-flights.js,v $, $Revision: 1.1 $, $Date: 2010/07/12 10:32:37 $, $State: Exp $
*/

function open_flightinfo() {
Window = window.open("flight-information.php","airlines","width=700,height=515,toolbar=no,directories=no,status=yes,scrollbars=yes,resize=no,menubar=no,screenX=100,screenY=100,left=100,right=100");
}

//funtion to add days to any date
Date.prototype.addDays=function(num)
{
	return new Date((new Number(this)) + (num * 60 * 60 * 24 * 1000))
}

function fixElement(element, message) {
	alert(message);
	element.focus();
	}

// Hide Paphos from Heathrow
function checkAirport(airport) {
	/*
	if (airport=="LHR") {
		document.getElementById('arrivalAJAX').selectedIndex = 1;	
	}*/
	
	// get current duration value
	selectedDur = document.CriteriaForm.duration.value;
	if (selectedDur == "") {selectedDur = 7};
	// write duration selection box
	strDuContText = "<select name=\"duration\" size=\"1\" tabindex=\"\" onChange=\"checkAirport(document.getElementById('departureAJAX').options[document.getElementById('departureAJAX').selectedIndex].value)\">";
	if (airport == "LTN" || airport == "LHR" || airport == "STN" || airport == "MAN") {
		airport == "MAN" ? minDur = 7 : minDur = 3;
		for (i = minDur; i<31; i++) {
			selected = "";
			if (i == selectedDur) {
				selected = " selected";
			}
			strDuContText += "<option value=\"" + i + "\"" + selected + ">" + i + " Days</option>";
		}
	} else {
		selected = "";
		if (selectedDur != 14) {
			selected = " selected";
		}		
		strDuContText += "<option value=\"7\"" + selected + ">7 Days</option>";
		selected = "";
		if (selectedDur == 14) {
			selected = " selected";
		}		
		strDuContText += "<option value=\"14\"" + selected + ">14 Days</option>";
	}
	strDuContText += "</select>"
	
	if (document.getElementById) {
		document.getElementById("dContainer").innerHTML = strDuContText;
	}
}

// Number validation
function numberVal(numberString) {
	var valid = 0;
	for (var i=0; i<numberString.length; i++) {
		for (var j=48; j < 58; j++) {
			if (numberString.charCodeAt(i) == j) {
				valid = 1;
			}
		}
		// if any character other than number is present, error occurs
		if (valid == 0) {
			return 0;
			break;
		} else {
		// reset valid to false for next character
			valid = 0;
		}
	} 
	return 1
}


function checkForm(form) {

	//alert(document.CriteriaForm.AdultPax.value);
	
	// Check for valid numerical entries
	if (document.CriteriaForm.AdultPax.value != "0") {
		var isValid = numberVal(document.CriteriaForm.AdultPax.value);
		//alert('isValid ' + isValid);
		if (isValid == 0) {
			fixElement(form.AdultPax, "You must enter a valid number of adult travellers.\n\n Please change your entry and click the search button again.");
			return false;
		}	
	}
	
	if (document.CriteriaForm.ChildPax.value != "0") {
		var isValid = numberVal(document.CriteriaForm.ChildPax.value);
		//alert('isValid ' + isValid);
		if (isValid == 0) {
			fixElement(form.ChildPax, "You must enter a valid number of children.\n\n Please change your entry and click the search button again.");
			return false;
		}	
	}
	
	if (document.CriteriaForm.InfantPax.value != "0") {
		var isValid = numberVal(document.CriteriaForm.InfantPax.value);
		if (isValid == 0) {
			fixElement(form.InfantPax, "You must enter a valid number of children.\n\n Please change your entry and click the search button again.");
			return false;
		}	
	}

	// Check that airports are selected
	if (document.getElementById('departure').options[document.getElementById('departure').selectedIndex].value == "") {
  		fixElement(document.getElementById('departure'), "You have not selected a departure airport.\n\n Please select an airport and click the search button again.");
			return false;
	}
		else if (document.getElementById('arrival').options[document.getElementById('arrival').selectedIndex].value == "") {
  		fixElement(document.getElementById('arrival'), "You have not selected the destination you wish to go to.\n\n Please select a destination and click the search button again.");
			return false;
	} 
	//format data to send to atop server
		else {
			var day_depart=document.CriteriaForm.OutDay.options[document.CriteriaForm.OutDay.selectedIndex].value;
			var month_depart=(document.CriteriaForm.OutMonth.options[document.CriteriaForm.OutMonth.selectedIndex].value).substring(0,2);
			var year_depart=(document.CriteriaForm.OutMonth.options[document.CriteriaForm.OutMonth.selectedIndex].value).substring(4,6);
			
			var OutDepDate = day_depart + "/" + month_depart + "/" + year_depart;
			//var OutDepDate = month_depart + "/" + day_depart + "/" + year_depart;
			form.OutDepDate.value = OutDepDate;

			// create new return date object and asign departure date to it
			var RetDepDate = new Date()
			RetDepDate.setYear("20"+year_depart)
			RetDepDate.setMonth(month_depart-1)
			RetDepDate.setDate(day_depart)

			// create duration var and send it with return date to the  addDays function
			var duration = 0
			duration = 0 + (document.getElementById('duration').options[document.getElementById('duration').options.selectedIndex].value);
			RetDepDate = RetDepDate.addDays(duration);

			//get newly calculated year, month, day
			year_return = RetDepDate.getYear()
			year_return = year_return.toString()//turn into string for slicing
			year_return = year_return.slice(2)//slice year to last 2 numbers, eg. 2002 - 02
			month_return = RetDepDate.getMonth()+1//sync month
			day_return = RetDepDate.getDate()

			//build return date string into ATOP format dd/mm/yy
			RetDepDate = day_return + "/" + month_return + "/" + year_return
			//RetDepDate = month_return + "/" + day_return + "/" + year_return
			form.RetDepDate.value = RetDepDate;
			
			// Return trip airports
			form.RetDepAir.value = document.getElementById('arrival').options[document.getElementById('arrival').selectedIndex].value;			
			form.RetArrAir.value = document.getElementById('departure').options[document.getElementById('departure').selectedIndex].value;
			if (form.Destination1) {
				form.Destination1.value = document.getElementById('arrival').options[document.getElementById('arrival').selectedIndex].value;
			}
			if (form.Departure1) {
				form.Departure1.value = document.getElementById('departure').options[document.getElementById('departure').selectedIndex].value;
			}
			
			document.getElementById('duration').value = document.getElementById('duration').options[document.getElementById('duration').selectedIndex].value;
			$('lateDeals').style.display = "none";
			
			$('searchBack').style.display = "block";
			fadeBox();
		}
	}
