﻿function HideAll() {
    $("span.ShowTextField").each(
        function(i) {
            $("div#" + $(this).attr("title")).hide();
        }
    )
}



$(document).ready(function() {
    InitSearch();
})

function InitSearch() {
    HideAll();

    // Code added to hide the Rental checkbox list for the Lee County Region .
    ddlRegion = document.getElementById('ctl00_mainContent_drpRegion');
    /*$(".Searchcriteria label").each(
    function(i) {
    $("div#" + $(this).attr("for")).hide();
    }
    )*/

    $(".Searchcriteria .Popupsearch label").each(function() {
        if ($(this).html().toLowerCase() == "not applicable") {
            $("#Adv" + $(this).parents(".Popupsearch").attr("id")).hide();
        }
    })

    $("span.ShowTextField").click(
        function(e) {
            HideAll();
            $("div#" + $(this).attr("title")).show();
        }
    )

    $(".Searchcriteria div.Popupsearch").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());
                    //alert("arrValues.length = " + arrValues.length);
                    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 {

                if ($("span[title='" + $(this).parents("div").attr("id") + "']").html() == "" || arrValues.length == "0")
                    $("span[title='" + $(this).parents("div").attr("id") + "']").html("Any(Click for more)");
                //alert($("span[title='" + $(this).parents("div").attr("id") + "']").html());
                break;
            }
        }
    });

    $(".Searchcriteria input[type=checkbox]:checked").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 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);
    });
    CheckIsSaveSearch();
}


function CheckIsSaveSearch() {
    var fullURL = window.location.href;
    var QueryString = fullURL.substring(fullURL.indexOf('?') + 1, fullURL.length)
    var SearchCriteriaKey = getQuerystring("SearchCriteriaKey", "");
    if (SearchCriteriaKey != "") {
        $("ul#SearchNavTabs li a").each(function() {
            $(this).attr("href", $(this).attr("href") + "?" + QueryString);
        })
        setAdvVisible();            
    }
}


function startCalculate()
{
	var MinPay  = $('#ctl00_mainContent_MinPayment').val();
	if (MinPay == '' || isNaN(MinPay))
	{
		alert('Please enter the required field');
		$('#ctl00_mainContent_MinPayment').focus();
		return;
	}
	var MaxPay  = $('#ctl00_mainContent_MaxPayment').val();
	if (MaxPay == '' || isNaN(MaxPay))
	{
		alert('Please enter the required field');
		$('#ctl00_mainContent_MaxPayment').focus();
		return;
	}
	if (parseInt(MinPay)>parseInt(MaxPay))
	{
		alert('Invalid entry for maximum payment');
		return;
	}
	var DownPay = ($('#ctl00_mainContent_DownPayment').val()!='') ? $('#ctl00_mainContent_DownPayment').val() : 0;
	if (DownPay == '0')	$('#ctl00_mainContent_DownPayment').val('0');
	if (isNaN(DownPay)) {alert('Please enter a valid down payment amount'); $('#ctl00_mainContent_DownPayment').focus();}
	var LoanTerm = $('#ctl00_mainContent_RateID').val();
	if (LoanTerm=='-1')
	{
		alert('Please enter the required field');
		$('#ctl00_mainContent_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_mainContent_EstMinPrice').val(parseInt(MinPrice));
	$('#ctl00_mainContent_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;
		
	}
}

    var flag_adv=0;
    function setAdvVisible(){
        if(flag_adv==0){
            $("#advtabs").show();
            flag_adv=1;
        }
        else{
            $("#advtabs").hide();
            flag_adv=0;
        }
    }   
    
    
    
			    
    function InitFilter(ctrl1, ctrl2, ctrl3)
    {
        
        $("#" + ctrl1).keydown(function (e) {
            eWhere = '';
            if($("#" + ctrl2).val() == '2') {
                eWhere = " mlsboard = 'Mobile MLS' ";
            }
            else if($("#" + ctrl2).val() == '1') {
                eWhere = " mlsboard = 'Baldwin MLS' ";
            }
            else if($("#" + ctrl2).val() == '3') {
                eWhere = " mlsboard = 'Auburn/Opelika MLS' ";
            }
            else if($("#" + ctrl2).val() == '4') {
                eWhere = " mlsboard = 'Pensacola MLS' ";
            }
            else if($("#" + ctrl2).val() == '5') {
                eWhere = " mlsboard = 'Lake Martin' ";
            }
            else if($("#" + ctrl2).val() == '6') {
                eWhere = " mlsboard = 'SmithLake/Cullman' ";
            }
            else if($("#" + ctrl2).val() == '7') {
                eWhere = " mlsboard = 'Mlsboard=Birmingham MLS' ";
            }
            else if($("#" + ctrl2).val() == '8') {
                eWhere = " mlsboard = 'Montgomery MLS' ";
            }
            else if($("#" + ctrl2).val() == '9') {
                eWhere = " mlsboard = 'Huntsville' ";
            }
            else if($("#" + ctrl2).val() == '10') {
                eWhere = " mlsboard = 'Tuscaloosa MLS' ";
            }            
            ePropTypes = '';
            $("#" + ctrl3 + " input:checkbox").each( function (ec) {
                if($(this).attr("checked") == true)
                {			                    
                    ePropTypes += '\'' + $('label[for="' + $(this).attr("id") + '"]').html() + '\',';
                }
            });
            
            ePropTypes = ePropTypes.substring(0,ePropTypes.length-1);			            
            
            
            if(ePropTypes != '') eWhere += 'and ppropertytype in (' + ePropTypes + ')'
            
            $("#" + ctrl1).attr("eWhere", eWhere);
            
        });
    }
    
    function InitFilterFirst(ctrl2)
    {
            eWhere = '';
            if($("#" + ctrl2).val() == '2') {
                eWhere = " mlsboard = 'Mobile MLS' ";
            }
            else if($("#" + ctrl2).val() == '1') {
                eWhere = " mlsboard = 'Baldwin MLS' ";
            }
            else if($("#" + ctrl2).val() == '3') {
                eWhere = " mlsboard = 'Auburn/Opelika MLS' ";
            }
            else if($("#" + ctrl2).val() == '4') {
                eWhere = " mlsboard = 'Pensacola MLS' ";
            }
            else if($("#" + ctrl2).val() == '5') {
                eWhere = " mlsboard = 'Lake Martin' ";
            }
            else if($("#" + ctrl2).val() == '6') {
                eWhere = " mlsboard = 'SmithLake/Cullman' ";
            }
            else if($("#" + ctrl2).val() == '7') {
                eWhere = " mlsboard = 'Mlsboard=Birmingham MLS' ";
            }
            else if($("#" + ctrl2).val() == '8') {
                eWhere = " mlsboard = 'Montgomery MLS' ";
            }
            else if($("#" + ctrl2).val() == '9') {
                eWhere = " mlsboard = 'Huntsville' ";
            }
            else if($("#" + ctrl2).val() == '10') {
                eWhere = " mlsboard = 'Tuscaloosa MLS' ";
            } 
            $("#" + ctrl2).attr("eWhere", eWhere);       
    }
    
    function getComplex() 
     {   
      
       if(document.getElementById("ctl00_mainContent_drpRegion").value==1)
       { 
           if (document.getElementById("ctl00_mainContent_propertyType_1").checked || document.getElementById("ctl00_mainContent_propertyType_4").checked)
           {             
          
            $("#complexname").show();
           }
           else
           {
            var Al=$("#ctl00_mainContent_lbComplex");
            $("option", Al).attr('selected', false);
            $("#complexname").hide();
           }            
       }
    } 
