function toggleDisplay(tg) {

	switch(tg) {
	case 'flights':
		//document.getElementById('OpodoQuickSearch').style.backgroundImage = 'url(http://promos.opodo-promotions.com/affiliateqs/bg_flightsMultiple300x250.jpg)';
		document.getElementById('OpodoQuickFlightSearch').style.display = 'block';
		document.getElementById('OpodoQuickHotelSearch').style.display = 'none';
		document.getElementById('OpodoQuickCarSearch').style.display = 'none';
		break
	case 'hotel':
		//document.getElementById('OpodoQuickSearch').style.backgroundImage = 'url(http://promos.opodo-promotions.com/affiliateqs/bg_hotelsMultiple300x250.jpg)';
		document.getElementById('OpodoQuickFlightSearch').style.display = 'none';
		document.getElementById('OpodoQuickHotelSearch').style.display = 'block';
		document.getElementById('OpodoQuickCarSearch').style.display = 'none';
		break
	case 'cars':
		//document.getElementById('OpodoQuickSearch').style.backgroundImage = 'url(http://promos.opodo-promotions.com/affiliateqs/bg_carsMultiple300x250.jpg)';
		document.getElementById('OpodoQuickFlightSearch').style.display = 'none';
		document.getElementById('OpodoQuickHotelSearch').style.display = 'none';
		document.getElementById('OpodoQuickCarSearch').style.display = 'block';
		break
	}
}


var monthDays=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var months = new Array(new month(0,"Jan"), new month(1,"Feb"),new month(2,"Mar"),new month(3,"Apr"),new month(4,"May"), new month(5,"Jun"), new month(6,"Jul"), new month(7,"Aug"), new month(8,"Sep"), new month(9,"Oct"), new month(10,"Nov"), new month(11,"Dec"));

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+" 0";
	(i<rightnow.getMonth()) ? a +=(nextyear-2000) : a +=(thisyear-2000);
	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))){
			var outboundDate = createDate(firstDay, firstMonth);
			var returnDate = createDate(secondDay, secondMonth);

			return ((outboundDate.valueOf() <= returnDate.valueOf()) && (outboundDate.valueOf() > rightnow.valueOf()) ) ? true : false;
			return true;

		} 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;
	var 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 changeDate(objMonth, objDate) {
	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<10)?"0"+(d+1):d+1));

	objDate.selectedIndex = oldSelect;
}

// flights /////////////////////////////////////////////////////////////////////////////////////////
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("Please enter a valid departure date");
				return false;
			} else {
				if(!validMonthDay(secondDay, secondMonth, secondYear)){
					alert("Please enter valid return and departure dates");
					return false;
				} else {
						if (validReturnDate(formName, datesArray)){

                                                window.open('http://www.opodo.co.uk/affiliate/AffiliateLanding.jsp?searchType=flight'+
						'&CMP='+document.getElementById('CMP').value+'&Locale=uk&performFlightSearch=TRUE&BE_QUICK_SEARCH_FLAG=TRUE'+
						'&BE_AIR_IS_FROM_SAVED=TRUE&HP_QUICKSEARCH_USED=TRUE&whereTo=&D_Time=ANY&BE_AIR_R_Time=ANY'+
						'&SEARCH_BY=2&Destination%231_DIRECT_NON_STOP=N&Destination%231_Airline%231='+
						'&Destination%231_Airline%232=&Destination%231_Airline%233=&ARRANGE_BY=2'+
						'&availSubmit=*&availSearchSubmit=*&TripType=2&ClassOfService=E&ParamSet=1&Prepop=N'+
						'&D_City='+document.getElementById('D_City').value+
						'&A_City='+document.getElementById('A_City').value+
						'&D_Day='+document.getElementById('D_Day').value+
						'&Mkt_Id='+document.getElementById('Mkt_Id').value+
						'&WLS_ID='+document.getElementById('WLS_ID').value+
						'&D_Month='+document.getElementById('D_Month').value+
						'&R_Day='+document.getElementById('R_Day').value+
						'&R_Month='+document.getElementById('R_Month').value+
						'&BE_AIR_NUM_OF_ADULTS=1', 'Searchwindow');

						return false;

					} else {
						alert("Please enter valid return and departure dates");
						return false;
					}
				}
			}
		} else {
			alert("Please enter a valid Departure and Arrival City");
			document.getElementById("cityDepart").style.color="#FF3300";
			document.getElementById("cityArrive").style.color="#FF3300";
			return false;
		}
}

