function goSearch(searchType) {
window.location.href = searchType;
}

function highlightField(field) {
	field.style.background = 'LemonChiffon';
	field.focus();
}

function selectOneWay(gneForm) {
	gneForm.tripType.value='O';
	document.getElementById('GNEreturnDate').disabled = true;
	document.getElementById('oneWay').checked = 'true';
}

function selectReturn(gneForm) {
	gneForm.tripType.value='R';
	document.getElementById('GNEreturnDate').disabled = false;
	document.getElementById('return').checked = 'true';
}

/* 
 *  THIS IS ONLY BEING COMMENTED OUT TEMPORARILY DUE TO THE STUDENT FLIGHTS DOMESTIC BOOKING ENGINE BEING TURNED OFF ON 25/05/2009. - Taz 22/09/2009 
 
function checkLocalDepartures(gneForm){

	var field;
	var newdestination;
	if(document.getElementById("destinationdisplay")) { //www3
		field	= 	gneForm['destinationdisplay'];
		newdestination	=	gneForm.destinationdisplay.value;
	} else { //tomcat
		field	= 	gneForm['destination'];
		newdestination	=	gneForm.destination.value;
	}
	
	codeArray = new Array("ADL","WSY","ABX","ASP","ARM","AYQ","BNK","BHS","BNE","BME","BDB","BWT","CNS","CBR","CSI","CFS","OOM","CMD","CWT","CUG","DRW","DPO","DBO","EMD","FRB","GLI","OOL","GFN","GFF","HTI","HVB","HBA","IVR","KGI","KTA","KPS","LST","LSY","MKY","MBH","MEL","MIM","MQL","MRZ","MYA","MGB","ISA","DGE","NAA","NRA","NTL","ZNE","OAG","PKE","PBO","PER","PHE","PQQ","PTJ","PPP","ROK","MCY","SYD","TRO","TMW","TPR","TWB","TSV","TGN","WGA","WWY","NGA");
	descriptionArray = new Array("Adelaide","Airlie Beach","Albury","Alice Springs","Armidale","Ayers Rock","Ballina","Bathurst","Brisbane","Broome","Bundaberg","Burnie","Cairns","Canberra","Casino","Coffs Harbour","Cooma","Cootamundra","Cowra","Cudal","Darwin","Devonport","Dubbo","Emerald","Forbes","Glen Innes","Gold Coast","Grafton","Griffith","Hamilton Island","Hervey Bay","Hobart","Inverell","Kalgoorlie","Karratha","Kempsie","Launceston","Lismore","Mackay","Maryborough","Melbourne","Merimbula","Mildura","Moree","Moruya","Mount Gambier","Mount Isa","Mudgee","Narrabri","Narrandera","Newcastle","Newman","Orange","Parkes","Paraburdoo","Perth","Port Hedland","Port Macquarie","Portland","Proserpine","Rockhampton","Sunshine Coast","Sydney","Taree","Tamworth","Tom Price","Toowoomba","Townsville","Traralgon","Wagga Wagga","West Wyalong","Young");
	

	for (i = 0; i <= codeArray.length - 1; i++){
		if (newdestination == codeArray[i]){
			
			alert('Your destination is an Australian domestic city. \n\nPlease use the Domestic search feature to search for Domestic flights.');
			field.style.background = 'LemonChiffon';
			field.focus();	
			return false;
		}
	}

	for (i = 0; i <= descriptionArray.length - 1; i++){
		if (newdestination == descriptionArray[i]){
			alert('Your destination is an Australian domestic city. \n\nPlease use the Domestic search feature to search for Domestic flights.');
			field.style.background = 'LemonChiffon';
			field.focus();	
			return false;
		}
	}
	
	return true;

}
*/


