/********************************************************************************************
 Author: Pier Paolo Manca
 Date: 08/04/2009
 
 Flights functions to set and format calendar date, set and get cookies and form 
 validation
 
 Used in the Flights main section
********************************************************************************************/

var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);

function checkTo(){
	if (document.getElementById("to_list").value == "U/L") {
		alert("Choose a destination");
		return (false);
	}
	else { return (true); }
}

function checkSubmitFlight(){
	if (checkDates(document.forms['MainForm'].DepartDate, 'Depart date', document.forms['MainForm'].ReturnDate, 'Return date', getCheckedValue(document.MainForm.onewayswitch) == '1') && checkAirportCodeCalendar() && checkTo()) {
		document.MainForm.submit();
	}
	else { return (false); }
}

function copyValue(obj){
	document.getElementById("other").value = obj.value;
}

function getFlightPageCookies(){

	if (readCookie('cheapFlightSearchFrom') != null) {
		//changeDefaultSelectedValue(document.getElementById("from"), readCookie('cheapFlightSearchFrom'));
	}
	if (readCookie('cheapFlightSearchToList') != null) {
		//changeDefaultSelectedValue(document.getElementById("to_list"), readCookie('cheapFlightSearchToList'));
	}
	if (readCookie('cheapFlightSearchOneWaySwitch') != null) {
		changeDefaultRadioSelectedValue(document.MainForm.onewayswitch, readCookie('cheapFlightSearchOneWaySwitch'));
	}
	if (readCookie('cheapFlightSearchTarifKlasse') != null) {
		changeDefaultRadioSelectedValue(document.MainForm.tarif_klasse, readCookie('cheapFlightSearchTarifKlasse'));
	}
	if (readCookie('cheapFlightSearchDepartDate') != null) {
		document.getElementById("DepartDate").value = readCookie('cheapFlightSearchDepartDate');
	}
	else {
		fillCurrentDate();
	}
	
	if (readCookie('cheapFlightSearchReturnDate') != null) {
		document.getElementById("ReturnDate").value = readCookie('cheapFlightSearchReturnDate');
	}
	if (readCookie('cheapFlightSearchPaxAdult') != null) {
		changeDefaultSelectedValue(document.getElementById("pax_adult"), readCookie('cheapFlightSearchPaxAdult'));
	}
	if (readCookie('cheapFlightSearchPaxChd') != null) {
		changeDefaultSelectedValue(document.getElementById("pax_chd"), readCookie('cheapFlightSearchPaxChd'));
	}
	if (readCookie('cheapFlightSearchPaxInf') != null) {
		changeDefaultSelectedValue(document.getElementById("pax_inf"), readCookie('cheapFlightSearchPaxInf'));
	}
	
}

function changeDefaultSelectedValue(objSelect, value){
	try {
		for (var i = 0; i < objSelect.options.length; i++) {
			if (objSelect.options[i].value == value) {
				objSelect.options[i].selected = true;
				break
			}
		}
	} 
	catch (e) {
		//alert(e);
	}
}

function fillCurrentDate(){
	var currentDate = new Date();
	var currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate() + 14);
	
	var day = currentDate.getDate();
	var month = currentDate.getMonth() + 1;
	
	if (parseInt(day) < 10) {
		day = "0" + day;
	}
	
	if (parseInt(month) < 10) {
		month = "0" + month;
	}
	
	var currentDateStr = day + "/" + month + "/" + currentDate.getFullYear();
	
	document.getElementById("DepartDate").value = currentDateStr;
	document.getElementById("ReturnDate").value = currentDateStr;
}

function changeDefaultRadioSelectedValue(objRadio, value){
	try {
		if ((value == 1) || (value == 'N')) {
			objRadio[0].checked = true;
			objRadio[1].checked = false;
		}
		else {
			objRadio[0].checked = false;
			objRadio[1].checked = true;
		}
		
	} 
	catch (e) {
		//Not a radio
	}
}

function updateReturnDate(){
	if ((document.getElementById("DepartDate") != null) && (document.getElementById("DepartDate").value != "")) {
		var leavingDate = document.getElementById("DepartDate").value;
		try {
			if (IsNumeric(leavingDate.substring(0, 2)) && IsNumeric(leavingDate.substring(3, 5)) && IsNumeric(leavingDate.substring(6, 10))) {
				var dateDep = new Date(leavingDate.substring(6, 10), leavingDate.substring(3, 5), leavingDate.substring(0, 2));
				var dateRetPlusWeek;
				if(leavingDate.substring(0, 2)!=31){ 
					 dateRetPlusWeek = new Date(leavingDate.substring(6, 10), ((leavingDate.substring(3, 5)) - 1), dateDep.getDate() + 7);
				}else{
					dateRetPlusWeek = new Date(leavingDate.substring(6, 10), ((leavingDate.substring(3, 5))), dateDep.getDate() + 6);
				}
				
				document.getElementById("ReturnDate").value = formatInputDate(dateRetPlusWeek);
			}
			
		} 
		catch (e) {
			//alert(e);
			return (false);
		}
	}
	else { return (false); }
}

function formatInputDate(value){

	var dateToFormat = new Date(value);
	
	var day = dateToFormat.getDate();
	var month = dateToFormat.getMonth() + 1;
	
	if (parseInt(day) < 10) {
		day = "0" + day;
	}
	
	if (parseInt(month) < 10) {
		month = "0" + month;
	}
	
	var DateStr = day + "/" + month + "/" + dateToFormat.getFullYear();
	return (DateStr);
}

function IsNumeric(value){
	var objRegExp = /^[0-9]*$/;
	try {
		return (objRegExp.test(value));
	} 
	catch (e) {
		return false;
	}
}

/* Internet Explorer 6 z-index fix */

function hideFixFirstDate(){
	if(isIE6){
		document.getElementById('pax_adult').style.visibility='hidden';
	}
}
	
function showFixFirstDate(){
	if(isIE6){
		document.getElementById('pax_adult').style.visibility='visible';
	}
}
	
function hideFixSecondDate(){
	if(isIE6){
		document.getElementById('pax_chd').style.visibility='hidden';
		document.getElementById('pax_inf').style.visibility='hidden';
	}
}
	
function showFixSecondDate(){
	if(isIE6){
		document.getElementById('pax_chd').style.visibility='visible';
		document.getElementById('pax_inf').style.visibility='visible';
	}
}

