﻿/*----------------------------------------------------
Date Created		24/03/2008
Created By		    Tristan Mcvean
Description		    The following script is part
                    of Hotels4u.com new Search Panel
----------------------------------------------------*/


var divSearchTypeText, divSearchTypeList, hypChooseList, divAdvanced, hfSearchType, hfHolType;
var lblDestination, lblResort, divBuilding;
var uc, ucName;
var hfSmartSearchName, hfSmartSearchGatewayCode, hfSmartSearchValue, hfSmartSearchText, txtSmartSearch, smartsearch_choices, hfAdvanced, hypAdvancedSearch, hfAdvancedOnText, hfAdvancedOffText;
var txtBuilding;
var wText;

    function searchPanelScript(cid)
    {
        uc = cid + "_";
        ucName = uc.replace(/_/g, '$');

        wText = document.getElementById(uc + 'hfWaterMarkText').value
        
        //txtBuilding = document.getElementById(uc + 'txtBuilding');
        txtBuilding = document.getElementById(uc + 'txtSmartSearchHotel');
        
        lblDestination = document.getElementById(uc + 'lblDestination');
        lblResort = document.getElementById(uc + 'lblResort');
        divBuilding = document.getElementById(uc + 'divBuilding');
        hypChooseList = document.getElementById(uc + 'hypChooseList');

        //hfSearchType = document.getElementById(uc + 'hfSearchType');
        hfHolType = document.getElementById(uc + 'hfHolType');

        divSearchTypeText = document.getElementById(uc + 'divSearchTypeText');
        divSearchTypeList = document.getElementById(uc + 'divSearchTypeList');

        divAdvanced = document.getElementById(uc+'divAdvanced');
        hfAdvanced = document.getElementById(uc + 'hfAdvanced');
        hypAdvancedSearch = document.getElementById(uc + 'hypAdvancedSearch');
        hfAdvancedOnText = document.getElementById(uc + 'hfAdvancedOnText');
        hfAdvancedOffText = document.getElementById(uc + 'hfAdvancedOffText');

        hfSmartSearchName = document.getElementById(uc + 'hfSmartSearchName');
        hfSmartSearchGatewayCode = document.getElementById(uc + 'hfSmartSearchGatewayCode');
        hfSmartSearchValue = document.getElementById(uc + 'hfSmartSearchValue');
        hfSmartSearchText = document.getElementById(uc + 'hfSmartSearchText');

        txtSmartSearch = document.getElementById(uc + 'txtSmartSearch');
        smartsearch_choices = document.getElementById('smartsearch_choices');

        waterMarkBlur(txtSmartSearch);

        var path = location.pathname;

        if (path.indexOf('/WhiteLabel/') > 0)
            path = "/Sites/WhiteLabel/SmartSearchResults.aspx";
        else
            path = "/SmartSearchResults.aspx";
        
        
        //new Ajax.Autocompleter(uc + "txtSmartSearch", "autocomplete_choices", "SmartSearchResults.aspx", {paramName: 'value', indicator: '', afterUpdateElement : getSelectionId});
        new Ajax.Autocompleter(uc + "txtSmartSearch", "smartsearch_choices", path, { paramName: 'value', indicator: '', updateElement: addItemToList, afterUpdateElement: getSelectionId });
    
        //ShowHideAdvancedSearchOnStart();
    }

    function ShowFilterProgress() {
        if (document.getElementById("divProgress")) {
            document.getElementById("divFilterProgress").style.display = "";
            document.getElementById("divFilterProgress").style.visibility = "visible";
        }
    }