function setDepartures(gneForm){
	
	var destination;
	if(document.getElementById("destinationdisplay")) { //www3
		destination = gneForm.destinationdisplay.value.toLowerCase();
	} else { //tomcat
		destination = gneForm.destination.value.toLowerCase();
	}
	switch(destination)
	{
		case 'london':   gneForm.destination.value = 'LON'; break;
		case 'united kingdom':   gneForm.destination.value = 'LON'; break;
		case 'united states':   gneForm.destination.value = 'NYC'; break;
		case 'london heathrow':   gneForm.destination.value = 'LON';break;
		case 'london gatwick':   gneForm.destination.value = 'LON';break;
		case 'london stansted':   gneForm.destination.value = 'LON';break;
		case 'gatwick':   gneForm.destination.value = 'LON';break;
		case 'stansted':   gneForm.destination.value = 'LON';break;		
		case 'heathrow':   gneForm.destination.value = 'LON';break;
		case 'rome':   gneForm.destination.value = 'ROM';break;
		case 'uk':   gneForm.destination.value = 'LON';break;
		case 'lhr':   gneForm.destination.value = 'LON';break;
		case 'england':   gneForm.destination.value = 'LON';break;
		case 'dublin':   gneForm.destination.value = 'DUB';break;
		case 'paris':   gneForm.destination.value = 'PAR';break;
		case 'venice':   gneForm.destination.value = 'ROM';break;		
		case 'florence':   gneForm.destination.value = 'FLR';break;
		case 'barcelona':   gneForm.destination.value = 'BCN';break;		
		case 'berlin':   gneForm.destination.value = 'BER';break;		
		case 'manchester':   gneForm.destination.value = 'MAN';break;		
		case 'stockholm':   gneForm.destination.value = 'STO';break;			
		case 'athens':   gneForm.destination.value = 'ATH';break;			
		case 'bern':   gneForm.destination.value = 'BRN';break;					
		case 'switzerland':   gneForm.destination.value = 'BRN';break;		
		case 'st. petersburg':   gneForm.destination.value = 'LED';break;		
		case 'st petersburg':   gneForm.destination.value = 'LED';break;		
		case 'los angeles':   gneForm.destination.value = 'LAX';break;		
		case 'hawaii':   gneForm.destination.value = 'HNL';break;		
		case 'toronto':   gneForm.destination.value = 'YTO';break;
		case 'las vegas':   gneForm.destination.value = 'LAS';break;		
		case 'new york city':   gneForm.destination.value = 'NYC';break;	
		case 'washington':   gneForm.destination.value = 'WAS';break;		
		case 'washington dc':   gneForm.destination.value = 'WAS';break;	
		case 'washington d.c.':   gneForm.destination.value = 'WAS';break;			
		case 'bali':   gneForm.destination.value = 'DPS';break;			
		case 'denpassar':   gneForm.destination.value = 'DPS';break;
		case 'port douglas':   gneForm.destination.value = 'CNS';break;
		case 'dempasar':   gneForm.destination.value = 'DPS';break;
		case 'dempesar':   gneForm.destination.value = 'DPS';break;
		case 'dempaser':   gneForm.destination.value = 'DPS';break;
		case 'dempassar':   gneForm.destination.value = 'DPS';break;
		case 'dempasser':   gneForm.destination.value = 'DPS';break;
		case 'denpasar':   gneForm.destination.value = 'DPS';break;
		case 'denpaser':   gneForm.destination.value = 'DPS';break;
		case 'denpassar':   gneForm.destination.value = 'DPS';break;
		case 'denpasser':   gneForm.destination.value = 'DPS';break;
		case 'katmandu':   gneForm.destination.value = 'KTM';break;
		case 'saigon':   gneForm.destination.value = 'SGN';break;		
		case 'manila':   gneForm.destination.value = 'MNL';break;			
		case 'manilla':   gneForm.destination.value = 'MNL';break;					
		case 'taiwan':   gneForm.destination.value = 'TPE';break;					
		case 'kl':   gneForm.destination.value = 'KUL';break;		
		case 'malaysia':   gneForm.destination.value = 'KUL';break;			
		case 'hk':   gneForm.destination.value = 'HKG';break;			
		case 'tuvalu':   gneForm.destination.value = 'FUN';break;			
		case 'phenom phen':   gneForm.destination.value = 'PNH';break;			
		case 'senegal':   gneForm.destination.value = 'DKR';break;			
		case 'capetown':   gneForm.destination.value = 'CPT';break;			
		case 'cairo':   gneForm.destination.value = 'CAI';break;			
		default :   gneForm.destination.value = destination;break;					
	}
		
	return true;
	
	

}

