﻿function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function del_cookie(name) {
    var date = new Date();
    date.setTime(date.getTime() - 1);
    var expires = '; expires=' + date.toGMTString();
    document.cookie = name + '=' + expires;
    //alert(document.cookie = name +'='+ expires);
    return 0
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date(); exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

var address = "";
function checkCookie() {
    QsValue = getCookie('QsValue');
    if (QsValue != null && QsValue != "") {
        if (QsValue.indexOf(";") > -1)
            var split = QsValue.split(";");

        if (split.length == 2) {
            document.getElementById('txtLocation').value = split[0];
            var QSsplit = split[0];
            QSsplit = QSsplit.split(",")
            if (QSsplit.length == 2) {
                SearchAppend += "CityName~'" + QSsplit[0] + "' And " + "(StateAbv~'" + trim(QSsplit[1], " ") + " ' or StateName~'" + trim(QSsplit[1], " ") + "') And ";
            }
            else {
                var fromPage = getQuerystring('fp', '0');
                if (fromPage != 'idx')
                    SearchAppend += "(PostalCode~'" + QSsplit[0] + "' or AgentId~" + QSsplit[0] + ") And ";
                else
                    SearchAppend += "PostalCode~'" + QSsplit[0] + "' And ";
            }
            address = split[0];
            StateContent = address;
            loadData(1)
            if (!GeneratingXmlforMap(1))
                return false;
        }
        else {
            document.getElementById('txtLocation').value = split[0];
            document.getElementById('selHomesType').value = split[1];
            document.getElementById('txtPriceMIN').value = split[2];
            document.getElementById('txtPriceMAX').value = split[3];
            document.getElementById('selBeds').value = split[4];
            document.getElementById('selBaths').value = split[5];
            document.getElementById('SaveSearch').style.display = 'block';
            document.getElementById('AdvanceSearch').style.display = 'block';
            if (split.length > 6) {
                document.getElementById('SelGarage').value = split[6];
                document.getElementById('SelYearBuilt').value = split[7];
                document.getElementById('SelMinSqYd').value = split[8];
                document.getElementById('SelMaxSqYd').value = split[9];
                document.getElementById('SelLivingArea').value = split[10];

                splitStyle = split[11].split('|');
                if (splitStyle.length > 0) {
                    Style = document.getElementById('SelStyle');
                    for (i = 0; i < Style.options.length; i++) {
                        if (Style.options[i].text == splitStyle[i]) {
                            Style.options[i].selected = true;
                        }
                    }
                }
                ShowHide_AdvSearch('1')
            }
            else {
                return false;
            }
        }
    }
}


var QsValue;
var StateContent = "";
function AssignCookie(param) {
    QsValue = "";
    var selectedStyle = "";
    var selectedType = "";
    del_cookie('QsValue')
    loc = document.getElementById('txtLocation').value;
    StateContent = loc;
    htype = document.getElementById('selHomesType').value;
    pmin = document.getElementById('txtPriceMIN').value;
    pmax = document.getElementById('txtPriceMAX').value;
    beds = document.getElementById('selBeds').value;
    baths = document.getElementById('selBaths').value;
    if (param == 'Advance') {
        if (document.getElementById('SelGarage').value != 0) {
            if (document.getElementById('SelGarage').value == 'yes')
                SearchAppend += "GarageStalls is not null And "
            else if (document.getElementById('SelGarage').value == 'No')
                SearchAppend += "GarageStalls is null And "
        }
        if (document.getElementById('SelYearBuilt').value != 0)
            SearchAppend += "YearBuilt~'" + document.getElementById('SelYearBuilt').value + "' And ";

        QsValue += loc + ";" + htype + ";" + pmin + ";" + pmax + ";" + beds + ";" + baths + ";";
        QsValue += document.getElementById('SelGarage').value + ';';
        QsValue += document.getElementById('SelYearBuilt').value + ';';

        if ((document.getElementById('SelMinSqYd').value != 0) && (document.getElementById('SelMaxSqYd').value != 0)) {
            SearchAppend += "([length]*[width])/9 in(" + document.getElementById('SelMinSqYd').value + "," + document.getElementById('SelMaxSqYd').value + ") And ";
        } else if (document.getElementById('SelMinSqYd').value != 0) {
            SearchAppend += "([length]*[width])/9 <~'" + document.getElementById('SelMinSqYd').value + "' And ";
        } else if (document.getElementById('SelMaxSqYd').value != 0) {
            SearchAppend += "([length]*[width])/9 >~'" + document.getElementById('SelMaxSqYd').value + "' And ";
        }

        QsValue += document.getElementById('SelMinSqYd').value + ';';
        QsValue += document.getElementById('SelMaxSqYd').value + ';';

        if (document.getElementById('SelLivingArea').value != 0)
            SearchAppend += "LivingArea<~'" + document.getElementById('SelLivingArea').value + "' And ";

        QsValue += document.getElementById('SelLivingArea').value + ';';

        Style = document.getElementById('SelStyle');
        var StyleStr = "";
        for (i = 0; i < Style.options.length; i++) {
            if (Style.options[i].selected) {
                selectedStyle += Style.options[i].text + "|";
                StyleStr += Style.options[i].text + "','";

            }
        }
        if (StyleStr.length > 0) {
            StyleStr = StyleStr.replace("&", "|")
            StyleStr = StyleStr.substring(0, StyleStr.length - 3)
            SearchAppend += " StyleName in ('" + StyleStr + "') And ";
        }

        QsValue += selectedStyle;

        //                 Type = document.getElementById('SelType');
        //                  var TypeStr = "";
        //                 for(j=0;j<Type.options.length;j++)
        //                 {
        //                     if (Type.options[j].selected)
        //                      {
        //                       selectedType += Type.options[j].text+"|";
        //                        TypeStr += Type.options[j].text + "','";
        //                      }
        //                 }
        //                 if(TypeStr.length>0)
        //                 {
        //                     TypeStr = TypeStr.replace("&","|")
        //                     TypeStr = TypeStr.substring(0,TypeStr.length-3)
        //                     SearchAppend += " PropertyType in ('" + TypeStr + "') And ";
        //                 }
        //                 
        //                 QsValue += selectedType;
    }
    else if (param == 'Quick') {
        QsValue += loc + ";" + htype + ";" + pmin + ";" + pmax + ";" + beds + ";" + baths;
    }
    if (QsValue != null && QsValue != "") {
        setCookie('QsValue', QsValue, 10);
        //alert("setCookie="+QsValue);
        document.getElementById('SaveSearch').style.display = 'block';
        if (param != 'Advance')
            document.getElementById('AdvanceSearch').style.display = 'block';
    }
}
var selHomesType = "";

var SearchAppend = "";
function Location_Validation() {
    SearchAppend = "";
    var city = state = Zip = null;

    if (document.getElementById('txtLocation').value.length == 0) {
        ShowMessage('Divmessage', 'Please enter location!')
        return false;
    }
    if (document.getElementById('txtLocation').value) {
        var value = trim(document.getElementById('txtLocation').value, " ");
        address = trim(document.getElementById('txtLocation').value, " ");

        if (!SpecialCharacters(value)) {
            ShowMessage('Divmessage', 'Special characters are not allowed !')
            return false;
        }
        var browser = navigator.userAgent;
        if (browser.indexOf("Firefox") > 0) {
            if (value == 'City & State,or Zip') {
                ShowMessage('Divmessage', 'Please enter location!')
                //alert('please enter location');
                return false;
            }
        }
        if (value == 'City & State, or ZIP') {
            ShowMessage('Divmessage', 'Please enter location!')
            //alert('please enter location');
            return false;
        }
        else {
            var split = value.split(",");

            if (split.length == 2) {

                if (isNaN(split[0]) && isNaN(split[1]) && split[1] != "") {
                    city = split[0];
                    state = split[1];
                    SearchAppend += "CityName~'" + city + "' And " + "(StateAbv~'" + trim(state, " ") + " ' or StateName~'" + trim(state, " ") + "') And ";
                }
                else {
                    ShowMessage('Divmessage', 'Please enter valid location!')
                    //alert('please enter valid location');
                    return false;
                }
                //                city = split[0];
                //                state = split[1];
                //                SearchAppend += "CityName~'" + city + "' And " + "(StateAbv~'" + trim(state," ") + " ' or StateName~'" + trim(state," ") + "') And ";

            }
            else if (split.length > 2) {
                ShowMessage('Divmessage', 'Please enter valid location!')
                //alert('please enter valid location');
                return false;
            }
            // else if (!isNaN(value) && value.length == 5) {
            else if (!isNaN(value) && value.length >= 5 && value.length <= 10) {
                Zip = value;
                SearchAppend += "PostalCode~'" + Zip + "' And ";
            }
            else {
                ShowMessage('Divmessage', 'Please enter valid location!')
                return false;
            }
        }
    }

    if (document.getElementById('txtPriceMIN').value != 'Min') {
        if (!check_price(document.getElementById('txtPriceMIN').value, 'Min'))
            return false;
    }

    if (document.getElementById('txtPriceMAX').value != 'Max') {
        if (!check_price(document.getElementById('txtPriceMAX').value, 'Max'))
            return false;
    }
    //var TMin = document.getElementById('txtPriceMIN').value;
    //var TMax = document.getElementById('txtPriceMAX').value;

    if ((document.getElementById('txtPriceMIN').value != 'Min') && (document.getElementById('txtPriceMAX').value != 'Max')) {
        if (parseFloat(document.getElementById('txtPriceMIN').value) > parseFloat(document.getElementById('txtPriceMAX').value)) {
            //if (parseInt(TMin) > parseInt(TMax)) {
            ShowMessage('Divmessage', 'MinPrice should be less than maxprice!')
            //alert('MinPrice should be less than maxprice !');
            return false;
        }
    }

    if (document.getElementById('txtPriceMIN').value != 'Min' && document.getElementById('txtPriceMAX').value != 'Max')
        SearchAppend += 'ListPrice between ' + document.getElementById('txtPriceMIN').value + " And " + document.getElementById('txtPriceMAX').value + " And ";
    else if (document.getElementById('txtPriceMIN').value != 'Min')
        SearchAppend += 'ListPrice >~' + document.getElementById('txtPriceMIN').value + " And ";
    else if (document.getElementById('txtPriceMAX').value != 'Max')
        SearchAppend += 'ListPrice <~' + document.getElementById('txtPriceMAX').value + " And ";


    if (document.getElementById('selHomesType').value)
        document.getElementById('selHomesType').value != 0 ? document.getElementById('selHomesType').value : '0';



    if (document.getElementById('selHomesType').value != 0) {
        SearchAppend += "PropertyType~'" + document.getElementById('selHomesType').value + "' And ";
        selHomesType = document.getElementById('selHomesType').value;
    }
    else {
        selHomesType = "";
    }



    if (document.getElementById('selBeds').value)
        document.getElementById('selBeds').value != 0 ? document.getElementById('selBeds').value : '0';

    if (document.getElementById('selBeds').value != 0)
        SearchAppend += "BedRooms >~'" + document.getElementById('selBeds').value + "' And ";

    if (document.getElementById('selBaths').value)
        document.getElementById('selBaths').value != 0 ? document.getElementById('selBaths').value : '0';

    if (document.getElementById('selBaths').value != 0)
        SearchAppend += "BathRooms >~'" + document.getElementById('selBaths').value + "' And ";

    return true;
}
function check_price(price, param) {
    regex = /^[.,0-9]+$/;
    if (!regex.test(price)) {
        ShowMessage('Divmessage', param + " " + "is not valid amount")
        //alert(param + " " + "is not valid amount");
        return false;
    }
    else {
        return price + "|";
    }
}
var OrderByStr = "";
function OrderByStrContent(Param, UpperOrderId, LowerOrderId, Controlfor) {
    if (Param == 1) {
        OrderByStr = document.getElementById(UpperOrderId).value;
        document.getElementById(LowerOrderId).value = document.getElementById(UpperOrderId).value;
        if (OrderByStr != "0") {
            if (Controlfor == 'MyListing') {
                LoadMyListings(1)
            }
            else if (Controlfor == 'QuickListing') {
                loadData(1);
            }
            else if (Controlfor == 'MyNewListing') {
                LoadMyNewListings(1)
            }
            else if (Controlfor == 'SuggestedBy') {
                LoadSuggestionsResult(1)
            }
            else if (Controlfor == 'MyHomesClient') {
                LoadMyHomesClientResult(1)
            }
            else if (Controlfor == 'ClientsBook') {
                LoadClientsBookResult(1)
            }
            else if (Controlfor == 'ListingComments') {
                LoadRealtor_ListingComments(1)
            }
            else if (Controlfor == 'MyRecentView') {
                LoadRecentViewResult(1)
            }
            else if (Controlfor == 'SmartSearch') {
                SmartSearchResultsBind(1)
            }
        }
    }
    else if (Param == 2) {
        OrderByStr = document.getElementById(LowerOrderId).value;
        document.getElementById(UpperOrderId).value = document.getElementById(LowerOrderId).value;
        if (OrderByStr != "0") {
            if (Controlfor == 'MyListing') {
                LoadMyListings(1)
            }
            else if (Controlfor == 'QuickListing') {
                loadData(1);
            }
            else if (Controlfor == 'MyNewListing') {
                LoadMyNewListings(1)
            }
            else if (Controlfor == 'SuggestedBy') {
                LoadSuggestionsResult(1)
            }
            else if (Controlfor == 'MyHomesClient') {
                LoadMyHomesClientResult(1)
            }
            else if (Controlfor == 'ClientsBook') {
                LoadClientsBookResult(1)
            }
            else if (Controlfor == 'ListingComments') {
                LoadRealtor_ListingComments(1)
            }
            else if (Controlfor == 'MyRecentView') {
                LoadRecentViewResult(1)
            }
            else if (Controlfor == 'SmartSearch') {
                SmartSearchResultsBind(1)
            }
        }
    }
}
function callQuickSearch() {
    if (!Location_Validation())
        return false;
    else
        AssignCookie('Quick')
    loadData(1)
    if (!GeneratingXmlforMap(1))
        return false;

}

function AdvanceSearch() {
    if (!Location_Validation())
        return false;
    else if (document.getElementById('SelMinSqYd').value != 0 && document.getElementById('SelMaxSqYd').value != 0) {
        if (parseFloat(document.getElementById('SelMinSqYd').value) > parseFloat(document.getElementById('SelMaxSqYd').value)) {
            ShowMessage('Divmessage', 'Max Sq Yd  should be greater than Min Sq Yd!')
            return false;
        }
    }
    else
        AssignCookie('Advance')

    loadData(1)
    if (!GeneratingXmlforMap(1))
        return false;

}

function ShowHide_AdvSearch(param) {
    if (param == 1) {
        document.getElementById('AdvSearch').style.display = 'block'
        document.getElementById('HideAdvanceSearch').style.display = 'block'
        document.getElementById('AdvanceSearch').style.display = 'none'
        //document.getElementById('SaveQuickSearch').style.display = 'none'
        document.getElementById('SaveQuickSearch').style.visibility = 'hidden'
    }
    else if (param == 0) {
        document.getElementById('AdvSearch').style.display = 'none'
        document.getElementById('HideAdvanceSearch').style.display = 'none'
        document.getElementById('AdvanceSearch').style.display = 'block'
        //document.getElementById('SaveQuickSearch').style.display = 'block'
        document.getElementById('SaveQuickSearch').style.visibility = 'visible'
    }
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function gotoConsumerPage_Tabs(TabId) {
    del_cookie('TabValue2')
    // Set New Cookie
    var TabValue = 0 + ',' + TabId;
    setCookie('TabValue2', TabValue, 10);
    window.location = 'CHomesBook.aspx';
}

function gotoRealtorPage_Tabs(TabId) {
    del_cookie('TabValue1')
    // Set New Cookie
    var TabValue = 0 + ',' + TabId;
    setCookie('TabValue1', TabValue, 10);
    window.location = 'MyHomesBook.aspx';
}
function AddNewClientPopup() {
    HidePopup();
    Displaypopup('0', 'divNewClient', 0, 0);
}

function disableAnchor(obj, disable) {
    var CurrentObj = document.getElementById(obj);
    if (disable == 1) {
        var href = CurrentObj.getAttribute("href");
        if (href && href != "" && href != null) {
            CurrentObj.setAttribute('href_bak', href);
        }
        CurrentObj.removeAttribute('href');
        CurrentObj.style.color = "gray";
    }
    else {
        CurrentObj.setAttribute('href', CurrentObj.attributes['href_bak'].nodeValue);
        CurrentObj.style.color = "blue";
    }
}


function CallFunction(Type) {
    if (Type == 'Consumers')
        Load_ConsumersHomeCount()

}


