var destcitylist=new Array('AKL-DOM','WLG-DOM','CHC-DOM','DUD-DOM','ZQN-DOM','IVC-DOM','BHE-DOM','GIS-DOM','HLZ-DOM','HKK-DOM','KAT-DOM','KKE-DOM','NPE-DOM','NSN-DOM','NPL-DOM','OAM-DOM','PMR-DOM','ROT-DOM','TUO-DOM','TRG-DOM','TIU-DOM','WKA-DOM','WAG-DOM','WSZ-DOM','WHK-DOM','WRE-DOM','SYD-TT','MEL-TT','BNE-TT','ADL-TT','PER-TT','CNS-TT','ASP-TT','BME-TT','CNB-TT','DRW-TT','OOL-TT','HBA-TT','NTL-TT','MCY-TT','TSV-TT','APW-TT','NAN-TT','NLK-TT','NOU-TT','IUE-TT','PPT-TT','VLI-TT','RAR-TT','SUV-TT','TBU-TT','AMS-INT','ATH-INT','CPH-INT','DUB-INT','EDI-INT','FRA-INT','GLA-INT','HEL-INT','IST-INT','LON-INT','MAD-INT','MAN-INT','MIL-INT','MOW-INT','MUC-INT','OSL-INT','PAR-INT','ROM-INT','STO-INT','VIE-INT','ZRH-INT','BWN-INT','BJS-INT','SHA-INT','HKG-INT','DPS-INT','JKT-INT','BOM-INT','DEL-INT','MAA-INT','NGO-INT','OSA-INT','TYO-INT','SEL-INT','CMB-INT','BKI-INT','KUL-INT','PEN-INT','MNL-INT','SIN-INT','BKK-INT','HKT-INT','USM-INT','TPE-INT','HAN-INT','SGN-INT','BOS-INT','CHI-INT','DEN-INT','DFW-INT','HNL-INT','LAS-INT','LAX-INT','MIA-INT','NYC-INT','ORL-INT','SEA-INT','SFO-INT','WAS-INT','YMQ-INT','YTO-INT','YVR-INT','CPT-INT','JNB-INT','CAI-INT','DXB-INT','BUE-INT','RIO-INT','SCL-INT','MEX-INT');


function padout(number) { return (number < 10) ? '0' + number : number; }

function chooseActionAll(){
	var altiURL = "https://secure.flightcentre.co.nz/fclnz/en_AU/homepage_FlightSearch";
	var dtmURL = "https://bookings.flightcentre.co.nz/matrixtdp-flightcentre-nz/InterstitialScreen.do";
//	var dtmURL = "https://bookings.flightcentre.co.nz";
//	var dtmURL = "http://10.10.2.242/matrixtdp-galileo/InterstitialScreen.do";
//	var dtmURL = "http://10.4.238.53/matrixtdp-flightcentre-nz";
//	var dtmURL = "http://10.4.238.55/matrixtdp-flightcentre-nz/AirLowFareSearchActionResult.do?followAction=AirLowFareSearchAction";
//	var dtmURL = "https://bookings.flightcentre.co.nz/matrixtdp-flightcentre-nz/InterstitialScreen.do";

	endCityValue = 	document.forms[searchFormName].elements['availabilityRequest[0].destinationCode'].value ;

if (validateSearchFormToNewWindow()){
	if (endCityValue == 'AKL' || endCityValue == 'BHE' || endCityValue == 'CHC' || endCityValue == 'DUD' || endCityValue == 'GIS' || 		endCityValue == 'HLZ' || endCityValue == 'HKK' || endCityValue == 'IVC' || endCityValue == 'KAT' || endCityValue == 'KKE' || endCityValue == 'NPE' || endCityValue == 'NSN' || endCityValue == 'NPL' || endCityValue == 'OAM' || endCityValue == 'PMR' || endCityValue == 'ZQN' || endCityValue == 'ROT' || endCityValue == 'TUO' || endCityValue == 'TRG' || endCityValue == 'TIU' || endCityValue == 'WKA' || endCityValue == 'WAG' || endCityValue == 'WLG' || endCityValue == 'WSZ' || endCityValue == 'WHK' || endCityValue == 'WRE'){

				actionURL = altiURL;
				document.forms[searchFormName].action = actionURL;
				oFlightWin = window.open("/cms_images/form_scripts/process.html","window_flightsearch","toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes");
				document.forms[searchFormName].target = 'window_flightsearch';
				return false;
			}else{
				actionURL = dtmURL;
				document.forms[searchFormName].target = "_parent";
				document.forms[searchFormName].action = actionURL;
	//			oFlightWin = window.open("about:blank","foo","toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes");
		}
   }else{
	   return false;
   }
								   
}

