<!--

// GLOBAL VARIABLES

var outDate = new Date();
var inDate = new Date();
var now = new Date(); // Date now
var duration = '';
var oneDay=1000*60*60*24; // Set 1 day in milliseconds
var req;
var params;
var outDay;
var outMonth;
var outYear;
var duration;
var isBusy;

// -------------------------------------------------------------------------------

// VALIDATE INPUT

// Get dates (v1.0) - T. Nelson '06
function getDates() {

// Outbound
outDay = document.FlightSearch.LeavingDay.options.selectedIndex + 1; // Cannot use value after menu dynamically adjusts for days
var outMonthStr = document.getElementById('LeavingMonth').value;
var outMonthLength = parseInt(Len(outMonthStr))

if (outMonthLength == 6) {
		outMonth = (parseInt(Left(outMonthStr,1))-1); // Before October
	}
	else {
		outMonth = (parseInt(Left(outMonthStr,2))-1); // After October
}
	
outYear = parseInt(Right(outMonthStr,4));

outDate.setFullYear(outYear,outMonth,outDay)

// Inbound
var inDay = document.FlightSearch.ReturningDay.options.selectedIndex + 1; // Cannot use value after menu dynamically adjusts for days
var inMonthStr = document.getElementById('ReturningMonth').value;
var inMonthLength = parseInt(Len(inMonthStr))

if (inMonthLength == 6) {
		inMonth = (parseInt(Left(inMonthStr,1))-1); // Before October
	}
	else {
		inMonth = (parseInt(Left(inMonthStr,2))-1); // After October
}

var inYear = parseInt(Right(inMonthStr,4));

inDate.setFullYear(inYear,inMonth,inDay)
	
// Calculate millisecond difference between the two dates, and convert to days
duration = Math.ceil((inDate.getTime()-outDate.getTime())/(oneDay))

}

// Valid dates (v1.0) - T. Nelson '06
window.dbIE  = document.all ? true : false; // IE 4+
window.dbDOM = (document.getElementById && ! document.all) ? true : false; // All other browsers

function limitList(listObj,length) { // Limit drop menu
   var list = listObj
   if (length<(list.selectedIndex+1)) { // Reduce current selected index to new length
      list.selectedIndex=length-1; 
   }
   if (window.dbIE || window.dbDOM) { // All browsers
      if (list.options.length<length) {
         for (var i=list.options.length+1; i<=length; i++) { 
             var oOption = document.createElement('OPTION');
             if (window.dbIE) { // IE
                list.options.add(oOption);
                oOption.innerText = i;
                oOption.Value = i;
             } else if (window.dbDOM) { // Mozilla      
                oOption.text = ' '+i;
                oOption.Value = i;
                list.add(oOption,null);
             }
          }
      } 
	  else if (list.options.length>length) {
         for (var i=list.options.length; i>=length; i--) { 
             list.remove(i);
         }               
      }
  }
}

function validDate(menu) { // Set 'Day' drop menu based on month and year selected

var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31); // Days in the month

var dayMenu = menu + 'Day'
var monthMenu = menu + 'Month'

var day = parseInt(document.getElementById(dayMenu).value); 
var monthStr = document.getElementById(monthMenu).value;

// Check length of string and get month
var monthStrLength = parseInt(Len(monthStr))

if (monthStrLength == 6) {
		month = parseInt(Left(monthStr,1)); // Before October
	} 
	else {
		month = parseInt(Left(monthStr,2)); // After October
}

var year = parseInt(Right(monthStr,4));

if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { // Leap year so adjust Feb days to 29
	monthLength[1] = 29;
}

var objDay = document.getElementById(dayMenu) // Set 'Day' menu object

var listLength = monthLength[month-1] // Adjust 'Day' menu
limitList(objDay,listLength);
}

// Future dates (v1.1) - T. Nelson '06
function futureDate() {

// Get dates
getDates();
	
// Check departure date is in the future	
if (now.getTime() > outDate.getTime()) {
	alert('Your departure date cannot be in the past.');
	return false;
}

// Check flight type, duration and return date
if (document.FlightSearch.FlightType[0].checked == true) { // Return
	if (duration == 0) { // Duration is greater than zero
		alert('Your departure and return dates cannot be the same day.');
		return false;
	} else if (inDate.getTime() < outDate.getTime()) {
	alert('Your return date cannot be before your departure date.');
	return false;
	}
}

return true;
}

