﻿


$(document).ready(function()
{
    initEvents();
    PrintMySearchCriteria();
    
    var objArea = document.getElementById("ctl00_mainContent_lstAreaSira");
    
    $("#ctl00_mainContent_txtSearchName").hide();
    $("#ctl00_mainContent_lnkSaveSearch").hide();
    $("#ctl00_mainContent_txtSearchFrequency").hide();
});
    
$(function(){

	$('#goto_step_1').click(function(e){
		e.preventDefault();
		swap($('#by_area_step_2'),$('#by_area_step_1'));
	});
	function swap(el1,el2){
		$(el1).fadeOut(function(){
			$(el2).fadeIn();
		});
	}
});

function GoSearch() {
    document.location.href = ('Search.aspx?i=' + Math.random());
}

function initEvents()
{
    //SetCriteria("9396", document.getElementById('ctl00_mainContent_drpRegion').value);
    
    $("select").bind("change", function(e) {
        //SetCriteria($("#" + this.id).attr("fieldkey"), $("select#" + this.id + ":selected").text());
        selectedValues = '';
        for(i=0; i<this.options.length; i++)
        {
            if(this.options[i].selected) { selectedValues += this.options[i].text + '#'; }
        }
        SetCriteria($("#" + this.id).attr("fieldkey"), selectedValues);
    });
    
    $("input:text").blur(function(e) { 
        SetCriteria($("#" + this.id).attr("fieldkey"), $("input#" + this.id).val());
    });
    $("input:text").keyup(function(e) { 
        SetCriteria($("#" + this.id).attr("fieldkey"), $("input#" + this.id).val());
    });

    
    $("input:checkbox").click(function(e) {
        IdsArray = this.id.split("_");
        MainCtrlId = "";
        for(i=0; i<IdsArray.length-1; i++)
        {
            if(MainCtrlId != "") MainCtrlId += "_";
            MainCtrlId += IdsArray[i];
        }
        //if(isNaN(IdsArray[IdsArray.length-1])) MainCtrlId += IdsArray[IdsArray.length-1];
        
        selectedValues = '';
        for(i=0; i<100; i++)
        {
            if(document.getElementById(MainCtrlId + "_" + i) != null)
            {
                if(document.getElementById(MainCtrlId + "_" + i).checked == true)
                {
                    selectedValues += $("label[for='" + MainCtrlId + "_" + i + "']").html() + '#';
                }
            }
            else
            {
                break;
            }
        }
        SetCriteria($("#" + MainCtrlId).attr("fieldkey"), selectedValues);
    });
}

function SetCriteria(fldKey, value)
{
    for(i=0; i<MySearch.Groups.length; i++)
    {
        for(j=0; j<MySearch.Groups[i].Fields.length; j++)
        {
            if(MySearch.Groups[i].Fields[j].Key == fldKey)
            {
                MySearch.Groups[i].Fields[j].Value = value;
            }
        }
    }
    PrintMySearchCriteria();
}

function PrintMySearchCriteria()
{   
    html = '<ul>';
    for(i=0; i<MySearch.Groups.length; i++)
    {
        html += '<li class="CriteriaGrpHead">' + MySearch.Groups[i].Name + '</li><ul>';
        for(j=0; j<MySearch.Groups[i].Fields.length; j++)
        {
            //html += '<li class="CriteriaField">' + MySearch.Groups[i].Fields[j].Label + '<strong>' + MySearch.Groups[i].Fields[j].Value + '</strong></li><ul>';
            NoOfItems = MySearch.Groups[i].Fields[j].Value.split('#').length;
            for(k=0; k<NoOfItems; k++)
            {
                if(MySearch.Groups[i].Fields[j].Value.split('#')[k].length > 1)
                    html += '<li class="CriteriaField">' + MySearch.Groups[i].Fields[j].Label + '<strong>' + MySearch.Groups[i].Fields[j].Value.split('#')[k] + '</strong></li>';
            }
        }
        html += "</ul>";
    }
    html += '</ul>';
    
    $("#MySearchCriteria").html(html);
}


function startCalculate()
	{
		var MinPay  = $('#MinPayment').val();
		if (MinPay == '' || isNaN(MinPay))
		{
			alert('Please enter the required field');
			$('#MinPayment').focus();
			return;
		}
		var MaxPay  = $('#MaxPayment').val();
		if (MaxPay == '' || isNaN(MaxPay))
		{
			alert('Please enter the required field');
			$('#MaxPayment').focus();
			return;
		}
		if (parseInt(MinPay)>parseInt(MaxPay))
		{
			alert('Invalid entry for maximum payment');
			return;
		}
		var DownPay = ($('#DownPayment').val()!='') ? $('#DownPayment').val() : 0;
		if (DownPay == '0')	$('#DownPayment').val('0');
		if (isNaN(DownPay)) {alert('Please enter a valid down payment amount'); $('#DownPayment').focus();}
		var LoanTerm = $('#RateID').val();
		if (LoanTerm=='-1')
		{
			alert('Please enter the required field');
			$('#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.');
		$('#EstMinPrice').val(parseInt(MinPrice));
		$('#EstMaxPrice').val(parseInt(MaxPrice));		
		if (LoanTerm!='-1')
		{
			var MinPriceOptions = document.getElementById('ctl00_mainContent_txtPriceMin').options;
			var MaxPriceOptions = document.getElementById('ctl00_mainContent_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_mainContent_txtPriceMin').options[selectMin-1].selected=true;
			document.getElementById('ctl00_mainContent_txtPriceMax').options[selectMax].selected=true;
			
		}
	}
	
//	function resetForm()
//    {
//        $('#MinPayment').val('');
//        $('#MaxPayment').val('');
//        $('#DownPayment').val('');
//        $('#EstMinPrice').val('');
//        $('#EstMaxPrice').val('');
//        $('#RateID').val('-1');
//        $('#5020_min_fullSearch').val('-1');
//        $('#5020_max_fullSearch').val('-1');
//        window.location.href=window.location.href;
//    }
