var paymentAmount = 0;

function format_me(obj_name){
		obj_name.value = formatCurrency(obj_name.value);
}

function formatCurrency(num) {
		num = num.toString().replace(/$|\,/g,'');
		if(isNaN(num)) num = "0";

		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
		if(cents<10)
		cents = "0" + cents;
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
		//no cents
		//return (((sign)?'':'-') + '$' + num + '.' + cents);
		return (((sign)?'':'-') + num);
}

function check_form_1() {
  var value=(document.forms['calculator_step_1']["value"].value).replace(",","");
  //alert(value);
  var growth_rate=document.forms['calculator_step_1']["growth_rate"].value;
  var interest_rate=document.forms['calculator_step_1']["interest_rate"].value;
  var age_1 = document.forms['calculator_step_1']["age_1"].value;
  var age_2 = document.forms['calculator_step_1']["age_2"].value;
  var  message_1 = "You must enter:\n";
  var pass= true;
  
  if(age_2!=""){
	if(age_2 < 55 && age_1 > 59){
	//alert(age_2);
		pass = false;
		message_1+=" - an age_1 for the second borrower from 55 or over\n";
	}
	else{
		if(age_2 < age_1){
			document.forms['calculator_step_1']["age_1"].value=age_2;
		}
		else{
			document.forms['calculator_step_1']["age_1"].value=age_1;
		}
	}
  }
  else{
  	document.forms['calculator_step_1']["age_1"].value=age_1;
  }
  var age_1=document.forms['calculator_step_1']["age_1"].value;
  //alert(age_1);
  if(age_1 < 60){
  	if(age_2==""){
		pass = false;
		message_1+=" - an age_1 of 60 or over\n";
	}
  } 
  if(age_1 < 60 ){
  	if(age_2!="" && age_2 < 60){
		pass = false;
		message_1+=" - an age_1 for the second borrower from 60 or over\n";
	}
	else if(age_2!="" && age_1 < 55){
		pass = false;
		message_1+=" - an age_1 for the first borrower from 55 or over\n";
	}
  } 
  if(value=="" || value <= 0) {
 	 pass = false;
	 message_1+=" - an estimated value for your property'.\n";
  }
  
  if(growth_rate=="" || growth_rate > 8 || growth_rate < 0){
 	 pass = false;
	 message_1+=" - a property growth rate between 0% and 8%\n";
  }
  if(interest_rate > 14 || interest_rate < 4){
 	 pass = false;
	 message_1+=" - an interest rate between 4% and 14%\n";
  }
  if(pass){
  	return true;
  }
  else{
   alert(message_1);
  	return false;
  }
}

//display second borrower details field
function manage_section(id,action) {
   var theIE;
   if (document.getElementById) {
      theIE = document.getElementById(id);
   } else if (document.all) {
      theIE = eval("document.all."+id);
   }
   if (!theIE) return;
	if (action==1){
		theIE.style.display = "";
	}
    else if (action==0){
		theIE.style.display = "none";
		document.forms['calculator_step_1']["age_1"].value=document.forms['calculator_step_1']["age_1"].value;
		document.forms['calculator_step_1']["age_2"].value="";
		//alert(document.forms['calculator_step_1']["age_1"].value);
	}
   //ucalClose();
}

function checkage_2(){
	var age_2 = document.forms['calculator_step_1']["age_2"].value;
	if(age_2!=""){
	if (document.getElementById) {
      theIE = document.getElementById('divdetails2');
   } else if (document.all) {
      theIE = eval("document.all.divdetails2");
   }
   	  theIE.style.display = "";
	}
}













function change_payment(interval){
	var nbYears = 5;
	var lump = document.forms['calculator_step_3']["initialLumpSum"].value.replace(",","");
	var maxLoan = document.forms['calculator_step_3']["maxLoan"].value-parseInt(lump);
	var fee = 0;
	if(document.forms['calculator_step_3']["fee"].checked==true){
		fee = 1149;
	}
	if(interval==1){
		document.forms['calculator_step_3']["annualPaymentAmount"].value=formatCurrency((maxLoan/nbYears));
		document.forms['calculator_step_3']["numberOfAnnualPayments"].value=1;
	}
	else{
		document.forms['calculator_step_3']["annualPaymentAmount"].value=formatCurrency((maxLoan/nbYears/4));
		document.forms['calculator_step_3']["numberOfAnnualPayments"].value=4;
	}
}

function change_payment_from_lump_sum(){
	var nbYears = 5;
	var lump = document.forms['calculator_step_3']["initial_lump_sum"].value.replace(",","");
	var maxLoanWLump = document.forms['calculator_step_3']["maximum_loan"].value;
	var maxLoan = 0;
	if(parseInt(lump) > parseInt(maxLoanWLump)){
		alert("Loan amount "+formatCurrency(lump)+" requested exceeds maximum loan available "+formatCurrency(maxLoanWLump)+". Please try again.");
		maxLoan = document.forms['calculator_step_3']["maximum_loan"].value;
		document.forms['calculator_step_3']["initial_lump_sum"].value = 0;
	}
	else{
	    maxLoan = document.forms['calculator_step_3']["maximum_loan"].value-parseInt(lump);
	}
	document.forms['calculator_step_3']["annual_payment_amount"].value=(parseInt(maxLoan)/20);
}

