var monthLengths=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var monthNameForCalc = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var dayNameShort= new Array('Dom','Lun','Mar','Mer','Gio','Ven','Sab');

rightnow = new Date();

function month(i,name) {
	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+" 0";
	(i<rightnow.getMonth()) ? a +=(nextyear-2000) : a +=(thisyear-2000);
	a += "</option>\n";
	this.option = a;
}
var months = new Array(new month(0,"Gen"), new month(1,"Feb"),new month(2,"Mar"),new month(3,"Apr"),new month(4,"Mag"), new month(5,"Giu"), new month(6,"Lug"), new month(7,"Ago"), new month(8,"Set"), new month(9,"Ott"), new month(10,"Nov"), new month(11,"Dic"));

var Maximum = 10;
function setOptions(chosen) {
	frm = document.flightSearch;
	var selbox = frm.BE_AIR_NUM_OF_CHILDREN;
	selbox.options.length = 0;
	var selbox2 = frm.BE_AIR_NUM_OF_INFANTS;
	selbox2.options.length = 0;
	var box1 = frm.BE_AIR_NUM_OF_ADULTS.options[frm.BE_AIR_NUM_OF_ADULTS.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);
			}
		}
	}
}

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;
}

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 flightSearchSubmit(buttonValue){
	document.flightSearch.whereTo.value = buttonValue;
	document.flightSearch.BE_QUICK_SEARCH_FLAG.value = true;
	var datesArray= new Array('D_Day', 'D_Month', 'R_Day', 'R_Month');
	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.D_City.value.length > 0) && (document.flightSearch.A_City.value.length > 0)) {
		if(!validMonthDay(firstDay, firstMonth, firstYear)) {
			alert("Inserire una data corretta");
			return false;
		} else {
			if(document.forms.flightSearch.TripType[0].checked == true){
				if(!validMonthDay(secondDay, secondMonth, secondYear)){
					alert("Inserire una data corretta");
					return false;
				} else {
					if (validReturnDate(formName, datesArray)){
						return true;
					} else {
						alert("Inserire date di partenza e di ritorno corrette");
						return false;
					}
				}
			} else {
				ReturnTrip();
				return true;
			}
		}
	} else {
		alert("Inserire destinazioni di partenza e di arrivo valide");
		document.getElementById("cityDepart").style.color="#FF3300";
		document.getElementById("cityArrive").style.color="#FF3300";
		return false;
	}
}

function emptyDropDown(whichDD) {
	var thisDD = eval('document.forms.flightSearch.' + whichDD);
	var oOptions = thisDD.options;
	for (var i=oOptions.length; i>=0; i--) {
		oOptions[i]	= null;
	}
	thisDD.selectedIndex=0;
	thisDD.length=0;
}

function addOptionToDD(whichDD, optionText, optionValue) {
	var thisDD = eval('document.forms.flightSearch.' + whichDD);
	thisDD[thisDD.length] = new Option(optionText, optionValue);
}

function populateDDforThisMonth(sWhich, iYear, iMonth, iDay) {
	var thisDate;
	emptyDropDown(sWhich);
	for (counter=1; counter<=monthLengths[parseInt(iMonth - 1)]; counter++) {
		thisDate = new Date(monthNameForCalc[parseInt(iMonth - 1)] + ' ' + counter + ', ' + iYear);
		addOptionToDD(sWhich, dayNameShort[thisDate.getDay()] + ' ' + counter, (counter<10)?"0"+counter:counter);
	}

	addOptionToDD(sWhich, ' ', 100);
	var newIndex = monthLengths[parseInt(iMonth - 1)] - 1;
	if((iDay<=monthLengths[parseInt(iMonth - 1)])&&(iDay>0)){
		newIndex = iDay - 1;
	}
	var thisDD = eval('document.flightSearch.' + sWhich);
	thisDD.selectedIndex=newIndex;
}

function calcDayOfWeek(sWhich) {
	if (sWhich == 'D_Day') {
		var iYear = parseInt(document.forms.flightSearch.D_Month.options[document.forms.flightSearch.D_Month.selectedIndex].value.substring(0,4));
		var iMonth = document.forms.flightSearch.D_Month.options[document.forms.flightSearch.D_Month.selectedIndex].value.substring(4,6);
		var iDay = document.forms.flightSearch.D_Day.options[document.forms.flightSearch.D_Day.selectedIndex].value;

		monthLengths[1] = ((iYear%4==0 && iYear%100!=0)||(iYear%400==0)) ? 29 : 28;
	} else {
		var iYear = parseInt(document.forms.flightSearch.R_Month.options[document.forms.flightSearch.R_Month.selectedIndex].value.substring(0,4));
		var iMonth = document.forms.flightSearch.R_Month.options[document.forms.flightSearch.R_Month.selectedIndex].value.substring(4,6);
		if (document.forms.flightSearch.R_Day.selectedIndex < 0){
			document.forms.flightSearch.R_Day.selectedIndex=0;
		}
		var iDay = document.forms.flightSearch.R_Day.options[document.forms.flightSearch.R_Day.selectedIndex].value;

		monthLengths[1] = ((iYear%4==0 && iYear%100!=0)||(iYear%400==0)) ? 29 : 28;
	}
	populateDDforThisMonth(sWhich, iYear, iMonth, iDay);
}

function init() {
	tomorrow = new Date(rightnow.getTime()+(86400000));
	document.flightSearch.D_Day.selectedIndex = tomorrow.getDate()-1;
	then = new Date(tomorrow.getTime()+(604800000))
	document.flightSearch.R_Day.selectedIndex = then.getDate()-1;
	document.flightSearch.R_Month.selectedIndex = (then.getMonth() - tomorrow.getMonth()) % 12;

	var oldDepartIndex = document.flightSearch.D_Day.selectedIndex;
	calcDayOfWeek('D_Day');
	document.flightSearch.D_Day.selectedIndex = oldDepartIndex;
	var oldReturnIndex = document.flightSearch.R_Day.selectedIndex;
	calcDayOfWeek('R_Day');
	document.flightSearch.R_Day.selectedIndex = oldReturnIndex;
}

function OnewayTrip(){
document.flightSearch.R_Day.disabled = true;
document.flightSearch.R_Month.disabled = true;

}
function ReturnTrip(){
document.flightSearch.R_Day.disabled = false;
document.flightSearch.R_Month.disabled = false;

}
