﻿var servername = 'http://iproperty.com.my';


function callJsonLoadState(id, st) {
    var options_state = '';
    $.each(data_state, function(i, d) {   //data_state is refer from /scripts/referenceData/state_city.js
        if (st.toLowerCase() == d.id.toLowerCase()) options_state += '<option value="' + d.id + '" selected="true">' + d.name + '<\/option>';
        else options_state += '<option value="' + d.id + '">' + d.name + '<\/option>';
    });
    $('#' + id).html(options_state);
}

function callJsonLoadCity(index, id, ct, hd) {
    //alert('index:' + index + '   ct:' +  ct);
    var d = data_state[index];
    var options_city = '<option value="' + hd + '">' + hd + '<\/option>';
    if (index >= 0) {
        $.each(d.cities, function(i, j) {
            if (ct.toLowerCase() == j.city.toLowerCase()) options_city += '<option value="' + j.city + '" selected="true">' + j.city + '<\/option>';
            else options_city += '<option value="' + j.city + '">' + j.city + '<\/option>';
        });
    } else {
        options_city += '<option value=""><\/option>';
    }
    $('#' + id).html(options_city);
}

function callJsonLoadPropertyGroup(id, gid) {
    var options_propertygroup = '';
    var sCategory = '';
    $.each(data_propertygroup, function(i, d) {   //data_propertygroup is refer from /scripts/referenceData/propertycategory_group.js
        if (sCategory != d.cid) {
            switch (d.cid) {
                case 'R':
                    if (gid == 'AR') options_propertygroup += '<option value="AR" selected="true">----All Residential----<\/option>';
                    else options_propertygroup += '<option value="AR">----All Residential----<\/option>';
                    break;
                case 'C':
                    if (gid == 'AC') options_propertygroup += '<option value="AC" selected="true">----All Commercial----<\/option>';
                    else options_propertygroup += '<option value="AC">----All Commercial----<\/option>';
                    break;
                case 'I':
                    if (gid == 'AI') options_propertygroup += '<option value="AI" selected="true">----All Industrial----<\/option>';
                    else options_propertygroup += '<option value="AI">----All Industrial----<\/option>';
                    break;
                case 'A':
                    if (gid == 'AA') options_propertygroup += '<option value="AA" selected="true">----All Agricultural----<\/option>';
                    else options_propertygroup += '<option value="AA">----All Agricultural----<\/option>';
                    break;
            }
            sCategory = d.cid;
        }
        if (gid.toLowerCase() == d.gid.toLowerCase()) options_propertygroup += '<option value="' + d.gid + '" selected="true">' + d.name + '<\/option>';
        else options_propertygroup += '<option value="' + d.gid + '">' + d.name + '<\/option>';
    });
    $('#' + id).html(options_propertygroup);
}

function callJsonLoadPropertyType(id, pid) {
    var options_propertytype = '' //'<option><\/option>';
    $.each(data_propertytype_order, function(i, d) {   //data_state is refer from /scripts/referenceData/propertygroup_type.js
        if (pid.toLowerCase() == d.pid.toLowerCase()) options_propertytype += '<option value="' + d.pid + '" selected="true">' + d.name + '<\/option>';
        else options_propertytype += '<option value="' + d.pid + '">' + d.name + '<\/option>';
    });
    $('#' + id).html(options_propertytype);
}

function callJsonLoadPropertyTypeByGroup(id, pid, gid) {
    var options_propertytype = '<option value=""><\/option>';
    $.each(data_propertytype_group, function(i, d) {   //data_state is refer from /scripts/referenceData/propertygroup_type.js
        if (gid.toLowerCase() == d.gid.toLowerCase()) {
            $.each(d.protypes, function(i, j) {
                if (pid.toLowerCase() == j.pid.toLowerCase()) options_propertytype += '<option value="' + j.pid + '" selected="true">' + j.name + '<\/option>';
                else options_propertytype += '<option value="' + j.pid + '">' + j.name + '<\/option>';
            });
        }
    });
    $('#' + id).html(options_propertytype);
}

function jsClickButton(e, buttonid) {
    var evt = e ? e : window.event;
    var bt = document.getElementById(buttonid);
    if (bt) {
        if (evt.keyCode == 13) {
            $('#' + buttonid).click();
            return false;
        }
    }
}