function change_payment_from_annual_lump_sum(){
	var nbYears = 5;
	var lump = document.forms['calculator_step_3']["initial_lump_sum"].value.replace(",","");
	var maxLoanWLump = document.forms['calculator_step_3']["maximum_loan"].value;
	var maxLoan = 0;
	if(parseInt(lump) > parseInt(maxLoanWLump)){
		alert("Loan amount "+formatCurrency(lump)+" requested exceeds maximum loan available "+formatCurrency(maxLoanWLump)+". Please try again.");
		maxLoan = document.forms['calculator_step_3']["maximum_loan"].value;
		document.forms['calculator_step_3']["initial_lump_sum"].value = 0;
	}
	else{
	    maxLoan = document.forms['calculator_step_3']["maximum_loan"].value-parseInt(lump);
	}
	document.forms['calculator_step_3']["annual_payment_amount"].value=(parseInt(maxLoan)/5);
}


//only for regular payment
function change_payment_quarterly(interval){
	var nbYears = 5;
	var maxLoan = document.forms['calculator_step_3']["maxLoan"].value;
	var fee = 0;
	if(document.forms['calculator_step_3']["fee"].checked==true){
		fee = 1149;
	}
	if(interval==1){
		document.forms['calculator_step_3']["annualPaymentAmount"].value=formatCurrency((maxLoan/nbYears));
		document.forms['calculator_step_3']["numberOfAnnualPayments"].value=1;
	}
	else{
		document.forms['calculator_step_3']["annualPaymentAmount"].value=(maxLoan/nbYears/4);
		document.forms['calculator_step_3']["numberOfAnnualPayments"].value=4;
	}
	document.forms['calculator_step_3']["totalAmount"].value=formatCurrency(parseInt(maxLoan)+fee);
}


function checkForm() {
addFee();
  var noalert = 0;
  var initialLumpSum=document.forms['form1']["initialLumpSum"].value.replace(",","");
  var annualPaymentAmount=document.forms['form1']["annualPaymentAmount"].value.replace(",","");
  var maxLoan = document.forms['form1']["maxLoan"].value.replace(",","");
  var message = "You must enter:\n";
  var pass= true;
  if(initialLumpSum < 20000){
 	 pass = false;
	 message+=" -  a Lump Sum Amount of at least 20,000.\n";
  }
  if(annualPaymentAmount==""){
 	 pass = false;
	 message+=" - a value for 'Annual amount of Regular Incomes'.\n";
  }
  if(parseInt(initialLumpSum) > parseInt(maxLoan)){
		//alert("here");
		pass = false;
		//message=al+"Loan amount "+initialLumpSum+" requested exceeds maximum loan available "+maxLoan+". Please try again.";
		document.forms['form1']["initialLumpSum"].value = 0;
		noalert = 1;
	}
  if(pass){
  	return true;
  }
  else{
   if(noalert==0){alert(message);}
  	return false;
  }
}

function checkForm2() {
addFee();
	var noalert = 0;
	var pass= true;
	var lump = document.forms['form1']["initialLumpSum"].value.replace(",","");
	var nbYears = 5;
	var maxLoan2 = document.forms['form1']["maxLoan"].value-lump;
	var regPay = 0;
	var interval = document.forms['form1']["numberOfAnnualPayments"].value;
	if(interval==1){
		regPay=(maxLoan2/nbYears);
	}
	else{
		regPay=(maxLoan2/nbYears/4);
	}
	if(document.forms['form1']["annualPaymentAmount"].value.replace(",","") > regPay){
 	 	pass = false;
		document.forms['form1']["annualPaymentAmount"].value = regPay;
		noalert = 1;
	}
  
  
  var initialLumpSum=document.forms['form1']["initialLumpSum"].value.replace(",","");
  var annualPaymentAmount=document.forms['form1']["annualPaymentAmount"].value.replace(",","");
  var maxLoan = document.forms['form1']["maxLoan"].value.replace(",","");
  var message = "You must enter:\n";
  if(initialLumpSum < 20000){
 	 pass = false;
	 message+=" -  a Lump Sum Amount of at least 20,000.\n";
  }
  if(annualPaymentAmount==""){
 	 pass = false;
	 message+=" - a value for 'Annual amount of Regular Payments'.\n";
  }
  if(parseInt(initialLumpSum) > parseInt(maxLoan)){
		//alert("here");
		pass = false;
		//message=al+"Loan amount "+initialLumpSum+" requested exceeds maximum loan available "+maxLoan+". Please try again.";
		document.forms['form1']["initialLumpSum"].value = 0;
		noalert = 1;
	}
  if(pass){
  	return true;
  }
  else{
   if(noalert==0){alert(message);}
  	return false;
  }
}


