var bookingScenario = "scenario_normal"; 
var iTD;
var defaultGoSite = "nzairnz01";
var erequestURL = "http://erequest.airnz.co.nz/nz/";
var isPDA = false;
var strNav = navigator.userAgent;
var isCE = strNav.indexOf("Windows CE");
if(isCE!=-1)isPDA=true;
isCE = strNav.indexOf("240x320");
if(isCE!=-1)isPDA=true;
var DM = "We are sorry, but we cannot accept online bookings from Pocket PC's at this time.\n\nPlease call\nAir New Zealand Reservations on\n";
var nzpdamessage =DM+"0800 737 000";
var aupdamessage =DM+"13 24 76";

function fast_fare_finder(locale){
	this.locale = locale;
	/**
	 * Domestic codes and locations
	 */
	this.domesticLocations = [
		["AKL", "Auckland"],
		["KKE", "Bay of Islands"],
		["BHE", "Blenheim"],
		["CHC", "Christchurch"],
		["DUD", "Dunedin"],
		["GIS", "Gisborne"],
		["HLZ", "Hamilton"],
		["HKK", "Hokitika"],
		["IVC", "Invercargill"],
		["KAT", "Kaitaia"],
		["KKE", "Kerikeri"],
		["NPE", "Napier/Hastings"],
		["NSN", "Nelson"],
		["NPL", "New Plymouth"],
		["PMR", "Palmerston North"],
		["ZQN", "Queenstown"],
		["ROT", "Rotorua"],
		["TUO", "Taupo"],
		["TRG", "Tauranga"],
		["TIU", "Timaru"],
		["WKA", "Wanaka"],
		["WAG", "Wanganui"],
		["WLG", "Wellington"],
		["WSZ", "Westport"],
		["WHK", "Whakatane"],
		["WRE", "Whangarei"]];
	/**
	 * Tasman codes and locations
	 * There are Pacific codes here as well because the fare
	 * preferences work the same for both
	 */
	this.tasmanLocations = [
		["BNE", "Brisbane"],
		["CNS", "Cairns"],
		["MEL", "Melbourne"],
		["PER", "Perth"],
		["SYD", "Sydney"],
		["NLK", "Norfolk Island"]];
	/**
	 * Pacific codes and locations
	 */
	this.pacificLocations = [
		["APW", "Apia"],
		["APW", "Samoa"],
		["NAN", "Nadi"],
		["NAN", "Fiji"],
		["RAR", "Rarotonga"],
		["RAR", "Cook Islands"],
		["TBU", "Tonga"],
		["TBU", "Nuku'alofa"],
		["TBU", "Nukualofa"]];

	/**
	 * Premium economy codes and locations
	 */
	 this.premiumLocations = [
		["AKL", "Auckland", "SFO", "San Francisco", "August 07, 2005"],
	 	["AKL", "Auckland", "LAX", "Los Angeles", "November 05, 2005"],
	 	["AKL", "Auckland", "LHR", "London Heathrow", "November 05, 2005"]
	 ];
	 
	// new site
	this.formFieldMapping = [	  
		["text", "fare.type", "rt_ow", "trip_return","normal"],
		["text", "departure.point", "depart", "dept_point","normal"],
		["text", "arrival.point", "dest.0", "arr_point","normal"],
		["select", "departure.time", "hour_24.0", "dept_time_1","istime"],
		["select", "departure.date", "date.0", "dept_day_1","normal"],
		["select", "departure.month", "mon_abbr.0", "dept_month_1","ismonth"],
		//["select", "arrival.time", "hour_24.1", "dept_time_2","istime"],
		["select", "arrival.date", "date.1", "dept_day_2","normal"],
		["select", "arrival.month", "mon_abbr.1", "dept_month_2","ismonth"],
		["select", "adults", "persons.1", "adults","normal"],
		["select", "children", "persons.3", "children","normal"],
		["select", "service.class", "cabin_class", "fare_type","normal"]
    ];

	/**
	 *	Flag for defining booking type
	 */
	this.bookingtype = "iTD";
	this.todaysday = new Date();
	this.departurearray = new Array();
}

  
/**
 * Hidden form names
 */
var formIndexGetThere = 1;
var formIndexiTD = 2;
var formNames = ["frmGetThere", "frmiTD"];

/**
 * Maps the FFF field names to booking engine specific ones
 */
var fieldTypeText = "text";
var fieldTypeSelect = "select";

// for setSelectedIndexFromMonth and getMonthFromSelected functions
var dDate = new Date();
var monthOffset = (dDate.getMonth());

/**
 * Service classes
 */
 
var serviceClassesGetThere = [
      ["coach", "Economy - lowest"],
      ["coach", "Economy"],
      ["business", "Business"]
    ]
	