function setAltitudeDomesticFlightReturnDate(startFieldObj, endFieldObj, returnFieldObj) {
	var startDateString = startFieldObj.value;
	
	if (startDateString == null || !isDateFormat(startDateString)) {
		return false;
	}
	
	var startDate = stringToDate(startDateString);
	if (startDate == null) {
		highlightField(startFieldObj);
		startFieldObj.select();
		startFieldObj.focus();
		return;
	}
	//startFieldObj.style.background = 'White';
	var newDateString = dateToYYYYMMDD(startDate);
	startFieldObj.value = newDateString.substring( 6, 8 ) + "/" + newDateString.substring( 4, 6 ) + "/" + newDateString.substring( 0, 4 );

	if (returnFieldObj.checked) {

		var endDate;
		if (endFieldObj.value != null && endFieldObj.value != "") {
			var returnDate = stringToDate(startFieldObj.value);
			var milliseconds;
			
			var newReturnDate = new Date();
			milliseconds = returnDate.getTime();
			milliseconds = milliseconds + 1000*60*60*24*7;
			newReturnDate.setTime(milliseconds);
		}
		if (endDate == null) {
			endDate = stringToDate(startFieldObj.value);
		}

		newDateString = dateToYYYYMMDD(newReturnDate);
		endFieldObj.value = newDateString.substring( 6, 8 ) + "/" + newDateString.substring( 4, 6 ) + "/" + newDateString.substring( 0, 4 );

		if (endDate < startDate) {
			var endDate = new Date();
			endDate.setTime(startDate.getTime());

			// Day
			var day = endDate.getDate();
			var dayString = day.toString();
			if (dayString.length == 1) {
				dayString = '0' + dayString;
			}

			// Month
			var month = endDate.getMonth()+1;
			var monthString = month.toString();
			if (monthString.length == 1) {
				monthString = '0' + monthString;
			}

			// Year
			var year = endDate.getYear();
			if ( year < 1900 ) year += 1900;
			var yearString = year.toString();

			endFieldObj.value = dayString + '/' + monthString + '/' + yearString;
		}
	} else 
		endFieldObj.value = startFieldObj.value;

}

