/*
 * // $RCSfile: inc-package-search.js,v $
// $Source: /cvs/cyplonweb-live/includes/inc-package-search.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();
}

function leadingZero(num) {
num = num-0;
	if (num < 10) {
		num = "0" + num;
		return num;
	} else {
		return num;
	}
}


// 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) {
	// Check that airports are selected
	if (document.getElementById('departure').options[document.getElementById('departure').selectedIndex].value == "") {
		alert("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 == "") {
		alert("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;
	} 
    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)
	month_depart = month_depart - 1;
	RetDepDate.setMonth(month_depart)
	RetDepDate.setDate(day_depart)
	
	// create duration var and send it with return date to the  addDays function
	var duration = 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 = leadingZero(day_return) + "/" + leadingZero(month_return) + "/" + leadingZero(year_return);
	//RetDepDate = month_return + "/" + day_return + "/" + year_return
	if (!form.Departure1) {
		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;
	form.OutDepAir.value = document.getElementById('departure').options[document.getElementById('departure').selectedIndex].value;
	form.OutArrAir.value = document.getElementById('arrival').options[document.getElementById('arrival').selectedIndex].value;	
	
	setFormSessions(form);
	
	return true;                                                                                  
}

function setFormSessions(form){
	var data = form.serialize();
	new Ajax.Request("setformsessions.php", {
		method: 'post',
		parameters: data,
		onSuccess: function(t){
			t.responseTest;
		}	
	});
}