function jsClickNCheck(e, buttonid) {
    var b = (window.event) ? event.keyCode : e.which;
    var evt = e ? e : window.event;
    if (evt.keyCode == 13) {
        $('#' + buttonid).click();
        return false;
    }
    else {
        return !((b >= 65 && b <= 90) || (b >= 97 && b <= 122) || (b >= 33 && b <= 47) || (b >= 58 && b <= 64) || (b >= 91 && b <= 96) || (b >= 123 && b <= 126))
    }
}

function g(c, e, d) { if (d) { if (c.value == e) c.value = '' } else { if (c.value == '') c.value = e } }


$(document).ready(function() {
    callJsonLoadPropertyGroup('searchBoxPropertyGroupType', '');

    //Edited by dandy 25/04/2011
    //States and Cities ddl will be loaded based on agent and agencies data
    if (document.getElementById("searchBoxState")[0] == null)
        callAjaxLoadState(document.getElementById("hfAgentID").value, document.getElementById("hfAgencyID").value);
    else
        if (document.getElementById("searchBoxState")[0].value != "")
        callAjaxLoadCity($('#searchBoxState').val(), document.getElementById("hfAgentID").value, document.getElementById("hfAgencyID").value);



    $('#searchBoxState').change(function() {
        //callJsonLoadCity($('#searchBoxState').get(0).selectedIndex, 'searchBoxCity', '', '');
        callAjaxLoadCity($('#searchBoxState').val(), document.getElementById("hfAgentID").value, document.getElementById("hfAgencyID").value);
    });





    function callAjax() {
        if ($("#hfValAA").val() != "false") {
            var querystring = "searchBoxTxtMinBath=" + escape($("#searchBoxTxtMinBath").val()) +
                              "&searchBoxTxtMaxBath=" + escape($("#searchBoxTxtMaxBath").val()) +
                              "&ag=" + escape($("#ag").val()) +
                              "&ai=" + escape($("#ai").val()) +
                              "&ar=" + escape($("#ar").val()) +
                              "&searchBoxOptSearchType=" + escape($('input:radio[name=searchBoxOptSearchType]:checked').val()) +
                              "&searchBoxPropertyGroupType=" + escape($("#searchBoxPropertyGroupType").val()) +
                              "&searchBoxState=" + escape($("#searchBoxState").val()) +
                              "&searchBoxCity=" + escape($("#searchBoxCity").val()) +
                              "&searchBoxTxtKeyword=" + escape($("#searchBoxTxtKeyword").val()) +
                              "&searchBoxRefinedPT=" + escape($("#searchBoxRefinedPT").val()) +
                              "&searchBoxTxtMinPrice=" + escape($("#searchBoxTxtMinPrice").val()) +
                              "&searchBoxTxtMaxPrice=" + escape($("#searchBoxTxtMaxPrice").val()) +
                              "&searchBoxTxtMinBed=" + escape($("#searchBoxTxtMinBed").val()) +
                              "&searchBoxTxtMaxBed=" + escape($("#searchBoxTxtMaxBed").val()) +
                              "&searchBoxTxtMinSize=" + escape($("#searchBoxTxtMinSize").val()) +
                              "&searchBoxTxtMaxSize=" + escape($("#searchBoxTxtMaxSize").val()) +
                              "&searchBoxDdlListedWithin=" + escape($("#searchBoxDdlListedWithin").val()) +
                              "&searchBoxChkWtPhoto=" + escape($("#searchBoxChkWtPhoto").val());
            //callCrossDomainAjax(servername + "\/property\/exsearch_action.aspx?" + querystring, function(results) {
            callCrossDomainAjax(servername + "\/property\/newexsearch_action.aspx?" + querystring, function(results) {

                $.fn.colorbox({ width: "80%", height: "80%", iframe: true, href: results });

            }, "html");
        }
    }


    //        $.ajax({
    //            type: "POST",
    //            url: servername + "\/property\/exsearch_action.aspx",
    //            data: "searchBoxTxtMinBath=" + $("#searchBoxTxtMinBath").val() +
    //                      "&searchBoxTxtMaxBath=" + $("#searchBoxTxtMaxBath").val() +
    //                      "&ag=" + $("#ag").val() +
    //                      "&ai=" + $("#ai").val() +
    //                      "&ar=" + $("#ar").val() +
    //                      "&searchBoxOptSearchType=" + $('input:radio[name=searchBoxOptSearchType]:checked').val() +
    //                      "&searchBoxPropertyGroupType=" + $("#searchBoxPropertyGroupType").val() +
    //                      "&searchBoxState=" + $("#searchBoxState").val() +
    //                      "&searchBoxCity=" + $("#searchBoxCity").val() +
    //                      "&searchBoxTxtKeyword=" + $("#searchBoxTxtKeyword").val() +
    //                      "&searchBoxRefinedPT=" + $("#searchBoxRefinedPT").val() +
    //                      "&searchBoxTxtMinPrice=" + $("#searchBoxTxtMinPrice").val() +
    //                      "&searchBoxTxtMaxPrice=" + $("#searchBoxTxtMaxPrice").val() +
    //                      "&searchBoxTxtMinBed=" + $("#searchBoxTxtMinBed").val() +
    //                      "&searchBoxTxtMaxBed=" + $("#searchBoxTxtMaxBed").val() +
    //                      "&searchBoxTxtMinSize=" + $("#searchBoxTxtMinSize").val() +
    //                      "&searchBoxTxtMaxSize=" + $("#searchBoxTxtMaxSize").val() +
    //                      "&searchBoxDdlListedWithin=" + $("#searchBoxDdlListedWithin").val() +
    //                      "&searchBoxChkWtPhoto=" + $("#searchBoxChkWtPhoto").val()
    //                      ,
    //            success: function(msg) {
    //                $.fn.colorbox({ width: "80%", height: "80%", iframe: true, href: msg });
    //            }
    //        });
    //}

    $("#imgBtnSearch").click(function() {

        callAjax();
    });



    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-182004-1']);
    _gaq.push(['_trackPageview']);

    (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
    })();

    var _comscore = _comscore || [];
    _comscore.push({ c1: "2", c2: "8555383" });
    (function() {
        var s = document.createElement("script"), el = document.getElementsByTagName("script")[0]; s.async = true;
        s.src = (document.location.protocol == "https:" ? "https://sb" : "http://b") + ".scorecardresearch.com/beacon.js";
        el.parentNode.insertBefore(s, el);
    })();
});