//  move to master.js TM
//    function SearchClicked() {
//        if (document.getElementById("divSearching")) {
//            document.getElementById("divSearching").style.display = '';
//        }
//    }
    
    function addItemToList(li)
    {
        if (li.firstChild.textContent != null)
        {
            txtSmartSearch.value = li.firstChild.textContent.trim();
            hfSmartSearchText.value = li.firstChild.textContent.trim();     
        }
        else
        {
            txtSmartSearch.value = li.firstChild.innerText;
            hfSmartSearchText.value = li.firstChild.innerText;
        }
        
        var arr = li.id.split('-');
        
        hfSmartSearchName.value = arr[0];
        hfSmartSearchGatewayCode.value = arr[1];
        hfSmartSearchValue.value = arr[2];
    }

    function getSelectionId(text, li) {

    }

    function testUpdate() {
        
        var test;

        try {

            li = smartsearch_choices.childNodes[0].childNodes[0];

            var arr = li.id.split('-');

            hfSmartSearchName.value = arr[0];
            hfSmartSearchGatewayCode.value = arr[1];
            hfSmartSearchValue.value = arr[2];

            if (li.firstChild.textContent != null) {
                hfSmartSearchText.value = li.firstChild.textContent;
            }
            else {
                hfSmartSearchText.value = li.firstChild.innerText.trim();
            }
        }
        catch (ex) {

        }
    }
    
    function changeDest(ev) {

        if (txtBuilding) {
            if (txtBuilding.value != '')
                txtBuilding.value = '';
        }
                    
        var KeyCode = ev.keyCode ? ev.keyCode : ev.which ? ev.which : ev.charCode;

        if (KeyCode == 13) {

            if (smartsearch_choices.style.display == '') {

                ev.returnValue = false;
                ev.cancel = true;
            }
            else {
                if (hfSmartSearchName.value != '' || hfSmartSearchGatewayCode.value != '' || hfSmartSearchValue.value != '' || txtSmartSearch.value != '') {

                    if (ValidateSearch()) {
                        var nameSplit = '$';
                        var ucName = uc.replace(/_/g, nameSplit);
                        __doPostBack('btnSearch', '');
                    }
                }
                else {

                    ev.returnValue = false;
                    ev.cancel = true;
                    alert('You must select a valid Destination, Resort or Airport Code');
                }
            }
        }
        else if (KeyCode != 9) {
            
            hfSmartSearchName.value = '';
            hfSmartSearchGatewayCode.value = '';
            hfSmartSearchValue.value = '';
            hfSmartSearchText.value = '';
        }
    }

    function fireSearch(ev) {
    
        var KeyCode = ev.keyCode ? ev.keyCode : ev.which ? ev.which : ev.charCode;
        if (KeyCode == 13) {

            if (ValidateSearch()) {
                var nameSplit = '$';
                var ucName = uc.replace(/_/g, nameSplit);
                //__doPostBack('ctl00$cp1$ucSearchPanel$btnSearch', '')
                
                
                __doPostBack('btnSearch', '');
            }
        }
    }

    function emptySmartSearch(el) {

        if (el.value != "") {
            hfSmartSearchName.value = '';
            hfSmartSearchGatewayCode.value = '';
            hfSmartSearchValue.value = '';
            hfSmartSearchText.value = '';
            txtSmartSearch.value = wText;
        }
    }
   
    function showVal()
    {
        alert('SmartSearchName ' + hfSmartSearchName.value + ', SmartSearchGatewayCode ' + hfSmartSearchGatewayCode.value + ', SmartSearchValue ' + hfSmartSearchValue.value + ', SmartSearchText ' + hfSmartSearchText.value + ', HolType ' + hfHolType.value + ', Advanced = ' + hfAdvanced.value);
        
        return false;
    }
    
    function waterMarkFocus(el) {
        if (el.value.trim() == wText) {
            el.value = '';
        }
    }
    function waterMarkBlur(el) {
       if (el.value.trim() == '') {
            el.value = wText;
        }
    }
    
    function chooseList()
    {
        if (divSearchTypeList.style.display == 'none')
        {
            divSearchTypeList.style.display = '';
            divSearchTypeText.style.display = 'none';
            hypChooseList.innerHTML = 'new combined search';
            //hfSearchType.value = 'ddl';
        }
        else
        {
            divSearchTypeList.style.display = 'none';
            divSearchTypeText.style.display = '';            
            hypChooseList.innerHTML = 'select destination from list';
            //hfSearchType.value = 'txt';
        }

        return false;
    }
    
    function ShowHideAdvancedSearchOnStart()
    {
        if (hfAdvanced.value == 'true') {
            hypAdvancedSearch.innerHTML = hfAdvancedOnText.value;
            //hypAdvancedSearch.innerHTML = 'Close Advanced Search &lt;&lt;&lt;';
            divAdvanced.style.display = 'block';
        }
        else
        {
            hypAdvancedSearch.innerHTML = hfAdvancedOffText.value;
            //hypAdvancedSearch.innerHTML = 'Advanced Search &gt;&gt;&gt;';
            divAdvanced.style.display = 'none';
        }
    }
    
    function advancedSearchClick()
    {
        if (hfAdvanced.value == 'true')
        {
            hypAdvancedSearch.innerHTML = hfAdvancedOffText.value;
            //hypAdvancedSearch.innerHTML = 'Advanced Search &gt;&gt;&gt;';
            divAdvanced.style.display = 'none';
            hfAdvanced.value = 'false';            
        }
        else
        {
            hypAdvancedSearch.innerHTML = hfAdvancedOnText.value;
            //hypAdvancedSearch.innerHTML = 'Close Advanced Search &lt;&lt;&lt;';
            divAdvanced.style.display = 'block';
            hfAdvanced.value = 'true';
        }

        return false;
    }
    
    
    
    function holidayTypeChange(el)
    {
        var elID = el.id.split('_');
        
        var uc = '';
        
        for (var i = 0; i < elID.length-1; i++)
        {
            uc += elID[i] + '_';
        }
        
        if ((elID[elID.length-1]) == 'rbBeach')
        {
            lblDestination.innerHTML = 'Beach Destination';
            lblResort.innerHTML = 'Resort';
            divBuilding.style.display = 'block';
            hfHolType.value = 'Beach';
        }
        else
        {
            lblDestination.innerHTML = 'City Destination';
            lblResort.innerHTML = 'City Location';
            divBuilding.style.display = 'none';
            hfHolType.value = 'City';
        }
    }

    function searchPanelScriptHotel(cid) {
        
        //new Ajax.Autocompleter(uc + "txtSmartSearch", "autocomplete_choices", "SmartSearchResults.aspx", {paramName: 'value', indicator: '', afterUpdateElement : getSelectionId});
        new Ajax.Autocompleter(uc + "txtSmartSearchHotel", "smartsearch_choices_hotel", "SmartSearchResults.aspx?cmd=hotelName", { paramName: 'value', indicator: '', updateElement: storeSelItemsForSmartSearchByHotelName, afterUpdateElement: getSelectionId });

        //ShowHideAdvancedSearchOnStart();
    }

    function storeSelItemsForSmartSearchByHotelName(li) {
        var txt;
        var value;
       
        if (li.firstChild.textContent != null) {
            txt = li.firstChild.textContent.trim();            
            //txtSmartSearch.value = li.firstChild.textContent.trim();
            //hfSmartSearchText.value = li.firstChild.textContent.trim();
        }
        else {
            txt = li.firstChild.innerText.trim();
            
            //txtSmartSearch.value = li.firstChild.innerText.trim();
            //hfSmartSearchText.value = li.firstChild.innerText.trim();
        }

        var arr = li.id.split('-');


        var txtBuildingID = document.getElementById(uc + 'txtBuildingID');
        //var txtBuilding = document.getElementById(uc + 'txtBuilding');
        var txtSmartSearchHotel = document.getElementById(uc + 'txtSmartSearchHotel');
        
        var hfChainCode = document.getElementById(uc + 'hfChainCode');

        txtBuildingID.value = arr[0];
        hfChainCode.value = arr[1];



        //txtBuilding.value = txt;
        txtSmartSearchHotel.value = txt;
    }