var serviceClassesGetTherePremium = [
      ["coach", "Economy - lowest"],
      ["coach", "Economy"],
      ["coach", "Premium Economy"],
      ["business", "Business"]
    ]	
    
var serviceClassesiTD = [
      ["smartsaver", "Lowest Available"],
      ["flexisaver", "Flexi Saver Fare"],
      ["fullyflexi", "Flexi Fare"]	  	  
    ]

var serviceClassesiTDtasman = [
      ["smartsaver", "Lowest Available"],
      ["flexisaver", "Flexi Saver"],
      ["fullyflexi", "Flexi"],
      ["business", "Business Class"]	  	  
    ]

/**
 *	array for mapping month number to month e.g. 12 = DEC
 */
var arrMonths = [
	  ["JAN","1"],
      ["FEB","2"],
      ["MAR","3"],
      ["APR","4"],
      ["MAY","5"],
      ["JUN","6"],
      ["JUL","7"],
      ["AUG","8"],
      ["SEP","9"],
      ["OCT","10"],
      ["NOV","11"],
      ["DEC","12"]
	  ];

	function getMonthIndex(sMonth) {
		switch (sMonth) {
		case 'JAN': return 1;
		case 'FEB': return 2;
		case 'MAR': return 3;
		case 'APR': return 4;
		case 'MAY': return 5;
		case 'JUN': return 6;
		case 'JUL': return 7;
		case 'AUG': return 8;
		case 'SEP': return 9;
		case 'OCT': return 10;
		case 'NOV': return 11;
		case 'DEC': return 12;
		default: return 0;
		}
	}

function isDomestic(DP, AP) 
{
		var	i=0;			
		var deptP, arrP;
		var inListCheck = false;
		var domesticDeparture = false;
		var domesticArrival = false;
		var domesticLoc = [
		["AKL", "Auckland"],
		["KKE", "Bay of Islands"],
		["BHE", "Blenheim"],
		["CHC", "Christchurch"],
		["DUD", "Dunedin"],
		["GIS", "Gisborne"],
		["HLZ", "Hamilton"],
		["HKK", "Hokitika"],
		["IVC", "Invercargill"],
		["KAT", "Kaitaia"],
		["KKE", "Kerikeri"],
		["NPE", "Napier/Hastings"],
		["NSN", "Nelson"],
		["NPL", "New Plymouth"],
		["PMR", "Palmerston North"],
		["ZQN", "Queenstown"],
		["ROT", "Rotorua"],
		["TUO", "Taupo"],
		["TRG", "Tauranga"],
		["TIU", "Timaru"],
		["WKA", "Wanaka"],
		["WAG", "Wanganui"],
		["WLG", "Wellington"],
		["WSZ", "Westport"],
		["WHK", "Whakatane"],
		["WRE", "Whangarei"]];
	
		do {
				// Check departure point
				inListCheck =  (DP.toLowerCase() == domesticLoc[i][0].toLowerCase()) ||
											 (DP.toLowerCase() == domesticLoc[i][1].toLowerCase());
				domesticDeparture = domesticDeparture || inListCheck;

				// Check arrival point
				inListCheck = (AP.toLowerCase() == domesticLoc[i][0].toLowerCase()) ||
											(AP.toLowerCase() == domesticLoc[i][1].toLowerCase());
				domesticArrival = domesticArrival || inListCheck;
		    i++;
		} while (i < domesticLoc.length);
		
		return domesticDeparture && domesticArrival;
}

function popNoAPD(thelocale){
		var noapd;
		var dDate = new Date();
		var RAPStartDate = new Date(2004, 10, 16, 0, 0, 0); 		
		if(RAPStartDate.valueOf() <= dDate.valueOf()) { 
				noapd = window.open('http://erequest.airnz.co.nz/booking_support/noapd_' + thelocale + '.htm','noapd','scrollbars=no,resizable=no,toolbar=no,location=no,menubar=no,status=no,height=260,width=345');
		}
}

function getFulliTDURL(theForm){
	var iTDURL = theForm.action + "?";
	var i = 0;
	var pointExist = "";
	do{
		if(theForm.elements[i].value > "") pointExist = "Airport.";
		if(theForm.elements[i].name == "dept_point" || theForm.elements[i].name == "arr_point") iTDURL += theForm.elements[i].name + "=" + pointExist + theForm.elements[i].value + "&";
		else if (theForm.elements[i].name == "dept_month_1")
		{
			iTDURL += "dept_yearmonth_1" + "=" + getYearMonthFormat(theForm.elements[i].value, theForm.dept_day_1.value) + "&";
		}
		else if (theForm.elements[i].name == "dept_month_2" && theForm.dept_day_2.value != "")
		{
			iTDURL += "dept_yearmonth_2" + "=" + getYearMonthFormat(theForm.elements[i].value, theForm.dept_day_2.value) + "&";
		}
		else iTDURL += theForm.elements[i].name + "=" + theForm.elements[i].value + "&";
		i++;
		pointExist = "";
	}while(i<theForm.elements.length);

	return iTDURL;
}