function callAjaxLoadCity(st, agentId, agencyId) {
    var querystring = 'st=' + st + '&agentId=' + agentId + '&agencyId=' + agencyId + '&SaleOrRent=' + $('input:radio[name=searchBoxOptSearchType]:checked').val();
    callCrossDomainAjax(servername + '/ajax_server/property/svr_newsearchboxtab_city.aspx?' + querystring, function(xml) {
        document.getElementById('searchBoxCity').length = 0;
        AddItem('', '');
        $(xml).find('record').each(function() {
            var city = $(this).text();
            AddItem(city, city);
        });
    }, "xml");
}
 function callAjaxLoadState(agentId, agencyId) {
    var querystring = 'agentId=' + agentId + '&agencyId=' + agencyId + '&SaleOrRent=' + $('input:radio[name=searchBoxOptSearchType]:checked').val();
    callCrossDomainAjax(servername + '/ajax_server/property/svr_newsearchboxtab_state.aspx?' + querystring, function(xml) {
        document.getElementById('searchBoxState').length = 0;
        AddItemState('--State--', '');
        $(xml).find('record').each(function() {
            var state = $(this).text();
            AddItemState(state.split(",")[1], state.split(",")[0]);
        });
    }, "xml");
}
//    $.ajax({
//        url: servername + '/ajax_server/property/svr_searchboxtab_city.aspx',
//        data: 'st=' + st + '&agentId=' + agentId + '&agencyId=' + agencyId + '&SaleOrRent=' + $('input:radio[name=searchBoxOptSearchType]:checked').val(),
//        type: 'GET',
//        datatype: 'application/xml',
//        timeout: 100000,
//        cache: false,
//        error: function(err) {
//            alert("Please reselect your state.");
//        },
//        success: function(xml) {
//            document.getElementById('searchBoxCity').length = 0;
//            AddItem('', '');
//            $(xml).find('record').each(function() {
//                var city = $(this).text();
//                AddItem(city, city);
//            });
//        }
//    });
//}


function AddItem(Text, Value) {
    var opt = document.createElement("option");
    opt.text = Text;
    opt.value = Value;
    document.getElementById('searchBoxCity').options.add(opt);
}
function AddItemState(Text, Value) {
    var opt = document.createElement("option");
    opt.text = Text;
    opt.value = Value;
    document.getElementById('searchBoxState').options.add(opt);
}
function validate() {
    /*
    if ($.trim($("#ag").val()).length == 0 && $.trim($("#ai").val()).length == 0) {
    alert("Invalid agency or agent code.");
    return false;
    }
    */
    return true;
}