function chooseAction(){
	var altiURL = "https://secure.flightcentre.co.nz/fclnz/en_AU/homepage_FlightSearch";
	var dtmURL = "https://bookings.flightcentre.co.nz/matrixtdp-flightcentre-nz/InterstitialScreen.do";
	var tmpURL = "http://www.flightcentre.co.nz/story19455.asp";
	var typeOfRoute;
//	var dtmURL = "http://10.4.238.53/matrixtdp-flightcentre-nz";
//	var dtmURL = "http://10.10.2.242/matrixtdp-galileo/InterstitialScreen.do";
//	var dtmURL = "https://bookings.flightcentre.co.nz/matrixtdp-flightcentre-nz/AirBrandNewSearchForward.do";
//	var dtmURL = "https://bookings.flightcentre.co.nz/matrixtdp-flightcentre-nz/InterstitialScreen.do";
	endCityValue = 	document.forms[searchFormName].elements['availabilityRequest[0].destinationCode'].value ;
	
	for(i=0;i<destcitylist.length;i++){
	 if (endCityValue==destcitylist[i].substring(0,3)){
		typeOfRoute=destcitylist[i].substring(4);
		break;
	 }
	}
	document.getElementById("typeOfRoute").value=typeOfRoute;

	
	if (validateSearchFormToNewWindow()){
	if (typeOfRoute =='TT'||typeOfRoute =='INT') {
			actionURL = dtmURL;
			document.forms[searchFormName].target = "_blank";
			document.forms[searchFormName].action = actionURL;
			}else if (typeOfRoute =='TMP'){
				actionURL = tmpURL;
				window.open(actionURL,'_blank','height=600,width=1100,resizable=1');
				return false;		
			}else{
			actionURL = altiURL;
			document.forms[searchFormName].action = actionURL;
			oFlightWin = window.open("/cms_images/form_scripts/process.html","window_flightsearch","toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes");
			document.forms[searchFormName].target = 'window_flightsearch';
			return false;
		}
   }else{
	   return false;
   }
								   
}

function assignValues(sourceVal, destinationName, formName){
	document.forms[formName].elements[destinationName].value = sourceVal;
}

function assignAltitudeValues(formName){
	document.forms[formName].elements['numAdults'].value = document.forms[formName].elements['numOfAdults'].value;
	document.forms[formName].elements['numChildren'].value = document.forms[formName].elements['numOfChildren'].value;
	document.forms[formName].elements['numInfants'].value = document.forms[formName].elements['numOfInfantsSeat'].value;
	
	if (document.forms[formName].elements['typeOfSearch'][1].checked){
		document.forms[formName].flightType.value = "oneway";
	}
	if (document.forms[formName].elements['typeOfSearch'][0].checked){
		document.forms[formName].flightType.value = "return";
	}
}

