// JavaScript Document
function calcularTerm(){
	campoEdad=parseInt(document.getElementById("age").value);
	campoTerm=parseInt(document.getElementById("yearsTerm").value);
	campoTerm=75-campoEdad;
	document.getElementById("yearsTerm").value=campoTerm;
}

function calcular(){
	campoIncome=parseInt(document.getElementById("netIncome").value);
	campoExpenditure=parseInt(document.getElementById("netExpenditure").value);
	campoEdad=parseInt(document.getElementById("age").value);
	campoTerm=parseInt(document.getElementById("yearsTerm").value);
	campoRate=parseFloat(document.getElementById("interestRate").value/100);
	// se valida que los campos no estén vacios
	if (campoIncome!="" && campoExpenditure!="" && campoEdad!="" && campoTerm!="" && campoRate!=""){
		// se comprueba que el campo term sea correcto
		if (campoTerm>(75-campoEdad)){
			alert("Term is not affordable for this age");
			return false;
		}
		availableMonth=parseInt((campoIncome*40/100)-campoExpenditure);
		rateMonthly=campoRate/12;
		totalPayments=campoTerm*12;
		totalAmount=Math.round(availableMonth*(1 - Math.pow(1+rateMonthly,-1*totalPayments)) / rateMonthly);
		resultadoHTML='<img src="images/flecha_dcha_news.gif" align="absmiddle" /> ';
		if (isNaN(totalAmount)){
			resultadoHTML=resultadoHTML+"Maximum loan available: <span class='error'>Error calculating the amount, some of the inserted values are not numbers</span>";
		}
		else{
			resultadoHTML=resultadoHTML+"Maximum loan available: "+number_format(totalAmount, 2, '.', ',');
		}
		document.getElementById("divResultado").innerHTML=resultadoHTML;
	}
	else{
		alert("Please, fill in all the required fields");
	}
}

function MCCalcular(){
	campoSalePrice=parseInt(document.getElementById("MCSalePrice").value);
	campoTerm=parseInt(document.getElementById("MCYearsTerm").value);
	campoRate=parseFloat(document.getElementById("MCInterestRate").value/100);
	// se valida que los campos no estén vacios
	if (campoSalePrice!="" && campoTerm!="" && campoRate!=""){
		rateMonthly=campoRate/12;
		totalPayments=campoTerm*12;
		if (campoRate==0){
			monthlyPayment=campoSalePrice/totalPayments;
		}
		else{
			monthlyPayment=(campoSalePrice*rateMonthly) / (1 - Math.pow(1+rateMonthly,-1*totalPayments));
		}
		resultadoHTML='<img src="http://www.moroccanmortgagealliance.com/images/flecha_dcha_news.gif" align="absmiddle" /> ';
		if (isNaN(monthlyPayment)){
			resultadoHTML=resultadoHTML+"Monthly Payment: <span class='error'>Error calculating the amount, some of the inserted values are not numbers</span>";
		}
		else{
			resultadoHTML=resultadoHTML+"Monthly Payment: "+number_format(monthlyPayment, 2, '.', ',');
		}
		document.getElementById("divResultadoMC").innerHTML=resultadoHTML;
	}
	else{
		alert("Please, fill in all the required fields");
	}
}

function number_format(total, numeroDecimales, separadorDecimal, separadorMiles) {
 total = Math.round(total * Math.pow(10, numeroDecimales)) / Math.pow(10, numeroDecimales);
 e = total + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < numeroDecimales) {
  g = f[1];
  for (i=f[1].length + 1; i <= numeroDecimales; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(separadorMiles != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = separadorMiles + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 separadorDecimal = (numeroDecimales <= 0) ? '' : separadorDecimal;
 return f[0] + separadorDecimal + f[1];
}