// Validate form submission (v1.0) - T. Nelson '06
function searchValidate() {

// Get menu positions
var menuDepIATA = (parseInt(document.getElementById('Departing').options.selectedIndex));
var menuDestIATA = (parseInt(document.getElementById('Going').options.selectedIndex));

// Set menu positions in hidden field
var selectedStr = (menuDepIATA + ',' + menuDestIATA);
document.getElementById('Menu').value = selectedStr;

// Check airports
if (menuDepIATA == 0) {
	alert('Please select a departure airport.');
	return false;
} else if (menuDestIATA == 0) {
	alert('Please select a destination airport.');
	return false;
}

return futureDate();
return true;
}

// Check radio buttons and disable menus if necessary
function setRadio() {
	// Set variables
	var returnDay = document.FlightSearch.ReturningDay;
	var returnMonth = document.FlightSearch.ReturningMonth;
	// Check radio
	if (document.FlightSearch.FlightType[0].checked == true) { // Return
		returnDay.disabled=false;
		returnMonth.disabled=false;
	}
	else if (document.FlightSearch.FlightType[1].checked == true) { // One way
		returnDay.disabled=true;
		returnMonth.disabled=true;
	}
}

// If second date menu is toggled first then don't change first menu 
function dateChanged() {
	menuActive = true
}

// On the first menu change jump the month forward to help out
function matchDate() {
if (menuActive == false) {
	var depMonth = document.FlightSearch.LeavingMonth.options.selectedIndex
	document.FlightSearch.ReturningMonth.options.selectedIndex = depMonth
	validDate('Returning');
	menuActive = true // Only run once
	}
}

// Submit form
function submitForm() {
	
	// Check data
	var dataCheck = searchValidate();
	
	// If valid then submit
	if (dataCheck == true) {
		document.FlightSearch.submit();
	}
}

// -------------------------------------------------------------------------------

// GET PRICES WITH AJAX

// Standard AJAX request function
function loadXMLDoc(url) {

	// Set XMLHttp false
	req = false;
		
    // Branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // Branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if (req) {
		req.onreadystatechange = processReqChange;
		req.open("POST", url, true);
		req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		req.send(params);
}
}

// AJAX function to get flight prices (v1.0) - T. Nelson '06
function costFlight() {

// Do we have all of the values to make a costing?
reqData = false

// Check if we have all the data for a flight
var depIATA = document.getElementById('Departing').value;
var destIATA = document.getElementById('Going').value;

if ((depIATA != 'ANY') && (destIATA != 'ANY')) { // We have the airports

	// Get dates
	getDates();
	
	// Check departure date is in the future	
	if (now.getTime() <= outDate.getTime()) {
	
		// Check flight type and duration
		if (document.FlightSearch.FlightType[0].checked == true) { // Return
			if (duration > 0) { // Duration is greater than zero
				reqData = true
			}
		} else if (document.FlightSearch.FlightType[1].checked == true) { // One Way
			duration = 0
			reqData = true
		}
	}	
}

// If we have the relevant data we can call the AJAX handler for results
if (reqData) {
	params = 'code=' + brochureCode + '&d=' + outDay + '&m=' + (outMonth + 1) + '&y=' + outYear + '&duration=' + duration + '&dep=' + depIATA + '&dest=' + destIATA;
	loadXMLDoc('/NoIndex/codeIncludes/ajaxHandler.asp');
} else {
// Reset the price message
priceError()
}

}

function priceError() {
// We don't know the price so just show a message to 'Get Quote'
document.getElementById("searchPanel").innerHTML = '<h1 class=\"white modifierPrice\">Price your<br />flight now</h1>'	
}

function setPrice(price) {
// Do we have the price?
if (price == 0) {
	// Either we don't know the price or there has been an error
	priceError()
} else {
	// Write the price
	// alert(price)
	document.getElementById("searchPanel").innerHTML = '<h3 class=\"white noPadding modifierAjax\"><strong>Book From</strong></h3><h1 class=\"white searchPrice\">&pound;' + price + '</h1><h3 class=\"white\"><strong>Per Person</strong></h3>'	
}
}

// Handle AJAX onreadystatechange 
function processReqChange() {
    // Only if req shows "loaded"
    if (req.readyState == 4) {
        // Only if "OK"
        if (req.status == 200) {
            setPrice(req.responseText)
        } else {
			req.abort();
            priceError();
        }
	}
}

// -->