// Static values (not localised)
var maxPAX = 8;

var adult = '1';
var child = '0';
var infant = '0';

skiRoutes = new Array("AGP", "SZG", "TLS", "BGY", "GVA", "CMF");

interIslandRoutes = new Array();

validBERoutes = new Array("ABZBHX","BHXABZ",
"BHXFRA","FRABHX",
"BHXDUS","DUSBHX", 
"BHXSTR","STRBHX",
"BHXCDG","CDGBHX",
"BHXMXP","MXPBHX",
"BHXHAM","HAMBHX",
"GLAMAN","MANGLA",
"EDIMAN","MANEDI",
"BRUMAN","MANBRU",	
"DUSMAN","MANDUS",
"FRAMAN","MANFRA",	
"MANMXP","MXPMAN",
"MANIOM","IOMMAN",		
"CDGMAN","MANCDG",		
"HAJMAN","MANHAJ",		
"EDICDG","CDGEDI",		
"INVLGW","LGWINV",	
"IOMLGW","LGWIOM");


var HELPDESK_NUMBER = "08708 890908";

var bookingForm;

// Variables
var okayToSubmit = true;
var depDate = new Date();
var destDate = new Date();
var returnTrip = false;
var routeIndex = 0;
var codeShareCode = "";
var version = "unknown";

// Run immediate code
if (navigator.appName.indexOf('Netscape') != -1) {
	versionType = 'n';
	versionNum = navigator.appVersion.substring(0, 1);
	version = versionType + versionNum;
} else if (navigator.appVersion.indexOf('MSIE') != -1) {
	versionType = 'e';
	MS = navigator.appVersion.indexOf('MSIE');
	versionNum = navigator.appVersion.substring(MS + 5, MS + 6);
	version = versionType + versionNum;
} else {

	versionType = 'unknown';
}

// Private functions
function isSkiRoute(departure, destination) {

	var returnState = false;

	if (skiRoutes != null && departure != null && destination != null) {
		for (i = 0; i < skiRoutes.length; i++) {
			if (departure.substr(0, 3) == skiRoutes[i] ||
				destination.substr(0,3) == skiRoutes[i]) {
				returnState = true;
				break;
			}
		}
	}
	return returnState;
}


function isInterIslandRoute(departure, destination) {

	var returnState = false;

	if (interIslandRoutes != null && departure != null && destination != null) {
		for (i = 0; i < interIslandRoutes.length; i++) {
			if ((departure.substr(0, 3) + destination.substr(0,3))  == interIslandRoutes[i] ) {
				returnState = true;
				break;
			}
		}
	}
	return returnState;
}

function isBERoute(departure, destination, depDay, depMonth, destDay, destMonth) {

        var returnState = false;

        if (validBERoutes != null && departure != null && destination != null) {
                for (i = 0; i < validBERoutes.length; i++) {
                        if ((departure.substr(0, 3) + destination.substr(0,3))  == validBERoutes[i] ) {

                                // Check the selected date for this BA Route, if before 24th March then show message for redirect
                                if( depMonth < 200703 ) {
                                        returnState = true;
                                        break;
                                }

                                if( depMonth == 200703 && depDay < 25) {
                                        returnState = true;
                                        break;
                                }
                        }
                }
        }


        return returnState;
}

function isWinterFlight(depMonth) {

	var returnState = false;

	if (depMonth != null) {
		//winter flight is Nov-Apr
		if (depMonth.substring(4, 6) == '11' || depMonth.substring(4, 6) == '12' ||
			depMonth.substring(4, 6) == '01' || depMonth.substring(4, 6) == '02' ||
			depMonth.substring(4, 6) == '03' || depMonth.substring(4, 6) == '04') {

			returnState = true;

		}
	}
	return returnState;
}