function assignDatalexValues(formName){
	document.forms[formName].elements['availabilityRequest[1].originCode'].value = document.forms[formName].elements['availabilityRequest[0].destinationCode'].value;
	document.forms[formName].elements['availabilityRequest[1].destinationCode'].value = document.forms[formName].elements['availabilityRequest[0].originCode'].value;

if (isValidDate(document.forms[searchFormName].startDate.value))
{
		startDateValue = document.forms[searchFormName].startDate.value;
//		startDateValue = document.forms[searchFormName].datalexStartDate.value;
		aD = startDateValue.split('/');
		if(aD.length==3){
			document.AirAvailabilitySearchForm.elements['availabilityRequest[0].departureYear'].value = aD[2];
			document.AirAvailabilitySearchForm.elements['availabilityRequest[0].departureMonth'].value = aD[1]-1;
			document.AirAvailabilitySearchForm.elements['availabilityRequest[0].departureDay'].value = aD[0];
		}
}

if (isValidDate(document.forms[searchFormName].endDate.value) && document.forms[searchFormName].elements['typeOfSearch'][0].checked )
{
	endDateValue = document.forms[searchFormName].endDate.value;
//	endDateValue = document.forms[searchFormName].datalexEndDate.value;
	aY = endDateValue.split('/');
	if(aY.length==3){
		document.AirAvailabilitySearchForm.elements['availabilityRequest[1].departureYear'].value = aY[2];
		document.AirAvailabilitySearchForm.elements['availabilityRequest[1].departureMonth'].value = aY[1]-1;
		document.AirAvailabilitySearchForm.elements['availabilityRequest[1].departureDay'].value = aY[0];
	}
}

	if (document.AirAvailabilitySearchForm.elements['availabilityRequest[0].departureTime'].value == 'anytime'){
		document.AirAvailabilitySearchForm.elements['availabilityRequest[0].departureTime'].value = 'M';
	}
	if (document.AirAvailabilitySearchForm.elements['availabilityRequest[1].departureTime'].value == 'anytime'){
		document.AirAvailabilitySearchForm.elements['availabilityRequest[1].departureTime'].value = "M";
	}
//	alert(document.AirAvailabilitySearchForm.elements['availabilityRequest[1].departureTime'].value);

}

function checkDestination(){
	endCityValue = 	document.forms[searchFormName].elements['availabilityRequest[0].destinationCode'].value ;
	numInfantsValue = 	document.forms[searchFormName].elements['numOfInfantsSeat'].value ;
	
	if (endCityValue == '' || endCityValue == 'AKL' || endCityValue == 'WLG' || endCityValue == 'CHC' || endCityValue == 'BHE' ||
		endCityValue == 'DUD' || endCityValue == 'GIS' || endCityValue == 'HLZ' || endCityValue == 'HKK' || endCityValue == 'IVC' ||
		endCityValue == 'KAT' || endCityValue == 'KKE' || endCityValue == 'NPE' || endCityValue == 'NSN' || endCityValue == 'NPL' ||
		endCityValue == 'OAM' || endCityValue == 'PMR' || endCityValue == 'ZQN' || endCityValue == 'ROT' || endCityValue == 'TUO' ||
		endCityValue == 'TRG' || endCityValue == 'TIU' || endCityValue == 'WKA' || endCityValue == 'WAG' || endCityValue == 'WSZ' ||
		endCityValue == 'WHK' || endCityValue == 'WRE')
	{
		document.forms[searchFormName].elements['numInfants'].disabled = false;
		return false;
	}
	if ((endCityValue != '' && numInfantsValue != 0) || (endCityValue != 'AKL' && numInfantsValue != 0) ||
		(endCityValue != 'WLG' && numInfantsValue != 0) || (endCityValue != 'CHC' && numInfantsValue != 0) ||
		(endCityValue != 'BHE' && numInfantsValue != 0) || (endCityValue != 'DUD' && numInfantsValue != 0) ||
		(endCityValue != 'GIS' && numInfantsValue != 0) || (endCityValue != 'HLZ' && numInfantsValue != 0) ||
		(endCityValue != 'HKK' && numInfantsValue != 0) || (endCityValue != 'IVC' && numInfantsValue != 0) ||
		(endCityValue != 'KAT' && numInfantsValue != 0) || (endCityValue != 'KKE' && numInfantsValue != 0) ||
		(endCityValue != 'NPE' && numInfantsValue != 0) || (endCityValue != 'NSN' && numInfantsValue != 0) ||
		(endCityValue != 'NPL' && numInfantsValue != 0) || (endCityValue != 'OAM' && numInfantsValue != 0) ||
		(endCityValue != 'PMR' && numInfantsValue != 0) || (endCityValue != 'ZQN' && numInfantsValue != 0) ||
		(endCityValue != 'ROT' && numInfantsValue != 0) || (endCityValue != 'TUO' && numInfantsValue != 0) ||
		(endCityValue != 'TRG' && numInfantsValue != 0) || (endCityValue != 'TIU' && numInfantsValue != 0) ||
		(endCityValue != 'WKA' && numInfantsValue != 0) || (endCityValue != 'WAG' && numInfantsValue != 0) ||
		(endCityValue != 'WSZ' && numInfantsValue != 0) || (endCityValue != 'WHK' && numInfantsValue != 0) ||
		(endCityValue != 'WRE' && numInfantsValue != 0))
	{
		alert('If you are travelling with infants (<2years), please contact one of our Flight Centre stores on 0800 24 35 44 to book.');
		document.forms[searchFormName].elements['numInfants'].value = 0;
		document.forms[searchFormName].elements['numOfInfantsSeat'].value = 0;
//		numInfants.disabled = true;
		return false;
	}
		if (endCityValue != '' || endCityValue != 'AKL' || endCityValue != 'WLG' || endCityValue != 'CHC' || endCityValue != 'BHE' ||
		endCityValue != 'DUD' || endCityValue != 'GIS' || endCityValue != 'HLZ' || endCityValue != 'HKK' || endCityValue != 'IVC' ||
		endCityValue != 'KAT' || endCityValue != 'KKE' || endCityValue != 'NPE' || endCityValue != 'NSN' || endCityValue != 'NPL' ||
		endCityValue != 'OAM' || endCityValue != 'PMR' || endCityValue != 'ZQN' || endCityValue != 'ROT' || endCityValue != 'TUO' ||
		endCityValue != 'TRG' || endCityValue != 'TIU' || endCityValue != 'WKA' || endCityValue != 'WAG' || endCityValue != 'WSZ' ||
		endCityValue != 'WHK' || endCityValue != 'WRE')
	{
		document.forms[searchFormName].elements['numInfants'].value = 0;
		document.forms[searchFormName].elements['numOfInfantsSeat'].value = 0;
//		numInfants.inactive = true;
		return false;
	}
	
	
}

