// JavaScript Document

function mailpage()
{
mail_str = "mailto:?subject=Information from Home123 - " + document.title;
mail_str += "&body=Information as Easy as Home123 - " + document.title;
mail_str += ".  To view this page, please click the following link - " + location.href; 
location.href = mail_str;
}

function DoSearch(txtBox_id)
    {
	    if(txtBox_id != null)
	    {
	        txtBox = document.getElementById(txtBox_id);
	    }
            if(txtBox.value != "")
	        {           
		        var pageUrl = "../SearchResult.aspx?key="+txtBox.value;
		        window.open (pageUrl, 'windowName', 'width=1000,height=630 status=yes,location=yes,toolbar=no,scrollbars=yes,resizable=1'); 
		    }
			
		    return false;
	}
	
	
 function imgButtonRollOver(row, effect)
    {
        if(row.all)
        {
	        if(effect==1) {
		        row.all.item(0).className = "table-button-left-hover";
		        row.all.item(1).className = "table-button-middle-hover";
		        row.all.item(2).className = "table-button-right-hover";
	        } else {
		        row.all.item(0).className = "table-button-left";
		        row.all.item(1).className = "table-button-middle";
		        row.all.item(2).className = "table-button-right";
	        }
        }
        else {
	        if(effect==1) {
		        row.childNodes.item(1).className = "table-button-left-hover";
		        row.childNodes.item(3).className = "table-button-middle-hover";
		        row.childNodes.item(5).className = "table-button-right-hover";
	        } else {
		        row.childNodes.item(1).className = "table-button-left";
		        row.childNodes.item(3).className = "table-button-middle";
		        row.childNodes.item(5).className = "table-button-right";
	        }
        }
    }


function ShowEMICalculatorControlvalues(HiddenValue)
{
   try
      {
         var url = 'Dialogs/EditHeading.aspx';
      
          var dialogFeatures ;
          dialogFeatures = 'dialogHeight:115px;dialogWidth:440px;center:yes;dialogHide:no;edge:raised;resizable:no;scroll:yes;status:no;unadorned:no;'
          var returnValue =  window.showModalDialog(url,window,dialogFeatures);
         
            HiddenValue.value = returnValue;
            return true;
      }
      catch(err)
      {
        alert(err.description);
      }
    return false;
}


function CloseEMICalculatorControlVerbs(tr_id,hdn)
{
   hdn.value = "true";
   tr_id.style.display = 'none';
   return false;
}

function doAmortization() 
{

    ShowErrorMessage("");
    document.getElementById("_ctl0_ContentPlaceHolder1_EMICalculator1_txtBoxMonthlyPayments").value = '';
    var unroundedPayment = null;
    var loanAmount   = document.getElementById("_ctl0_ContentPlaceHolder1_EMICalculator1_txtLoanAmount").value;
    var rateValue    = document.getElementById("_ctl0_ContentPlaceHolder1_EMICalculator1_txtBoxRate").value;
    var term         = document.getElementById("_ctl0_ContentPlaceHolder1_EMICalculator1_txtBoxTerms").value;
    
    if(ValidateRequired(document.getElementById("_ctl0_ContentPlaceHolder1_EMICalculator1_txtLoanAmount")) && ValidateRequired(document.getElementById("_ctl0_ContentPlaceHolder1_EMICalculator1_txtBoxTerms")) && ValidateRequired(document.getElementById("_ctl0_ContentPlaceHolder1_EMICalculator1_txtBoxRate")) )
    {
        if(ValidateFloat(document.getElementById("_ctl0_ContentPlaceHolder1_EMICalculator1_txtLoanAmount")))
        {
            if(ValidateIntNotNegative(document.getElementById("_ctl0_ContentPlaceHolder1_EMICalculator1_txtBoxTerms")))
            {
                 if(ValidateFloat(document.getElementById("_ctl0_ContentPlaceHolder1_EMICalculator1_txtBoxRate")))
                 {  
                    ShowErrorMessage(""); 
                    var rate = rateValue / 1200.0; 
                    var nrOfMonths = 12 * term;
                    var factr        = 1;
                    var ratePlusOne  = rate + 1;
                	
                    for (var i = 0; i <  nrOfMonths; i++)
                    {
                        factr *= ratePlusOne;
                    }
                    if (factr > 1) 
                    {
                         unroundedPayment =( loanAmount * factr * rate) / (factr - 1);
                    }
                    
                    document.getElementById("_ctl0_ContentPlaceHolder1_EMICalculator1_txtBoxMonthlyPayments").value = displayDec(unroundedPayment,2);
                }
            }
        }
    }
    return false;
}
function displayDec(val, decs){
    var factr = 1;
    for (var i = 0; i < decs; i++){
        factr *= 10;
    }
    var outputStr = Math.round(factr * val) + '';
    while (outputStr.length - decs < 1){
        outputStr = '0' + outputStr;
    }
    var pos = outputStr.length - decs;
    return outputStr.substring(0, pos) + '.' + outputStr.substring(pos);
}

function ValidateRequired(elt)
{

       if (elt != null && elt.value != 'undefined')
       {
        strValue =  TrimSpaces(elt.value);
        if (strValue == "")
        {
            ShowErrorMessage("Enter the value.");
            elt.focus();
            return false;
        }
      }
      return true;
    }
    
function ValidateIntNotNegative(elt)
{
    if (elt != null)
    {
        strValue =  TrimSpaces(elt.value);
        if(strValue != "")
        {
            pattern = /^[]?[0-9]*$/;
            if (pattern.test(strValue)==false)
            {
                ShowErrorMessage("Enter positive integer.");
                elt.focus();
                return false;
            }
        }
        return true
    }
    return false;
}


 function ValidateFloat(elt)
{
    if(elt != null)
    {

        strValue =  TrimSpaces(elt.value);
        if(strValue != "")
        {
          var pattern = /^\d*(\.\d{1,5})?$/;
          if (pattern.test(strValue) == false)
          {
            ShowErrorMessage("Enter positive value with atlost 5 decimal places.");
            elt.focus();
            return false;
          }
        }
        return true;
    }
}



function TrimSpaces(sString)
{
 while (sString.substring(0,1) == ' ')
 {
    sString = sString.substring(1, sString.length);
 }
 
 while (sString.substring(sString.length-1, sString.length) == ' ')
 {
    sString = sString.substring(0,sString.length-1);
 }
 
 return sString;
}

function ShowErrorMessage(msg)
{
    if(document.getElementById("_ctl0_ContentPlaceHolder1_EMICalculator1_lblErrorMsg") != null)
    {
        document.getElementById("_ctl0_ContentPlaceHolder1_EMICalculator1_lblErrorMsg").innerHTML = msg;
    }
}