﻿function HideAll() {
    $("span.ShowTextField").each(
        function(i) {
            $("div#" + $(this).attr("title")).hide();
        }
    )
}



$(document).ready(function() 
{
    HideAll();
    
    // Code added to hide the Rental checkbox list for the Lee County Region .
    ddlRegion=document.getElementById('ctl00_cphMain_drpRegion');
    
    $(".Searchcriteria label").each(
        function(i) {
            $("div#" + $(this).attr("for")).hide();
        }
    )
            
    $("span.ShowTextField").click(
        function(e) {
            HideAll();
            $("div#" + $(this).attr("title")).show();
        }
    )
    $(".Searchcriteria input:checkbox").each(function(e) { 
        IdsArray = this.id.split("_");
        MainCtrlId = "";
        for(i=0; i<IdsArray.length-1; i++)
        {
            if(MainCtrlId != "") MainCtrlId += "_";
            MainCtrlId += IdsArray[i];
        }
        var arrValues = new Array();
        for(i=0; i<100; i++)
        {
            if(document.getElementById(MainCtrlId + "_" + i) != null)
            {
                if(document.getElementById(MainCtrlId + "_" + i).checked == true)
                {
                    arrValues.push($("label[for='" + MainCtrlId + "_" + i + "']").html());
                    if(arrValues.length > 0)
                        $("span[title='" + $(this).parents("div").attr("id") + "']").html( arrValues.join(", ") );
                    else
                        $("span[title='" + $(this).parents("div").attr("id") + "']").html( "Any(Click for more)" );
                }
            }
            else
            {
                break;
            }
        }
     } );
     
    $(".Searchcriteria input:checkbox").click( function(e) {
        IdsArray = this.id.split("_");
        MainCtrlId = "";
        for(i=0; i<IdsArray.length-1; i++)
        {
            if(MainCtrlId != "") MainCtrlId += "_";
            MainCtrlId += IdsArray[i];
        }
        var arrValues = new Array();
        for(i=0; i<100; i++)
        {         
            if(document.getElementById(MainCtrlId + "_" + i) != null)
            {
                if(document.getElementById(MainCtrlId + "_" + i).checked == true)
                {
                    arrValues.push($("label[for='" + MainCtrlId + "_" + i + "']").html());
                    if(arrValues.length > 0)
                        $("span[title='" + $(this).parents("div").attr("id") + "']").html( arrValues.join(", ") );
                    else
                        $("span[title='" + $(this).parents("div").attr("id") + "']").html( "Any(Click for more)" );
                }
            }
            else
            {
                break;
            }
        }        
    }); 
    
    $(".Searchcriteria select").each(function(e) {
        selectedValues = '';
        for(i=0; i<this.options.length; i++)
        {
            if(this.options[i].selected) { selectedValues += this.options[i].text; }
        }
        if(selectedValues=='Any')
        selectedValues='Any (Click for more)';
       $("span[title='" + $(this).parents("div").attr("id") + "']").html( selectedValues);       
    }); 
    $(".Searchcriteria select").change( function(e) {  
        selectedValues = '';
        for(i=0; i<this.options.length; i++)
        {
            if(this.options[i].selected) { selectedValues += this.options[i].text; }
        }
       $("span[title='" + $(this).parents("div").attr("id") + "']").html( selectedValues);       
    });    
});

function startCalculate()
{
	var MinPay  = $('#ctl00_cphMain_MinPayment').val();
	if (MinPay == '' || isNaN(MinPay))
	{
		alert('Please enter the required field');
		$('#ctl00_cphMain_MinPayment').focus();
		return;
	}
	var MaxPay  = $('#ctl00_cphMain_MaxPayment').val();
	if (MaxPay == '' || isNaN(MaxPay))
	{
		alert('Please enter the required field');
		$('#ctl00_cphMain_MaxPayment').focus();
		return;
	}
	if (parseInt(MinPay)>parseInt(MaxPay))
	{
		alert('Invalid entry for maximum payment');
		return;
	}
	var DownPay = ($('#ctl00_cphMain_DownPayment').val()!='') ? $('#ctl00_cphMain_DownPayment').val() : 0;
	if (DownPay == '0')	$('#ctl00_cphMain_DownPayment').val('0');
	if (isNaN(DownPay)) {alert('Please enter a valid down payment amount'); $('#ctl00_cphMain_DownPayment').focus();}
	var LoanTerm = $('#ctl00_cphMain_RateID').val();
	if (LoanTerm=='-1')
	{
		alert('Please enter the required field');
		$('#ctl00_cphMain_RateID').focus();
		return;
	}

	var IR = rateArray[LoanTerm][0];
	var LoanMonths   = rateArray[LoanTerm][1];
	if (LoanTerm !='5')
	{
		var MinPrice = parseInt(((Math.pow((1 + IR/12),LoanMonths) - 1)/((IR/12)*Math.pow((1+IR/12),LoanMonths))) * MinPay) + parseInt(DownPay);
		var MaxPrice = parseInt(((Math.pow((1 + IR/12),LoanMonths) - 1)/((IR/12)*Math.pow((1+IR/12),LoanMonths))) * MaxPay) + parseInt(DownPay);
	}else if (LoanTerm == '5')
	{
		var MinPrice = MinPay/(IR/12);
		var MaxPrice = MaxPay/(IR/12);
	}
	//$('#estimatedPrice').html('Estimated Min Price: <span>$<input type=text size=8 readonly=readonly value=' + Comma(parseInt(MinPrice)) +' />'+ '</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Estimated Max Price: <span>$<input type=text value=' + Comma(parseInt(MaxPrice)) +' readonly=readonly size=8 />' + '</span>');
	$('#estimatedPrice').html('Click Find My Home below to search all match properties.');
	$('#ctl00_cphMain_EstMinPrice').val(parseInt(MinPrice));
	$('#ctl00_cphMain_EstMaxPrice').val(parseInt(MaxPrice));		
	if (LoanTerm!='-1')
	{
		var MinPriceOptions = document.getElementById('ctl00_cphMain_txtPriceMin').options;
		var MaxPriceOptions = document.getElementById('ctl00_cphMain_txtPriceMax').options;
		var selectMin = -1;
		var selectMax = -1;
		$.each( MinPriceOptions, function(i, n){
			if (MinPrice <= n.value && selectMin==-1){
				selectMin = i;
			}
		});
		$.each( MaxPriceOptions, function(i, n){
			if (MaxPrice <= n.value && selectMax==-1){
				selectMax = i;
			}
		});

		if (selectMin==0) selectMin = 1;
		if (selectMax==0) selectMax = 1;
		document.getElementById('ctl00_cphMain_txtPriceMin').options[selectMin-1].selected=true;
		document.getElementById('ctl00_cphMain_txtPriceMax').options[selectMax].selected=true;
		
	}
}

    var flag_adv=0;
    function setAdvVisible(){
        if(flag_adv==0){
            $("#advtabs").show();
            flag_adv=1;
        }
        else{
            $("#advtabs").hide();
            flag_adv=0;
        }
    }   