function dateDiff(firstDate, secondDate){
	//Set the two dates

		aD = firstDate.split('/');
		if(aD.length==3){
			iY = aD[2];
			iM = aD[1];
			iD = aD[0];
		}

		aE = secondDate.split('/');
		if(aE.length==3){
			eY = aE[2];
			eM = aE[1];
			eD = aE[0];
		}

	var stoday=new Date(iY,iM-1,iD) ;
	var etoday=new Date(eY,eM-1,eD) ;

	var one_day=1000*60*60*24
	
	//Calculate difference btw the two dates, and convert to days
	return Math.ceil((etoday.getTime()-stoday.getTime())/(one_day));
}

function dateCheck(checkDate){
	//Set the two dates

	aD = checkDate.split('/');

	if(aD.length==3){
		iY = aD[2];
		iM = aD[1];
		iD = aD[0];
	}

	var stoday=new Date();
	var etoday=new Date(iY,iM-1,iD);

	var one_day=1000*60*60*24 ;
	
	//Calculate difference btw the two dates, and convert to days
	if (Math.ceil((etoday.getTime()-stoday.getTime())/(one_day)) > 330){
		return false;
	}else{
		return true;
	}
}

function isValidDate(dateStr) {
	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	// Also separates date into month, day, and year variables

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	
	// To require a 4 digit year entry, use this line instead:
	// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
	
	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) {
	return false;
	}
	month = matchArray[3]; // parse date into variables
	day = matchArray[1];
	year = matchArray[4];
	if (month < 1 || month > 12) { // check month range
		return false;
	}
	if (day < 1 || day > 31) {
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		return false
	}
	if (month == 2) { // check for february 29th
	var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
	if (day>29 || (day==29 && !isleap)) {
		return false;
	   }
	}
	return true;  // date is valid
}
