﻿/*----------------------------------------------------
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, ucFilter;
var hfSmartSearchName, hfSmartSearchGatewayCode, hfSmartSearchValue, hfSmartSearchText, txtSmartSearch, smartsearch_choices, hfAdvanced, hypAdvancedSearch, hfAdvancedOnText, hfAdvancedOffText;
//var chkAdvancedSearch;
var ddlRooms, ddlR1A, ddlR1C, ddlR1I
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');

        //chkAdvancedSearch = document.getElementById(uc + 'chkAdvancedSearch');
        ddlRooms = document.getElementById(uc + 'ddlRooms');
        ddlR1A = document.getElementById(uc + 'ddlR1A');
        ddlR1C = document.getElementById(uc + 'ddlR1C');
        ddlR1I = document.getElementById(uc + 'ddlR1I'); 

        waterMarkBlur(txtSmartSearch);

        var path = location.pathname;


        if (path.toLowerCase().indexOf('/sites/') > -1) {
            var tmpPath = path.split('/');

            path = "/Sites/" + tmpPath[2] + "/SmartSearchResults.aspx?cmd=combined";
        }
        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 searchPanelScriptCombined(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.toLowerCase().indexOf('/sites/') > -1) {
            var tmpPath = path.split('/');
            
            path = "/Sites/" + tmpPath[2] + "/SmartSearchResults.aspx?cmd=combined";            
        }
        else
            path = "/SmartSearchResults.aspx?cmd=combined";


        //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: addItemToListCombined, afterUpdateElement: getSelectionId });

        //ShowHideAdvancedSearchOnStart();
    }

    function ShowFilterProgress() {


        if (document.getElementById("divFilterProgress")) {
            var div = document.getElementById("divFilterProgress");
            
            //div.top = document.body.parentNode.scrollHeight + 'px';
            div.style.display = "";
            div.style.visibility = "visible";
        }
        
        /*
        var filterButton = document.getElementById(ucFilter + '_btnFilterPrice');
        if (filterButton == null) {
            
            if (document.getElementById("divProgress")) {
                document.getElementById("divFilterProgress").style.display = "";
                document.getElementById("divFilterProgress").style.visibility = "visible";
            }
        }
        else {
            var divFilterButton = document.getElementById('divFilterButton');


            if (filterButton.disabled) {
                filterButton.disabled = "";
                filterButton.className = "";
                divFilterButton.className = "fc-submit"
            }
        }
        */
    }

    function EnableFilterButton() {
        var filterButton = document.getElementById(ucFilter + '_btnFilterPrice');
        var divFilterButton = document.getElementById('divFilterButton');

        if (filterButton && filterButton.disabled) {
            filterButton.disabled = "";
            filterButton.className = "";
            divFilterButton.className = "fc-submit"
        }
    }

    function DisableFilterButton() {
        var filterButton = document.getElementById(ucFilter + '_btnFilterPrice');

        if (filterButton != null) {
            if (filterButton.disabled == "") {
                filterButton.disabled = "disabled";
            } 
        }
    }

//  move to master.js TM
//    function SearchClicked() {
//        if (document.getElementById("divSearching")) {
//            document.getElementById("divSearching").style.display = '';
//        }
    //    }

    function toggleAdvancedOpions(value, chainCode) {

        var ddlBoardBasis = document.getElementById(uc + 'ddlBoardBasis');
        var lblBoardBasis = document.getElementById(uc + 'lblBoardBasis');
        var ddlRating = document.getElementById(uc + 'ddlRating');
        var lblRating = document.getElementById(uc + 'lblRating');

        if (value == 'off') {
            //if (chainCode != 'H4U') {
                ddlBoardBasis.selectedIndex = 0;
                ddlBoardBasis.style.display = 'none';
                lblBoardBasis.style.display = 'none';
            //}
            //else {
            //    ddlBoardBasis.style.display = 'block';
            //    lblBoardBasis.style.display = 'block';
            //}
            ddlRating.selectedIndex = 0;
            ddlRating.style.display = 'none';
            lblRating.style.display = 'none';
        }
        else {
            ddlBoardBasis.style.display = 'block';
            lblBoardBasis.style.display = 'block';
            ddlRating.style.display = 'block';
            lblRating.style.display = 'block';
        }
    }

    function addItemToListCombined(li) {
        var arr = li.id.split('-');
        //alert("_" + arr[0].trim() + "_");
        if (arr[0].trim() == "Hotel") {
            var txt;
            var value;

            if (li.firstChild.textContent != null) {
                txt = li.firstChild.textContent.trim();
            }
            else {
                txt = li.firstChild.innerText.trim();
            }

            //var arr = li.id.split('-');

            var txtBuildingID = document.getElementById(uc + 'txtBuildingID');
            var txtSmartSearchHotel = document.getElementById(uc + 'txtSmartSearchHotel');
            var hfChainCode = document.getElementById(uc + 'hfChainCode');

            hfChainCode.value = arr[1];
            txtBuildingID.value = arr[2];
            txtSmartSearchHotel.value = txt;
            txtSmartSearch.value = txt;

            toggleAdvancedOpions('off', arr[1].trim());      
        }
        else {
            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];

            toggleAdvancedOpions('on', '');
        }
    }
    
    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.value != '' && txtSmartSearch.value == '') {
            if (txtBuilding.value != '')
                txtBuilding.value = '';
        }
                            
        var KeyCode = ev.keyCode ? ev.keyCode : ev.which ? ev.which : ev.charCode;
        //if ((KeyCode == 13 && ev.type == 'keydown') || (ev.type == 'focus')) {
        if (KeyCode == 13) {
            if ((smartsearch_choices.style.display == '') || ((smartsearch_choices.style.display == 'none') && (smartsearch_choices.childNodes.length == 0))) {

                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 = '';
        }

//        if ((KeyCode == 13 && ev.type == 'keydown') || (ev.type == 'blur')) {
//            var div= document.getElementById(uc + 'smartserach_choices');
//        }
    }

    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 && el.value && el.value == wText) {
            el.value = '';
            
        }
    }
    function waterMarkBlur(el) {
        if (el && el.value && el.value == '') {
            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 advancedSearchPanel4Click() {
        if (hfAdvanced.value == 'true') {
            divAdvanced.style.display = 'none';
            hfAdvanced.value = 'false';
            ddlRooms.value = 1;
            ddlR1A.value = 2;
            ddlR1C.value = 0;
            ddlR1I.nalue = 0;
            DisplayChild();
        }
        else {
            divAdvanced.style.display = 'block';
            hfAdvanced.value = 'true';
        }
    }        
    
    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;
    }