function isReservationsFlight(departure, destination) {
	//check the codeShareCarrier code, if any. For example CO for Continental.
	if (isInRouteList(codeShareRoutes, departure, destination)) {
		codeShareCode = new String(codeShareCodes[routeIndex]);
	}

	//if reservations only flight
	if (isInRouteList(reservationsOnlyFlights, departure, destination)) {
		//if also a Continental Code Share
		if (isInRouteList(codeShareRoutes, departure, destination) && (codeShareCode == "CO")) {
			//display both parts of the message
			alert(flightStrings.invalidAirportCombination1 + reservations + ". " + flightStrings.invalidAirportCombination2);
         return true;
		} else if (codeShareCode == "CB") {
			//display special message for scot air
			alert(flightStrings.invalidAirportCombination3 + reservations + ". ");
			return true;
		}
		//display just the first part of message
		alert(flightStrings.invalidAirportCombination1 + reservations + ". ");
		return true;
	}
	return false;

}
/**
 *param departure the airport code for departure
 *param destination the airport code for destination
 *param yearMonth the Year and month of journey e.g. 200510
 *param day the day of journey
 */
function isSaturdayOnlyFlight(info, departure, destination, yearMonth, day) {

	if (isInRouteList(saturdayOnlyFlights, departure, destination)) {
		if(isSelectedDateOnSaturday(yearMonth, day)==false){
			alert(info);
			return false;
		}
	}
	return true;
}

function isSelectedDateOnSaturday(yearMonth, day){

	var	theTime = new Date(yearMonth.substr(0, 4), yearMonth.substr(4)-1, day, 0, 0, 0);

	if (theTime.getDay() == 6){
		return true;
	}
	return false;
}

function isBusinessClassSelected() {
	var returnState = false;
	var fareClass = bookingForm.fareType.options[bookingForm.fareType.selectedIndex].value;
	if (fareClass == "BUSINESS") {
		returnState = true;
	}
	return returnState;
}

function isInRouteList(routeList, departure, destination) {

	var returnState = false;

	if (routeList != null && departure != null && destination !=null) {
		for (routeIndex = 0; routeIndex < routeList.length; routeIndex++) {
			if (departure.substr(0, 3) + destination.substr(0, 3) == routeList[routeIndex]) {
				returnState = true;
				break;
			}
		}
	}

	return returnState;

}

// whether we need to extend the dates
function checkExtendDates(formObject) {

	selectedDeparture = (formObject.selDep.options[formObject.selDep.selectedIndex].value).substring(0,3);
	selectedDestination = (formObject.selDest.options[formObject.selDest.selectedIndex].value).substring(0,3);
	selectedDepartureAndDestination = selectedDeparture + selectedDestination;

	var matchedRoute = false;

	if (selectedDepartureAndDestination != null) {
		if (isInRouteList(dateSpecificRoutes, selectedDeparture, selectedDestination)) {
			updateDates(minimumFlyDates[routeIndex].substring(0,6), maximumFlyDates[routeIndex].substring(0,6), formObject.selDepMonth);
			updateDates(minimumFlyDates[routeIndex].substring(0,6), maximumFlyDates[routeIndex].substring(0,6), formObject.selRetMonth);
			matchedRoute = true;
		}
	}

	if(!matchedRoute) {
		updateDates(defaultScheduleStart.substring(0,6), defaultScheduleEnd.substring(0,6), formObject.selDepMonth);
		updateDates(defaultScheduleStart.substring(0,6), defaultScheduleEnd.substring(0,6), formObject.selRetMonth);
	}

}

// public functions
/*
 * Function to control the 2 select boxes on the front page.
 * Depending on which one is selected it will populate the other
 * with the airports that can be flown to from the selected airport.
 * params
 * formObject - The form object that contains the select boxes
 * calledBy - what object called the function.
 *            Either one of the select boxes or the body tag
 */
