﻿// JScript File

// --------------------------------------------------------------------------------
// AR Start 31/12/2008 - Allow the flight destination to be specified automatically
// via a hyperlink url. Select the from airport, destination airport and resort
// --------------------------------------------------------------------------------
function GetQueryString(key, default_)
{
    // check to see if the specified query string parameter
    // has been added to the url. if it does exist, return
    // the value
    if (default_ == null)
        default_="";
    key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if(qs == null)
        return default_;
    else
        return qs[1];
}

function RemoveSpacesAndLower(s)
{
    // change the value to lowercase
    s = s.toLowerCase();

    // replace in firefox, not ie
    s = s.replace(/^\s+|\s+$/g,''); 
    
    // replace in ie
    var result = '';
    for (var i = 0; i < s.length; i++)
        if (s.charCodeAt(i) != 160)
            result += s.charAt(i);
    return result;
}

function XssFilter(s)
{
    // decode an encoded url parameter
    s = unescape(s);
    
    // replace xss exploit variables
    s = s.replace(/[\"\'][\s]*javascript:(.*)[\"\']/g, "\"\"");
    s = s.replace(/script(.*)/g, "");    
    s = s.replace(/eval\((.*)\)/g, "");
    
    // using a whitelist for allowed ascii characters,
    // remove any character that is not allowed as a
    // valid character for the url
    var result = '';
    for (var i = 0; i < s.length; i++)
        if (s.charAt(i) == 'a' ||
            s.charAt(i) == 'b' ||
            s.charAt(i) == 'c' ||
            s.charAt(i) == 'd' ||
            s.charAt(i) == 'e' ||
            s.charAt(i) == 'f' ||
            s.charAt(i) == 'g' ||
            s.charAt(i) == 'h' ||
            s.charAt(i) == 'i' ||
            s.charAt(i) == 'j' ||
            s.charAt(i) == 'k' ||
            s.charAt(i) == 'l' ||
            s.charAt(i) == 'm' ||
            s.charAt(i) == 'n' ||
            s.charAt(i) == 'o' ||
            s.charAt(i) == 'p' ||
            s.charAt(i) == 'q' ||
            s.charAt(i) == 'r' ||
            s.charAt(i) == 's' ||
            s.charAt(i) == 't' ||
            s.charAt(i) == 'u' ||
            s.charAt(i) == 'v' ||
            s.charAt(i) == 'w' ||
            s.charAt(i) == 'x' ||
            s.charAt(i) == 'y' ||
            s.charAt(i) == 'z' ||
            s.charAt(i) == 'A' ||
            s.charAt(i) == 'B' ||
            s.charAt(i) == 'C' ||
            s.charAt(i) == 'D' ||
            s.charAt(i) == 'E' ||
            s.charAt(i) == 'F' ||
            s.charAt(i) == 'G' ||
            s.charAt(i) == 'H' ||
            s.charAt(i) == 'I' ||
            s.charAt(i) == 'J' ||
            s.charAt(i) == 'K' ||
            s.charAt(i) == 'L' ||
            s.charAt(i) == 'M' ||
            s.charAt(i) == 'N' ||
            s.charAt(i) == 'O' ||
            s.charAt(i) == 'P' ||
            s.charAt(i) == 'Q' ||
            s.charAt(i) == 'R' ||
            s.charAt(i) == 'S' ||
            s.charAt(i) == 'T' ||
            s.charAt(i) == 'U' ||
            s.charAt(i) == 'V' ||
            s.charAt(i) == 'W' ||
            s.charAt(i) == 'X' ||
            s.charAt(i) == 'Y' ||
            s.charAt(i) == 'Z' ||
            s.charAt(i) == '0' ||
            s.charAt(i) == '1' ||
            s.charAt(i) == '2' ||
            s.charAt(i) == '3' ||
            s.charAt(i) == '4' ||
            s.charAt(i) == '5' ||
            s.charAt(i) == '6' ||
            s.charAt(i) == '7' ||
            s.charAt(i) == '8' ||
            s.charAt(i) == '9' ||
            s.charAt(i) == '-' ||
            s.charAt(i) == ' ')
            result += s.charAt(i);
    
    // return the safe string
    return result;
}

function GetFromParameter()
{
    // get the from parameter, and filter it's content, then
    // return as a safe string
    var from = GetQueryString('from');
    if (from != null)
        return XssFilter(from);
    else
        return null;
}

function GetCountryParameter()
{
    // get the country name parameter, and filter it's content, then
    // return as a safe string
    var country = GetQueryString('destcountryname');
    if (country != null)
        return XssFilter(country);
    else
        return null;
}

function GetAirportNameParameter()
{
    // get the airport name parameter, and filter it's content, then
    // return as a safe string
    var airportName = GetQueryString('destairportname');
    if (airportName != null)
        return XssFilter(airportName);
    else
        return null;
}

function GetAirportIdParameter()
{
    // get the airport name parameter, and filter it's content, then
    // return as a safe string
    var airportId = GetQueryString('destairportid');
    if (airportId != null)
        return XssFilter(airportId);
    else
        return null;
}
// ------------------------------------------------------------------------------
// AR End 31/12/2008 - Allow the flight destination to be specified automatically
// via a hyperlink url. Select the from airport, destination airport and resort
// ------------------------------------------------------------------------------

function LoadResort(val)
{
    // --------------------------------------------------------------------------------
    // AR Start 31/12/2008 - Allow the flight destination to be specified automatically
    // via a hyperlink url. Select the from airport, destination airport and resort
    // --------------------------------------------------------------------------------
    if (GetFromParameter().length != 0)
    {
        // check that we are using a flight only search
        var flight = document.getElementById(includeClientId('btnFlight'));
        var dynamic = document.getElementById(includeClientId('btnDynamic'));
        var hotel = document.getElementById(includeClientId('btnHotel'));

        if (flight.checked || dynamic.checked || hotel.checked)
        {
            // find the specified departure in the drop down list box
            var departure = document.getElementById(includeClientId('ddlDeparture'));
            for (var i = 0; i < departure.length; i++)
            {
                // if found, select the item in the drop down list box, and quit
                if (RemoveSpacesAndLower(departure.options[i].text) == 
                    RemoveSpacesAndLower(GetFromParameter()))                   
                {
                    departure.selectedIndex = i;
                    break;
                }
            }
        }
    }
    if (GetCountryParameter().length != 0 &&
        GetAirportNameParameter().length != 0 &&
        GetAirportIdParameter().length != 0)
    {
        // check that we are using a flight only search
        var flight = document.getElementById(includeClientId('btnFlight'));
        var dynamic = document.getElementById(includeClientId('btnDynamic'));
        var hotel = document.getElementById(includeClientId('btnHotel'));

        if (flight.checked || dynamic.checked || hotel.checked)
        {
            // find the specified country in the drop down list box
            for (var i = 0; i < val.length; i++)
            {
                // if found, select the item in the drop down list box, and quit
                if (RemoveSpacesAndLower(val.options[i].text) == 
                    RemoveSpacesAndLower(GetCountryParameter()))
                {
                    val.selectedIndex = i;
                    break;
                }
            }
        }
    }
    // ------------------------------------------------------------------------------
    // AR End 31/12/2008 - Allow the flight destination to be specified automatically
    // via a hyperlink url. Select the from airport, destination airport and resort
    // ------------------------------------------------------------------------------
    
    var direction=document.getElementById(includeClientId("direction")).value;
    var Option = document.getElementById(includeClientId("searchOption")).value;
    if(Option==1)
    {
        LoadResortDropDown(val, false);
    }
    else
    {
        LoadResortDropDown(val, true);
    }    
    
    StoreCountryDropDownValue();
}

function LoadResortDropDown(val, usePointCode)
{
    pleaseWait( document.getElementById(includeClientId("ddlResorts")) );
    var direction = document.getElementById(includeClientId("direction")).value;
    
    var btnFlight = true;
    var btnDynamic = true;
    var btnHotel = true;
    if(document.getElementById(includeClientId("btnFlight")) != null)
    {
        btnFlight = document.getElementById(includeClientId("btnFlight")).checked;
    }

    if(document.getElementById(includeClientId("btnDynamic")) != null)
    {
        btnDynamic = document.getElementById(includeClientId("btnDynamic")).checked;
    }

    if(document.getElementById(includeClientId("btnHotel")) != null)
    {
        btnHotel = document.getElementById(includeClientId("btnHotel")).checked;
    }


    if (btnFlight)
    {    
        LoadAirports(val, usePointCode);
    }
    else 
    {
        if(direction == "Inbound")
        {
            LoadAirports(val, usePointCode);
        }
        else
        {
            LoadResorts(val, usePointCode);
        }
    }
}

function LoadCountry()
{
    var updateSelectedIndex = false;
    LoadCountryDropDown(updateSelectedIndex);  
}

function LoadCountryDropDown(updateSelectedIndex)
{
    // AR Start 30/05/2008 Bug: 5980 
    pleaseWait( document.getElementById(includeClientId("ddlCountries")) );
    // AR End 30/05/2008 Bug: 5980
    
    var departureCodes;
    if(document.getElementById(includeClientId("ddlDeparture")) != null)
    {
        var val = document.getElementById(includeClientId("ddlDeparture"));
        departureCodes = val.options[val.selectedIndex].value;
        document.getElementById(includeClientId("pointValue")).value = departureCodes;
    }
    else
    {
        departureCodes = document.getElementById(includeClientId("pointValue")).value;
    }
    //alert(departureCodes);
    LoadCountries(departureCodes, updateSelectedIndex);
}

function LoadCountryDropDownAndResortDropDown(updateSelectedIndex)
{
    var departureCodes;
    if(document.getElementById(includeClientId("ddlDeparture")) != null)
    {
        var val = document.getElementById(includeClientId("ddlDeparture"));
        departureCodes = val.options[val.selectedIndex].value;
        document.getElementById(includeClientId("pointValue")).value = departureCodes;
    }
    else
    {
        departureCodes = document.getElementById(includeClientId("pointValue")).value;
    }
    
    LoadCountries(departureCodes, updateSelectedIndex);
}



function LoadCountries(departureCodes, updateSelectedIndex)
{
    //alert("updateSelectedIndex="+updateSelectedIndex);
    if (updateSelectedIndex == false)
    {
      var countryNameSelected = document.getElementById(includeClientId("countryName"));
      countryNameSelected.value = "";
    }

    //alert('LoadCountries');
    var directOnly = true;
    if(document.getElementById(includeClientId("cbDirectOnly")) != null)
    {
        directOnly = document.getElementById(includeClientId("cbDirectOnly")).checked;
    }
    
    var Option = document.getElementById(includeClientId("searchOption")).value;
    var direction = document.getElementById(includeClientId("direction")).value;
    

    var Inbound = false;
    if (direction == "Inbound") Inbound = true;
    
    if(Option == 1)
    {
      directOnly = false;
    }
    
    //alert(directOnly);
    var portalPath = document.getElementById(includeClientId("portalPath")).value;
    Noviatech.Modules.NewNTSearch.ViewNewNTSearch.GetCountries(portalPath, departureCodes, directOnly, Inbound, LoadCountries_CallBack);
    //hideResorts(true);
}

function LoadCountries_CallBack(response)
{
     //if the server-side code threw an exception
     if (response.error != null)
     {
      //we should probably do better than this
      // RG 09/04/08 alert("Country Call Back Error " + response.error); 
      return;
     }
    
     //alert("LoadCountries_CallBack");
     var countries = response.value;  
     //if the response wasn't what we expected  
     if (countries == null || typeof(countries) != "object")
     {
      return;
     }
     
     //alert("country count = " + countries.Rows.length);
     
     //Get the cities drop down
     var countryList = document.getElementById(includeClientId("ddlCountries"));
     
     //RG Start 29/04/09 Insurance does not have this stuff but still calls this file
     if (countryList == null)
     {
     return;
     }
     //RG End 29/04/09 Insurance does not have this stuff but still calls this file
     
     countryList.options.length = 0; //reset the countries dropdown
     
      //note that this is JavaScript casing and the L in length is lowercase for arrays
      if (countries.Rows.length == 0)
      {
        // countryList.options[countryList.options.length] = new Option("please wait...", "0");       
    
        // AR Start 30/05/2008 Bug: 5980 
        countryList.options[countryList.options.length] = new Option("No Available Countries", "0");       
        // AR End 30/05/2008 Bug: 5980
      }
      else
      {
          if (countries.Rows.length > 1)
          {
            countryList.options[countryList.options.length] = new Option("please select...", "0"); 
          }
          for (var i = 0; i < countries.Rows.length; ++i)
          {
            countryList.options[countryList.options.length] = new Option(countries.Rows[i].Name, countries.Rows[i].Code);       
          }  
      }
      var countrySelected = document.getElementById(includeClientId("countrySelected"));
      var countryNameSelected = document.getElementById(includeClientId("countryName"));
      //alert("LoadCountryDropDown-NameSelected= " + document.getElementById(includeClientId("countryName")).value);
      var n = 0;
      for(n=0;n<countryList.options.length;n++)
      {
        if (countryNameSelected.value == countryList.options[n].text)
        {
            countryList.selectedIndex = n;
            break;
        }
      }
     StoreCountryDropDownValue();   
     setResortDropDown();
}

function LoadAirports(countries, usePointCode)
{

    var index = countries.selectedIndex;

    if(index > -1)
    {
        var countryId = countries.options[countries.selectedIndex].text;
        var portalPath = document.getElementById(includeClientId("portalPath")).value;
        var pointValue = '';
        if (usePointCode)
        {
            pointValue = document.getElementById(includeClientId("pointValue")).value;
        }

        var directOnly = true;
        if(document.getElementById(includeClientId("cbDirectOnly")) != null)
        {
            directOnly = document.getElementById(includeClientId("cbDirectOnly")).checked;
        }
        
        var Option = document.getElementById(includeClientId("searchOption")).value;
        var direction = document.getElementById(includeClientId("direction")).value;
        var Inbound = false;
        if (direction == "Inbound") Inbound = true;

        if(Option == 1)
        {
          directOnly = false;
        }
        if(index > 0)
        {
            hideResorts(false);
        }
        else
        {
// RG Start 28/03/08 bug not showing resorts when only 1 country is listed        
//            hideResorts(true);
            if (countryId == "please select...")
            {
                hideResorts(true);
            }
            else
            {
                hideResorts(false);
            }
            
// RG End 28/03/08 bug not showing resorts when only 1 country is listed   
        }
        Noviatech.Modules.NewNTSearch.ViewNewNTSearch.GetAirports(countryId, portalPath, pointValue, directOnly, Inbound, LoadAirports_CallBack);
    }
}

function LoadAirports_CallBack(response)
{
     //if the server-side code threw an exception
     if (response.error != null)
     {
      //we should probably do better than this
      // RG 09/04/08 alert("Airport Call Back Error " + response.error); 
      return;
     }

     var cities = response.value;  
     //if the response wasn't what we expected  
     if (cities == null || typeof(cities) != "object")
     {
      return;
     }

     //alert("airport count = " + cities.Rows.length);


     //Get the cities drop down
     var citiesList = document.getElementById(includeClientId("ddlResorts"));
     citiesList.options.length = 0; //reset the cities dropdown

      //note that this is JavaScript casing and the L in length is lowercase for arrays
      if (cities.Rows.length == 0)
      {
        // citiesList.options[citiesList.options.length] = new Option("please wait...", "0");       
        
        // AR Start 30/05/2008 Bug: 5980 
        citiesList.options[citiesList.options.length] = new Option("No Available Airports", "0");       
        // AR End 30/05/2008 Bug: 5980
        
        // --------------------------------------------------------------------------------
        // AR Start 31/12/2008 - Allow the flight destination to be specified automatically
        // via a hyperlink url. Select the from airport, destination airport and resort
        // --------------------------------------------------------------------------------
        if (GetCountryParameter().length != 0 &&
            GetAirportNameParameter().length != 0 &&
            GetAirportIdParameter().length != 0)
        {
            var flight = document.getElementById(includeClientId('btnFlight'));
            
            if (flight.checked)
            {
                try
                {
                    for(var i = citiesList.options.length - 1; i >= 0; i--)
                        citiesList.remove(i);
                    citiesList.options[0] = new Option("please select...", "0");
                    citiesList.options.add(new Option(
                    GetAirportNameParameter() + " (" +
                    GetAirportIdParameter() + ")", 
                    GetAirportIdParameter()));
                    citiesList.options.selectedIndex = 1;
                }
                catch (ex)
                { 
                    alert('Error: Could not decode airport and code! Please call help desk');
                }
            }
        }
        // --------------------------------------------------------------------------------
        // AR End 31/12/2008 - Allow the flight destination to be specified automatically
        // via a hyperlink url. Select the from airport, destination airport and resort
        // --------------------------------------------------------------------------------
      }
      else
      {
          if (cities.Rows.length > 1)
          {
            citiesList.options[citiesList.options.length] = new Option("please select...", "0");       
          }
          for (var i = 0; i < cities.Rows.length; ++i)
          {
            citiesList.options[citiesList.options.length] = new Option(cities.Rows[i].Name, cities.Rows[i].Code);       
          }  
          
        // --------------------------------------------------------------------------------
        // AR Start 31/12/2008 - Allow the flight destination to be specified automatically
        // via a hyperlink url. Select the from airport, destination airport and resort
        // --------------------------------------------------------------------------------
        if (GetCountryParameter().length != 0 &&
            GetAirportNameParameter().length != 0 &&
            GetAirportIdParameter().length != 0)
        {
            // check that we are using a flight only search
            var flight = document.getElementById(includeClientId('btnFlight'));
            
            if (flight.checked)
            {
                // find the specified resort in the drop down list box
                var resorts = document.getElementById(includeClientId("ddlResorts"));
                for (var i = 0; i < resorts.length; i++)
                {
                    // if found, select the item in the drop down list box, and quit
                    if (RemoveSpacesAndLower(resorts.options[i].text) == 
                        RemoveSpacesAndLower(unescape(
                        GetAirportNameParameter() + " (" + 
                        GetAirportIdParameter() + ")")))
                    {
                        resorts.selectedIndex = i;
                        break;
                    }
                }
            }
        }
        // ------------------------------------------------------------------------------
        // AR End 31/12/2008 - Allow the flight destination to be specified automatically
        // via a hyperlink url. Select the from airport, destination airport and resort
        // ------------------------------------------------------------------------------
      }

      StoreResortDropDownValue();

      var resortSelected = document.getElementById(includeClientId("resortSelected"));

//alert(resortSelected.value);
//      var n = 0;
//      for(n=0;n<citiesList.options.length;n++)
//      {
//          if (resortSelected.value == citiesList.options[n].value)
//          {
//              citiesList.selectedIndex = n;
//              StoreResortDropDownValue();
//              break;
//          }
//      }
}

function LoadResorts(countries, usePointCode)
{
    var index = countries.selectedIndex;

    if (index > -1)
    {   
        var countryId = countries.options[index].text;
        
        var portalPath = document.getElementById(includeClientId("portalPath")).value;
        var pointValue = '';
        if (usePointCode)
        {
            pointValue = document.getElementById(includeClientId("pointValue")).value;
        }
        var directOnly = document.getElementById(includeClientId("cbDirectOnly")).checked;
        var Option = document.getElementById(includeClientId("searchOption")).value;

        var direction = document.getElementById(includeClientId("direction")).value;
        var Inbound = false;
        if (direction == "Inbound") Inbound = true;

        if(Option == 1)
        {
          directOnly = false;
        }
    //    hideCities(true);
    //    hideAirports(true);
        if (index == 0)
        {
// RG Start 28/03/08 bug not showing resorts when only 1 country is listed        
//            hideResorts(true);
            if (countryId == "please select...")
            {
                hideResorts(true);
            }
            else
            {
                hideResorts(false);
            }
            
// RG End 28/03/08 bug not showing resorts when only 1 country is listed        
        }
        else
        {
            hideResorts(false);
        }
        
        Noviatech.Modules.NewNTSearch.ViewNewNTSearch.GetAccommodationResorts(countryId, portalPath, pointValue, directOnly, Inbound, LoadResorts_CallBack);
    }
}

function LoadResorts_CallBack(response)
{


     //if the server-side code threw an exception
     if (response.error != null)
     {
      //we should probably do better than this
      // RG 09/04/08 alert("Resort Call Back Error " + response.error); 
      return;
     }

     var cities = response.value;  
     //if the response wasn't what we expected  
     if (cities == null || typeof(cities) != "object")
     {
      return;
     }

     //alert("resort count = " + cities.Rows.length);


     //Get the cities drop down
     var citiesList = document.getElementById(includeClientId("ddlResorts"));
     citiesList.options.length = 0; //reset the cities dropdown
//alert("cities.Rows.length" + cities.Rows.length);
      //note that this is JavaScript casing and the L in length is lowercase for arrays
                
      if (cities.Rows.length == 0)
      {
        // citiesList.options[citiesList.options.length] = new Option("please wait...", "0");       

    if (GetCountryParameter().length == 0 &&
        GetAirportNameParameter().length == 0 &&
        GetAirportIdParameter().length == 0)        
        {
        // AR Start 30/05/2008 Bug: 5980 
        citiesList.options[citiesList.options.length] = new Option("No Available Resorts", "0"); 
        // AR End 30/05/2008 Bug: 5980
        }
      }
      else
      {
          if (cities.Rows.length > 1)
          {
            citiesList.options[citiesList.options.length] = new Option("please select...", "0");       
          }
          for (var i = 0; i < cities.Rows.length; ++i)
          {
            citiesList.options[citiesList.options.length] = new Option(cities.Rows[i].Name, cities.Rows[i].Code);       
          }  
          
                  // --------------------------------------------------------------------------------
        // AR Start 31/12/2008 - Allow the flight destination to be specified automatically
        // via a hyperlink url. Select the from airport, destination airport and resort
        // --------------------------------------------------------------------------------
        if (GetCountryParameter().length != 0 &&
            GetAirportNameParameter().length != 0 &&
            GetAirportIdParameter().length != 0)
        {
            var dynamic = document.getElementById(includeClientId('btnDynamic'));
            var hotel = document.getElementById(includeClientId('btnHotel'));
            
            if (dynamic.checked || hotel.checked)
            {
                // find the specified resort in the drop down list box
                var resorts = document.getElementById(includeClientId("ddlResorts"));
                for (var i = 0; i < resorts.length; i++)
                {
                    // if found, select the item in the drop down list box, and quit
                    if (RemoveSpacesAndLower(resorts.options[i].text) == 
                        RemoveSpacesAndLower(unescape(
                        GetAirportNameParameter())))
                    {
                        resorts.selectedIndex = i;
                        break;
                    }
                }
            }
        }
        // --------------------------------------------------------------------------------
        // AR End 31/12/2008 - Allow the flight destination to be specified automatically
        // via a hyperlink url. Select the from airport, destination airport and resort
        // --------------------------------------------------------------------------------
        else
        {
          var resortSelected = document.getElementById(includeClientId("resortSelected"));
          //alert("LoadResorts_CallBack" + resortSelected.value);
          var n = 0;
          for(n=0;n<citiesList.options.length;n++)
          {
              if (resortSelected.value == citiesList.options[n].value)
              {
                  citiesList.selectedIndex = n;
                  break;
              }
          }
        
        }  
      }
      StoreResortDropDownValue();
      var resortSelected = document.getElementById(includeClientId("resortSelected"));
}

function SettingsResortList(countries)
{
    //used to return the resorts for the settings page of the module
    var index = countries.selectedIndex;
    if (index > -1)
    {   
        var countryId = countries.options[index].text;
        var portalPath = document.getElementById(includeClientId("portalPath")).value;
        Noviatech.Modules.NewNTSearch.Settings.GetAccommodationResorts(countryId, portalPath, "", false, false, SettingsResortList_CallBack);
    }   
}

function SettingsResortList_CallBack(response)
{
     //if the server-side code threw an exception
     if (response.error != null)
     {
      //we should probably do better than this
      // RG 09/04/08 alert("Settings Resort Call Back Error " + response.error); 
      return;
     }

     var cities = response.value;  
     //if the response wasn't what we expected  
     if (cities == null || typeof(cities) != "object")
     {
      return;
     }

     //Get the cities drop down
     var citiesList = document.getElementById(includeClientId("ddlResorts"));
     citiesList.options.length = 0; //reset the cities dropdown
      //note that this is JavaScript casing and the L in length is lowercase for arrays
      if (cities.Rows.length == 0)
      {
        //citiesList.options[citiesList.options.length] = new Option("please wait...", "0");       
        
        // AR Start 30/05/2008 Bug: 5980 
        citiesList.options[citiesList.options.length] = new Option("No Available Resorts", "0");       
        // AR End 30/05/2008 Bug: 5980
      }
      else
      {
          if (cities.Rows.length > 1)
          {
            citiesList.options[citiesList.options.length] = new Option("please select...", "0");       
          }
          for (var i = 0; i < cities.Rows.length; ++i)
          {
            citiesList.options[citiesList.options.length] = new Option(cities.Rows[i].Name, cities.Rows[i].Code);       
          }  
      }    
}

function StoreResortDropDownValue()
{
     var resortSelected = document.getElementById(includeClientId("resortSelected"));
     var resort = document.getElementById(includeClientId("ddlResorts"));
     resortSelected.value = resort.options[resort.selectedIndex].value;

     
    //    <script>LoadCountryDropDown(true);</script>

     if (resort.options.length > 1)
     {
         if (resort.options[resort.selectedIndex].value != '0')
         {
         resortSelected.value = resort.options[resort.selectedIndex].value;
         }
         else
         {
         resortSelected.value = "";
         }
     }
     else
     {
         resortSelected.value = resort.options[0].value;
     }

}


function StoreCountryDropDownValue()
{
     var countrySelected = document.getElementById(includeClientId("countrySelected"));
     var countryName = document.getElementById(includeClientId("countryName"));
     var country = document.getElementById(includeClientId("ddlCountries"));

     if (country.selectedIndex > -1)
     {
     countrySelected.value = country.options[country.selectedIndex].value;
     countryName.value = country.options[country.selectedIndex].text;
     }
     else
     {
     countrySelected.value = "";
     countryName.value = "";
     }
}

function StoreCountryAndResortDropDownValues()
{
    StoreCountryDropDownValue();
    StoreResortDropDownValue();
}

function hideResorts(flag)
{
     //Get the airports drop down
     var List = document.getElementById(includeClientId("ddlResorts"));
     if (flag == true)
     {
        List.style.visibility = "hidden";
        List.className = "zeroWidth";
     }
     else
     {
        List.style.visibility = "visible";
        var moduleFormat = document.getElementById(includeClientId('moduleFormat')).value
        if(moduleFormat=="Horizontal")
        {
            List.style.width="187px";
        }
        else if(moduleFormat=="HorizontalRedirect"){
            // do nothing! we want the css to define this... naturally!
            //List.style.width="161px"; 
        }
        else
        {
            List.style.width="137px";
        }
     }
}

function hideDynamicDropDowns()
{
     var Cities = document.getElementById(includeClientId("ddlCities"));
     var Countries = document.getElementById(includeClientId("ddlCountries"));
     var obj = document.getElementById(includeClientId("btnFlight"));
    if(Countries.value == "")
    {
       hideResorts(true);        
    }
}

function inboundChangeSearchOption(option)
{
    changeOption(option,1);         
    document.getElementById(includeClientId("lblDirectOnly")).style.visibility = "hidden";
    document.getElementById(includeClientId("cbDirectOnly")).style.visibility = "hidden";
}


    function changeOption(option, direction)
    {
        // AR Start 25/07/2008 - Show the return date that was hidden by the 
        // checking of the oneway checkbox
// RG Start 22/08/08
//        document.getElementById(includeClientId("chkOneWay")).checked = false;
//        document.getElementById(includeClientId("divReturnDate")).style.visibility = "visible";
//        document.getElementById(includeClientId("divReturnDate")).style.display = "block";

        var chkoneway = document.getElementById(includeClientId("chkOneWay"));
        
        if (chkoneway != null)
        {
            chkoneway.checked = false;
        }

        var divReturnDate = document.getElementById(includeClientId("divReturnDate"));
        showEl(divReturnDate);
        
        // alternative layout
        var fieldRetDt=document.getElementById("fieldRetDate");
        showEl(fieldRetDt);
        
        
// RG End 22/08/08        // AR End 25/07/2008
    
        var moduleFormat = document.getElementById(includeClientId('moduleFormat')).value;
        var roomText = "<b>How Many</b> rooms";
        var flightText = "<b>How Many</b> people";

        if(moduleFormat=="Horizontal")
        {
            roomText += " do you require?";
            flightText += " are travelling?";
        }

        var holder =document.getElementById("txtRooms");
        if (option == 0)
        {
            if(moduleFormat=="HorizontalRedirect")
            {
                //Certain Horizontal Redirect sites eg. bhx don't have the background image so ignore the error
                try
                {
                    changeBGImage(1);
                }
                catch(e)
                {
                }
            }
            // *** FLIGHT ONLY ***
            // AR Start 07/07/2008 - Advanced Search Options
            if (document.getElementById(includeClientId("chkOneWay")) != null &&
                document.getElementById(includeClientId("lblOneWay")) != null)
            {
                document.getElementById(includeClientId("chkOneWay")).style.visibility = "visible";
                document.getElementById(includeClientId("lblOneWay")).style.visibility = "visible";
            }
            // AR End 07/07/2008 - Advanced Search Options
            
            if (document.getElementById(includeClientId("ddlCountries")) != null)
            {
                document.getElementById(includeClientId("ddlCountries")).style.visibility = "visible";
            }                
            if (document.getElementById(includeClientId("ddlResorts")) != null)
            {
                document.getElementById(includeClientId("ddlResorts")).style.visibility = "visible";
            }                
            if (document.getElementById(includeClientId("lblTo")) != null)
            {
              document.getElementById(includeClientId("lblTo")).style.visibility = "visible";
            }
            if (document.getElementById(includeClientId("ddlDeparture")) != null)
            {
                document.getElementById(includeClientId("ddlDeparture")).style.visibility = "visible";
                document.getElementById(includeClientId("lblFrom")).style.visibility = "visible";
            }
            document.getElementById(includeClientId("lblDirectOnly")).style.visibility = "visible";
            document.getElementById(includeClientId("cbDirectOnly")).style.visibility = "visible";
            document.getElementById(includeClientId("ddlRooms")).style.visibility = "hidden";
            
            //RG Start 26/06/09 Fix to remove the wording for Rooms when the rooms ddl should not be shown
            var FreeText_Rooms1 = document.getElementById("FreeText_Rooms1");
            if (FreeText_Rooms1 != null)
            {
                document.getElementById("FreeText_Rooms1").style.visibility = "hidden";
            }

            var FreeText_Rooms2 = document.getElementById("FreeText_Rooms2");
            if (FreeText_Rooms2 != null)
            {
                document.getElementById("FreeText_Rooms2").style.visibility = "hidden";
            }
            //RG End 26/06/09 Fix to remove the wording for Rooms when the rooms ddl should not be shown


            /* null check needed!!! */
            if(holder!=null){
              holder.innerHTML = flightText;
            }

            if(moduleFormat=="HorizontalRedirect")
            {
                var labelRm=document.getElementById("labelRooms");
                //hideEl(labelRm);
                labelRm.style.visibility = "hidden";
            }

            //document.getElementById(includeClientId("lblRooms")).style.visibility = "hidden";
            ShowPaxSelection(0);
        }
        if (option == 1)
        {
            //accommodation only search           
            if(direction==1) //inbound
            {
                HideDestination();
         
            }

            // *** HOTEL ONLY ***
            // AR Start 07/07/2008 - Advanced Search Options
            if (document.getElementById(includeClientId("chkOneWay")) != null &&
                document.getElementById(includeClientId("lblOneWay")) != null)
            {
                document.getElementById(includeClientId("chkOneWay")).style.visibility = "hidden";
                document.getElementById(includeClientId("lblOneWay")).style.visibility = "hidden";
            }
            // AR End 07/07/2008 - Advanced Search Options

            if (document.getElementById(includeClientId("ddlDeparture")) != null)
            {
                document.getElementById(includeClientId("ddlDeparture")).style.visibility = "hidden";
                document.getElementById(includeClientId("lblFrom")).style.visibility = "hidden";
            }
            document.getElementById(includeClientId("ddlRooms")).style.visibility = "visible";
            //RG Start 26/06/09 Fix to remove the wording for Rooms when the rooms ddl should not be shown
            var FreeText_Rooms1 = document.getElementById("FreeText_Rooms1");
            if (FreeText_Rooms1 != null)
            {
                document.getElementById("FreeText_Rooms1").style.visibility = "visible";
            }

            var FreeText_Rooms2 = document.getElementById("FreeText_Rooms2");
            if (FreeText_Rooms2 != null)
            {
                document.getElementById("FreeText_Rooms2").style.visibility = "visible";
            }
            //RG End 26/06/09 Fix to remove the wording for Rooms when the rooms ddl should not be shown
            

            document.getElementById(includeClientId("lblDirectOnly")).style.visibility = "hidden";
            document.getElementById(includeClientId("cbDirectOnly")).style.visibility = "hidden";
            //document.getElementById(includeClientId("lblRooms")).style.visibility = "visible";              
            /* null check needed!!! */
            if(holder!=null){
                       holder.innerHTML = roomText;
            }
            if(moduleFormat=="HorizontalRedirect")
            {
                var labelRm=document.getElementById("labelRooms");
                //showEl(labelRm);
                labelRm.style.visibility = "visible";
                
                
                // alternative layout, msut show ret date
                var fieldRetDt=document.getElementById("fieldRetDate");
                showEl(fieldRetDt);
            } 
           ShowAdditionalPaxSelection();
        }
        if (option == 5)
        {
            // *** DYNAMIC CHOICE ***
            // AR Start 07/07/2008 - Advanced Search Options
            if (document.getElementById(includeClientId("chkOneWay")) != null && 
                document.getElementById(includeClientId("lblOneWay")) != null)
            {
                document.getElementById(includeClientId("chkOneWay")).style.visibility = "hidden";
                document.getElementById(includeClientId("lblOneWay")).style.visibility = "hidden";
            }                
            // AR End 07/07/2008 - Advanced Search Options

            if (document.getElementById(includeClientId("ddlCountries")) != null)
            {
                document.getElementById(includeClientId("ddlCountries")).style.visibility = "visible";
            }                
            if (document.getElementById(includeClientId("ddlResorts")) != null)
            {
                document.getElementById(includeClientId("ddlResorts")).style.visibility = "visible";
            }                
            if (document.getElementById(includeClientId("lblTo")) != null)
            {
              document.getElementById(includeClientId("lblTo")).style.visibility = "visible";
            }
            if (document.getElementById(includeClientId("ddlDeparture")) != null)
            {
                document.getElementById(includeClientId("ddlDeparture")).style.visibility = "visible";
                document.getElementById(includeClientId("lblFrom")).style.visibility = "visible";
            }
            document.getElementById(includeClientId("lblDirectOnly")).style.visibility = "visible";
            document.getElementById(includeClientId("cbDirectOnly")).style.visibility = "visible";
            document.getElementById(includeClientId("ddlRooms")).style.visibility = "visible";
            //RG Start 26/06/09 Fix to remove the wording for Rooms when the rooms ddl should not be shown
            var FreeText_Rooms1 = document.getElementById("FreeText_Rooms1");
            if (FreeText_Rooms1 != null)
            {
                document.getElementById("FreeText_Rooms1").style.visibility = "visible";
            }

            var FreeText_Rooms2 = document.getElementById("FreeText_Rooms2");
            if (FreeText_Rooms2 != null)
            {
                document.getElementById("FreeText_Rooms2").style.visibility = "visible";
            }
            //RG End 26/06/09 Fix to remove the wording for Rooms when the rooms ddl should not be shown
            
            //document.getElementById(includeClientId("lblRooms")).style.visibility = "visible";              
            /* null check needed!!! */
            if(holder!=null){
                        holder.innerHTML = roomText;
            }

            if(moduleFormat=="HorizontalRedirect")
            {
                var labelRm=document.getElementById("labelRooms");
                labelRm.style.visibility = "visible";
                
                            
                // alternative layout, msut show ret date
                var fieldRetDt=document.getElementById("fieldRetDate");
                showEl(fieldRetDt);
            } 


            ShowAdditionalPaxSelection();
        }           

        var oldOption = document.getElementById(includeClientId("searchOption")).value;
        document.getElementById(includeClientId("searchOption")).value = option;          

        // if the old option was a flight, or the new option is a flight, then we are changing the drop down selection.
        //alert("oldOption=" + oldOption + " option=" + option);
        if(document.getElementById(includeClientId("searchOption")).value.length > 0)
        {
            if((oldOption != option))
            {
                var usePointCode = true;
                //if the old option was accommodation only and the new option is a flight or flight+accommodation then change
                //the country list to show only the countries serviced by the departure point
                if ( (option==0 || option == 5) && oldOption == 1)
                {
                    //modify the country list to show the countries filtered on the departure airport routes
                    var departureCodes = document.getElementById(includeClientId("pointValue")).value;
                    LoadCountries(departureCodes, true);                
                }
                                
                //if the old option was a flight or flight+accommodation and the new option is accommodation only then change
                //the country list to reflect that any country can be used.
                if (option == 1)
                {
                    //modify the country list to show all countries as it does not need to filter on the departure airport routes
                    usePointCode = false;
                    LoadCountries('',false);                
                }
                
                var List = document.getElementById(includeClientId("ddlResorts"));
                if (List.style.visibility == "visible" || List.style.visibility == "")
                {
                    var Countries = document.getElementById(includeClientId("ddlCountries"));
                    LoadResortDropDown(Countries, usePointCode);           
                }
           }
//           document.getElementById(includeClientId("searchOption")).value = option;          
        }
    }
    function pleaseWait(obj)
    {
        //RG 29/04/09 Start check that this is not null which it can be for insurance
        if (obj != null)
        {
        obj.options.length = 1; //reset the cities dropdown
        obj.options[0] = new Option("please wait...", "-1");       
        }
        //RG 29/04/09 End check that this is not null which it can be for insurance
    }
    
    function initialiseRooms()
    {
      try{
          var option = document.getElementById(includeClientId("searchOption")).value;  
          changeOption(option);
      }catch(e){
//             var moduleFormat = document.getElementById(includeClientId('moduleFormat')).value;
//            
//            if(moduleFormat=="HorizontalRedirect")
//            {
//            alert(e);
//            }
 
 
      }
    }     
   
    function initialiseInboundRooms()
    {
      try{    
          var option = document.getElementById(includeClientId("searchOption")).value;   
          inboundChangeSearchOption(option);  
      }catch(e){
      }
 }     
               
    function setResortDropDown()
    {
        //alert('SetResortDropDown');
        //this is populated by AJAX so sdoes not exist in viewsate, it must be rebuilt every page load event
        var countryDD = document.getElementById(includeClientId("ddlCountries"));
        LoadResort(countryDD);
        if (countryDD.selectedIndex > -1)
        {
            if(countryDD.options[countryDD.selectedIndex].value == '' || countryDD.style.visibility == "hidden")
            {
                hideResorts(true);
            }
        }
    }
    
    function HideDestination()
    {
        document.getElementById(includeClientId("ddlCountries")).style.visibility = "hidden";
        document.getElementById(includeClientId("ddlResorts")).style.visibility = "hidden";
        document.getElementById(includeClientId("lblTo")).style.visibility = "hidden";

    }
    
    // AR Start 28/07/2008 - Hide and show the return date control, based on whether
    // the flight is one way or not
    function ShowHideReturnDate()
    {
    

        try{
        if (document.getElementById(includeClientId("chkOneWay")).checked)
        {
            document.getElementById(includeClientId("divReturnDate")).style.display = "none";
            document.getElementById(includeClientId("divReturnDate")).style.visibility = "hidden";
        }
        else
        {
            document.getElementById(includeClientId("divReturnDate")).style.display = "block";
            document.getElementById(includeClientId("divReturnDate")).style.visibility = "visible";
        }
        }catch(e){

        }
        
                // alternative layout
        try{
            var moduleFormat = document.getElementById(includeClientId('moduleFormat')).value;
            
            if(moduleFormat=="HorizontalRedirect")
            {
                var fieldRetDt=document.getElementById("fieldRetDate");
                            
                if (document.getElementById(includeClientId("chkOneWay")).checked)
                {
                    hideEl(fieldRetDt);
                }else{
                
                    showEl(fieldRetDt);
                }
            }
        }catch(ex){

        }
    }
    // AR End 28/07/2008
    


// let's try increasing the AjaxPro timeout    
if(typeof AjaxPro != "undefined" && AjaxPro !== null){
    //  let's got for a minute and a half to get the dropdowns.
    AjaxPro.timeoutPeriod = 90000;
        AjaxPro.onTimeout = function(b,req){
        //alert('Timeout');
        return true;   // apparently this means to keep waiting :) we could have a number of retries here.
        //  returning true means we will stop setting timeouts, that's why we choose a large enough initial timeout.
    }
}   


function changingRooms(numRooms){
		var moduleFormat = document.getElementById(includeClientId('moduleFormat')).value
		switch(numRooms){
			case "1":
				hideEl(document.getElementById("room2Row"));
				hideEl(document.getElementById("room3Row"));
                if(moduleFormat=="HorizontalRedirect" || moduleFormat=="VerticalRedirect")
                {
                    try
                    {
                        changeBGImage(1);
                    }
                    catch(e)
                    {
                    }
                }
				break;
			case "2":
				showEl(document.getElementById("room2Row"));
				hideEl(document.getElementById("room3Row"));
                if(moduleFormat=="HorizontalRedirect" || moduleFormat=="VerticalRedirect")
                {
                    try
                    {
                        changeBGImage(2);
                    }
                    catch(e)
                    {
                    }
    			}
				break;			
			case "3":
				showEl(document.getElementById("room2Row"));
  			    showEl(document.getElementById("room3Row"));
                if(moduleFormat=="HorizontalRedirect" || moduleFormat=="VerticalRedirect")
                {
                    try
                    {
                        changeBGImage(3);
                    }
                    catch(e)
                    {
                    }
                }
				break;
			default:
				hideEl(document.getElementById("room2Row"));
				hideEl(document.getElementById("room3Row"));
				break;
		}
}

function showEl(el){
    
    
    try{
        if (el != null)
        {
            el.style.visibility="visible";
            el.style.display="block";
        }
    }catch(ex){
    }
}

function hideEl(el){
    try{
        el.style.visibility="hidden";
        el.style.display="none";
    }catch(ex){
    }
}


function toggleEl(el){
    try{
        if(el.style.visibility=="hidden"){
            showEl(el);
        }else{
            hideEl(el);
        }
    }catch(ex){
    }
}