function initFlights() {
	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;
	document.flightSearch.D_Day.selectedIndex = oldDepartIndex;
	var oldReturnIndex = document.flightSearch.R_Day.selectedIndex;
	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())
	}
}


// hotels /////////////////////////////////////////////////////////////////////////////////////////
function setDest() {
	document.hotelOutput.destinationNameCheck.value = document.hotelOutput.destinationName.value;
}

function hotelSubmit(){
	var f1 = document.hotel
	var f2 = document.hotelOutput
	monthYear = f1.checkInDateB.options[f1.checkInDateB.selectedIndex].value;
	dayVal = f1.checkInDateA.options[f1.checkInDateA.selectedIndex].value;
	monthVal= monthYear.substr(4,2);
	yearVal = monthYear.substring(0,4);

	secMonthYear = f1.depDateB.options[f1.depDateB.selectedIndex].value;
	secDayVal = f1.depDateA.options[f1.depDateA.selectedIndex].value;
	secMonthVal = secMonthYear.substr(4,2);
	secYearVal = secMonthYear.substring(0,4);

	if (document.hotelOutput.destinationNameCheck.value == "") {
		alert("Please enter a destination");
		document.getElementById("hotelSearch").style.color="#FF3300";
		//document.getElementById("destinationName").focus();
		//document.getElementById("destinationName").select();
		return false;
	}

	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 aCity = f2.destinationName.value;
	if (secdate <= date) {
		alert("Please enter valid arrival and departure dates");
		return false;
	}
	if (aCity==0) {
		alert("Please enter a city name");
		document.getElementById("hotelSearch").style.color="#FF3300";
		return false;
	}
	if ((rightnow > date)||(rightnow > secdate)) {
		alert("Please enter valid arrival and departure dates");
		return false;
	}
	if ((theDate == null)&&(theSecdate == null)) {
		alert("Please enter valid arrival and departure dates");
		return false;
	}
	if ((dayVal != theDate)||(secDayVal != theSecDate)) {
		alert("Please enter valid arrival and departure dates");
		return false;
	} else {
		f2.checkInDate.value = monthYear + dayVal;
		f2.checkOutDate.value = secMonthYear + secDayVal;
		f2.submit();
		return false;
	}
 }

function initHotels() {
	var f1 = document.hotel;
	tomorrow = new Date(rightnow.getTime()+(86400000));
	for(d=0;d<31;d++) {
		f1.checkInDateA[d] = new Option(d+1,((d<10)?"0"+(d+1):d+1));
		f1.depDateA[d] = new Option(d+1,((d<10)?"0"+(d+1):d+1));
	}
	f1.checkInDateA.selectedIndex = tomorrow.getDate()-1;
	then = new Date(tomorrow.getTime()+(172800000));
	f1.depDateA.selectedIndex = then.getDate()-1;

	then = new Date(tomorrow.getTime()+(604800000))
	f1.depDateA.selectedIndex = then.getDate()-1;
	f1.depDateB.selectedIndex = (then.getMonth() - tomorrow.getMonth()) % 12;
}


