var isNS = (document.layers)? true:false;
function newsLetterFormSubmit(buttonVal) {
if (isNS) {
document.layers.bodyDiv.document.newsletter.buttonClicked.value = buttonVal;

document.layers.bodyDiv.document.newsletter.submit();
	} else {
	document.newsletter.buttonClicked.value = buttonVal;
	document.newsletter.submit();
	}
}

function tnc() {
	newsWin = window.open("http://images.opodo.com/evouchers/fr/alitalia/tnc.html","newWin","width=450,height=500, scrollbars=yes");
}

function openWin() {
	newsWin = window.open("http://www.newsletter.opodo.com/fr/index.html?Int_Id=001452~SAMPLE","newWin","width=650,height=600, scrollbars=yes");
}
if(isNS){
var widthCheck=window.innerWidth;
var heightCheck=window.innerHeight;
window.onResize=resizeFix;
}
function resizeFix(){
if(widthCheck!=window.innerWidth||heightCheck!=window.innerHeight)
history.go(0);
}
var monthDays=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,"janvier"), new month(1,"f&eacute;vrier"),new month(2,"mars"),new month(3,"avril"),new month(4,"mai"), new month(5,"juin"), new month(6,"juillet"), new month(7,"ao&ucirc;t"), new month(8,"septembre"), new month(9,"octobre"), new month(10,"novembre"), new month(11,"d&eacute;cembre"));


function filterDropdownForAdults(passedDropdown, maxSize) {
  for (i=0; i < maxSize; i++) {
    passedDropdown.options[i] = new Option(i);
    passedDropdown.options[i].value = parseInt(i)+1;
    passedDropdown.options[i].text = parseInt(i)+1;
  }
}

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;
		}
		date = new Date(year, month-1, day);
		return date;
}
	
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))){
			createDate(firstDay, firstMonth);
			outboundDate = date;
			createDate(secondDay, secondMonth);
			returnDate = date;
			return ((outboundDate.valueOf() <= returnDate.valueOf()) && (outboundDate.valueOf() > rightnow.valueOf())) ? true : false;
		} else {
			return false;
		}	
}
function isLeapYear(year){
		if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
			return true;
		} else {
		return false;
		}
	}
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 de d&eacute;part valable");
						return false;
					} else { 
							if(!validMonthDay(secondDay, secondMonth, secondYear)){
								alert("Veuillez saisir une date de d&eacute;part correcte");
								return false;
						   } else { 
									if (validReturnDate(formName, datesArray)){
										return true;
									} else {
								  			alert("Veuillez saisir une date de retour valable");
								 			return false;
									}
						   }
					}
		} else {
				alert("Veuillez saisir votre ville de d&eacute;part et d'arriv&eacute;e");
					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;
	if(sWhich == 'D'){
		emptyDropDown('D_Day');
		for (counter=1; counter<=monthDays[parseInt(iMonth - 1)]; counter++) {
			thisDate = new Date(monthNameForCalc[parseInt(iMonth - 1)] + ' ' + counter + ', ' + iYear);
			addOptionToDD('D_Day', dayNameShort[thisDate.getDay()] + ' ' + counter, (counter<10)?"0"+counter:counter);
		}
	
		addOptionToDD('D_Day', ' ', 100);
		var newIndex = monthDays[parseInt(iMonth - 1)];
		if((iDay<=monthDays[parseInt(iMonth - 1)])&&(iDay>0)){
			newIndex = iDay - 1;
		}
		document.flightSearch.D_Day.selectedIndex=newIndex;
	} else {
		emptyDropDown('R_Day');
		for (counter=1; counter<=monthDays[parseInt(iMonth - 1)]; counter++) {
			thisDate = new Date(monthNameForCalc[parseInt(iMonth - 1)] + ' ' + counter + ', ' + iYear);
			addOptionToDD('R_Day', dayNameShort[thisDate.getDay()] + ' ' + counter, (counter<10)?"0"+counter:counter);
		}
		addOptionToDD('R_Day', ' ', 100);
		var newIndex = monthDays[parseInt(iMonth - 1)];
		if((iDay<=monthDays[parseInt(iMonth - 1)])&&(iDay>0)){
			newIndex = iDay - 1;
		}
		document.flightSearch.R_Day.selectedIndex = newIndex;
	}
}

function calcDayOfWeek(sWhich) {

if (sWhich == 'D') {
	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;
	
	monthDays[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;
	
	monthDays[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;
		document.flightSearch.D_Month.selectedIndex = tomorrow.getMonth();	
		then = new Date(tomorrow.getTime()+(604800000))
		document.flightSearch.R_Day.selectedIndex = then.getDate()-1;
		document.flightSearch.R_Month.selectedIndex = then.getMonth();

		var oldDepartIndex = document.flightSearch.D_Day.selectedIndex;
		calcDayOfWeek('D');
		document.flightSearch.D_Day.selectedIndex = oldDepartIndex;
		var oldReturnIndex = document.flightSearch.R_Day.selectedIndex;
		calcDayOfWeek('R');
		document.flightSearch.R_Day.selectedIndex = oldReturnIndex;
}