/** Modification History
================================================= 
IS - 21 09 2004 Brand New file based on fff_tasman_partner.js
================================================= 
**/

/**
 * Get the appropriate booking form index based on the users
 * departure and arrival points.  Domestic to domestic is to
 * go to iTD, otherwise GetThere.
 *
 * @return formIndex The form index:
 *         1 - Get There
 *         2 - iTD
 */

function getBookingFormIndex(fieldDeparturePoint, fieldArrivalPoint) {
	locale = "en_NZ";
	if(document.forms[formNames[1]]){
		if(document.forms[formNames[1]].elements["locale"]){
			locale = document.forms[formNames[1]].elements["locale"].value;
		}
	}
  var departurePoint = fieldDeparturePoint.value;
  var arrivalPoint = fieldArrivalPoint.value;
  var domesticDeparture = false;
  var domesticArrival = false;
  var tasmanDeparture = false;
  var tasmanArrival = false;	
  var pacificDeparture = false;
  var pacificArrival = false;
  var i = 0;
   
  do {
    // Check departure point
    inListCheck =  (departurePoint.toLowerCase() == domesticLocations[i][0].toLowerCase()) ||
                   (departurePoint.toLowerCase() == domesticLocations[i][1].toLowerCase());
    if (inListCheck) {
      fieldDeparturePoint.value = domesticLocations[i][0];
    }
    domesticDeparture = domesticDeparture || inListCheck;

    // Check arrival point
    inListCheck = (arrivalPoint.toLowerCase() == domesticLocations[i][0].toLowerCase()) ||
                  (arrivalPoint.toLowerCase() == domesticLocations[i][1].toLowerCase());
    if (inListCheck) {
      fieldArrivalPoint.value = domesticLocations[i][0];
    }
    domesticArrival = domesticArrival || inListCheck;
    
    i++;
  } while (i < domesticLocations.length);
  i = 0;
  do {
    // Check departure point (tasman)
 	inListCheck =  (departurePoint.toLowerCase() == tasmanLocations[i][0].toLowerCase()) ||
                   (departurePoint.toLowerCase() == tasmanLocations[i][1].toLowerCase());
    if (inListCheck) {
      fieldDeparturePoint.value = tasmanLocations[i][0];
    }
    tasmanDeparture = tasmanDeparture || inListCheck;

    // Check arrival point (tasman)
    inListCheck = (arrivalPoint.toLowerCase() == tasmanLocations[i][0].toLowerCase()) ||
                  (arrivalPoint.toLowerCase() == tasmanLocations[i][1].toLowerCase());
    if (inListCheck) {
      fieldArrivalPoint.value = tasmanLocations[i][0];
    }
    tasmanArrival = tasmanArrival || inListCheck;
    i++;
  	} while (i < tasmanLocations.length);
	i=0;
	  do {
		// Check departure point (pacific)
		inListCheck =  (departurePoint.toLowerCase() == this.pacificLocations[i][0].toLowerCase()) ||
					   (departurePoint.toLowerCase() == this.pacificLocations[i][1].toLowerCase());
		if (inListCheck) {
		  fieldDeparturePoint.value = this.pacificLocations[i][0];
		}
		pacificDeparture = pacificDeparture || inListCheck;
	
		// Check arrival point (pacific)
		inListCheck = (arrivalPoint.toLowerCase() == this.pacificLocations[i][0].toLowerCase()) ||
					  (arrivalPoint.toLowerCase() == this.pacificLocations[i][1].toLowerCase());
		if (inListCheck) {
		  fieldArrivalPoint.value = this.pacificLocations[i][0];
		}
		pacificArrival = pacificArrival || inListCheck;
		
		i++;
		} while (i < this.pacificLocations.length);
	
	isPacific = false;
	if((pacificDeparture||pacificArrival)&&(domesticDeparture||domesticArrival||tasmanDeparture||tasmanArrival))isPacific = true;
	if(pacificDeparture&&pacificArrival)isPacific = true;	
  	if ((domesticDeparture && domesticArrival) || (arrivalPoint=='')) { //default to show iTD list	
			formIndex = formIndexiTD;
			bookingtype = "iTD";		
	}else if ((domesticDeparture && tasmanArrival) || (tasmanDeparture && domesticArrival)) {
			formIndex = formIndexiTD;
			bookingtype = "iTDtasman";			
	}else if(isPacific){
			formIndex = formIndexiTD;
			bookingtype = "iTDtasman";		
	}else{
		formIndex = formIndexGetThere;
		bookingtype = "GetThere";
	}
	
	
}