function isValidGNESearch(gneForm){



	//Check departure and set the requested fields
	
	
	if (! setDepartures(gneForm)){
		alert('This is an international airfare guide, there are no domestic airfares in this system. \nIf you want to find and book domestic airfares go to the flights page.');
		return false;
	}
/* 
 *  THIS IS ONLY BEING COMMENTED OUT TEMPORARILY DUE TO THE STUDENT FLIGHTS DOMESTIC BOOKING ENGINE BEING TURNED OFF ON 25/05/2009. - Taz 22/09/2009 	
	if (! checkLocalDepartures(gneForm)){
		return false;
	}
*/
	//  Check that a departure has been selected

	if (gneForm.departure.value.length == 0){
		highlightField(gneForm.departure);
		alert('Please select your departure.');
		return false;
	}
	if(document.getElementById("destinationdisplay")) { //www3
	// Destination
		if (gneForm.destinationdisplay.value.length == 0) {
			alert('Please enter your destination.');
			highlightField(gneForm.destinationdisplay);
			return false;
		}else{
			if (gneForm.destinationdisplay.value.indexOf(',') != -1){
				alert('Please enter your destination without a commar.');
				highlightField(gneForm.destinationdisplay);7
				return false;			
			}
		}

		// Departure must not equal destination
		if (gneForm.departure[gneForm.departure.selectedIndex].text == gneForm.destinationdisplay.value) {
			alert('Your destination must be different to your departure.');
			highlightField(gneForm.departure);
			highlightField(gneForm.destinationdisplay);
			return false;
		}
	} else { //tomcat
			if (gneForm.destination.value.length == 0) {
			alert('Please enter your destination.');
			highlightField(gneForm.destination);
			return false;
		}else{
			if (gneForm.destination.value.indexOf(',') != -1){
				alert('Please enter your destination without a commar.');
				highlightField(gneForm.destination);7
				return false;			
			}
		}

		// Departure must not equal destination
		if (gneForm.departure[gneForm.departure.selectedIndex].text == gneForm.destination.value) {
			alert('Your destination must be different to your departure.');
			highlightField(gneForm.departure);
			highlightField(gneForm.destination);
			return false;
		}
		

	}
		
	
	/*****************************************************************************
 * Validate departure date
 ******************************************************************************/

	if (gneForm.departureDate.value.length == 0) {
		alert('Please enter your departure date.');
		//highlightField(gneForm.departureDate);
		return false;
	}
	
	var departureDate = stringToDate(gneForm.departureDate.value);
	if (departureDate == null) {
		highlightField(gneForm.departureDate);
		return false;
	}
	
	if (isNaN(departureDate)){
		alert('Please enter your departure date format of dd/mm/yyyy.');	
		highlightField(gneForm.departureDate);
		return false;	
	}
	
	var dateArray = gneForm.departureDate.value.split("/");
	
	try{
		if (dateArray[2].length != 4){
			alert('Please enter your departure date year format of yyyy.');
			highlightField(gneForm.departureDate);
			return false;			
		}
	} catch (e){
			alert('Please enter your departure date year format of yyyy.');
			highlightField(gneForm.departureDate);
			return false;			
	
	}
	
	var todaysDate = stringToDate(gneForm.todaysDate.value);
	var milliseconds;

	//Check the minimum departure time of 3 days from now
	var minimumDate = new Date();
	milliseconds = todaysDate.getTime();
	milliseconds = milliseconds + 1000*60*60*24*3;
	minimumDate.setTime(milliseconds);
	if (departureDate <= minimumDate) {
		alert('Please enter a departure date more than 3 days from now.');
		highlightField(gneForm.departureDate);
		return false;
	}

	// Check the maximum departure time of 300 days from now
	var maximumDate = new Date();
	milliseconds = todaysDate.getTime();
	milliseconds = milliseconds + 1000*60*60*24*300;
	maximumDate.setTime(milliseconds);
	if (departureDate >= maximumDate) {
		alert('Please enter a departure date less than 300 days from now.');
		//highlightField(gneForm.departureDate);
		return false;
	}

	
/******************************************************************************
 * Validate return date
 ******************************************************************************/
if(gneForm.tripType.value == "R" || gneForm.tripType.value == "RT") {
	if (gneForm.GNEreturnDate.value.length == 0) {
		alert('Please enter your return date.');
		return false;
	}
	
	var GNEreturnDate = stringToDate(gneForm.GNEreturnDate.value);
	if (GNEreturnDate == null) {
		highlightField(gneForm.GNEreturnDate);
		return false;
	}
	
	if (isNaN(GNEreturnDate)){
		alert('Please enter your return date format of dd/mm/yyyy.');	
		highlightField(gneForm.departureDate);
		return false;	
	}
	
	var dateArray2 = gneForm.GNEreturnDate.value.split("/");
	
	try{
		if (dateArray2[2].length != 4){
			alert('Please enter your return date year format of yyyy.');
			highlightField(gneForm.GNEreturnDate);
			return false;			
		}
	} catch (e){
			alert('Please enter your return date year format of yyyy.');
			highlightField(gneForm.GNEreturnDate);
			return false;			
	}
	
	if (GNEreturnDate <= departureDate) {
		alert('Please enter a return date after the departure date.');
		highlightField(gneForm.GNEreturnDate);
		return false;
	}
	
	var todaysDate = stringToDate(gneForm.todaysDate.value);
	var milliseconds;

	//Check the minimum departure time of 3 days from now
	var minimumDate = new Date();
	milliseconds = todaysDate.getTime();
	milliseconds = milliseconds + 1000*60*60*24*3;
	minimumDate.setTime(milliseconds);
	if (GNEreturnDate <= minimumDate) {
		alert('Please enter a return date more than 3 days from now.');
		highlightField(gneForm.GNEreturnDate);
		return false;
	}

	// Check the maximum departure time of 300 days from now
	var maximumDate = new Date();
	milliseconds = todaysDate.getTime();
	milliseconds = milliseconds + 1000*60*60*24*300;
	maximumDate.setTime(milliseconds);
	if (GNEreturnDate >= maximumDate) {
		alert('Please enter a return date less than 300 days from now.');
		highlightField(gneForm.GNEreturnDate);
		return false;
	}
	
}
	return true;
}



function buildList(code, description){
	// Populate the list
	// Both the code and description is a delimited list so we need to parse into a array
	codearray		= code.split(",");
	descriptionarray	= description.split(",");

	newlist = document.forms[0].selectedcities;
	newlist.options.length = 0;

	for (i= 0; i < descriptionarray.length; i++){
		newlist.options[i] = new Option( descriptionarray[i], codearray[i]);
	}


}


function openAddWindow(){
	// Populate the list
	myWindow = window.open("/admin/GNEAdmin/AirportAdd.jsp", "addwindow", 'toolbar,width=850,height=300');

}

