﻿function search2(a, acode, form) {
    var loc = ' ';

    var num = '';
    if ($("#searchnumtab").is(":visible"))
        if (document.getElementById('NumberTextBox') != null)
            num = document.getElementById('NumberTextBox').value;
    if (document.getElementById('datefrom') == null)
        _date = 31;
    else
        _date = document.getElementById('datefrom').value;
    _province = document.getElementById('Province');
    if (_province == null)
        if (document.getElementById('ctl00_content_Agn1_EstateID') != null)
            s = document.getElementById('ctl00_content_Agn1_EstateID').value;
        else
            if (document.getElementById('content_Agn1_EstateID') != null)
                s = document.getElementById('content_Agn1_EstateID').value;

    _city = document.getElementById('City');
    if (_city == null)
        if (document.getElementById('ctl00_content_Agn1_City') != null)
            _city = document.getElementById('ctl00_content_Agn1_City');
        else
            if (document.getElementById('content_Agn1_City') != null)
                _city = document.getElementById('content_Agn1_City');

    if (document.getElementById('Province') != null)
        if (document.getElementById('Province').selectedIndex == -1)
            s = "";
        else
            s = document.getElementById('Province')[document.getElementById('Province').selectedIndex].text;

    var cname = 'ctl00_content_Agn1_ag';
    if (document.getElementById(cname) == null)
        var cname = 'content_Agn1_ag';

    if (document.getElementById(cname) != null) {
        ag = document.getElementById(cname).value;
        agnn = document.getElementById(cname).value;
        _date = -1;
    }
    else {
        ag = -10;
        agnn = '';

    }
    if (document.getElementById('ctl00_content_Agn1_Province') != null || document.getElementById('content_Agn1_Province') != null) {
        if (document.getElementById('ctl00_content_Agn1_Province') != null)
            estt = document.getElementById('ctl00_content_Agn1_Province').value;
        else
            estt = document.getElementById('content_Agn1_Province').value;
    }
    else
        estt = '';

    //        if (sEstate == 'undefined')
    //                sEstate = 'تهران';
    if (estt != '')
        loc += 'استان ' + estt + ' ';
    c = _city.value.replace(/^\s+|\s+$/g, '');
    if (c == 'شهر')
        c = '';
    if (c != '')
        loc += 'شهر ' + c + ' ';
    var mtype = document.getElementById('MType').value;
    el = document.getElementById("SearchType").value;
    sumprice1 = "";
    sumprice2 = "";
    rahnprice1 = "";
    rahnprice2 = "";
    leaseprice1 = "";
    leaseprice2 = "";
    if (document.getElementById('minprice').selectedIndex != -1)
        price1 = document.getElementById('minprice')[document.getElementById('minprice').selectedIndex].text;
    else
        price1 = "";
    if (document.getElementById('maxprice').selectedIndex != -1)
        price2 = document.getElementById('maxprice')[document.getElementById('maxprice').selectedIndex].text;
    else
        price2 = "";

    if (el == 2) {
        rahnprice1 = price1;
        rahnprice2 = price2;
    }
    else
        if (el == 3) {
            leaseprice1 = price1;
            leaseprice2 = price2;
        }
        else {
            sumprice1 = price1;
            sumprice2 = price2;
        }

    if (document.getElementById('Region').value.replace(/^\s+|\s+$/g, '') == 'محدوده')
        region = '';
    else
        region = document.getElementById('Region').value;
    searchtype = document.getElementById('SearchType').value;
    if (document.getElementById('Province') != null)
        estid = document.getElementById('Province').value;
    else
        if (document.getElementById('ctl00_content_Agn1_EstateID') != null)
            estid = document.getElementById('ctl00_content_Agn1_EstateID').value;
        else
            estid = document.getElementById('content_Agn1_EstateID').value;
    var s = 'SearchResult.aspx?ag=' + escape(ag) +
                '&agnn=' + escape(agnn) +
                '&est=' + estid +
                '&ct=' + escape(c) +
                '&loc=' + escape(loc) +
                '&rgn1=' + escape(region) +
                '&num=' + num +
                '&mtp=' + escape(mtype) +
                '&bld=' +
                '&slp=' + escape(document.getElementById('sleepcnt')[document.getElementById('sleepcnt').selectedIndex].text) +
                '&rhn1=' + escape(rahnprice1) +
                '&rhn2=' + escape(rahnprice2) +
                '&les1=' + escape(leaseprice1) +
                '&les2=' + escape(leaseprice2) +
                '&sprs1=' + escape(sumprice1) +
                '&sprs2=' + escape(sumprice2) +
                '&slvl=' + escape(searchtype) +
                '&dt=' + _date +
                '&mt=&nslc=0';

    if (form == 'sr')
        window.location.replace(encodeURI(s));
    else
        window.open(encodeURI(s), '', '', '');

}

