/*****************************************************/
/* EKBookingWidget Class                             */
/*****************************************************/

// Instantiate EKBookingWidget namespace and initialize
function EKBookingWidget() 
{
    // Variable declarations
    var jInput, oDropdown1, oDropdown2, oDropdown3, oProvider1, oProvider2, oProvider3; 

    // Obtain all document elements the bookingwidget script interacts with
    EKBookingWidget.ctrlReturn     = document.getElementById("chkReturn");
    EKBookingWidget.ctrlCalReturn  = document.getElementById("calReturn");
    EKBookingWidget.ctrlReturnDate = document.getElementById("seladate1");
    EKBookingWidget.ctrlOneWay     = document.getElementById("seladate1OW");
    
    EKBookingWidget.ctrlPriceFlex  = document.getElementById("rdoByPriceFlex");
    EKBookingWidget.ctrlByPrice    = document.getElementById("rdoByPrice");    
    EKBookingWidget.ctrlPriceList  = document.getElementById("resultby");
    
    EKBookingWidget.ctrlAdults     = document.getElementById("seladults");
    EKBookingWidget.ctrlChildren   = document.getElementById("selchildren");
    EKBookingWidget.ctrlInfants    = document.getElementById("selinfants");    

    // Signal to IBE that JavaScript is enabled    
    jInput = document.getElementById("j");
    
    if (jInput)
        jInput.value = "t";
    
    // Initialize the valid traveller combinations
    EKBookingWidget.travellerChange();

    // Initialize the return checkbox to checked
    if (EKBookingWidget.ctrlReturn)
        EKBookingWidget.ctrlReturn.checked = true;
    
    // Keep the initial "ResultsBy" value and revert when needed
    EKBookingWidget.defaultResultsByPriceFlex = false;
    
    if (EKBookingWidget.ctrlPriceFlex!= null)
    {
        EKBookingWidget.defaultResultsByPriceFlex = EKBookingWidget.ctrlPriceFlex.checked;
    }
    else
    {
        if (EKBookingWidget.ctrlPriceList != null && EKBookingWidget.ctrlPriceList.options[EKBookingWidget.ctrlPriceList.selectedIndex].value == "2")
            EKBookingWidget.defaultResultsByPriceFlex = true;
    }
    
    // Check if this site is disabled for predictive search
	
    if (!IsValidSite())
        return ;
    function IsValidSite()
	{
		//alert();
		return false;
	}
    oDropdown1 = document.getElementById("seldcity1");
    
    if (oDropdown1)
    {
        oProvider1 = new EKXMLProvider(oDropdown1, sIBEDeparture, sIBEXMLUrl + "&mode=1");
        EKBookingWidget.oSuggest1  = new EKSuggest(oProvider1, false, true, true, "bookingSuggestions");
    }
    
    oDropdown2 = document.getElementById("seldcity2");
    
    if (oDropdown2)
    {
        oProvider2 = new EKXMLProvider(oDropdown2, sIBEDeparture, sIBEXMLUrl + "&mode=3");
        EKBookingWidget.oSuggest2  = new EKSuggest(oProvider2, false, true, true);
    }
    
    oDropdown3 = document.getElementById("selacity1");
    
    if (oDropdown3)
    {    
        oProvider3 = new EKXMLFilterProvider(oDropdown3, sIBEArrival, oProvider1.submitInput(), sIBEXMLUrl + "&mode=2", "/system/aspx/IBEAirports.aspx?c=");
        EKBookingWidget.oSuggest3  = new EKSuggest(oProvider3, false, true, true, "bookingSuggestions");
    }
}

