var Maximum = 10;
function setOptions(chosen) {
frm = document.flightSearch;
var selbox = frm.NUM_OF_CHD;
	selbox.options.length = 0;
var selbox2 = frm.NUM_OF_INFS;
	selbox2.options.length = 0;
var box1 = frm.NUM_OF_ADTS.options[frm.NUM_OF_ADTS.selectedIndex].value;
for(q=0;q<=box1;q++){
	 selbox2.options[selbox2.options.length] = new Option(q,q);
}
for(i=0;i <Maximum ; i++){
	if (chosen == i) {
		selbox.options[selbox.options.length] = new Option(0,0);
			for(z=1;z <(Maximum-i) ; z++){
			  selbox.options[selbox.options.length] = new Option(z,z);
			}
		}
	}
}

var monthDays=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var months = new Array(new month(0,"Januar"), new month(1,"Februar"),new month(2,"März"),new month(3,"April"),new month(4,"Mai"), new month(5,"Juni"), new month(6,"Juli"), new month(7,"August"), new month(8,"September"), new month(9,"Oktober"), new month(10,"November"), new month(11,"Dezember"));

function month(i,name) {
rightnow = new Date();
	var thisyear = rightnow.getFullYear();
	var nextyear = thisyear + 1;
	var a = "<option value=\"";
	(i<rightnow.getMonth()) ? a +=nextyear : a +=thisyear;
	(i<9) ? a += "0"+(i+1) : a +=(i+1);
	a += "\">"+name+' ';
	(i<rightnow.getMonth()) ? a +=((nextyear<10)?'0'+(nextyear):(nextyear)) : a +=((thisyear<10)?'0'+(thisyear):(thisyear));
	a += "</option>\n";
	this.option = a;
}
																		
function validReturnDate(formName, elementArray) {
		firstDay = eval('document.' + formName + '.' + elementArray[0] + ".options[" + 'document.' + formName + '.' + elementArray[0] + '.selectedIndex].value');
		firstMonth = eval('document.' + formName + '.' + elementArray[1] + ".options[" + 'document.'	+ formName + '.' + elementArray[1] + '.selectedIndex].value');
		firstYear = firstMonth.substr(0,4);
		secondDay = eval('document.' + formName + '.' + elementArray[2] + ".options[" + 'document.'	+ formName + '.' + elementArray[2] + '.selectedIndex].value');
		secondMonth = eval('document.' + formName + '.' + elementArray[3] + ".options[" + 'document.'	+ formName + '.' + elementArray[3] + '.selectedIndex].value');
		secondYear = secondMonth.substr(0,4);
		if ((validMonthDay(firstDay, firstMonth, firstYear))&& (validMonthDay(secondDay, secondMonth, secondYear))){
			outboundDate = createDate(firstDay, firstMonth);
			returnDate = createDate(secondDay, secondMonth);
			return ((outboundDate.valueOf() <= returnDate.valueOf()) && (outboundDate.valueOf() > rightnow.valueOf()) ) ? true : false;
		} else {
			return false;
		}	
}

function isLeapYear(year) {
	return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
}

function validMonthDay(dayVal, monthVal, yearVal){
	var error = 0;
	if ((monthVal.substr(4,2) == 4)||(monthVal.substr(4,2) == 6)||(monthVal.substr(4,2) == 9)||(monthVal.substr(4,2) == 11)){
		if (dayVal>30) error = 1;
	} else if (monthVal.substr(4,2) == 2) {
		if (isLeapYear(yearVal)){ 
			if (dayVal>29) error = 1;
		} else {
			if (dayVal>28) error = 1;
		}
	} else {
		if (dayVal>31) error = 1;
	}
	return (error == 1) ? false : true;	
}

function createDate(day, monthYear) {
	dayVal = day;
	monthYearVal = monthYear;
	x = dayVal.substr(0,1);
	(x == "0") ? day = parseInt(dayVal.substr(1,1)) : day = parseInt(dayVal.substr(0,2));
	
	y = monthYearVal.substr(4,1);
	(y == "0") ? month = parseInt(monthYearVal.substr(5,1)) : month = parseInt(monthYearVal.substr(4,2));
	
	year = parseInt(monthYearVal.substr(0,4));
	if (year < 1900) year = year + 1900;
	newDate = new Date(year, month-1, day);
	return newDate;
}

var monthLengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

function isLeapYear(year) {
	return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}

var rightnow = new Date();

function isNull(val){return(val==null);}

function changeDate(objMonth, objDate, adoptMonth, adoptDate, difference) {
	maxDays = monthLengths[(rightnow.getMonth() + objMonth.selectedIndex) % 12];
	if ( (objMonth.selectedIndex == 1) && (isLeapYear(objMonth.value.substring(0, 4))) ) maxDays="29";

	var oldSelect = objDate.selectedIndex;
	if (oldSelect > maxDays-1) oldSelect = maxDays-1;
	
	objDate.length = 0;
	for (d=0;d<maxDays;d++) 
	{
	    objDate[d] = new Option((d+1),(((d+1)<10)?"0"+(d+1):d+1));
	}

	objDate.selectedIndex = oldSelect;

	if (difference > 0 || difference == undefined)
	{
	    var priMonth = objMonth;
	    var priDate = objDate;
	    var secMonth = adoptMonth;
	    var secDate = adoptDate;
	}
	else
	{
	    var secMonth = objMonth;
	    var secDate = objDate;
	    var priMonth = adoptMonth;
	    var priDate = adoptDate;
	}
		
	if (secMonth != undefined)
	{
	    if (priMonth.value > secMonth.value)
	    {
		secMonth.selectedIndex = priMonth.selectedIndex;
	    }
	    if (priMonth.value == secMonth.value && priDate.value > secDate.value)
	    {
	        secDate.selectedIndex = priDate.selectedIndex;		
	    }
	}	

}


// flights /////////////////////////////////////////////////////////////////////////////////////////
function flightSearchSubmit(buttonValue){

		if (submitform == false) return false;
		
		
//		document.flightSearch.whereTo.value = buttonValue;
//		document.flightSearch.BE_QUICK_SEARCH_FLAG.value = true;
		
		var datesArray= new Array('departureDay', 'departureMonth', 'returnDay', 'returnMonth');
		var formName = 'flightSearch';
		firstDay = eval('document.' + formName + '.' + datesArray[0] + ".options[" + 'document.' + formName + '.' + datesArray[0] + '.selectedIndex].value');
		firstMonth = eval('document.' + formName + '.' + datesArray[1] + ".options[" + 'document.' + formName + '.' + datesArray[1] + '.selectedIndex].value');
		firstYear=firstMonth.substr(0,4);
		secondDay = eval('document.' + formName + '.' + datesArray[2] + ".options[" + 'document.' + formName + '.' + datesArray[2] + '.selectedIndex].value');
		secondMonth = eval('document.' + formName + '.' + datesArray[3] + ".options[" + 'document.'	+ formName + '.' + datesArray[3] + '.selectedIndex].value');
		secondYear=secondMonth.substr(0,4);
		
		if((document.flightSearch.departureAirport.value.length > 0 && document.flightSearch.arrivalAirport.value.length > 0)) {
			if(!validMonthDay(firstDay, firstMonth, firstYear)) {

				alert("Bitte geben Sie ein gültiges Datum ein");
				return false;
			} else { 
				if(!validMonthDay(secondDay, secondMonth, secondYear)){
					alert("Bitte geben Sie ein gültiges Datum ein");
					return false;
				} else { 
					if (validReturnDate(formName, datesArray)){
						document.flightSearch.submit();
						return true;
						
					} else {
						alert("Bitte geben Sie ein gültiges Datum ein");
						return false;
					}
				}
			}
		} else {
			alert("Bitte geben Sie einen Abflugort/Zielort ein");
			document.getElementById("cityDepart").style.color="#FF6600";
			document.getElementById("cityArrive").style.color="#FF6600";
			return false;
		}
		
}

