﻿$(document).ready(function(){
    $('#ForSale').click(function(){
        setPrices('#minprice', prices, '$0');
        setPrices('#maxprice', prices, 'no limit');
    });
    $('#ForRent').click(function(){
        setPrices('#minprice', rents, '$0');
        setPrices('#maxprice', rents, 'no limit');
    });
    $('#ForSale').click();
    
    //galery handlers
    $('#trumpGallery').click(function(){
        window.location.href = "bldggallery.aspx";
    });
    $('#greenthalGallery').click(function(){
        window.location.href = "bldggallery.aspx?show=greenthal";
    });
    
    //populate previous search
    if (typeof(search) != 'undefined')
    {
        if (search.SearchType == 0) { $('input#ForSale').attr('checked','checked'); $('#ForSale').click(); }
        else { $('input#ForRent').attr('checked','checked'); $('#ForRent').click(); }
        
        $(search.MGCompanyID).each(function(){
            var val = this;
            $('input[@name="company"]').each(function(){
                if ($(this).val() == val) $(this).attr('checked','checked');
            });
        });
        
        $(search.Ownerships).each(function(){
            var val = this;
            $('input[@name="ownership"]').each(function(){
                if ($(this).val() == val) $(this).attr('checked','checked');
            });
        });
        
        $(search.AptFeatures).each(function(){
            var val = this;
            $('input[@name="amenities"]').each(function(){
                if ($(this).val() == val) $(this).attr('checked','checked');
            });
        });
        
        $(search.BldgFeatures).each(function(){
            var val = this;
            $('input[@name="bldgamenities"]').each(function(){
                if ($(this).val() == val) $(this).attr('checked','checked');
            });
        });
        
        $(search.BuildingAge).each(function(){
            var val = this;
            $('input[@name="buildingage"]').each(function(){
                if ($(this).val() == val) $(this).attr('checked','checked');
            });
        });
        
        if (search.MinPrice>0) SetSelectedValue('#minprice', search.MinPrice);
        if (search.MaxPrice>0) SetSelectedValue('#maxprice', search.MaxPrice);
        
        if (search.MinBeds>0) SetSelectedValue('select[@name="bedrooms"]', search.MinBeds);
        if (search.Bathrooms>0) SetSelectedValue('select[@name="baths"]', search.Bathrooms);
        
        $('input[@name="neighborhood"]').each(function(){
            if (CompareArray($(this).val().split(','), search.Neighborhood))
                $(this).attr('checked','checked');
        });
        
    }
});

var rents = [ 500,600,700,800,900,1000,1250,1500,1750,2000,2500,3000,3500,4000,4500,5000,6000,7000,8000,9000,10000,15000,20000,25000,30000 ];
var prices = [ 100000,200000,300000,400000,500000,600000,700000,800000,900000,1000000,1250000,1500000,
            1750000,2000000,2500000,3000000,3500000,4000000,4500000,5000000,5500000,6000000,6500000,7000000,
            7500000,8000000,8500000,9000000,9500000,10000000,15000000,20000000,30000000,40000000,50000000,
            60000000,70000000,80000000,90000000,100000000 ];

//helper functions

function setPrices(container, pricearray, label)
{
    jQuery(container).empty().append(createOption("",label));
    jQuery.each( pricearray, function(i) {
        jQuery(container).append(createOption(pricearray[i], formatCurrency(pricearray[i]) ));
    });
    jQuery(container).get(0).selectedIndex = 0;
}

function createOption(value, text )
{
	return jQuery('<option></option>').attr("value", value).append(text);
}

function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + '$' + num);
}

function SetSelectedValue(selector, val)
{
    var success = false;
    jQuery(selector + ' option').each(function() {
        if (this.value == val)
        {
            this.selected = true;
            success = true;
        }
    });
    return success;
}

//check if all values in a1 are in a2
function CompareArray(a1, a2)
{
    var compare = true;
    $(a1).each(function(i){ if (!InArray(a1[i], a2)) compare = false; });
    return compare;
}

function InArray(val, array)
{
    var ret = false;
    $(array).each(function(){ if (this == val) ret = true; });
    return ret;
}

    function Check(from,to,cbID)
    {
        var myCheckbox = document.getElementById(cbID);
        if(myCheckbox.checked == true)
        { 
            CheckAll(from,to);
        }
        else
        { 
            UnCheckAll(from,to);
        }
    }
    function CheckAll(from,to)
    { 
   
        for(var i=from;i<=to;i++)
        {        
            var myCheckbox = document.getElementById("cb"+i);
            myCheckbox.checked=true;
        }        
    }
    function UnCheckAll(from,to)
    {  
   
        for(var i=from;i<=to;i++)
        {        
            var myCheckbox = document.getElementById("cb"+i);
            myCheckbox.checked=false;
        }        
    }
    
    
   
