// JavaScript Document
// Calculates monthly mortgage payments
// REQUIRES: calccommon.js 
// --------------------------------------------

var submitButton = null;

// Validates the form w/ MDFO (Effective APR Calculation) 
function validateOnSubmitMDFO(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 (!validateRequired('durationMDFO', 'inf_durationMDFO', 'Rakam giriniz.', true)) {
		errs ++; 
	}
    if (!validateNumber('creditamountMDFO', 'inf_creditamountMDFO', 'Rakam giriniz.', true)) {
		errs ++; 
	}
	if (errs > 0) {
		doAutoformat();
	}
	return (errs==0);
};

// --------------------------------------------
// Validates the form w/ MDFO (Basic Payments Calculation) 
function validateOnSubmitBasic(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);
};


// --------------------------------------------
// Displays the result of the Basic Calculator
function displayResult (obj, targetId)
{
  if (!validateOnSubmitBasic(obj)) {return false;}
  var payperperiod = docalc(obj.LA.value, obj.IR.value, obj.YR.value);
  var payinterest = payperperiod * obj.YR.value * 12 - obj.LA.value;
  
  target = document.getElementById(targetId);
  target.MT.value = formatnumber(payperperiod,0,false,false,true);
  target.MP.value = formatnumber(payinterest,0,false,false,true);
  $('#result').show();
  $('#disc').show();
  return true;
}

// --------------------------------------------
// Hides all calculators until user chooses either Basic or MDFO calculator
function initCalc() {
    $('#calcdivBasic').hide(); $('#calcdivMDFO').hide();
}

// --------------------------------------------
// Displays the appropriate calculator: Basic or MDFO
function displayCalc(obj) {
    // displays the calculator
    $('#disc').hide();
    var calctype = (obj.value);
    if (calctype == 'MDFO') {
		$('#calcdivBASIC').hide();
		$('#calcdivMDFO').show(); 
    } else {
		$('#calcdivMDFO').hide(); 
		$('#calcdivBASIC').show();
    }
}
// --------------------------------------------

$(document).ready(function() {
	initCalc();
    });