﻿var words = "";
var rentmin = 0;
var rentmax = 50000;
var sizemin = 0;
var sizemax = 700;
var badrooms = 0;
var ordertype = 1;
var entstart = 0;
var limit = 50;

function GetStatesByID(obj) {
    var CountryId;
    CountryId = obj.value;

    subOptions(document.getElementById('selstate'));
    addOption(document.getElementById('selstate'), "0", "Loading data...");

    subOptions(document.getElementById('selcounty'));
    addOption(document.getElementById('selcounty'), "0", "Choose a county");

    subOptions(document.getElementById("ddlArea"));
    addOption(document.getElementById("ddlArea"), "0", "Choose an area");

    Bloc.ObjectModel.State.GetStatesByCountryID(CountryId, OnPostGetStatesByIDCallBack);
    return false;
}

function OnPostGetStatesByIDCallBack(res) {

    subOptions(document.getElementById('selstate'));
    addOption(document.getElementById('selstate'), "0", "Choose a state");

    if (res.value != null & res.value != "") {
        var state = document.getElementById("selstate");
        subOptions(state);
        addOption(state, "0", "Choose a state");

        var num = res.value.length;

        for (i = 0; i < num; i++) {
            if (res.value[i].StateName != "") {
                addOption(state, res.value[i].StateID, res.value[i].StateName);
            }
        }
    }
}

function GetCountyByID(obj) {
    subOptions(document.getElementById('selcounty'));
    addOption(document.getElementById('selcounty'), "0", "Loading data...");

    subOptions(document.getElementById("ddlArea"));
    addOption(document.getElementById("ddlArea"), "0", "Choose an area");

    Bloc.ObjectModel.County.GetCountiesByStateID(obj.value, OnPostGetCountyByIDCallBack);

    return false;
}

function OnPostGetCountyByIDCallBack(res) {
    subOptions(document.getElementById('selcounty'));
    addOption(document.getElementById('selcounty'), "0", "Choose a county");

    if (res.value != null & res.value != "") {
        var county = document.getElementById("selcounty");


        var num = res.value.length;
        subOptions(document.getElementById('selcounty'));
        addOption(document.getElementById('selcounty'), "0", "Choose a county");

        for (i = 0; i < num; i++) {
            if (res.value[i].CountyName != "") {
                addOption(document.getElementById('selcounty'), res.value[i].CountyID, res.value[i].CountyName);
            }
        }
    }

}

function GetAreaByID(obj) {

    subOptions(document.getElementById("ddlArea"));
    addOption(document.getElementById("ddlArea"), "0", "Choose an area");

    Bloc.ObjectModel.Area.GetAreasByCountyID(obj.value, OnPostGetAreaByIDCallBack);

    return false;
}

function OnPostGetAreaByIDCallBack(res) {
    subOptions(document.getElementById("ddlArea"));
    addOption(document.getElementById("ddlArea"), "0", "Choose an area");

    if (res.value != null & res.value != "") {
        var area = document.getElementById("ddlArea");

        var num = res.value.length;
        subOptions(document.getElementById("ddlArea"));
        addOption(document.getElementById("ddlArea"), "0", "Choose an area");

        areabuf = [];
        for (i = 0; i < num; i++) {
            if (res.value[i].AreaName != "") {
                areabuf.push(res.value[i].PostalCode);
                addOption(document.getElementById("ddlArea"), res.value[i].AreaID, res.value[i].AreaName + "( post Code: " + res.value[i].PostalCode + " )");

            }
        }
    }
}

function ClearSelectArea(obj) {
    subOptions($("selstate"));
    addOption($("selstate"), "0", "Choose state");

    subOptions($("selcounty"));
    addOption($("selcounty"), "0", "Choose county");

    subOptions($("ddlArea"));
    addOption($("ddlArea"), "0", "Choose area");

    GetStatesByID(obj);

}

function searchhouseinselect() {
    countryid = $('ddlCountry').value;
    stateid = $("selstate").value;
    countyid = $("selcounty").value;
    areaid = $("ddlArea").value;
    $("alocation").innerHTML = $('ddlArea').options[$('ddlArea').selectedIndex].text + "," + $('selcounty').options[$('selcounty').selectedIndex].text;

    $("changelocation").href = "../popups/selectlocation.rails?Cid=" + countryid + "&Sid=" + stateid + "&Cyid=" + countyid + "&Aid=" + areaid;
    $("alocation").href = "../popups/selectlocation.rails?Cid=" + countryid + "&Sid=" + stateid + "&Cyid=" + countyid + "&Aid=" + areaid;


    SearchHouse();
}