function addtoList(value){
	document.forms[0].hselected.value	=	document.forms[0].hselected.value + "," + value;
	lgth = document.forms[0].selectedcities.options.length;
	document.forms[0].selectedcities.options[lgth] = new Option(value,value);
}

function deleteFromList(value){
	// Need to get the list into a string and parse it out
	list = document.forms[0].hselected.value;
	var listArray	=	list.split(",");
	var listvalue	=	"";
	var valuetext	=	document.forms[0].selectedcities.options[value].text;


	for (i=0; i < listArray.length; i++){
		if (listArray[i] != valuetext){
			listvalue += listArray[i] + ",";
		}
	}

	document.forms[0].hselected.value = listvalue;
	document.forms[0].selectedcities.options[value] = null;



}

function clearFromList(value){
	document.forms[0].selectedcities.options.length = 0;
	document.forms[0].hselected.value = "";
}

var ns4 = (document.layers);
var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById);
var ns6 = (!document.all && document.getElementById);

function show(id){

	// Netscape 4

	if(ns4){
		document.layers[id].visibility = "show";
	}
	// Explorer 4
	else if(ie4){
		document.all[id].style.visibility = "visible";
	}
	// W3C - Explorer 5+ and Netscape 6+
	else if(ie5 || ns6){
		document.getElementById(id).style.visibility = "visible";
	}

}


function hide(id){
	// Netscape 4
	if(ns4){
		document.layers[id].visibility = "hide";
	}
	// Explorer 4
	else if(ie4){
		document.all[id].style.visibility = "hidden";
	}
	// W3C - Explorer 5+ and Netscape 6+
	else if(ie5 || ns6){
		document.getElementById(id).style.visibility = "hidden";
	}
}


function setMarkupType(){
	form	=	document.forms[0].markuptype;
	for (var i = 0; i < form.length; i++) {
		if (form[i].checked){
			valuenew	=	form[i].value;
			show(valuenew);
		}else{
			valuenew	=	form[i].value;
			hide(valuenew);
		}
	}
}


function setNewMarkupType(){
	form	=	document.forms[0].markuptype;
	if (document.forms[0].hmarkuptype.value == "Percent"){
		form[0].checked	= true;
		show('percent');
		hide('value');
	}else{
		form[1].checked	= true;
		show("value");
		hide("percent");
	}

}

function arrowPressed(strValue){
	// Netscape 4
	if(ns4){}
	// Explorer 4
	else if(ie4){}
	// W3C - Explorer 5+ and Netscape 6+
	else if(ie5 || ns6){
		if (document.getElementById("percent").style.visibility == "visible"){
			// The percent variable is visible so need to increment the percent value
			// Get the value of the percent

			percentage	=	document.forms[0].markuppercentage.value;
			if (strValue == "Up"){
				if (percentage < 99){
					document.forms[0].markuppercentage.value	=	eval(percentage) + 1;
				}
			}else{
				if (percentage > 1){
					document.forms[0].markuppercentage.value	=	eval(percentage) - 1;
				}
			}

		}else{

			// The value must be visible
			markupvalue	=	document.forms[0].markupvalue.value;

			if (strValue == "Up"){
				document.forms[0].markupvalue.value	=	eval(markupvalue) + 1;
			}else{
				if (markupvalue > 1){
					document.forms[0].markupvalue.value	=	eval(markupvalue) - 1;
				}
			}
		}
	}
}

function arrowPressedOther(strValue, field){
	// Netscape 4
	if(ns4){}
	// Explorer 4
	else if(ie4){}
	// W3C - Explorer 5+ and Netscape 6+
	else if(ie5 || ns6){
		if (document.getElementById("percent").style.visibility == "visible"){
			// The percent variable is visible so need to increment the percent value
			// Get the value of the percent

			percentage	=	field.value;
			if (strValue == "Up"){
				if (percentage < 99){
					field.value	=	eval(percentage) + 1;
				}
			}else{
				if (percentage > 1){
					field.value	=	eval(percentage) - 1;
				}
			}

		}else{

			// The value must be visible
			markupvalue	=	field;

			if (strValue == "Up"){
				field.value	=	eval(markupvalue) + 1;
			}else{
				if (markupvalue > 1){
					field.value	=	eval(markupvalue) - 1;
				}
			}
		}
	}
}