var m_strNewDefaultText = "";
function DeleteDefaultText(el, strDefaultText) {

    if (el.value == strDefaultText || el.value == m_strNewDefaultText) {
        el.value = '';
        el.onclick = '';
        el.onkeypress = '';
    }
}
function ChangeDefaultText(strText) {
    var txtSearch = document.getElementById("HomepageSearch_RMX1_txtSearch_txtCSZ");

    if (strText == "") {
        strText = "Search by City or County and State, or ZIP";
    }
    txtSearch.value = strText;
    m_strNewDefaultText = strText;
}


function searchchanged(el) {
    document.getElementById("minprice").disabled = "";
    document.getElementById("maxprice").disabled = "";
    premin = '<select name="minprice" id="minprice" class="garycolor">';
    premax = '<select name="maxprice" id="maxprice" class="garycolor">';
    post = '</select>';
    pricetext = "<option value=''></option><option value='1ميليون'>1ميليون</option><option value='3ميليون'>3ميليون</option><option value='5ميليون'>5ميليون</option><option value='10ميليون'>10ميليون</option><option value='15ميليون'>15ميليون</option><option value='20ميليون'>20ميليون</option><option value='30ميليون'>30ميليون</option><option value='40ميليون'>40ميليون</option><option value='50ميليون'>50ميليون</option><option value='60ميليون'>60ميليون</option><option value='70ميليون'>70ميليون</option><option value='80ميليون'>80ميليون</option><option value='90ميليون'>90ميليون</option><option value='100ميليون'>100ميليون</option><option value='120ميليون'>120ميليون</option><option value='150ميليون'>150ميليون</option><option value='180ميليون'>180ميليون</option><option value='200ميليون'>200ميليون</option><option value='250ميليون'>250ميليون</option><option value='300ميليون'>300ميليون</option><option value='350ميليون'>350ميليون</option><option value='400ميليون'>400ميليون</option><option value='450ميليون'>450ميليون</option><option value='500ميليون'>500ميليون</option><option value='600ميليون'>600ميليون</option><option value='700ميليون'>700ميليون</option><option value='800ميليون'>800ميليون</option><option value='900ميليون'>900ميليون</option><option value='1ميليارد'>1ميليارد</option><option value='2ميليارد'>2ميليارد</option><option value='10ميليارد'>10ميليارد</option><option value='50ميليارد'>50ميليارد</option><option value='100ميليارد'>100ميليارد</option><option value='1000ميليارد'>1000ميليارد</option>";
    rahntext = "<option value=''></option><option value='100تومان'>100تومان</option><option value='1ميليون'>1ميليون</option><option value='3ميليون'>3ميليون</option><option value='5ميليون'>5ميليون</option><option value='10ميليون'>10ميليون</option><option value='15ميليون'>15ميليون</option><option value='20ميليون'>20ميليون</option><option value='25ميليون'>25ميليون</option><option value='30ميليون'>30ميليون</option><option value='35ميليون'>35ميليون</option><option value='40ميليون'>40ميليون</option><option value='45ميليون'>45ميليون</option><option value='50ميليون'>50ميليون</option><option value='60ميليون'>60ميليون</option><option value='70ميليون'>70ميليون</option><option value='80ميليون'>80ميليون</option><option value='90ميليون'>90ميليون</option><option value='100ميليون'>100ميليون</option>"
    leasetext = "<option value=''></option>	<option value='50تومان'>50تومان</option><option value='100تومان'>100تومان</option><option value='120تومان'>120تومان</option><option value='140تومان'>140تومان</option><option value='150تومان'>150تومان</option><option value='170تومان'>170تومان</option><option value='190تومان'>190تومان</option><option value='200تومان'>200تومان</option><option value='220تومان'>220تومان</option><option value='240تومان'>240تومان</option><option value='250تومان'>250تومان</option><option value='300تومان'>300تومان</option><option value='350تومان'>350تومان</option><option value='400تومان'>400تومان</option><option value='450تومان'>450تومان</option><option value='500تومان'>500تومان</option><option value='600تومان'>600تومان</option><option value='700تومان'>700تومان</option><option value='750تومان'>750تومان</option><option value='800تومان'>800تومان</option><option value='950تومان'>950تومان</option><option value='900تومان'>900تومان</option><option value='1ميليون'>1ميليون</option><option value='10ميليون'>10ميليون</option><option value='100ميليون'>100ميليون</option>";
    minel = document.getElementById("mindiv");
    maxel = document.getElementById("maxdiv");
    if (el.value == 0) {
        document.getElementById("minprice").disabled = "disabled";
        document.getElementById("maxprice").disabled = "disabled";
    }
    else
        if (el.value == 2) {
            document.getElementById("mincap").innerHTML = "حداقل رهن";
            document.getElementById("maxcap").innerHTML = "حداکثر رهن";
            minel.innerHTML = premin + rahntext + post;
            maxel.innerHTML = premax + rahntext + post;
        }
        else
            if (el.value == 3) {
                document.getElementById("mincap").innerHTML = "حداقل اجاره";
                document.getElementById("maxcap").innerHTML = "حداکثر اجاره";
                minel.innerHTML = premin + leasetext + post;
                maxel.innerHTML = premax + leasetext + post;
            }
            else {
                document.getElementById("mincap").innerHTML = "حداقل قیمت";
                document.getElementById("maxcap").innerHTML = "حداکثر قیمت";
                minel.innerHTML = premin + pricetext + post;
                maxel.innerHTML = premax + pricetext + post;
            }


}
function WebForm_FireDefaultButton(event, target) {

    if (event.keyCode == 13) {
        search2()
    }

    return true;

}