// set week day
var days =  ["So","Mo","Di","Mi","Do","Fr","Sa",];
var mDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31)
function calcDay(frm,nm,nm2,day){
	dateVal = document.forms[frm].elements[nm].options[document.forms[frm].elements[nm].selectedIndex].value;
	monthVal = document.forms[frm].elements[nm2].options[document.forms[frm].elements[nm2].selectedIndex].value;
	var isLeapYear = (((monthVal.substr(0,4) % 4 == 0) && (monthVal.substr(0,4) % 100 != 0)) || (monthVal.substr(0,4) % 400 == 0));
	mDays[1] = (isLeapYear)?29:28;
		if(dateVal > mDays[monthVal.substr(4,2)-1]){
			setDDValue(document.forms[frm].elements[nm],mDays[monthVal.substr(4,2)-1]);
			dateVal = document.forms[frm].elements[nm].options[document.forms[frm].elements[nm].selectedIndex].value;}
		newDate = new Date(Date.UTC(monthVal.substr(0,4),parseInt(monthVal.substr(4,2)-1),dateVal));
		document.forms[frm].elements[day].value = days[newDate.getDay()];
}

function initFlights() {
	tomorrow = new Date(rightnow.getTime()+(86400000));
	document.flightSearch.departureDay.selectedIndex = tomorrow.getDate()-1;
	then = new Date(tomorrow.getTime()+(604800000))
	document.flightSearch.returnDay.selectedIndex = then.getDate()-1;
	document.flightSearch.returnMonth.selectedIndex = (then.getMonth() - tomorrow.getMonth()) % 12;
	var oldDepartIndex = document.flightSearch.departureDay.selectedIndex;
	document.flightSearch.departureDay.selectedIndex = oldDepartIndex;
	var oldReturnIndex = document.flightSearch.returnDay.selectedIndex;
	document.flightSearch.returnDay.selectedIndex = oldReturnIndex;
	if(tomorrow.getDate()-1 == 0){
	document.flightSearch.departureMonth.selectedIndex = (then.getMonth()+1 - tomorrow.getMonth())
	document.flightSearch.returnMonth.selectedIndex = (then.getMonth()+1 - tomorrow.getMonth())
	}
	calcDay('flightSearch','departureDay','departureMonth','PDayOfWeek');
	calcDay('flightSearch','returnDay','returnMonth','DDayOfWeek');
}


function flightPrepSubmit()
{
    if (document.getElementById('departureAirport_hidden').value == '' && document.getElementById('departureAirport').value.length == 3) 
		document.getElementById('departureAirport_hidden').value = document.getElementById('departureAirport').value;
    if (document.getElementById('arrivalAirport_hidden').value == '' && document.getElementById('arrivalAirport').value.length == 3) 
		document.getElementById('arrivalAirport_hidden').value = document.getElementById('arrivalAirport').value;
		
    submitform = true;
    flightSearchSubmit();
}
					    
function dateChangeTrigger(){
}

// set preferred Airlines
function fillSelect(field, codeArray, nameArray,selectedValue){
	var selectedIndex = -1;
	for (var i = 0; i < codeArray.length; i++){
	if (selectedValue == codeArray[i])
		selectedIndex = field.length;
	field[field.length] = new Option(nameArray[i], codeArray[i]);
	}
	if (selectedIndex != -1)
	field.selectedIndex = selectedIndex;
}

var airlines = new Array("American Airlines","Air Berlin","Air Canada","Air France","Alitalia","British Airways","China Airlines","Continental Airlines","Cathay Pacific","Condor","Delta Air Lines","Emirates","Etihad Airways","Finnair","Icelandair","Gulf Air","Iberia","Spanair","KLM","Lufthansa","LOT","LTU","SWISS","All Nippon Airways","Czech Airlines","Austrian Airlines","South African Airways","SAS","Brussels Airlines","Air Transat","Thai Airways","Turkish Airlines","TAP","United Airlines","US Airways","Virgin Atlantic");

var airlineCodes = new Array("AA","AB","AC","AF","AZ","BA","CI","CO","CX","DE","DL","EK","EY","AY","FI","GF","IB","JK","KL","LH","LO","LT","LX","NH","OK","OS","SA","SK","SN","TA","TG","TK","TP","UA","US","VS");