function SearchHouse() {


    words = $('words').value;
    rentmin = $('rentmin').value;
    rentmax = $('rentmax').value;
    sizemin = $('sizemin').value;
    sizemax = $('sizemax').value;
    badrooms = $('bedrooms').value;
    ordertype = $('sortby').value;
    entstart = 0;
    var dds = $('ulhousing').childElements();
    for (i = 1, n = dds.length; i < n; i++) {
        dds[i].remove();
    }

    new Insertion.Bottom($('ulhousing'), '<dd><img class="loader" src="/Content/gfx/ajax-loader-small.gif" alt="" /> Loading...</dd>');

    //$('ulhousing').innerHTML = '<img class="loader" src="/Content/gfx/ajax-loader-small.gif" alt="" /> Loading...';

    Bloc.ObjectModel.Market.SearchHouse(countryid, stateid, countyid, areaid, words, rentmin, rentmax, sizemin, sizemax, badrooms, limit, entstart, ordertype, OnPostSearchHouseCallBack);
    jQuery(document).trigger('close.facebox');
}

function OrderResult() {
    entstart = 0;
    ordertype = $('sortby').value;
    var dds = $('ulhousing').childElements();
    for (i = 1, n = dds.length; i < n; i++) {
        dds[i].remove();
    }

    new Insertion.Bottom($('ulhousing'), '<dd><img class="loader" src="/Content/gfx/ajax-loader-small.gif" alt="" /> Loading...</dd>');
    //$('ulhousing').innerHTML = '<img class="loader" src="/Content/gfx/ajax-loader-small.gif" alt="" /> Loading...';

    Bloc.ObjectModel.Market.SearchHouse(countryid, stateid, countyid, areaid, words, rentmin, rentmax, sizemin, sizemax, badrooms, limit, entstart, ordertype, OnPostSearchHouseCallBack);
}

function PagingHouse(page) {
    entstart = page;

    var dds = $('ulhousing').childElements();
    for (i = 1, n = dds.length; i < n; i++) {
        dds[i].remove();
    }

    new Insertion.Bottom($('ulhousing'), '<dd><img class="loader" src="/Content/gfx/ajax-loader-small.gif" alt="" /> Loading...</dd>');
    //$('ulhousing').innerHTML = '';

    Bloc.ObjectModel.Market.SearchHouse(countryid, stateid, countyid, areaid, words, rentmin, rentmax, sizemin, sizemax, badrooms, limit, entstart, ordertype, OnPostSearchHouseCallBack);
}

function OnPostSearchHouseCallBack(res) {
    var buf = [];
    var clientid = 0;
    $('h1result').innerHTML = res.value[1];
    paging(res.value[1]);

    if (res.value[0] != null) {
        for (var i = 0, n = res.value[0].length; i < n; i++) {
            var house = res.value[0][i];
            buf.push('<dd>');
            if (house.HouseImage != "")
                buf.push('<a href="housing_object.rails?Rid=' + house.RentID + '" class="list-img"><img src="' + house.HouseImage + '" alt="" /></a>');
            else
                buf.push('<a href="housing_object.rails?Rid=' + house.RentID + '" class="list-img"><img src="http://bloc.net/content/gfx/market_default_house_image.jpg" alt="" /></a>');

            buf.push('<div class="wrap">');
            buf.push('<h2><a href="housing_object.rails?Rid=' + house.RentID + '">' + house.Title + '</a>');
            if (house.Sold)
                buf.push('<span class="red">(Sold)</span> ');
            buf.push('</h2>');
            buf.push('<p class="tagline">');
            buf.push(house.AreaName + '.' + house.CountyName + ',' + house.CountryName + '</p><br/>');
            buf.push('<dl>');
            
            if (house.Furnished == 0) {
                buf.push('<dt>Furnished:</dt>');
                buf.push('<dd>No</dd>');
            }

            if (house.Furnished == 1) {
                buf.push('<dt>Furnished:</dt>');
                buf.push('<dd>Partly</dd>');
            }

            if (house.Furnished == 2) {
                buf.push('<dt>Furnished:</dt>');
                buf.push('<dd>Yes</dd>');
            }

            buf.push('<dt>Size:</dt>');
            buf.push('<dd>' + house.Size + ' sqm</dd>');
            buf.push('<dt>Rent <abbr title="Per Calendar Month">pcm</abbr>:</dt>');
            buf.push('<dd>' + house.Price + ' RMB</dd>');

			buf.push('<dt>');
            buf.push('Address');
            buf.push('</dt>');
            buf.push('<dd>' + house.Address + '&nbsp;</dd>');

            buf.push('</dl>');
            buf.push('</div>');
            buf.push('</dd>');
        }
    }
    var all = buf.join("");

    var dds = $('ulhousing').childElements();
    for (i = 1, n = dds.length; i < n; i++) {
        dds[i].remove();
    }

    new Insertion.Bottom($('ulhousing'), buf.join(""));


    //$('ulhousing').innerHTML = all;

}

function paging(CountImgs) {

    var strHTML = "";
    var PageNow = entstart;

    if (PageNow > 0) {
        strHTML += '<li>'
        strHTML += '<a onclick="PagingHouse('
        strHTML += (entstart - 1).toString();
        strHTML += ')"';
        strHTML += '>Back</a>';
        strHTML += '</li>'
    }

    if ((entstart + 1) * limit < CountImgs) {
        strHTML += '<li><a onclick="PagingHouse('
        strHTML += PageNow + 1;
        strHTML += ')">Next</a><div></div></li>';
    }
    $('pageuser').update(strHTML);

}