function Removenums(s) {
    result = "";
    for (var i = 0; i < s.length - 8; i++)

        result += s.charAt(i);
    return result;
}
function getPriceStatment(p1, p2, pt) {
    if (p1 != "" && p2 == "")
        return " حداقل " + pt + p1;
    if (p1 == "" && p2 != "")
        return " حداکثر " + pt + p2;
    if (p1 != "" && p2 != "")
        return pt + p1 + " تا " + p2;
    return "";
}
function getMT(i) {
    if (i == "")
        return "";
    if (i == 0)
        return " متراژ  کمتر از 50 متر ";
    else
        if (i == 1)
            return " متراژ بين 50 تا 60 متر ";
        else
            if (i == 2)
                return " متراژ بين 60 تا 70 متر ";
            else
                if (i == 3)
                    return " متراژ بين 70 تا 80 متر ";
                else
                    if (i == 4)
                        return " متراژ بين 80 تا 90 متر ";
                    else
                        if (i == 5)
                            return " متراژ بين 90 تا 100 متر ";
                        else
                            if (i == 6)
                                return " متراژ بين 100 تا 120 متر ";
                            else
                                if (i == 7)
                                    return " متراژ بين 120 تا 140 متر ";
                                else
                                    if (i == 8)
                                        return " متراژ بين 140 تا 170 متر ";
                                    else
                                        if (i == 9)
                                            return " متراژ بين 170 تا 200 متر ";
                                        else
                                            if (i == 10)
                                                return " متراژ بيشتر از 200 متر ";
    return "";
}