// EKBookingWidget.switchTab
//
//  Switch to the tab clicked, deactivating all other tabs and their content
//
//      panelName:  Name of the tab panel to display
EKBookingWidget.switchTab = function(tab, panelName)
{
    var container, container2, panels, i, j, tabs, length, inputs;

    tab.removeAttribute("href");
            
    container = tab.parentNode.parentNode;
    
    if (container.tagName == "UL")
    {
        container.style.cursor = "default";
        container.onmousedown = function() { return false; };
        container.onselectstart = function() { return false; };
    
        tabs = container.getElementsByTagName("A");
        
        // De-activate all tabs
        for (i = 0, length = tabs.length; i < length; i++)
        {
            tabs[i].style.cursor = "pointer";
            tabs[i].className = "offState";        
        }
    }

    // Activate the clicked tab
    tab.className = "onState";

    if (EKBookingWidget.oSuggest1)
        EKBookingWidget.oSuggest1.hideSuggestions();
    if (EKBookingWidget.oSuggest2)
        EKBookingWidget.oSuggest2.hideSuggestions();
    if (EKBookingWidget.oSuggest3)
        EKBookingWidget.oSuggest3.hideSuggestions();

    container2 = container.parentNode.parentNode.parentNode;

    // Show the associated tabs content
    panels = container2.getElementsByTagName("DIV");
    
    // Switch the panels (only the ones with a defined ID)
    for (j = 0, length = panels.length; j < length; j++)
    {
        if (panels[j].id && panels[j].id.indexOf("part") != -1)
        {
            if (panels[j].id == panelName)
            {
                panels[j].style.display = "block";
            }
            else        
            {
                panels[j].style.display = "none"; 
            }    
        }
    }
    
    // Hide the popup calendar if its currently showing
    //EKCalendar.closeCalendar();    
}

// EKBookingWidget.travellerChange
//
//  Update the traveller combo-boxes according to the following rules
//
//  - Maximum of travellers is 9 for adults and children combined
//  - Each infant needs to have an accompanying adult (on-lap)
EKBookingWidget.travellerChange = function()
{
    var i, j, length;

    if (this.ctrlAdults && this.ctrlChildren && this.ctrlInfants)
    {   
        // The number of adults and children combined cannot exceed 9
        this.ctrlChildren.options.length = 0;
            
        for (i = 0, length = (9 - this.ctrlAdults.selectedIndex); i < length; i++)
        {
            this.ctrlChildren.options[this.ctrlChildren.options.length] = new Option (i, i);
        }
            
        // The number of infants cannot exceed the number of adults
        this.ctrlInfants.options.length = 0;
        
        for (j = 0, length = (this.ctrlAdults.selectedIndex + 2); j < length; j++)
        {
            this.ctrlInfants.options[this.ctrlInfants.options.length] = new Option (j, j);
        }
    }
}

// EKBookingWidget.returnStateChange
//
//  Update the related controls when the return state checkbox is clicked
EKBookingWidget.returnStateChange = function()
{
    var i;

    if (this.ctrlReturn.checked == true)
    {
        this.ctrlOneWay.style.display = "none";
        this.ctrlReturnDate.style.display = "block";
        this.ctrlCalReturn.style.cursor = "pointer";
            
        // Adjust the price listing if required
        // Check if we are in 'checkbox' mode
        if (this.ctrlPriceFlex != null)        
        {
            this.ctrlPriceFlex.disabled = false;
            
            if (this.defaultResultsByPriceFlex)
                this.ctrlPriceFlex.checked = true;
        }                
        
         if (this.ctrlPriceList != null && this.ctrlPriceList.options != null)
         {
            for (i = 0, length = this.ctrlPriceList.options.length; i < length; i++)
            {   
                if (this.ctrlPriceList.options[i].value == "2")
                {
                    this.ctrlPriceList.options[i].disabled = false;
                    
                    if (this.defaultResultsByPriceFlex)
                        this.ctrlPriceList.options[i].checked = true;
                }
            }
         }        
    }
    else
    {
        this.ctrlOneWay.style.display = "block";
        this.ctrlReturnDate.style.display = "none";
        this.ctrlCalReturn.style.cursor = "default";
    
        // Adjust the price listing if required
        // Check if we are in 'checkbox' mode
        if (this.ctrlPriceFlex != null)        
        {
            this.defaultResultsByPriceFlex = this.ctrlPriceFlex.checked;
        
            if (this.ctrlPriceFlex.checked == true)
            {
                this.ctrlPriceFlex.checked = false;
                this.ctrlByPrice.checked = true;
            }
            
            this.ctrlPriceFlex.disabled = true;
         }
         
         // Check if we are in 'pricelist' mode
         if (this.ctrlPriceList != null && this.ctrlPriceList.options != null)
         {
            for (i = 0, length = this.ctrlPriceList.options.length; i < length; i++)
            {   
                if (this.ctrlPriceList.options[ctrlPriceList.selectedIndex].value == "2" && this.ctrlPriceList.options[i].value == "0")
                    this.ctrlPriceList.selectedIndex = i;
                    
                if (this.ctrlPriceList.options[i].value == "2")
                    this.ctrlPriceList.options[i].disabled = true;
            }
         }
    }
}

EKBookingWidget();