//dates in the month dropdowns start from todays month not Jan (month 0) so,...
//we need to correct for this offset when evaluating what month has been selected
function getMonthFromSelected(sindex){
	if(typeof(document.frmCalendar) == "undefined") monthOffset = 0;
	rtnValue = sindex + monthOffset; 
	if (rtnValue > 11) rtnValue = rtnValue - 12;
	return rtnValue;
}

// dates in the month dropdown also include the year, starting from the current month
// to the same month in the next year, this returns the correct year.
function getYearFromSelected(sindex){
	if(typeof(document.frmCalendar) == "undefined") monthOffset = 0;
	monthValue = sindex + monthOffset; 
	currentDate = new Date();
	rtnValue = currentDate.getYear();
	if (monthValue > 11) rtnValue = rtnValue + 1;
	return rtnValue;
}

//dates in the month dropdowns start from todays month not Jan (month 0) so,...
//we need to correct for this offset when updating the drop down with a particular month
function setSelectedIndexFromMonth(smonth){
	if(typeof(document.frmCalendar) == "undefined") monthOffset = 0;
	rtnValue = smonth - monthOffset; 
	if (rtnValue < 0) rtnValue = rtnValue + 12;
	return rtnValue;	
}

function formatTimeForiTD(theTime){
	//times in iTD of the form 12:00 (not 1200)
	theTime = theTime.substring(0,2) + ":" + theTime.substring(2,4);
	return theTime;
}

function formatMonthForiTD(theMonth){
	//months for iTD are numeric (e.g. 4 for APR)
	var i = 0;
	do {
		if(arrMonths[i][0] == theMonth) theMonth =  arrMonths[i][1];
		i++;
	} while (i < arrMonths.length);
	return theMonth;
}

function calculateYear(iMonth, iDay){
	currentDate = new Date();
	rtnValue = currentDate.getFullYear();

	if (iMonth <= currentDate.getMonth() || (iMonth == currentDate.getMonth()+1 && iDay < currentDate.getDate())) rtnValue = rtnValue + 1;
	return rtnValue;
}

function getYearMonthFormat(theMonth, theDay){
	var sZero = "";

	if (theMonth.length == 1) sZero = "0";
	var sYearMonth =  calculateYear(Number(theMonth), Number(theDay)) + "-" + sZero + theMonth;

	return sYearMonth;
}

function getTimeStamp(){
	var dDate = new Date();
	var formatteddate = new String(dDate.getFullYear());
	if (dDate.getDate() < 10) var thedays = new String("0" + dDate.getDate());
	else var thedays = new String(dDate.getDate());
	if (dDate.getMonth() < 9) var themonth = new String("0" + (dDate.getMonth()+1));
	else var themonth = new String((dDate.getMonth()+1));
	if (dDate.getHours() < 10) var thehours = new String("0" + dDate.getHours());
	else var thehours = new String(dDate.getHours());
	if (dDate.getMinutes() < 10) var themins = new String("0" + dDate.getMinutes());
	else var themins = new String(dDate.getMinutes());
	if (dDate.getSeconds() < 10) var thesecs = new String("0" + dDate.getSeconds());
	else var thesecs = new String(dDate.getSeconds());
	formatteddate += themonth + thedays + thehours + ":" + themins + ":" + thesecs;
	return formatteddate;
}

function isgtclass(theclassvariable){
	classrtnval = false;
	for(k=0;k<serviceClassesGetThere.length;k++){
		if(theclassvariable.toUpperCase() == serviceClassesGetThere[k][1].toUpperCase())classrtnval = true;
	}
	return classrtnval;
}

function openGetThereError(){
	strGTErrorUrl = "http://" + getSiteURL() + "/" + getSite() + "gtbcperror.htm";

	window.open(strGTErrorUrl,"importainfo","scrollbars=no,resizable=no,toolbar=no,location=no,menubar=no,status=no,height=380,width=720");
}

function regionaliseStamp(theForm){
	gosite = getgosite();
	if (typeof(theForm.elements['stamp'])=="object"){
		if (theForm.elements['stamp'].name != null){
			if(typeof(gosite) == "undefined"){
				var gosite = defaultGoSite;
			} 
			theForm.elements['stamp'].value = "*itn/ord=NEWREC,itn/air/" + gosite;
		}	
	}
	return true;
}