function updateAirportExclDates(formObject, calledBy) {

	if (formObject != null && formObject != "" && calledBy != null
		&& calledBy != "" && version != 'e3') {

		var netscapeBodyId = "opener"
		fieldName = calledBy.name;

		if (fieldName == "selDep" || fieldName == "" || fieldName == netscapeBodyId) {
			fieldValue = "XXXXXX";
			changeFieldValue = "XXXXXX";

			if (formObject.depAirportCode && 
					formObject.depAirportCode.value != "" && fieldName == "") {
				fieldValue = formObject.depAirportCode.value;
				changeFieldValue = formObject.destAirportCode.value;
			}

			updateAirportList(formObject.selDep, formObject.selDest, fieldValue,changeFieldValue);
		}

		if (fieldName == "selDest" || fieldName == "" || fieldName == netscapeBodyId) {

			fieldValue = "XXXXXX";
			changeFieldValue = "XXXXXX";

			if (formObject.destAirportCode && 
					formObject.destAirportCode.value != "" && fieldName == "") {
				fieldValue = formObject.destAirportCode.value;
				changeFieldValue = formObject.depAirportCode.value;
			}

			updateAirportList(formObject.selDest, formObject.selDep, fieldValue,changeFieldValue);
		}

		if (fieldName != "" && fieldName != netscapeBodyId) {

			selectedDeparture = formObject.selDep.options[formObject.selDep.selectedIndex].value;

			selectedDestination = formObject.selDest.options[formObject.selDest.selectedIndex].value;

			if (isReservationsFlight(selectedDeparture, selectedDestination)) {
				return;
			}
		}

	}

}

function updateAirport(formObject, calledBy) {
	updateAirportExclDates(formObject, calledBy);
	checkExtendDates(formObject);
}

/*
 * Populate a airport select box
 * selectField - select box object that was selected
 * changeField - select box object to change
 */
function updateAirportList(selectField, changeField,fieldValue, changeFieldValue)  {

	if (selectField.selectedIndex  != -1 && fieldValue == 'XXXXXX') {
		fieldValue = selectField.options[selectField.selectedIndex].value;
	}

	if (changeField.selectedIndex  != -1 && changeFieldValue == 'XXXXXX') {
		changeFieldValue = changeField.options[changeField.selectedIndex].value;
	}

	changeFieldName = changeField.name;

	airportList = new Array();
	airportDesc = new Array();

	if (changeFieldName == 'selDep') {
		airportList = destinationAirports;
		airportDesc = departureAirports;
	} else {
		airportList = departureAirports;
		airportDesc = destinationAirports;

	}

	changeField.options.length = 1;

	if (airportList[fieldValue.substr(0,3)] != null) {

		for (var i = 2; i < airportList[fieldValue.substr(0,3)].length; i++) {

			if (changeFieldName == "selDep" && i == 2) {
				changeField.options[i-2] = new Option(flightStrings.departingFrom);
			} else if (changeFieldName == "selDest" && i == 2) {
				changeField.options[i-2] = new Option(flightStrings.goingTo);
			} else {
				changeField.options[i-2] =
					new Option(airportDesc[airportList[fieldValue.substr(0,3)][i]][0]);
			}

			changeField.options[i-2].value =
				airportList[fieldValue.substr(0,3)][i] +
				airportDesc[airportList[fieldValue.substr(0,3)][i]][1];

			if (changeField.options[i-2].value == changeFieldValue) {
				changeField.selectedIndex = i-2;
			}
		}
	}

}

function isValidRoute(dep, dest) {

	if (dep != null && dest != null && departureAirports[dep] != null) {
		for (var i = 2; i < departureAirports[dep].length; i++) {
			if (dest == departureAirports[dep][i]) {
				return true;
				break;
			}
		}
	}

	return false;

}

