
function checkInput(input, min, max, msg)
{   
	msg = msg + " field has invalid data: " + input.value;
	var lField = ltrim(rtrim(String(input.value)));
    myeareg=new RegExp("^[$]?[0-9]*\\.?[0-9]"); 
	if (!(myeareg.test(lField) && lField!='.'))
	{          
		alert("Only numeric values are allowed!");
		input.focus();
		input.select();
		return false;
	}   	
	var num = parseFloat(input.value);
	if (num < min || max < num)
	{        
		alert(msg + " not in range [" + min + ".." + max + "]"); 
		return false;    
	}      
		return true;
}
function rtrim(argvalue) {
  while (1) {
    if (argvalue.substring(argvalue.length - 1, argvalue.length) != " ")
      break;
    argvalue = argvalue.substring(0, argvalue.length - 1);
  }
  return argvalue;
}

function ltrim(argvalue) {
  while (1) {
    if (argvalue.substring(0, 1) != " ")
      break;
    argvalue = argvalue.substring(1, argvalue.length);
  }
    return argvalue;
}
// Round a field two (2) decimals
function round(number)
{
  return Math.round(number*Math.pow(10,2))/Math.pow(10,2);
}

function trim(str)
{
     return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function count(form)
{
    if (( trim(form.mortAmt.value) != "" && trim(form.numYears.value) != "" && trim(form.propTax.value) != "" &&
        trim(form.debt.value) != "" && trim(form.mortRate.value) != "")) 
{   	
	var tmp1,tmp2, tmp3, tmp4, tmp5, tmp6 ,tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15

	tmp1 = parseFloat(form.mortAmt.value);
	if (isNaN(tmp1)) tmp1=0;
	tmp2 = parseFloat(form.numYears.value);
	if (isNaN(tmp2)) tmp2=0;
	tmp3 = parseFloat(form.mortRate.value);
	if (isNaN(tmp3)) tmp3=0;
	tmp4 = parseFloat(form.propTax.value);
	if (isNaN(tmp4)) tmp4=0;
	tmp5 = parseFloat(form.debt.value);
	if (isNaN(tmp5)) tmp5=0;
	tmp6 = 0;

	 tmp7 = tmp4/12;
	 tmp8= tmp5;
     tmp9 = tmp3/1200;
     tmp10= tmp2 * 12;
     tmp11 = (1 + tmp9);
    for (i=1; i < tmp10; i++) {
        tmp11 = tmp11 * (1 + tmp9);
    }
     tmp12  = tmp1 * tmp11 * tmp9 / (tmp11 - 1);
     tmp13 = tmp12 + tmp7 + tmp8;
     tmp14= (tmp12 + tmp7) / 0.28;
     tmp15 = tmp13 / 0.35;

    if (tmp14 > tmp15) {
		tmp6 = 12 * tmp14;
    } else {
		tmp6 = 12 * tmp15;
    }
    form.mortPay.value = round(tmp12);
	form.totalPay.value = round(tmp13);
    form.reqdSal.value = round(tmp6);
}
  } 
  // n

  function MyCheckEnteredValue(element) {
	var lField = ltrim(rtrim(String(element.value)));

    myReg=new RegExp("^[0-9]*\\.?[0-9]*$"); 
        if (!(myReg.test(lField) && lField!='.')) {
			alert("Please enter valid numerical data in all fields!");
			return false;
		}

	element.value=lField; 
	return true;
}


function rtrim(argvalue) {

  while (1) {
    if (argvalue.substring(argvalue.length - 1, argvalue.length) != " ")
      break;
    argvalue = argvalue.substring(0, argvalue.length - 1);
  }
  return argvalue;
}

function ltrim(argvalue) {

  while (1) {
    if (argvalue.substring(0, 1) != " ")
      break;
    argvalue = argvalue.substring(1, argvalue.length);
  }
    return argvalue;
}

 function trim(str)
{
     return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}  

function count(form) 
{
var tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11
  if ( trim(form.income.value) != "" &&  trim(form.other.value) != "" &&  trim(form.taxes.value) != "" &&  trim(form.insurance.value) != "" &&  trim(form.auto.value) != "" &&  trim(form.cards.value) != "" &&  trim(form.term.value) != "" &&  trim(form.rate.value) != ""){
									 
    tmp12 = Math.round(eval(form.income.value * .28) + eval(form.other.value * .28) - form.taxes.value - form.insurance.value);

    tmp13 = Math.round(eval(form.income.value * .36) + eval(form.other.value * .36) - form.taxes.value -  form.insurance.value - form.auto.value - form.cards.value);

      if (tmp12>tmp13) {
      form.payment.value = tmp13
      }
      else {
      form.payment.value = tmp12
      }

    tmp1 = parseFloat(form.rate.value);
	if (isNaN(tmp1)) tmp1=0;
    tmp2 = parseFloat(form.term.value);
	if (isNaN(tmp2)) tmp2=0;
    tmp3 = parseFloat(form.payment.value);
	if (isNaN(tmp3)) tmp3=0;
    tmp4 = parseFloat(tmp1 / 1200); 
    tmp5 = parseFloat(tmp2 * 12);
    tmp6 = parseFloat(1 + tmp4);
    tmp7 = parseFloat(Math.pow(tmp6, tmp5));
    tmp8 = parseFloat(1 / tmp7);
    tmp9 = parseFloat(1 - tmp8);
    tmp10 = parseFloat(tmp9 / tmp4);
    tmp11 = parseFloat(tmp3 * tmp10);
    form.amount.value = Math.round(tmp11);

    }
else
	{
		alert("Please enter valid numerical data in all fields!");
		return false;
	}
}

// borrow
    




function GetDecimalDelimiter(nCountryCode)
{

       var sRet='';

       switch (nCountryCode)
       {

            case 3:   
                           
                           sRet = '#';
                           break;
            
            case 2:   
                           
                           sRet = ',';
                           break;
            default:
                           sRet = '.';
                           break;
 
        }

      return sRet;

}

function GetCommaDelimiter(nCountryCode)
{

       var sRet='';

       switch (nCountryCode)
       {
            
            case 3:   
                           
                           sRet = '*';
                           break;
            case 2:   
                           
                           sRet = ',';
                           break;
            default:
                           sRet = ',';
                           break;
 
        }

      return sRet;

}

function FormatClean(num)
{
     var sVal='';
     var nVal = num.length;
     var sChar='';
     
   try
   {
       for(i=0;i<nVal;i++)
      {
         sChar = num.charAt(i);
         nChar = sChar.charCodeAt(0);
         if ((nChar >=48) && (nChar <=57))  { sVal += num.charAt(i);   }
      }
   }
    catch (exception) { AlertError("Format Clean",e); }
    return sVal;
}
 

function FormatCurrency(num,nCountryCode)
{       
        var sVal='';
        var minus='';
        var Decimal='';
        Decimal = GetDecimalDelimiter(nCountryCode);
        if (num.lastIndexOf("-") == 0) { minus='-'; }
        if (num.lastIndexOf(Decimal) < 0) { num = num + '00'; }
        num = FormatClean(num);
        sVal = minus + FormatDollar(num,GetCommaDelimiter(nCountryCode)) + GetDecimalDelimiter(nCountryCode) + FormatCents(num); 
        return sVal;
}

function FormatNumber(num,nCountryCode)
{       
        var sVal='';
        var minus='';
        var CommaDelimiter='';

        try 
       {

           CommaDelimiter = GetCommaDelimiter(nCountryCode);

           if (num.lastIndexOf("-") == 0) { minus='-'; }

           num = FormatClean(num);

           num = parseInt(num);

           var samount = new String(num);
             
           for (var i = 0; i < Math.floor((samount.length-(1+i))/3); i++)
          {
             samount = samount.substring(0,samount.length-(4*i+3)) + CommaDelimiter + samount.substring(samount.length-(4*i+3));
           }

        }
         catch (exception) { AlertError("Format Number",e); }
        return minus + samount;
}

function FormatCents(amount)
{
     var cents = '';

      try
      {
           amount = parseInt(amount);
           var samount = new String(amount);

           if (samount.length == 0) { return '00'; }
           if (samount.length == 1) { return '0' + samount; }
           if (samount.length == 2) { return samount; }
         
           cents =  samount.substring(samount.length -2,samount.length);
          
      }
      catch (exception) { AlertError("Format Cents",e); }
      return cents;
}

function FormatDollar(amount,CommaDelimiter)
{
   try 
   {
  
        amount = parseInt(amount);

        var samount = new String(amount);

        if (samount.length < 3) { return 0; }  

        samount =  samount.substring(0,samount.length -2);
             
        for (var i = 0; i < Math.floor((samount.length-(1+i))/3); i++)
        {
           samount = samount.substring(0,samount.length-(4*i+3)) + CommaDelimiter + samount.substring(samount.length-(4*i+3));
         }

   }
    catch (exception) { AlertError("Format Comma",e); }
    return samount;
}

 function AlertError(MethodName,e)
 {
            if (e.description == null) { alert(MethodName + " Exception: " + e.message); }
            else {  alert(MethodName + " Exception: " + e.description); }
 }
// eof 


function add_income() {
	var income = 0;
var salary = document.CalculatorForm.salary.value;
salary = salary.replace(/,/,"");
salary = parseFloat(salary);

var overtime = document.CalculatorForm.overtime.value;
overtime = overtime.replace(/,/,"");
overtime = parseFloat(overtime);

var bonuses = document.CalculatorForm.bonuses.value;
bonuses = bonuses.replace(/,/,"");
bonuses = parseFloat(bonuses);

var commission = document.CalculatorForm.commission.value;
commission = commission.replace(/,/,"");
commission = parseFloat(commission);

var dividends = document.CalculatorForm.dividends.value;
dividends = dividends.replace(/,/,"");
dividends = parseFloat(dividends);

var other_income = document.CalculatorForm.other_income.value;
other_income = other_income.replace(/,/,"");
other_income = parseFloat(other_income);

var rental_income = document.CalculatorForm.rental_income.value;
rental_income = rental_income.replace(/,/,"");
rental_income = parseFloat(rental_income);


income = salary + overtime + bonuses + commission + dividends + other_income + rental_income;
document.CalculatorForm.income_total.value = income;
document.CalculatorForm.income_total.value = FormatCurrency(document.CalculatorForm.income_total.value,1);
return income;
} //eof

function add_outgoing() {
	var outgoing = 0;
	var cc_payments = document.CalculatorForm.cc_payments.value;
	cc_payments = cc_payments.replace(/,/,"");
	cc_payments = parseFloat(cc_payments); 
	
	var auto_loan = document.CalculatorForm.auto_loan.value;
	auto_loan = auto_loan.replace(/,/,"");
	auto_loan = parseFloat(auto_loan);
	
	var alimony = document.CalculatorForm.alimony.value;
	alimony = alimony.replace(/,/,"");
	alimony = parseFloat(alimony);
	
	var other_loans = document.CalculatorForm.other_loans.value;
	other_loans = other_loans.replace(/,/,"");
	other_loans = parseFloat(other_loans);
	
	var mortgage = document.CalculatorForm.mortgage.value;
	mortgage = mortgage.replace(/,/,"");
	mortgage = parseFloat(mortgage);

	var taxes = document.CalculatorForm.taxes.value;
	taxes = taxes.replace(/,/,"");
	taxes = parseFloat(taxes);

	var rental_taxes = document.CalculatorForm.rental_taxes.value;
	rental_taxes = rental_taxes.replace(/,/,"");
	rental_taxes = parseFloat(rental_taxes);

	var fees = document.CalculatorForm.fees.value;
	fees = fees.replace(/,/,"");
	fees = parseFloat(fees); 

	var other = document.CalculatorForm.other.value;
	other = other.replace(/,/,"");
	other = parseFloat(other);
	

	
outgoing = cc_payments + auto_loan + alimony + other_loans + mortgage + taxes + rental_taxes + fees + other;
document.CalculatorForm.outgoing_total.value = outgoing;
document.CalculatorForm.outgoing_total.value = FormatCurrency(document.CalculatorForm.outgoing_total.value,1);
return outgoing;
} //eof
	

function Contact_Validator() {
	
if (document.CalculatorForm.salary.value == "") {
	alert("Please enter your Salary");
	document.CalculatorForm.salary.focus();
	return (false);
}
if (document.CalculatorForm.salary.value == "0") {
	alert("Please enter your Salary");
	document.CalculatorForm.salary.focus();
	return (false);
}


if (document.CalculatorForm.overtime.value == "") {
	alert("Please enter your Overtime \rEnter 0 (zero) if not applicable");
	document.CalculatorForm.overtime.focus();
	return (false);
}
if (document.CalculatorForm.bonuses.value == "") {
	alert("Please enter your Bonuses \rEnter 0 (zero) if not applicable");
	document.CalculatorForm.bonuses.focus();
	return (false);
}
if (document.CalculatorForm.commission.value == "") {
	alert("Please enter your Commission \rEnter 0 (zero) if not applicable");
	document.CalculatorForm.commission.focus();
	return (false);
}
if (document.CalculatorForm.dividends.value == "") {
	alert("Please enter your Dividends \rEnter 0 (zero) if not applicable");
	document.CalculatorForm.dividends.focus();
	return (false);
}
if (document.CalculatorForm.other_income.value == "") {
	alert("Please enter your Other Income \rEnter 0 (zero) if not applicable");
	document.CalculatorForm.other_income.focus();
	return (false);
}






return (false);

} //eof

function theSum(income, outgoing) {
	var total, rate, term, temp3, temp4, temp5, temp6, temp7, temp8, temp9, temp10, temp11;
total = eval(income * .45) - outgoing;
//document.CalculatorForm.borrow_amount.value = total;
	
	rate = document.CalculatorForm.rate.value;
	rate = parseFloat(rate);
	temp4 = parseFloat(rate / 1200);
	term = document.CalculatorForm.term.value;
	term = parseFloat(term);
	temp5 = parseFloat(term * 12);
	temp6 = parseFloat(1 + temp4);
	temp7 = parseFloat(Math.pow(temp6, temp5));
	temp8 = parseFloat(1 / temp7);
	temp9 = parseFloat(1 - temp8);
	temp10 = parseFloat(temp9 / temp4);
	temp11 = parseFloat(total * temp10);
	total = Math.round(total*100)/100;
	document.CalculatorForm.payment_amount.value = total;
	document.CalculatorForm.payment_amount.value = FormatCurrency(document.CalculatorForm.payment_amount.value,1);
	document.CalculatorForm.borrow_amount.value = Math.round(temp11*100)/100;
	document.CalculatorForm.borrow_amount.value = FormatCurrency(document.CalculatorForm.borrow_amount.value,1);

} // eof

function cal() {
//Contact_Validator();
income = add_income();
outgoing = add_outgoing();
theSum(income, outgoing);
} //eof
