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('Dim','Lun','Mar','Mer','Jeu','Ven','Sam');

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,"janv"), new month(1,"f&eacute;v"),new month(2,"mar"),new month(3,"avr"),new month(4,"mai"), new month(5,"juin"), new month(6,"juil"), new month(7,"aoû"), new month(8,"sept"), new month(9,"oct"), new month(10,"nov"), new month(11,"d&eacute;c"));

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("Veuillez saisir une date valable");
			return false;
		} else {
			if(document.forms.flightSearch.TripType[0].checked == true){
				if(!validMonthDay(secondDay, secondMonth, secondYear)){
					alert("Veuillez saisir une date valable");
					return false;
				} else {
					if (validReturnDate(formName, datesArray)){
						return true;
					} else {
						alert("Veuillez saisir une date valable");
						return false;
					}
				}
			} else {
				return true;
			}
		}
	} else {
		alert("Veuillez saisir votre ville de départ et d'arrivée");
		//document.getElementById("searchFrom").style.color="#FF3300";
		//document.getElementById("searchTo").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;
	if(tomorrow.getDate()-1 == 0){
	document.flightSearch.D_Month.selectedIndex = (then.getMonth()+1 - tomorrow.getMonth())
	document.flightSearch.R_Month.selectedIndex = (then.getMonth()+1 - tomorrow.getMonth())
	}	
}


rightnow = new Date();
//write dates to dropdown
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+" ";
	(i<rightnow.getMonth()) ? a +=(nextyear) : a +=(thisyear);
	a += "</option>\n";
	this.option = a;
}														
var months = new Array(new month(0,"Janv."), new month(1,"Fév."),new month(2,"Mars"),new month(3,"Avril"),new month(4,"Mai"), new month(5,"Juin"), new month(6,"Juillet"), new month(7,"Août"), new month(8,"Sep."), new month(9,"Oct."), new month(10,"Nov."), new month(11,"Déc."));
var mDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31)	
	function calcDay(frm,nm,nm2){
 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;
  }
 }
function setDDValue(oDropDown, sValue){
 for(var iIndex=0; iIndex<oDropDown.options.length; ++iIndex){
  if(oDropDown.options[iIndex].value == sValue){
   oDropDown.selectedIndex = iIndex;
   break;
   }
  }
 }

function frmCheckSubmit(){
	var f1 = document.frm
	//var f2 = document.frm
	monthYear = f1.outboundDateMonthYear.options[f1.outboundDateMonthYear.selectedIndex].value;
	dayVal = f1.outboundDateDate.options[f1.outboundDateDate.selectedIndex].value;
	monthVal= monthYear.substr(4,2);
	yearVal = monthYear.substring(0,4);
				
	secMonthYear = f1.returnDateMonthYear.options[f1.returnDateMonthYear.selectedIndex].value;
	secDayVal = f1.returnDateDate.options[f1.returnDateDate.selectedIndex].value;
	secMonthVal = secMonthYear.substr(4,2);
	secYearVal = secMonthYear.substring(0,4);
	
	var date = new Date(yearVal, monthVal-1, dayVal);
	var secdate = new Date(secYearVal, secMonthVal-1, secDayVal);
	var theDate = date.getDate();
	var theSecDate = secdate.getDate();
	var DCity = f1.originInput.value;
	var ACity = f1.destination.value;
	
			if (DCity==0) {
				alert("Veuillez saisir une ville de départ correcte.");
				f1.originInput.focus();
				return false;
			}
			if (ACity==0) {
				alert("Veuillez saisir une ville de destination correcte.");
				f1.destination.focus();
				return false;
			}
	
	
			if (!((rightnow.valueOf() < date.valueOf()) && (date.valueOf() < secdate.valueOf()))) {
				alert("Veuillez corriger l'heure et/ou la date de retour.");
				return false;			
			} else {
	    		f1.submit();
				return false;	
			}
 }
	
function opodoInit() {
		var f1 = document.frm
		tomorrow = new Date(rightnow.getTime()+(604800000));
		for(d=0;d<31;d++) f1.outboundDateDate[d] = new Option(d+1,((d<10)?"0"+(d+1):d+1));
		for(d=0;d<31;d++) f1.returnDateDate[d] = new Option(d+1,((d<10)?"0"+(d+1):d+1));
		f1.outboundDateDate.selectedIndex = tomorrow.getDate()-1;
		f1.outboundDateMonthYear.selectedIndex = tomorrow.getMonth();	
		then = new Date(tomorrow.getTime()+(604800000));
		f1.returnDateDate.selectedIndex = then.getDate()-1;
		f1.returnDateMonthYear.selectedIndex = then.getMonth();
		copyDestinationName();
		stopMouseWheel();
}

function generateInfantRoomAllocation(){
	var searchForm = document.forms['frm'];
	if (searchForm == null){
		searchForm = document.forms['frm'];
	}
	var inputName = 'roomAllocations[0].adultQuantity';
	var infantRoomAllocation = 'roomAllocations[0].infantQuantity';
	var totalTravellers=parseInt(searchForm[inputName].value);
	generateOption(searchForm[infantRoomAllocation], parseInt(searchForm[inputName].value));
}
function generateOption(elem, num){
	var prevSelectedVal = elem.value;

	elem.length = 0;
	for (var i=0; i<=num; i++){
		var option = document.createElement('option');
		option.value = i;
		option.innerHTML = i;
		if (i==prevSelectedVal){
			option.selected = "true";
		}
		elem.appendChild(option);
		if (prevSelectedVal < elem.length){
			elem.selectedIndex = prevSelectedVal;
		} else {
			elem.selectedIndex = elem.length - 1;
		}
	}
}

function stopMouseWheel(){
if (window.attachEvent && document.getElementsByTagName) {
    var  sel = document.getElementsByTagName("select");
    for (i=0; i<sel.length; ++i) {
            sel[i].attachEvent('onmousewheel', function() {return false;});
    	}
	}
}
function copyDestinationName() {
    var destinationList = document.getElementById('destinationdp');
    var destElement = document.getElementById('destinationInputdp');
    var toCopy = destinationList.options[destinationList.selectedIndex];
    destElement.value = toCopy.innerHTML;
}

function show1() {
		document.getElementById('opodo_vols').style.display = 'block';
		document.getElementById('opodo_vol-hotel').style.display = 'none';
		document.getElementById('init').style.color = '#000';
		document.getElementById('init2').style.color = '#fff';
		document.getElementById('init').style.backgroundColor = '#f7f7e7';
		document.getElementById('init2').style.backgroundColor = '#f60';
}
function show2() {
		document.getElementById('opodo_vols').style.display = 'none';
		document.getElementById('opodo_vol-hotel').style.display = 'block';
		document.getElementById('init').style.color = '#fff';
		document.getElementById('init2').style.color = '#000';
		document.getElementById('init').style.backgroundColor = '#f60';
		document.getElementById('init2').style.backgroundColor = '#f7f7e7';
}
