﻿var SelectedText = "";
var SelectedValue = 0;
var Mode = "buy";
var DefaultMode;
var isObtainedthruWebservice = false;

function OnClientItemsRequestFailed(sender, eventArgs) {
    eventArgs.set_cancel(true);
}

function OnClientSelectedIndexChangedEventHandler(sender, args) {
    //var item = args.get_item();
    if (args.get_item() != null) {
        SelectedText = args.get_item().get_text();
        SelectedValue = args.get_item().get_value();

        //remove more than once value from it
        if (SelectedValue.indexOf(',') != -1) {
            SelectedValue = SelectedValue.split(',')[0];
        }

    }
}

function OnClientDropDownOpening(sender, args) {
    SelectedText = "";
    SelectedValue = 0;
}

/*
function EnterKeyHandler() {
    var sender = GetComoboObj();
    var textInTheCombo = sender.get_text().toLowerCase();
    var NeedIntPage = 0;
    
    for (var i = 0; i < sender.get_items().get_count(); i++) {
        item = sender.get_items().getItem(i);
        itemValue = item.get_text().toLowerCase();
        if (textInTheCombo == itemValue) {
            NeedIntPage = 1;
        }
    }

    if(NeedIntPage == 0) {
        var Location = ''
        Location = document.getElementById(GetClientID('hdnLocation')).value
        PopWindow(true, false, '../interim-page.aspx?mode=' + Mode + '&location=' + textInTheCombo, 318, 410, null);
        document.getElementById(GetClientID('hdnPopUp')).value = '';
    }
    else {
        NeedIntPage = 0;
        RedirectToResults();
    }
}
*/
function EnterKeyHandler() {
    //var sender = document.getElementById('ddlSearchBox');
    var sender = GetComoboObj();
    var textInTheCombo = sender.get_text();
    var UrlLocationText = textInTheCombo.toLowerCase();
    UrlLocationText = UrlLocationText.replace(/,/g, "-");
    UrlLocationText = UrlLocationText.replace(/\'/g, "");
    UrlLocationText = UrlLocationText.replace(/\"/g, "");
    UrlLocationText = UrlLocationText.replace(/\s/g, "-");
    UrlLocationText = UrlLocationText.replace(/---/g, "-");
    UrlLocationText = UrlLocationText.replace(/--/g, "-");
    if (UrlLocationText == "location") {
        if (Mode == 'buy') window.location = '../property-for-sale/search.aspx';
        else if (Mode == 'rent') window.location = '../property-to-rent/search.aspx';
        else window.location = '../new-homes/search.aspx';
    }
    else {
        if (Mode == 'buy') window.location = '../property-for-sale/search.aspx?location=' + UrlLocationText;
        else if (Mode == 'rent') window.location = '../property-to-rent/search.aspx?location=' + UrlLocationText;
        else window.location = '../new-homes/search.aspx?location=' + UrlLocationText;
    }
    /* Modified for interim page
    var item = sender.findItemByText(textInTheCombo);
    if (!item) {
        // if selected from list - redirect to results page
        var Location = ''
        Location = document.getElementById(GetClientID('hdnLocation')).value
        PopWindow(true, false, '../interim-page.aspx?mode=' + Mode + '&location=' + textInTheCombo, 318, 410, null);
        document.getElementById(GetClientID('hdnPopUp')).value = '';

    }
    else {
        RedirectToResults();
    }
    */
    //SelectedValue = 0;

    //    if (RedirectToResults()) {
    //        if (Mode == 'buy') location = '../property-for-sale/' + SelectedText;
    //        else if (Mode == 'rent') location = '../property-to-rent/' + SelectedText;
    //        else location = '../new-homes/' + SelectedText;
    //    }
}
function GetResult(result) {
    SelectedValue = result;
    isObtainedthruWebservice = true;
    document.getElementById('hdnLocation').value = result;
    document.form1.submit();
    RedirectToResults();
}


function RedirectToResults() {

    if (SelectedText == '')
        if (document.getElementById('ddlSearchBox').value != '')
            SelectedText = document.getElementById('ddlSearchBox').value;

    var UrlLocationText = SelectedValue;

    if (SelectedText != '') {
        
        //alert(SelectedText + '~' + SelectedValue + '~0~      ~s~d~1~l~-1~0~0~0~0~0~0~0~0~0~0~~~0~' + UrlLocationText);
        //Mode = document.getElementById('hdnMode').value;
        if (Mode == 'buy') setCookie('usrq', SelectedText + '~0~0~      ~s~d~1~l~-1~0~0~0~0~0~0~0~0~0~0~~~0~' + UrlLocationText, 7);
        else if (Mode == 'rent') setCookie('usrq', SelectedText + '~0~0~      ~l~d~1~l~-1~0~0~0~0~0~0~0~0~0~0~~~0~' + UrlLocationText, 7);
        else setCookie('usrq', SelectedText + '~0~0~      ~n~d~1~l~-1~0~0~0~0~0~0~0~0~0~0~~~0~' + UrlLocationText, 7);

        var RedirectUrl;
        if (Mode == 'buy') { window.location = '../property-for-sale/' + UrlLocationText; }
        else if (Mode == 'rent') { window.location = '../property-to-rent/' + UrlLocationText; }
        else { window.location = '../new-homes/' + UrlLocationText; }

        return false;
    }
    else {
        radalert('Please enter a location to continue search', 400, 200, 'Message From MyHomesUnlimited.com');
        return false;
    }
}
/*
function RedirectToResults() {

    if (SelectedText == '')
        if (document.getElementById('ddlSearchBox').value != '')
            SelectedText = document.getElementById('ddlSearchBox').value;

    var UrlLocationText = SelectedText.toLowerCase();
    UrlLocationText = UrlLocationText.replace(/,/g, "-");
    UrlLocationText = UrlLocationText.replace(/\'/g, "");
    UrlLocationText = UrlLocationText.replace(/\"/g, "");
    UrlLocationText = UrlLocationText.replace(/\s/g, "-");
    UrlLocationText = UrlLocationText.replace(/--/g, "-");
    UrlLocationText = UrlLocationText.replace(/--/g, "-");

    if (SelectedText != '') {
        if (SelectedValue == '' || SelectedValue == 0) {
            document.getElementById('hdnLocation').value = SelectedText;
            document.getElementById('hdnMode').value = Mode;
            document.forms[0].submit();
            //return false;
        }
        //alert(SelectedText + '~' + SelectedValue + '~0~      ~s~d~1~l~-1~0~0~0~0~0~0~0~0~0~0~~~0~' + UrlLocationText);
        Mode = document.getElementById('hdnMode').value;
        if (Mode == 'buy') setCookie('usrq', SelectedText + '~' + SelectedValue + '~0~      ~s~d~1~l~-1~0~0~0~0~0~0~0~0~0~0~~~0~' + UrlLocationText , 7);
        else if (Mode == 'rent') setCookie('usrq', SelectedText + '~' + SelectedValue + '~0~      ~l~d~1~l~-1~0~0~0~0~0~0~0~0~0~0~~~0~' + UrlLocationText, 7);
        else setCookie('usrq', SelectedText + '~' + SelectedValue + '~0~      ~n~d~1~l~-1~0~0~0~0~0~0~0~0~0~0~~~0~' + UrlLocationText, 7);

        var RedirectUrl;
        if (Mode == 'buy') { window.location = '../property-for-sale/' + UrlLocationText;  }
        else if (Mode == 'rent') { window.location = '../property-to-rent/' + UrlLocationText; }
        else { window.location = '../new-homes/' + UrlLocationText; }

        return false;
    }
    else {
        radalert('Please enter a location to continue search', 400, 200, 'Message From MyHomesUnlimited.com');
        return false;
    }
}
*/

function OnPageInit() {
    //alert('OnpageInit');
    try {
        if (document.getElementById(GetClientID('hdnWithSignin')).value == "0") {
            Mode = document.getElementById(GetClientID('hdnMode')).value;

            if (document.getElementById(GetClientID('hdnPopUp')).value == 'PopUp') {
                var Location = ''
                Location = document.getElementById(GetClientID('hdnLocation')).value
                //PopWindow(true, false, '../interim-page.aspx?mode=' + Mode + '&location=' + Location, 318, 410, null);
                document.getElementById(GetClientID('hdnPopUp')).value = '';
            }
        }
        else {
            PopWindow(true, false, '../signin.aspx', 318, 410, null);
        }
    }
    catch (ex) { }
}