function updatePAX(elementName) {

	if (version != 'e3') {

		adultChange = true;
		childChange = true;
		infantChange = true;

		// Reset adult index previously chosen

		if (elementName == 'load' || elementName == 'numAdults') {
			if (elementName != 'load') { adultChange = false; }
			adult = bookingForm.numAdults.options[bookingForm.numAdults.selectedIndex].value;
		}
		// Reset child index previously chosen
		if (elementName == 'load' || elementName == 'numChildren') {
			if (elementName != 'load') { childChange = false; }
			child = bookingForm.numChildren.options[bookingForm.numChildren.selectedIndex].value;
		}
		// Reset infant index previously chosen
		if (elementName == 'load' || elementName == 'numInfants') {
			if (elementName != 'load') { infantChange = false; }
			infant = bookingForm.numInfants.options[bookingForm.numInfants.selectedIndex].value;
		}

		if (adultChange) {
			// Populate adult drop down
			bookingForm.numAdults.options.length = 1;
			for (var i = 1; i <= maxPAX - parseInt(child); i++) {
				bookingForm.numAdults.options[i] = new Option(i);
				bookingForm.numAdults.options[i].value = i;
			}
			// Reset selection if previously selected value too high
			bookingForm.numAdults.selectedIndex = parseInt(adult) < i ? parseInt(adult) : 0;
		}

		if (childChange) {
			// Populate child drop down
			bookingForm.numChildren.options.length = 1;
			for (var i = 1; i <= maxPAX - parseInt(adult); i++) {
				bookingForm.numChildren.options[i] = new Option(i);
				bookingForm.numChildren.options[i].value = i;
			}
			// Reset selection if previously selected value too high
			bookingForm.numChildren.selectedIndex = parseInt(child) < i ? parseInt(child) : 0;
		}

		if (infantChange) {
			// Populate infant drop down
			bookingForm.numInfants.options.length = 1;
			for (var i = 1; i <= parseInt(adult); i++) {
				bookingForm.numInfants.options[i] = new Option(i);
				bookingForm.numInfants.options[i].value = i;
			}
			// Reset selection if previously selected value too high
			bookingForm.numInfants.selectedIndex = parseInt(infant) < i ? parseInt(infant) : 0;
		}

	}
}

/*
 Checks to see if date of booking is not too close to flight date
*/
function toCloseFlightDate(numberOfDays, flightDate, today) {

	if (numberOfDays == null || numberOfDays == "" ||
		isNaN(numberOfDays) || isNaN(new Date(flightDate)))  {
		return false;
	}

	if (isNaN(Date(today))) {
		today = new Date();
	}

	oneDay = (60000 * 60) * 24;
	maximumBookingDate = new Date(flightDate.getTime() - (numberOfDays * oneDay))

	if (today.getTime() >= maximumBookingDate.getTime() &&
		 today.getTime() <= flightDate.getTime()) {
		return true;
	}

	return false;

}

function dateOutsideSchedule(departure, destination, outboundDate, returnDate) {

	if (isNaN(outboundDate)) {
		return false;
	}
	if (isNaN(returnDate)) {
		returnDate = outboundDate;
	}

	minimumFlyDate = null;
	maximumFlyDate = null;

	if (isInRouteList(dateSpecificRoutes, departure, destination)) {
		minimumFlyDate = minimumFlyDates[(routeIndex)];
		maximumFlyDate = maximumFlyDates[(routeIndex)];
	}
	if (minimumFlyDate == '') {
		minimumFlyDate = null;
	}

	if (maximumFlyDate == null || maximumFlyDate == '') {
		maximumFlyDate = defaultScheduleEnd;
	}

	if (minimumFlyDate != null && outboundDate < minimumFlyDate) {
		alert(flightStrings.invalidDepartureDate +
		formatDate(minimumFlyDate) + flightStrings.forFlightFrom );
		return true;
	} else if (returnDate > maximumFlyDate) {
		alert(flightStrings.flyingAfterSchedule +
		formatDate(maximumFlyDate) + flightStrings.timetableAlert );
		return true;
	}

	return false;
}

function updateDates(startDate, endDate, list) {
	var currMonth = list.options[list.selectedIndex].value;
	var firstVal = list.options[0].value;
	
	if (firstVal == 'XX' || firstVal == 'XXXXXX') {
		list.options.length = 1;
	} else {
		list.options.length = 0;
	}

	var endYear = endDate.substring(0,4);
	var endMonth = new Number(endDate.substring(4,6));
	var currentYear = startDate.substring(0,4);
	var currentMonth = new Number(startDate.substring(4,6));
	for (var yrInd = currentYear; yrInd <= endYear; yrInd++) {
		var startMth;
		var endMth;
		if (yrInd == currentYear) {
			startMth = currentMonth++;
		} else {
			startMth = 1;
		}
		if (yrInd == endYear) {
			endMth = endMonth;
		} else {
			endMth = 12;
		}
		for (var mthInd = startMth; mthInd <= endMth && mthInd <= 12; mthInd++) {
			list.options.length++;
			var newVal = yrInd + (mthInd < 10 ? '0' : '') + mthInd;
			newOpt = new Option(flightStrings.months[mthInd - 1] + yrInd, newVal);
			list.options[list.options.length-1] = newOpt;
			if (newVal == currMonth) {
				list.options[list.options.length-1].selected = true;
			}
		}
	}
 }