var doubleClickUrl = "http://ad.au.doubleclick.net/clk;4119023;7117117;w%3f";
var iTD;
var bookingtype = "iTD";
var formIndex;

var iTD;
var defaultGoSite = "nzairnz01";
var erequestURL = "http://erequest.airnz.co.nz/nz/";
var isPDA = false;
var strANZNav = navigator.userAgent;
var isANZCE = strANZNav.indexOf("Windows CE");
if(isANZCE!=-1)isPDA=true;
isANZCE = strANZNav.indexOf("240x320");
if(isANZCE!=-1)isPDA=true;
var nzpdamessage = "We are sorry, but we cannot accept online bookings from Pocket PC's at this time.\n\nPlease call\nAir New Zealand Reservations on\n0800 737 000 ";
var aupdamessage = "We are sorry, but we cannot accept online bookings from Pocket PC's at this time.\n\nPlease call\nAir New Zealand Reservations on\n13 24 76 ";



/**
 * Domestic codes and locations
 */
var 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"],
  ["NPE", "Napier"],
  ["NPE", "Hastings"],
  ["NSN", "Nelson"],
  ["NPL", "New Plymouth"],
  ["OAM", "Oamaru"],
  ["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
	 */
var	tasmanLocations = [
  	["ADL", "Adelaide"],
	["ASP", "Alice Springs"],
	["BNE", "Brisbane"],
	["CNS", "Cairns"],
	["CBR", "Canberra"],
	["CFS", "Coffs Harbour"],
	["DRW", "Darwin"],
	["HBA", "Hobart"],
	["LST", "Launceston"],
	["MEL", "Melbourne"],
	["PER", "Perth"],
	["SYD", "Sydney"],
	["TSV", "Townsville"]];  
	/**
	 * 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"],
	["VLI","Port Vila"],
	["VLI","Vanuatu"]];
/* Australia locations as used by competition popup 18/07/05 */
var	auLocations = [
  	["ADL", "Adelaide"],
	["ASP", "Alice Springs"],
	["BNE", "Brisbane"],
	["CNS", "Cairns"],
	["CBR", "Canberra"],
	["CFS", "Coffs Harbour"],
	["DRW", "Darwin"],
	["HBA", "Hobart"],
	["LST", "Launceston"],
	["MEL", "Melbourne"],
	["PER", "Perth"],
	["SYD", "Sydney"],
	["TSV", "Townsville"]
  ];
  
/**
 * 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";
var formFieldMapping = [	  
      ["text", "departure_point", "depart_loc"],
      ["text", "arrival_point", "arrival_loc"],
      ["select", "departure_date", "depart_date"],
      ["select", "departure_month", "depart_yearmonth"],
      ["select", "arrival_date", "arrival_date"],
      ["select", "arrival_month", "arrival_yearmonth"],
      ["select", "adults", "adults"],
      ["select", "children", "children"],
      ["select", "infants", "infants"],
      ["select", "service_class", "service_class"],
      ["select", "locale", "locale"],
      ["select", "fare_type", "fare_type"],
      ["select", "searchType", "searchType"]
    ];

/**
 * Service classes
 */

var serviceClassesGetThere = [
      ["coach", "Economy - lowest"],
      ["coach", "Economy"],
      ["business", "Business"],
      ["first", "First"]
    ]	
	
var serviceClassesiTD = [
      ["smartsaver", "Lowest Available"],
      ["flexisaver", "Flexi Saver Fare"],
      ["fullyflexi", "Fully Flexi Fare"]	  	  
    ]
	
var serviceClassesiTDtasman = [
      ["smartsaver", "Lowest Available"],
      ["flexisaver", "Flexi Saver"],
      ["fullyflexi", "Fully 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;
		}
	}

/**
 * Populates the appropriate hidden form with FFF values
 *
 * @param formIndex The form index
 */
function mapFormFields(formIndex) {
  var formFrom = document.forms["frmVisible"];
  //var formTo = document.forms[formNames[formIndex - 1]];
  var formTo = document.frmGetThere;
  var fieldFrom = "";
  var fieldTo = "";
  var sFromValue = "";
  var sURL = "";
  var sDepartDate = "";
  var sArrivalDate = "";

  // Map visible fields to booking engine form fields
  var i = 0;
  do {
    fieldFrom = formFieldMapping[i][1];
    fieldTo = formFieldMapping[i][formIndex + 1];
	if (typeof(formFrom.elements[fieldFrom])=="object"){
		if (formFrom.elements[fieldFrom].name != null){
			if (formFieldMapping[i][0] == fieldTypeText) {
				 sFromValue = formFrom.elements[fieldFrom].value;
			} else if (formFieldMapping[i][0] == fieldTypeSelect) {	
				if(navigator.vendor=="Apple Computer, Inc.")
				{
					sFromValue = formFrom.elements[fieldFrom].value;

				}else{	
					try
					{			
						sFromValue = formFrom.elements[fieldFrom].options[formFrom.elements[fieldFrom].selectedIndex].value;
					}
					catch (ex) {}
				}

				switch (formFieldMapping[i][2])
				{
					case "depart_date":
						sDepartDate = formFrom.elements[fieldFrom].value;
						break;
					case "arrival_date":
						sArrivalDate = formFrom.elements[fieldFrom].value;
						break;
					case "depart_yearmonth":
						sFromValue = getYearMonthFormat(formatMonthForiTD(sFromValue), sDepartDate);	
						break;
					case "arrival_yearmonth":
						sFromValue = getYearMonthFormat(formatMonthForiTD(sFromValue), sArrivalDate);
						break;
					case "locale":
						sFromValue = formFrom.elements[fieldFrom].value;
						if (sFromValue == "")
						{
							sFromValue = "en_NZ";
						}
						break;
					case "searchType":
						sFromValue = formFrom.elements[fieldFrom].value;
						if (sFromValue == "")
						{
							sFromValue = "fixed";
						}
						break;
					case "infants":
						sFromValue = formFrom.elements[fieldFrom].value;
						if (sFromValue == "")
						{
							sFromValue = "0";
						}
						break;
					case "fare_type":
						sFromValue = formFrom.elements[fieldFrom].value;
						if (sFromValue == "" || sFromValue == null)
						{
							sFromValue = "return";
						}
						break;
				}
			}
		}
	}
	else
	{
		switch (formFieldMapping[i][2])
		{
			case "locale":
				sFromValue = "en_NZ";
				break;
			case "fare_type":
				sFromValue = "return";
				break;
				break;
			case "searchType":
				sFromValue = "fixed";
				break;
				break;
			case "infants":
				sFromValue = "0";
				break;
		}
	}
    sURL += formFieldMapping[i][2] + "=" + sFromValue + "&";
  
    i++;
  } while (i < formFieldMapping.length);

  return sURL;
}

/**
 * Refreshes the service class drop-down combo box
 */
function refreshServiceClass() {

  // Get the form index based on departure and arrival points
  getBookingFormIndex( document.forms["frmVisible"].elements["departure_point"],
                                   document.forms["frmVisible"].elements["arrival_point"] );

  var theForm = document.forms[formNames[formIndex - 1]];
  
  // Delete all items in service class list
  var theList = document.forms["frmVisible"].elements["service_class"];
  var i = theList.options.length - 1;
  while (i > -1) {
    theList.options[i] = null;
    i--;
  }

  // Update service class list
  if (bookingtype == "GetThere") {
    for (var i = 0; i < serviceClassesGetThere.length; i++) {
      theList.options[i] = new Option( serviceClassesGetThere[i][1],
                                       serviceClassesGetThere[i][0] );
    }
  } else if ((bookingtype == "iTD")||(bookingtype == "iTDnoairport")) {
    for (var i = 0; i < serviceClassesiTD.length; i++) {
      theList.options[i] = new Option( serviceClassesiTD[i][1],
                                       serviceClassesiTD[i][0] );
    }
  } else {
    for (var i = 0; i < serviceClassesiTDtasman.length; i++) {
      theList.options[i] = new Option( serviceClassesiTDtasman[i][1],
                                       serviceClassesiTDtasman[i][0] );
    }
  }
  theList.options[0].selected = true;
}




/**
 * Submits the form
 */
function AirNZ_submitForm() {
  var sURL = "";

  // Map form fields to booking engine form fields
  sURL =  "https://redirect.airnewzealand.com/redirectweb/servlet/FareDirector" + "?finder_type=FFF&" + mapFormFields(1);
  // Submit form
  anz_popWindow(sURL,'iTD','scrollbars=yes,resizable=yes,toolbar=yes,location=yes,menubar=yes,status=yes');

  /*
  if (isTasman(document.forms["frmVisible"].elements["departure_point"].value, document.forms["frmVisible"].elements["arrival_point"].value))
  {
  	oPopup = window.open('http://erequest.airnz.co.nz/nz/win200.htm','ImportantInformation','height=260,width=344,scrollbars=no,resizable=yes')
  }
  */
}

function anz_popWindow(theURL,winName,features) {
  mywin = window.open(theURL,winName,features);
  return mywin;
}

function isTasman(departurePoint, arrivalPoint) 
{
	return (isNZ(departurePoint) && isAustralia(arrivalPoint));
}

function setMonthToCurrent() {
	var tDate = new Date();
	tDate.setDate(tDate.getDate()+1);
	var monthOffset = (tDate.getMonth());	
	var dateoffset = (tDate.getDate());
	document.frmVisible.elements["departure_month"].selectedIndex = monthOffset;
	document.frmVisible.elements["departure_date"].selectedIndex = dateoffset - 1;
	tDate.setDate(tDate.getDate()+1);
    monthOffset = (tDate.getMonth());	
	dateoffset = (tDate.getDate());
	document.frmVisible.elements["arrival_month"].selectedIndex = monthOffset;
	document.frmVisible.elements["arrival_date"].selectedIndex = dateoffset - 1;
}

function formatMonthForiTD(theMonth){
	//months for iTD are numeric (e.g. 4 for APR)
	var i = 0;
	do {
		if(arrMonths[i][0] == theMonth.toUpperCase()) 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 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 isNZ(sLocation)
{
	var i=0;			

	do {
		// Check if location is tasman
		if ((sLocation.toLowerCase() == domesticLocations[i][0].toLowerCase()) ||
				(sLocation.toLowerCase() == domesticLocations[i][1].toLowerCase()))
		{
			return true;
		}
		i++;	
	} while (i < domesticLocations.length);
		
	return false;
}

function isAustralia(sLocation)
{
	var i=0;			

	do {
		// Check if location is in Australia
		if ((sLocation.toLowerCase() == auLocations[i][0].toLowerCase()) ||
				(sLocation.toLowerCase() == auLocations[i][1].toLowerCase()))
		{
			return true;
		}
		i++;	
	} while (i < auLocations.length);
		
	return false;
}