// cars /////////////////////////////////////////////////////////////////////////////////////////
function initCars(){

    if(document.carSearchFormBean.pickUpDateForm.value==""){
		sevendays = new Date(rightnow.getTime());
		document.carSearchFormBean.pickDate.selectedIndex = sevendays.getDate();
		then = new Date(sevendays.getTime()+(60480000));
		document.carSearchFormBean.dropDate.selectedIndex = then.getDate()-1;
		document.carSearchFormBean.dropMonth.selectedIndex = (then.getMonth() - sevendays.getMonth()) % 12;
	}else{
		var pickup=document.carSearchFormBean.pickUpDateForm.value;
		var drop=document.carSearchFormBean.dropOffDateForm.value;
		var pmonth=parseInt(pickup.slice(4,6),10);
		var pday=parseInt(pickup.slice(6,8),10);
		var ptime=parseInt(pickup.slice(8,10),10);
		document.carSearchFormBean.pickMonth.selectedIndex=pmonth-1;
		document.carSearchFormBean.pickDate.selectedIndex=pday-1;
		document.carSearchFormBean.pickHour.selectedIndex=ptime;
		var dmonth=parseInt(drop.slice(4,6),10);
		var dday=parseInt(drop.slice(6,8),10);
		var dtime=parseInt(drop.slice(8,10),10);
		document.carSearchFormBean.dropMonth.selectedIndex=dmonth-1;
		document.carSearchFormBean.dropDate.selectedIndex=dday-1;
		document.carSearchFormBean.dropHour.selectedIndex=dtime;
	}

	changeDate(document.carSearchFormBean.pickMonth, document.carSearchFormBean.pickDate);
	changeDate(document.carSearchFormBean.dropMonth, document.carSearchFormBean.dropDate);
}
function setPickLoc() {
	document.carSearchFormBean.destination1.value = document.carSearchFormBean.searchString.value;
}
function setDropLoc() {
	document.carSearchFormBean.destination2.value = document.carSearchFormBean.searchStringDrop.value;
}
function submitCheck() {
    if (document.carSearchFormBean.age.selectedIndex!=0){
    	var day = parseInt(document.carSearchFormBean.pickDate.options[document.carSearchFormBean.pickDate.selectedIndex].value,10);
    	var monthVal = new String(document.carSearchFormBean.pickMonth.options[document.carSearchFormBean.pickMonth.selectedIndex].value);
    	var month = parseInt(monthVal.slice(4),10);
    	var year = parseInt(monthVal.slice(0,4),10);
    	var pickupDate = new Date(year,month-1,day,12,0,0,0);
    	day = parseInt(document.carSearchFormBean.dropDate.options[document.carSearchFormBean.dropDate.selectedIndex].value,10);
    	monthVal = new String(document.carSearchFormBean.dropMonth.options[document.carSearchFormBean.dropMonth.selectedIndex].value);
    	month = parseInt(monthVal.slice(4),10);
    	year = parseInt(monthVal.slice(0,4),10);

		var dropoffDate = new Date(year,month-1,day,12,0,0,0);
    	if (pickupDate>dropoffDate || pickupDate<rightnow) {
	    	if (pickupDate>dropoffDate) {
	    		alert("Please check your drop-off date");
	    		document.getElementById("returnDate").style.color="#FF3300";
	    	}
	    	if (pickupDate<rightnow) {
	    		alert("Please check your pick-up date");
	    		document.getElementById("outboundDate").style.color="#FF3300";
	    	}
    		return false;
    	} else {
			document.carSearchFormBean.pickUpDateForm.value=""+pickupDate.getFullYear()+
			(((pickupDate.getMonth()+1)<10)?"0"+(pickupDate.getMonth()+1):(pickupDate.getMonth()+1))+
				((pickupDate.getDate()<10)?"0"+pickupDate.getDate():pickupDate.getDate())+
				((pickupDate.getHours()<10)?"0"+pickupDate.getHours():pickupDate.getHours())
				+((pickupDate.getMinutes()<10)?"0"+pickupDate.getMinutes():pickupDate.getMinutes());
			document.carSearchFormBean.dropOffDateForm.value=""+dropoffDate.getFullYear()+
			(((dropoffDate.getMonth()+1)<10)?"0"+(dropoffDate.getMonth()+1):(dropoffDate.getMonth()+1))+
				((dropoffDate.getDate()<10)?"0"+dropoffDate.getDate():dropoffDate.getDate())+
				((dropoffDate.getHours()<10)?"0"+dropoffDate.getHours():dropoffDate.getHours())+
				((dropoffDate.getMinutes()<10)?"0"+dropoffDate.getMinutes():dropoffDate.getMinutes());

			if (document.carSearchFormBean.destination2.value=="") {
				document.carSearchFormBean.destination2.value = document.carSearchFormBean.destination1.value;
			}
			document.carSearchFormBean.destination1.value=escape(document.carSearchFormBean.destination1.value);
			document.carSearchFormBean.destination2.value=escape(document.carSearchFormBean.destination2.value);
			return true;
        }
    }
}

function validateAge() {
	var valid = (document.carSearchFormBean.age.selectedIndex != 0);
	if (valid == false) {
		alert("You must enter a driver age");
		document.getElementById("driverAge").style.color="#FF3300";
	}
	return valid;
}

function validatePickup() {
	var valid = (document.carSearchFormBean.destination1.value != "");
	if (valid == false) {
		alert("You must enter a pick up destination");
		document.getElementById("pickupDest").style.color="#FF3300";
	}
	return valid;
}

function validateForm() {
	document.getElementById("driverAge").style.color="#000000";
	document.getElementById("pickupDest").style.color="#000000";
	document.getElementById("returnDate").style.color="#000000";
    document.getElementById("outboundDate").style.color="#000000";
	return ( validateAge() ) && ( validatePickup() ) && ( submitCheck() );
}
function init() {
	initFlights();
	initHotels();
	initCars();
}