function formatDate(date) {
	var day = date.substring(6,8);
	var month = date.substring(4,6);
	var year = date.substring(0,4);
	return day + ' ' + flightStrings.months[month - 1] + year;
}

function hasUnaccompaniedMinors(numAdults, numChildren) {
	return numAdults == 0 && numChildren > 0;
}

function processSelection(dayToday,monthToday,yearToday) {

	var now = new Date(yearToday,monthToday,yearToday);

	// Retrieve values from form for easy handling
	var dep = bookingForm.selDep.options[bookingForm.selDep.selectedIndex].value;
	var depDay = bookingForm.selDepDay.options[bookingForm.selDepDay.selectedIndex].value;
	var depMonth = bookingForm.selDepMonth.options[bookingForm.selDepMonth.selectedIndex].value;
	var dest = bookingForm.selDest.options[bookingForm.selDest.selectedIndex].value;
	var destDay = bookingForm.selRetDay.options[bookingForm.selRetDay.selectedIndex].value;
	var destMonth = bookingForm.selRetMonth.options[bookingForm.selRetMonth.selectedIndex].value;

	if (bookingForm.numAdults.options) {
		var adult = bookingForm.numAdults.options[bookingForm.numAdults.selectedIndex].value;
	}

	if (bookingForm.numChildren.options) {
		var child = bookingForm.numChildren.options[bookingForm.numChildren.selectedIndex].value;
	}

	if (bookingForm.numInfants.options) {
		var infant = bookingForm.numInfants.options[bookingForm.numInfants.selectedIndex].value;
	}

	var frenchDep = bookingForm.selDep.options[bookingForm.selDep.selectedIndex].value;

	frenchDep = frenchDep.substr(0,3);

	// Are we dealing with a return trip?
	if ((destDay + destMonth) == 'XXXXXXXX') {
		returnTrip = false;
	} else {
		returnTrip = true;
	}

	if (hasUnaccompaniedMinors(adult, child)) {
		alert(flightStrings.unmin);
		return false;
	} else if (adult + child == 0) {
		alert(flightStrings.invalidNumAdults);
		return false;
	}

	 // Check if BERoute
     if( isBERoute(dep, dest, depDay, depMonth, destDay, depMonth) ) {
     
                alert(flightStrings.baconnectmessage);
                	   
                window.location = 'http://www.britishairways.com'
                return false;
     }

	
	
	// check if is a ski route and is in a winter month
//	if ( (isSkiRoute(dep, dest) ) && isWinterFlight(depMonth) ) {
//		alert(flightStrings.skiRoute1 + '0871 700 2000' + flightStrings.skiRoute1a + 'Outside UK 00 44 1392 268529' + flightStrings.skiRoute1b + flightStrings.skiRoute2);
//	}

	// check if interIslandRoute - if so, display this message
	if (isInterIslandRoute(dep, dest)) {
		alert(flightStrings.interIslandRoutes);
	}


	if (returnTrip) {
		if (destDay == 'XX') {
			alert(flightStrings.requiredFieldStr + flightStrings.returnDayStr);
			return false;
		}
		if (destMonth == 'XXXXXX') {
			alert(flightStrings.requiredFieldStr + flightStrings.returnMonthStr);
			return false;
		}
	}

	if (dep == dest) {
		alert(flightStrings.depDestDifferent);
		return false;
	}

	// Warn against christmas day bookings
	if ((depMonth + depDay).substring(4, 8) == '1225') {
		alert(flightStrings.noOutwardXmasStr);
		return false;
	}
	if ((destMonth + destDay).substring(4, 8) == '1225') {
		alert(flightStrings.noReturnXmasStr);
		return false;
	}

	// Check for consistent date selections
	var selectedMonth = depMonth.substring(4, 6);
	var leapYear = (depMonth.substring(0, 4) % 4 == 0);
	var febDays = leapYear ? 29 : 28;

	if (selectedMonth == '02') {
		if (depDay > febDays) {
			alert(flightStrings.thereAreOnly + febDays + flightStrings.daysInMonthStr
				+ flightStrings.months[1] + flightStrings.amendDepDate);
				return false;
		}
	} else if ((selectedMonth == '04') || (selectedMonth == '06') || (selectedMonth == '09') || (selectedMonth == '11')) {
		if (depDay > 30) {
			alert(flightStrings.thereAreOnly + '30' + flightStrings.daysInMonthStr
				+ flightStrings.months[selectedMonth - 1] + flightStrings.amendDepDate);
			return false;
		}
	}

	if (returnTrip) {
		selectedMonth = destMonth.substring(4, 6);
		leapYear = (destMonth.substring(0, 4) % 4 == 0);
		febDays = leapYear ? 29 : 28;

		if (selectedMonth == '02') {
			if (destDay > febDays) {
				alert(flightStrings.thereAreOnly + febDays + flightStrings.daysInMonthStr
				+ flightStrings.months[1] + flightStrings.amendRetDate);
				return false;
			}
		} else if ((selectedMonth == '04') || (selectedMonth == '06') || (selectedMonth == '09') || (selectedMonth == '11')) {
			if (destDay > 30) {
				alert(flightStrings.thereAreOnly + '30' + flightStrings.daysInMonthStr
				+ flightStrings.months[selectedMonth - 1] + flightStrings.amendRetDate);
				return false;

			}
		}
	}

	// Apply business rules
	if ((depMonth + depDay) < ('' + yearToday + (monthToday < 10 ? '0' : '') +
			monthToday + (dayToday < 10 ? '0' : '') + dayToday)) {
		alert(flightStrings.earliest);
		return false;
	}

	if (dateOutsideSchedule(dep.substr(0, 3), dest.substr(0, 3), depMonth + depDay, destMonth + destDay)) {
		return false;
	}

	if (dep.substr(0, 3) == 'MAN' && dest.substr(0, 3) == 'EWR' &&
		toCloseFlightDate(3, getDateSelected(bookingForm.selDepDay, bookingForm.selDepMonth, now) , now)) {

		alert(flightStrings.flightsFrom + departureAirports[dep.substr(0, 3)][0] + flightStrings.flightsTo +
			destinationAirports[dest.substr(0, 3)][0] +
			flightStrings.advanceBookingRequired);
		return false;
	}



	if (returnTrip) {
		if ((depMonth + depDay) > (destMonth + destDay)) {
			alert(flightStrings.returnDateInvalid);
			return false;
		} else if ((depMonth + depDay) == (destMonth + destDay)) {
			alert(flightStrings.dayReturn);
		}
	}

	if (isReservationsFlight(dep, dest)) {
		return false;
	}

	//for departures
	if (isSaturdayOnlyFlight(flightStrings.saturdayOnlyDepartures, dep, dest, depMonth, depDay)==false){
		return false;
	}

	if (returnTrip){
		if (isSaturdayOnlyFlight(flightStrings.saturdayOnlyReturns, dest, dep, destMonth, destDay)==false){
			return false;
		}
	}

	if (!isValidRoute(dep.substr(0,3), dest.substr(0,3))) {
		alert(flightStrings.invalidRoute);
		return false;
	}

	// Store indicies selected values in form to enable repopulation when returning to page
	bookingForm.depAirportCode.value = bookingForm.selDep.options[bookingForm.selDep.selectedIndex].value;

	bookingForm.depAirportIndx.value = bookingForm.selDep.selectedIndex;

	bookingForm.destAirportCode.value = bookingForm.selDest.options[bookingForm.selDest.selectedIndex].value;

	bookingForm.destAirportIndx.value = bookingForm.selDest.selectedIndex;

	bookingForm.depDateIndx.value = bookingForm.selDepDay.options[bookingForm.selDepDay.selectedIndex].value;

	bookingForm.destDateIndx.value = bookingForm.selRetDay.options[bookingForm.selRetDay.selectedIndex].value;

	bookingForm.depMonthIndx.value = bookingForm.selDepMonth.selectedIndex;

	bookingForm.destMonthIndx.value =bookingForm.selRetMonth.selectedIndex;

	if (bookingForm.numAdults.selectedIndex) {
		bookingForm.noAdultsIndx.value = bookingForm.numAdults.selectedIndex;
	}

	if (bookingForm.numChildren.selectedIndex) {
		bookingForm.noChildrenIndx.value = bookingForm.numChildren.selectedIndex;
	}

	if (bookingForm.numInfants.selectedIndex) {
		bookingForm.noInfantsIndx.value = bookingForm.numInfants.selectedIndex;
	}

	if (bookingForm.fareType.options) {
		bookingForm.travelClass.value = bookingForm.fareType.options[bookingForm.fareType.selectedIndex].value;
	}
	// Submit form
	return true;

}