function UpdateConditions() {
    if (document.forms[0].txtnum.value != "") {
        document.forms[0].txtConditions.value = "ملک با شماره: " + document.forms[0].txtnum.value;
        return;

    }
    arMstatus = ["", "رهن و اجاره", "خريد", "پيش خريد", "معاوضه", "مشارکت در ساخت"];
    Conditions = "";
    if (document.forms[0].ddest.value != "-1")
        Conditions += " " + "استان " + document.forms[0].ddest.options[document.forms[0].ddest.selectedIndex].text;
    if (document.forms[0].txtct.value != "")
        Conditions += " " + "شهر " + document.forms[0].txtct.value;
    sslvl = arMstatus[document.forms[0].ddslvl.value];
    smtp = document.forms[0].ddmtp[document.forms[0].ddmtp.selectedIndex].text;
    sSleep = "";
    if (document.forms[0].ddslp.value != "")
        sSleep = document.forms[0].ddslp.value + "خوابه ";
    if (document.forms[0].ddbld[document.forms[0].ddbld.selectedIndex].text != "")
        sSleep += document.forms[0].ddbld[document.forms[0].ddbld.selectedIndex].text;
    sSleep += getMT(document.forms[0].ddmt.value);
    if (smtp != "")
        ss = " نوع ملک " + smtp + sSleep;
    else
        ss = sSleep;
    if (document.forms[0].ddag.value == -10) {
        if (sslvl != "")
            Conditions += ss + " جهت " + sslvl;
        else
            Conditions += ss;
    }
    else
        if (document.forms[0].ddag.value == -20)
            Conditions += ss + " " + sslvl + " موردهاي بي واسطه ";
        else
            Conditions += ss + " " + " ملکهاي " + sslvl + document.forms[0].ddag.options[document.forms[0].ddag.selectedIndex].text;
    bNoRegionSelected = 1;
    if (document.forms[0].txtrgn.value != "") {
        Conditions += " محدوده " + document.forms[0].txtrgn.value;
        bNoRegionSelected = 0;
    }


    Conditions += getPriceStatment(document.forms[0].ddsprs1[document.forms[0].ddsprs1.selectedIndex].text, document.forms[0].ddsprs2[document.forms[0].ddsprs2.selectedIndex].text, " قيمت کل ");
    Conditions += getPriceStatment(document.forms[0].ddrn1[document.forms[0].ddrn1.selectedIndex].text, document.forms[0].ddrn2[document.forms[0].ddrn2.selectedIndex].text, " مبلغ رهن ");
    Conditions += getPriceStatment(document.forms[0].ddls1[document.forms[0].ddls1.selectedIndex].text, document.forms[0].ddls2[document.forms[0].ddls2.selectedIndex].text, " مبلغ اجاره ");

    document.forms[0].txtConditions.value = Conditions;

}


function SetComboEnabled() {

    el = document.forms[0].ddslvl;
    document.forms[0].ddrn1.disabled = "";
    document.forms[0].ddrn2.disabled = "";
    document.forms[0].ddls1.disabled = "";
    document.forms[0].ddls2.disabled = "";
    document.forms[0].ddsprs1.disabled = "";
    document.forms[0].ddsprs2.disabled = "";
    if (el.value == 1) {
        document.forms[0].ddsprs1.disabled = "disabled";
        document.forms[0].ddsprs2.disabled = "disabled";
    }
    else
        if (el.value == 2 || el.value == 3) {
            document.forms[0].ddrn1.disabled = "disabled";
            document.forms[0].ddrn2.disabled = "disabled";
            document.forms[0].ddls1.disabled = "disabled";
            document.forms[0].ddls2.disabled = "disabled";
        }
    UpdateConditions();
}
function EstateChange() {
    UpdateConditions();
    document.forms[0].txtct.value = "";
    document.forms[0].txtrgn.value = "";
}



function initjs() {
    document.forms[0].ddag.onchange = UpdateConditions;
    document.forms[0].ddslvl.onchange = SetComboEnabled;
    document.forms[0].ddest.onchange = EstateChange;
    document.forms[0].txtct.onchange = UpdateConditions;
    document.forms[0].ddmtp.onchange = UpdateConditions;
    document.forms[0].txtrgn.onchange = UpdateConditions;
    document.forms[0].ddbld.onchange = UpdateConditions;
    document.forms[0].ddslp.onchange = UpdateConditions;
    document.forms[0].ddrn1.onchange = UpdateConditions;
    document.forms[0].ddrn2.onchange = UpdateConditions;
    document.forms[0].ddls1.onchange = UpdateConditions;
    document.forms[0].ddls2.onchange = UpdateConditions;
    document.forms[0].ddsprs1.onchange = UpdateConditions;
    document.forms[0].ddsprs2.onchange = UpdateConditions;
    document.forms[0].txtnum.onchange = UpdateConditions;
    document.forms[0].ddmt.onchange = UpdateConditions;
}

function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i = 0; i < gy.length; i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}

