// JavaScript Document
// Calculates how much total mortgage one can get based on income
// REQUIRES: calccommon.js 
// --------------------------------------------
// Validates the form
var submitButton = null;

// NEEDS TO BE FIXED
function validateOnSubmit(obj) {
  undoAutoformat(obj);
  if ((submitButton == 'cancel') || (submitButton == 'back')) {return true;}
    var errs=0;
    // execute all element validations in reverse order, so focus gets set to the first one in error.
	if (!validateNumber('IR', 'inf_IR', 'Rakam giriniz.', true)) {errs ++; }
	if (!validateRequired('YR', 'inf_YR', 'Rakam giriniz.', true)) {errs ++; }
    if (!validateNumber('LA', 'inf_LA', 'Rakam giriniz.', true)) {errs ++; }
	if (errs > 0) doAutoformat();
	return (errs==0);
};
// --------------------------------------------
function sumincome () {
  var mysum = parseFloat(document.temps.maasgelir.value) + parseFloat(document.temps.kiragelir.value) + parseFloat(document.temps.digergelir.value);
  document.temps.toplamgelir.value = mysum;
}

// --------------------------------------------
function sumexpense () {
  var mysum = parseFloat(document.temps.kartgider.value) + parseFloat(document.temps.tasitgider.value) + parseFloat(document.temps.digergider.value);
  document.temps.toplamgider.value = mysum;
}

// --------------------------------------------
function displayshort () {
  var mortgagegtoincome = 0.28; // max. allowed mortgage to income ratio
  var debttoincome = 0.50; // max. allowed total debt to income ratio
  var payperperiod = 0;
  var msg = ""; 
  
  sumincome();
  sumexpense();
  var x1 = document.temps.toplamgelir.value * mortgagegtoincome;
  var x2 = document.temps.toplamgelir.value * debttoincome - document.temps.toplamgider.value;
  
  if (x2 < 0) {x2 = 0};
  if (x1 < x2) {
    payperperiod = x1;
    msg = "Aylık gelirinizi artırarak daha fazla mortgage alabilirsiniz.";
  } 
  else {
    payperperiod = x2;
	msg = "Aylık borç giderlerinizi azaltarak ya da gelirinizi artırarak daha fazla mortgage alabilirsiniz."
  };
   
 var mi = document.temps.faiz.value/ 100;
 var base = 1;
 var mbase = 1 + mi;
  
 for (i=0; i<document.temps.vade.value * 12; i++)
 {
   base = base * mbase;
 }
 var la = payperperiod * ( 1 - (1/base)) / mi;
 var xla = document.temps.pesinat.value/document.temps.pesinatorani.value * 100;
  
 if (xla < la) {
   la = xla;
   payperperiod = la * mi / ( 1 - (1/base)); 
   msg = "Peşinat miktarını artırarak ya da daha az peşinat isteyen bir bankayla çalışarak daha fazla mortgage alabilirsiniz.";
 };
 var payinterest = payperperiod * document.temps.vade.value * 12 - la;
 var gayrimenkul = la + parseFloat(document.temps.pesinat.value);
  
 document.temps_result.MT.value = formatnumber(la,0,false,false,true);
 document.temps_result.gayrimenkul.value = formatnumber(gayrimenkul,0,false,false,true);
 document.temps_result.MP.value = formatnumber(payperperiod,0,false,false,true);
 document.temps_result.FZ.value = formatnumber(payinterest,0,false,false,true);
 document.getElementById('result').style.visibility = "visible";
 document.getElementById('disc').style.visibility = "visible";
 document.getElementById('msg').firstChild.nodeValue = msg;
 document.getElementById('msg').style.visibility = "visible";
}