function registerBookingForm(formObject) {
	bookingForm = formObject;
}

function updateAirports(thisForm) {
	updateAirportExclDates(thisForm, thisForm['selDest']);
	updateAirportExclDates(thisForm, thisForm['selDep']);
	checkExtendDates(thisForm);
}
function processSelection4ItinChanges(dayToday,monthToday,yearToday) {
	var now = new Date(yearToday,monthToday,yearToday);

	// Retrieve values from form for easy handling
	var dep = bookingForm.selDep.options[bookingForm.selDep.selectedIndex].value;
	var depDay = bookingForm.selDepDay.options[bookingForm.selDepDay.selectedIndex].value;
	var depMonth = bookingForm.selDepMonth.options[bookingForm.selDepMonth.selectedIndex].value;
	var dest = bookingForm.selDest.options[bookingForm.selDest.selectedIndex].value;
	var destDay = bookingForm.selRetDay.options[bookingForm.selRetDay.selectedIndex].value;
	var destMonth = bookingForm.selRetMonth.options[bookingForm.selRetMonth.selectedIndex].value;

	
	var frenchDep = bookingForm.selDep.options[bookingForm.selDep.selectedIndex].value;

	frenchDep = frenchDep.substr(0,3);

	// Check if BERoute	
	if( isBERoute(dep, dest) ) {
		alert('Please book this on the www.ba.com website for travel prior to the 25th March');
	}
	
	// Are we dealing with a return trip?
	if ((destDay + destMonth) == 'XXXXXXXX' || !bookingForm.isReturn.value) {
		returnTrip = false;
	} else {
		returnTrip = true;
	}

	if (hasUnaccompaniedMinors(adult, child)) {
		alert(flightStrings.unmin);
		return false;
	} else if (adult + child == 0) {
		alert(flightStrings.invalidNumAdults);
		return false;
	}

	// check if is a ski route and is in a winter month
//	if ( (isSkiRoute(dep, dest) ) && isWinterFlight(depMonth) ) {
//		alert(flightStrings.skiRoute1 + '0871 700 2000' + flightStrings.skiRoute1a + 'Outside UK 00 44 1392 268529' + flightStrings.skiRoute1b + flightStrings.skiRoute2);
//	}

	// check if interIslandRoute - if so, display this message
	if (isInterIslandRoute(dep, dest)) {
		alert(flightStrings.interIslandRoutes);
	}


	if (returnTrip) {

		if (destDay == 'XX') {
			alert(flightStrings.requiredFieldStr + flightStrings.returnDayStr);
			return false;
		}

		if (destMonth == 'XXXXXX') {
			alert(flightStrings.requiredFieldStr + flightStrings.returnMonthStr);
			return false;
		}
	}

	if (dep == dest) {
		alert(flightStrings.depDestDifferent);
		return false;
	}

	// Warn against christmas day bookings
	if ((depMonth + depDay).substring(4, 8) == '1225') {
		alert(flightStrings.noOutwardXmasStr);
		return false;
	}
	if ((destMonth + destDay).substring(4, 8) == '1225') {
		alert(flightStrings.noReturnXmasStr);
		return false;
	}

	// Check for consistent date selections
	var selectedMonth = depMonth.substring(4, 6);
	var leapYear = (depMonth.substring(0, 4) % 4 == 0);
	var febDays = leapYear ? 29 : 28;

	if (selectedMonth == '02') {
		if (depDay > febDays) {
			alert(flightStrings.thereAreOnly + febDays + flightStrings.daysInMonthStr
				+ flightStrings.months[1] + flightStrings.amendDepDate);
				return false;
		}
	} else if ((selectedMonth == '04') || (selectedMonth == '06') || (selectedMonth == '09') || (selectedMonth == '11')) {
		if (depDay > 30) {
			alert(flightStrings.thereAreOnly + '30' + flightStrings.daysInMonthStr
				+ flightStrings.months[selectedMonth - 1] + flightStrings.amendDepDate);
			return false;
		}
	}

	if (returnTrip) {
		selectedMonth = destMonth.substring(4, 6);
		leapYear = (destMonth.substring(0, 4) % 4 == 0);
		febDays = leapYear ? 29 : 28;

		if (selectedMonth == '02') {
			if (destDay > febDays) {
				alert(flightStrings.thereAreOnly + febDays + flightStrings.daysInMonthStr
				+ flightStrings.months[1] + flightStrings.amendRetDate);
				return false;
			}
		} else if ((selectedMonth == '04') || (selectedMonth == '06') || (selectedMonth == '09') || (selectedMonth == '11')) {
			if (destDay > 30) {
				alert(flightStrings.thereAreOnly + '30' + flightStrings.daysInMonthStr
				+ flightStrings.months[selectedMonth - 1] + flightStrings.amendRetDate);
				return false;

			}
		}
	}

	// Apply business rules
	if ((depMonth + depDay) < ('' + yearToday + (monthToday < 10 ? '0' : '') +
			monthToday + (dayToday < 10 ? '0' : '') + dayToday)) {
		alert(flightStrings.earliest);
		return false;
	}

	if (dateOutsideSchedule(dep.substr(0, 3), dest.substr(0, 3), depMonth + depDay, destMonth + destDay)) {
		return false;
	}

	if (dep.substr(0, 3) == 'MAN' && dest.substr(0, 3) == 'EWR' &&
		toCloseFlightDate(3, getDateSelected(bookingForm.selDepDay, bookingForm.selDepMonth, now) , now)) {

		alert(flightStrings.flightsFrom + departureAirports[dep.substr(0, 3)][0] + flightStrings.flightsTo +
			destinationAirports[dest.substr(0, 3)][0] +
			flightStrings.advanceBookingRequired);
		return false;
	}



	if (returnTrip) {
		if ((depMonth + depDay) > (destMonth + destDay)) {
			alert(flightStrings.returnDateInvalid);
			return false;
		} else if ((depMonth + depDay) == (destMonth + destDay)) {
			alert(flightStrings.dayReturn);
		}
	}

	if (isReservationsFlight(dep, dest)) {
		return false;
	}

	//for departures
	if (isSaturdayOnlyFlight(flightStrings.saturdayOnlyDepartures, dep, dest, depMonth, depDay)==false){
		return false;
	}

	if (returnTrip){
		if (isSaturdayOnlyFlight(flightStrings.saturdayOnlyReturns, dest, dep, destMonth, destDay)==false){
			return false;
		}
	}

	if (!isValidRoute(dep.substr(0,3), dest.substr(0,3))) {
		alert(flightStrings.invalidRoute);
		return false;
	}

	// Store indicies selected values in form to enable repopulation when returning to page
	bookingForm.depAirportCode.value = bookingForm.selDep.options[bookingForm.selDep.selectedIndex].value;

	bookingForm.depAirportIndx.value = bookingForm.selDep.selectedIndex;

	bookingForm.destAirportCode.value = bookingForm.selDest.options[bookingForm.selDest.selectedIndex].value;

	bookingForm.destAirportIndx.value = bookingForm.selDest.selectedIndex;

	bookingForm.depDateIndx.value = bookingForm.selDepDay.options[bookingForm.selDepDay.selectedIndex].value;

	bookingForm.destDateIndx.value = bookingForm.selRetDay.options[bookingForm.selRetDay.selectedIndex].value;

	bookingForm.depMonthIndx.value = bookingForm.selDepMonth.selectedIndex;

	bookingForm.destMonthIndx.value =bookingForm.selRetMonth.selectedIndex;
	
	// Submit form
	return true;

}
