﻿var _cboState;
var _cboCommunity;

function initializePage()
{
    _cboState = $get("cboState");
    _cboCommunity = $get("cboCommunity");
    
    _cboState.onchange = function(){getCommunityList();};
    
    clearOptions(_cboCommunity);
    addSelectOption(_cboCommunity, "Community");
    
    getStateList();
}

function clearOptions(cboOptions)
{
    var loopIndex;
       
    for (loopIndex = cboOptions.length - 1; loopIndex>=0; loopIndex--)
    {
       cboOptions.remove(loopIndex);
    }
}

function addLoadingOption(cboOptions)
{
    var item = document.createElement("OPTION");           
    
    item.text = "loading...";
    item.value = "";
    cboOptions.options.add(item);
    cboOptions.disabled = true;
}


function addSelectOption(cboOptions, OptionType)
{
    var item = document.createElement("OPTION");           
    
    item.text = "--Select " + OptionType + "--";
    item.value = "";
    cboOptions.options.add(item);
    cboOptions.disabled = false;
}

function getStateList()
{
    clearOptions(_cboState);
    addLoadingOption(_cboState);
    DachaRealEstate.getStates(country_pk, onSuccessStates, onFailureStates);
}

function onSuccessStates(results)
{
    clearOptions(_cboState);
    addSelectOption(_cboState, "State");
    
    var item; 
    var loopIndex;         
    
    if(results != null)
    {
        if(results.length > 0)
        {
            for(loopIndex = 0; loopIndex < results.length; loopIndex++)
            {
                item = document.createElement("OPTION"); 
                item.text = results[loopIndex].state_name;
                item.value = results[loopIndex].state_pk;
                _cboState.options.add(item);
            }
        }   
    }
}

function onFailureStates(results)
{
    clearOptions(_cboState);
    addSelectOption(_cboState, "State");
}

function getCommunityList()
{
    var state_pk = _cboState.options[_cboState.selectedIndex].value;
    clearOptions(_cboCommunity);
   
    if(parseInt(state_pk) > 0)
    {
        addLoadingOption(_cboCommunity);
        DachaRealEstate.getCommunities(state_pk, onSuccessCommunity, onFailureCommunity);
    }
    else
    {
        addSelectOption(_cboCommunity, "Community");
    }
}

function onSuccessCommunity(results)
{
    clearOptions(_cboCommunity);
    addSelectOption(_cboCommunity, "Community");
    
    var item; 
    var loopIndex;         
    
    if(results != null)
    {
        if(results.length > 0)
        {
            for(loopIndex = 0; loopIndex < results.length; loopIndex++)
            {
                item = document.createElement("OPTION"); 
                item.text = results[loopIndex].community_name;
                item.value = results[loopIndex].community_pk;
                _cboCommunity.options.add(item);
            }
        }   
    }
}

function onFailureCommunity(results)
{
    clearOptions(_cboCommunity);
    addSelectOption(_cboCommunity, "Community");
}

function doSearch(Path)
{
    var state_pk = _cboState.options[_cboState.selectedIndex].value;
    var strQry;
    var minprice = document.getElementsByName("minprice")[0];
    var maxprice = document.getElementsByName("maxprice")[0];
    var option = document.getElementsByName("option")[0];
    var bedroom = document.getElementsByName("bedroom")[0];
    var type = document.getElementsByName("type")[0];
    
    if(parseInt(state_pk) > 0)
    {
        //document.forms[0].action = Path;
        //document.forms[0].submit();
        strQry = "state=" + state_pk;
        strQry += "&community=" +  _cboCommunity.options[_cboCommunity.selectedIndex].value;
        strQry += "&minprice=" +  minprice.options[minprice.selectedIndex].value;
        strQry += "&maxprice=" +  maxprice.options[maxprice.selectedIndex].value;
        strQry += "&option=" +  option.options[option.selectedIndex].value;
        strQry += "&bedroom=" +  bedroom.options[bedroom.selectedIndex].value;
        strQry += "&type=" +  type.options[type.selectedIndex].value;
        
        location.href = Path + "?" + strQry;
    }
    else
    {
        alert("Please select Emirate");
    }
}