﻿var chkFurnished;
var chkNonFurnished;
var chkForRent;
var chkForSell;
var ddlPriceOperator ,ddlCurrency,txtPrice, txtArea ;
var checkCount =0;
var cancelPostBack = false;
function $(id) {return document.getElementById(id);}
function showHidePropertyFilters(ddlProperty)
{
    $("divPriceFilter").style.display = "inline";
    $("divFurnishedFilter").style.display = "inline";
    $("divRentSellFilter").style.display = "inline";
    $("divAreaFilter").style.display = "inline";
    $("divRoomsFilter").style.display = "inline";
    if (ddlProperty.value == "land")
    {
        $("divFurnishedFilter").style.display = "none";
        $("divRentSellFilter").style.display = "none";
        $("divRoomsFilter").style.display = "none";
    }
}

function checkCompareCount(checkedItem)
{
    checkCount = $('hidCheckCount').value;
    if(checkedItem.checked)
    {
        if(checkCount >= 3)
        {
            alert('You can compare only between 3 items at max');
            checkedItem.checked = false;
        }
        else
        {
            checkCount++;
        }
    }
    else
    {
            checkCount--;
    }
    $('hidCheckCount').value = checkCount;
}

function checkMinimumCompareCount()
{
    cancelPostBack = false;
    if(checkCount <= 1)
    {
        alert("Please select at least 2 properties to compare between");
        cancelPostBack = true;
        return false;
    }
}

function getVars()
{
    Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(CheckStatus);

    // furnished
    chkFurnished = $("searchBox_chkFurnished");
    chkNonFurnished = $("searchBox_chkNonFurnished");
    // for rent / sell
    chkForRent = $("searchBox_chkForRent");
    chkForSell = $("searchBox_chkForSell");
    // sell price
    ddlPriceOperator = $("searchBox_ddlPriceOperator");
    ddlCurrency = $("searchBox_currencyList_ddlCurrency");
    txtPrice = $("searchBox_txtPrice");
    // Area
    txtArea = $("searchBox_txtArea");
   
}

function checkFurnished(source)
{
    if(!chkFurnished.checked && !chkNonFurnished.checked)
    {
        if(source.id == "chkFurnished")
            chkNonFurnished.checked = true;
        else
            chkFurnished.checked = true;
    }
}


function showHidePriceRange(ddlPrice)
{
    
    if(ddlPrice.selectedIndex > 0)
    {
        $('divPriceRange').style.display = 'inline';     
    }
    else
    {
        $('divPriceRange').style.display = 'none'; 
    }
}

function showHideAreaRange(ddlArea)
{
    if(ddlArea.selectedIndex > 0)
        $('divAreaRange').style.display = 'inline'; 
    else
        $('divAreaRange').style.display = 'none'; 
}

//function showHideRoomRange(ddlRoom)
//{
//    if(ddlRoom.selectedIndex > 0)
//        $('divRoomRange').style.display = 'inline'; 
//    else
//        $('divRoomRange').style.display = 'none'; 
//}
function checkRentSell(source)
{
    if(!chkForRent.checked && !chkForSell.checked)
    {
        if(source.id == "chkForRent")
            chkForSell.checked = true;
        else
            chkForRent.checked = true;
    }

    if(chkForSell.checked)
        $('divPriceFilter').style.display = 'inline'; 
    else
        $('divPriceFilter').style.display = 'none';
}

function validateFilters()
{
    lblErrorMessageID = 'lblErrorMessage';
    if($('divPriceRange').style.display == 'inline' && ddlPriceOperator.selectedIndex > 0)
    {
        if(!validateRequired(txtPrice,"Sell Price Required") || !validateNumeric(txtPrice,"Invalid Sell Price Filter"))
        {
            cancelPostBack = true;
            return false;
        }
    }
    if($('divAreaRange').style.display == 'inline')
    {
        if(!validateRequired(txtArea,"Area Required") || !validateNumeric(txtArea,"Invalid Area Filter"))
        {
            cancelPostBack = true;
            return false;
        }
    }
    
    $(lblErrorMessageID).style.display = 'none';
    cancelPostBack = false;
    //alert('all ok');
    return true;
}

function CheckStatus(sender, args)
{
    if(cancelPostBack)
        args.set_cancel(true);
}

function ValidateMonthlyFees(sender, args)
{
    var chkBoxId;
    // chek for naming container in the aspx page that include this script
    if(namingContainer)
        chkBoxId = namingContainer + 'chkForRent';
    else
        chkBoxId = 'chkForRent';
    if(document.getElementById(chkBoxId).checked)
    {
        if(args.Value.length == 0 || isNaN(args.Value) )
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }
    else
    {
        args.IsValid = true;
    }
}
