﻿
    Type.registerNamespace("FTS");

    FTS.validateSearch = function() {

        var imageButton = FreeTextSearchClientRef.SearchImage;
        var regionSelector = FreeTextSearchClientRef.SearchSuggestCombo;
        
        if (FreeTextSearchClientRef.hidSelectedRegion.value == '' && !(/\d-\d/.test(FreeTextSearchClientRef.searchText.value))) {
            alert(FreeTextSearchClientRef.selectedRegionText);
            return false;
        }
        else {
            FTS.SwapImages('divButton', 'divWorking');
            var input = document.getElementById('QSearchType');

            if (input != null) {
                input.value = 'FreeTextSearch';                
            }
            return true;
        }
    };

    FTS.setBuyRent = function(buy) {
        var imgbuy = FreeTextSearchClientRef.imgBuy; //document.getElementById("imgBuy");
        var imgrent = FreeTextSearchClientRef.imgRent; // document.getElementById("imgRent");
        var lnkbuy = FreeTextSearchClientRef.lnkBuy; // document.getElementById("lnkBuy");
        var lnkrent = FreeTextSearchClientRef.lnkRent; // document.getElementById("lnkRent");
        var hidbuyrent = FreeTextSearchClientRef.hidBuyRent; // document.getElementById("<%= hidBuyRent.ClientID %>");
        hidbuyrent.value = buy;
        //alert(buy);
        //alert(hidbuyrent.value);
        if (buy) {
            imgbuy.src = "/sites/RemaxEuropeCommon/images/check_on.gif";
            imgrent.src = "/sites/RemaxEuropeCommon/images/check_off.gif";
            lnkbuy.style.color = "Red";
            lnkrent.style.color = "Gray";
        } else {
            imgbuy.src = "/sites/RemaxEuropeCommon/images/check_off.gif";
            imgrent.src = "/sites/RemaxEuropeCommon/images/check_on.gif";
            lnkbuy.style.color = "Gray";
            lnkrent.style.color = "Red";
        }
    };

    FTS.setSaleRent = function(buy) {
        FreeTextSearchClientRef.hidBuyRent.value = buy;
        if (buy) {
            FreeTextSearchClientRef.btnSale.className = "ftqs_salerent_on";
            FreeTextSearchClientRef.btnRent.className = "ftqs_salerent_off";
            FreeTextSearchClientRef.imgSale.src = "/Sites/RemaxEuropeCommon/images/tick_on.png";
            FreeTextSearchClientRef.imgRent.src = "/Sites/RemaxEuropeCommon/images/spacer.gif";

            if (typeof (SmartModuleListingQuickSearch_FreeText) != "undefined") {
                SmartModuleListingQuickSearch_FreeText.divPrice.style.display = "block";
                SmartModuleListingQuickSearch_FreeText.divRentalPrice.style.display = "none";
            }
        } else {
            FreeTextSearchClientRef.btnSale.className = "ftqs_salerent_off";
            FreeTextSearchClientRef.btnRent.className = "ftqs_salerent_on";
            FreeTextSearchClientRef.imgSale.src = "/Sites/RemaxEuropeCommon/images/spacer.gif";
            FreeTextSearchClientRef.imgRent.src = "/Sites/RemaxEuropeCommon/images/tick_on.png";

            if (typeof (SmartModuleListingQuickSearch_FreeText) != "undefined") {
                SmartModuleListingQuickSearch_FreeText.divPrice.style.display = "none";
                SmartModuleListingQuickSearch_FreeText.divRentalPrice.style.display = "block";
            }
        }
    }


    FTS.SwapImages = function(hide, show) {
        var SelectedValue = FreeTextSearchClientRef.SearchSuggestCombo.value.split(':')[2].split(',')[0];
        //alert(SelectedValue);
        if (SelectedValue.length == 2) {
            SelectedValue = FreeTextSearchClientRef.SearchSuggestCombo.value.split(':')[3].split(',')[0];
        }
        //alert(SelectedValue);
        FreeTextSearchClientRef.hdnSelectedValue.value = SelectedValue;

        document.getElementById(hide).style.display = "none";
        document.getElementById(show).style.display = "";
    };

    FTS.SearchSuggestCombo_OnClientItemsRequesting = function(comboBox, eventArgs) {
        var typedText = eventArgs.get_text();
        FreeTextSearchClientRef.searchText.value = typedText;
        if (typedText.length < 2 || typedText.match(/^[0-9]+/)) {
            comboBox.clearItems();
            comboBox.hideDropDown();
            FreeTextSearchClientRef.hidSelectedRegion.value = '';
            eventArgs.set_cancel(true);
            return false;
        }

        //The next statement will disable the entire page while the control goes to the server.
        //The reason is that we want to force the user to wait before continuing.
        if (typeof (FreeTextSearchClientRef) != 'undefined')
            FreeTextSearchClientRef.SearchSuggestComboInput.disabled = true;
    };

    FTS.SearchSuggestCombo_OnClientItemsRequested = function(comboBox) {
        //we enabled the page first regardless of the number of items found.
        if (typeof (FreeTextSearchClientRef) != 'undefined') {
            FreeTextSearchClientRef.SearchSuggestComboInput.disabled = false;
            FreeTextSearchClientRef.SearchSuggestComboInput.focus();
        }

        if (comboBox.get_items().get_count() <= 0) {
            comboBox.clearItems();
            comboBox.hideDropDown();
            FTS.ChangeInstructions(true);
        } else {
            FTS.ChangeInstructions(false);
        }
        if (comboBox.get_value() == '') {
            FreeTextSearchClientRef.hidSelectedRegion.value = '';
        }
    };

    FTS.ChangeInstructions = function(noListings) {
        if (noListings) {
            if (FreeTextSearchClientRef.divInstructions != null)
                FreeTextSearchClientRef.divInstructions.style.display = "none";
            if (FreeTextSearchClientRef.divNoListingsInstructions != null)
                FreeTextSearchClientRef.divNoListingsInstructions.style.display = "none";
        } else {
            if (FreeTextSearchClientRef.divInstructions != null)
                FreeTextSearchClientRef.divInstructions.style.display = "";
            if (FreeTextSearchClientRef.divNoListingsInstructions != null)
                FreeTextSearchClientRef.divNoListingsInstructions.style.display = "none";
        }
    };

    FTS.SearchSuggestCombo_OnClientKeyPressing = function(sender, e) {

        if (e.get_domEvent().keyCode == 13 && !sender.get_dropDownVisible()) {

            //SwapImages('divButton','divWorking');
            event.returnValue = false;
            event.cancel = true;
            FreeTextSearchClientRef.SearchImage.click();
        }
        else if (e.get_domEvent().keyCode == 13) {

            if (sender._highlightedItem != null && sender._highlightedItem != undefined) {
                FreeTextSearchClientRef.hidSelectedRegion.value = sender._highlightedItem.text;
            }
            FreeTextSearchClientRef.SearchImage.click();
        }
    };

    // Used for SmartModuleListingQuickSearch_FreeText.ascx
    FTS.ValidateSubmitSearch = function() {
    
        if (typeof (FreeTextSearchClientRef) != "undefined") {
            if (
                FreeTextSearchClientRef.hidSelectedRegion.value == '' &&
                FreeTextSearchClientRef.searchText.value != '' &&
                !(/\d-\d/.test(FreeTextSearchClientRef.searchText.value))
              ) {
                alert(FreeTextSearchClientRef.selectedRegionText);
                return false;
            }
            else {
                FTS.SetSearchType('QuickSearch');
                SmartModuleListingQuickSearch_FreeText.btnSearch.click();
            }
        }
        return true;
    }

    FTS.SetSearchType = function(type) {

        if (typeof (FreeTextSearchClientRef) != "undefined") {
            document.getElementById('QSearchType').value = type;
            var SelectedValue = FreeTextSearchClientRef.SearchSuggestCombo.value.split(':')[2].split(',')[0];
            if (SelectedValue.length == 2) {
                SelectedValue = FreeTextSearchClientRef.SearchSuggestCombo.value.split(':')[3].split(',')[0];
            }
            FreeTextSearchClientRef.hdnSelectedValue.value = SelectedValue;
            return true;
        }
    }
    
    FTS.SubmitToQuickSearchOnEnter = function(sender, e) {

        if (e.get_domEvent().keyCode == 13 && !sender.get_dropDownVisible()) {
            
            event.returnValue = false;
            event.cancel = true;

            //validate before submitting
            FTS.ValidateSubmitSearch();                      
        }
        else if (e.get_domEvent().keyCode == 13) {
            if (sender._highlightedItem != null && sender._highlightedItem != undefined) {
                FreeTextSearchClientRef.hidSelectedRegion.value = sender._highlightedItem.text;
            }
            //uncomment this code to submit with a single press of the enter key
            //            //validate before submitting
            //FTS.ValidateSubmitSearch();
        }
    };

    FTS.SearchSuggestCombo_OnClientSelectedIndexChanged = function(sender, e) {

        var item = e.get_item();
        if (item.get_value().charAt(0) == 'N') {
            FTS.ChangeInstructions(true);
        } else {
            FTS.ChangeInstructions(false);
        }

        if (item.get_text() == '') {
            FreeTextSearchClientRef.hidSelectedRegion.value = '';
        }
        else {
            FreeTextSearchClientRef.hidSelectedRegion.value = item.get_text();
        }
    };

    if (typeof (Sys) !== 'undefined') 
    Sys.Application.notifyScriptLoaded()




if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();