function check_form_3() {
	addFee();
  var errorMax = 0
  var pass= true;
	var nbYears = 5;
	var maxLoan = document.forms['calculator_step_3']["maxLoan"].value;
	var regPay = 0;
	var interval = document.forms['calculator_step_3']["numberOfAnnualPayments"].value;
	if(interval==1){
		regPay=(maxLoan/nbYears);
	}
	else{
		regPay=(maxLoan/nbYears/4);
	}
	if(document.forms['calculator_step_3']["annualPaymentAmount"].value.replace(",","") > regPay){
 	 	pass = false;
		document.forms['calculator_step_3']["annualPaymentAmount"].value = regPay;
		errorMax = 1;
	}
	
  //alert(pass);
  var initialLumpSum=document.forms['calculator_step_3']["initialLumpSum"].value.replace(",","");
  var annualPaymentAmount=document.forms['calculator_step_3']["annualPaymentAmount"].value.replace(",","");
  var  message = "You must enter:\n";
  if(annualPaymentAmount <= 0 ){
 	 pass = false;
	 message+=" - a value for 'Annual amount of Regular Payments'.\n";
  }
  if(pass){
  	return true;
  }
  else{
   if(errorMax==0){alert(message);}
  	return false;
  }
}


function addFee(){
	var fee = 1149;
	var totalAmount = document.forms['form1']["totalAmount"].value.replace(",","");
	document.forms['form1']["totalAmount"].value=formatCurrency(parseInt(totalAmount)+fee);
}

function manual_payment(amount){
	var lump = document.forms['calculator_step_3']["initial_lump_sum"].value.replace(",","");
	var nbYears = 5;
	var maxLoan = document.forms['calculator_step_3']["maximum_loan"].value-lump;
	var regPay = 0;
	//var interval = document.forms['calculator_step_3']["numberOfAnnualPayments"].value;
	//if(interval==1){
	//	regPay=(maxLoan/nbYears);
	//}
	//else{
		regPay=(maxLoan/nbYears/4);
	//}
	if(amount.value.replace(",","") > regPay){
		alert("You can't enter a value over: "+formatCurrency(regPay));
		amount.value = regPay;
	}
	else{
		//updateTotalAmount();
	}
}

//only for regular payment
function manual_payment_quarterly(amount){
	var nbYears = 5;
	var maxLoan = document.forms['calculator_step_3']["maxLoan"].value;
	var regPay = 0;
	var interval = document.forms['calculator_step_3']["numberOfAnnualPayments"].value;
	if(interval==1){
		regPay=(maxLoan/nbYears);
	}
	else{
		regPay=(maxLoan/nbYears/4);
	}
	if(amount.value.replace(",","") > regPay){
		alert("You can't enter a value over: "+formatCurrency(regPay));
		amount.value = regPay;
		paymentAmount = 1;
	}
	else{
		updateTotalAmount2();
	}
}


function updateTotalAmount(){
	var nbYears = 5;
	var fee = 0;
	var regPay = document.forms['form1']["annualPaymentAmount"].value.replace(",","");
	var interval = document.forms['form1']["numberOfAnnualPayments"].value;
	var lump = document.forms['form1']["initialLumpSum"].value.replace(",","");
	if(document.forms['form1']["fee"].checked==true){
		fee = 1149;
	}
	document.forms['form1']["totalAmount"].value=formatCurrency((regPay*interval*nbYears)+fee+parseInt(lump));
}

function updateTotalAmount2(){
	var nbYears = 5;
	var fee = 0;
	var regPay = document.forms['form1']["annualPaymentAmount"].value.replace(",","");
	var interval = document.forms['form1']["numberOfAnnualPayments"].value;
	if(document.forms['form1']["fee"].checked==true){
		fee = 1149;
	}
	document.forms['form1']["totalAmount"].value=formatCurrency((regPay*interval*nbYears)+fee);
}

function update_lump_sum(){
	var fee = 0;
	var lump = document.forms['calculator_step_3']["initial_lump_sum"].value.replace(",","");
	var maxLoan = document.forms['calculator_step_3']["maximum_loan"].value;
	if(parseInt(lump) > parseInt(maxLoan)){
		alert("Loan amount "+formatCurrency(lump)+" requested exceeds maximum loan available "+formatCurrency(maxLoan)+". Please try again.");
		document.forms['calculator_step_3']["initial_lump_sum"].value = "";
		lump = 0;
	}
/*
	if(document.forms['calculator_step_3']["fee"].checked==true){
		fee = 1149;
	}
*/	
	//document.forms['calculator_step_3']["totalAmount"].value=formatCurrency(fee+parseInt(lump));
}

