// JavaScript Document
//formulas for the pafilia financial calculator.
<!----------------------------------------------------Author:MARIANNE ANGELIDES------------------------------------------>

function max_loan_dur(cAge, bAge, loanDurationMax) {
	//if the max loan age - client age > 35 then max loan age = 35
	//else bank age - client age.
	var loanAge = "";
	if(bAge - cAge > loanDurationMax) {
	loanAge = loanDurationMax;	
		
	} else {
	loanAge = bAge - cAge;
	
	}
	return loanAge;
	
}

function max_poss_installment(monthlyIncome, otherInstalments, installmentToIncome) {
	
	//if monthly income - other mortgage installment * instalment to income < 0
	//then return 0
	//else return monthly income - other mortgage installment * instalment to income 
	var maxPossInstall = "";
	//changes as requested by demos.
	//if((monthlyIncome - otherInstalments)*installmentToIncome/100 < 0) {
		//maxPossInstall=0;
		//} else { 
	  				//maxPossInstall=(monthlyIncome - otherInstalments)*installmentToIncome/100
			//}
	if((monthlyIncome*installmentToIncome/100) - otherInstalments < 0) {
		maxPossInstall=0;
		} else { 
	  				maxPossInstall=(monthlyIncome*installmentToIncome/100) - otherInstalments;
			}
	return maxPossInstall;
}


function clientsInitalContr(propPrice, percIC){

// we multiply the property price by the percentage

var initCont = Number(contractPriceConversion(propPrice)) * percIC.value/100;
return Math.round(initCont);
		
}

function loanAmountVal(propPrice, percIC){

// we multiply the property price by the percentage
var initContr =  contractPriceConversion(propPrice) * percIC.value/100;
var loanAmount = contractPriceConversion(propPrice) - initContr;
return Number(loanAmount).toFixed(0);
	
}

function loanAmountPerc(percIC){

var blfPerc = 100 - percIC.value;
return blfPerc;
	
}

function clientToLoanConversion() 

{
	var clientMaxPossInstall = Number(document.getElementById('maximum_instalments_client').value);
	var clientCurrencyExchangeRate = Number(document.getElementById('client_currency').value);
	var bankLoanCurrency = Number(document.getElementById('bank_currency_hidden').value);
	
	
	var clientToBaseCurrency = clientMaxPossInstall * clientCurrencyExchangeRate;
	var clientToLoanCurrency = Number(clientToBaseCurrency * (1/bankLoanCurrency)).toFixed(0);
	
	document.getElementById('maximum_instalments_loan').value = clientToLoanCurrency;
}

function contractPriceConversion(contractPriceVal)
{
	var contractPrice = Number(document.getElementById(contractPriceVal).value);
	var bankLoanCurrency = Number(document.getElementById('bank_currency_hidden').value);
	
	var priceToLoanCurrency = contractPrice * (1/bankLoanCurrency);
	return priceToLoanCurrency;
	
}

// this function is used to set a default value for the contract start date.
//it uses the contract date from screen one, adds three months to it and displays the value.

function getLoanStartDate(contractDate, loanStartDate)

{
	if (document.getElementById(loanStartDate).value == "")
	{
	var contrDate = new Date(document.getElementById(contractDate).value);
	var date3Months = new Date(contrDate.getTime() + (90*24*60*60*1000));
	//var displayDate = contrDate.format('M jS, Y');
	var displayDate = date3Months.format("dd mmm yyyy");
	//var curTime = contrDate.getDate();
	document.getElementById(loanStartDate).value = displayDate;
	//return displayDate;
	}
}


function getDateDifference (contractDate,loanStartDate)
{
	
		// i need to check whether the date they entered is not in the past as he does in his excel sheet
		var today = new Date();
		var theloanDate = new Date(document.getElementById(loanStartDate).value)
		if (theloanDate < today)
		{
			showAlert(283,1);
			populateSingleInputText(loanStartDate,'');
			return false;
		}
		else
		{
		//this sets the difference betweeen the contract date and the loan start date in months and days
		var one_month=30*24*60*60*1000;
		var conDate = new Date(document.getElementById(contractDate).value);
		var loanStart = new Date(document.getElementById(loanStartDate).value);
		var monthDifference = (loanStart - conDate)/ one_month;
		var daysDifference = (loanStart - conDate)% one_month;
	
		var totalDays = daysDifference/1000/60/60/24;
		document.getElementById("loan_start_months").innerHTML = Math.round(monthDifference) + " months and " + Math.ceil(totalDays)  + 		" day/s";
		getLoanEndDate();
		}
	
	
}

function getLoanEndDate()
{
	var loanYears = Number(document.getElementById('loan_years').value);
	var loanStartYear = new Date(document.getElementById('loan_start_date').value);
	var loanEndYear = (loanStartYear.getFullYear() + loanYears);
	var loanEndDate = new Date(loanStartYear.setYear(loanEndYear));
	var displayDate = loanEndDate.format("dd mmm yyyy");
	document.getElementById('loan_end_date').innerHTML = displayDate;
	document.getElementById('loan_end_date_hidden').value = displayDate;
	
	
}

//global function for calculating the dates in the grace periods, 
//I have created it so you pass date you want to add the months to, then the months you are going to add
//then the field you want to display the new date into.
function monthValidation(months)
{
	if (document.getElementById('loan_years').value != "")
	{
	
		if(document.getElementById('loan_start_date').value != "" )
		{
		
	//**** i need to put validation here to make sure that the months parsed through are numeric and loanStartDate has to be filled
			if(IsNumeric(document.getElementById(months).value))
			{
			
			//get the months and convert it to a number so you can add it
			//var getTheMonths = Number(document.getElementById(months).value);
			//get the value of the date that you want to add the monthes to
			//var myDate = new Date(document.getElementById(dateToAddTo).value);
			//get the months from the date and add the new months onto it
			//var addMonths = (myDate.getMonth() + getTheMonths);
			//create a new date with the date and the new month calculated
			//var newDate = new Date(myDate.setMonth(addMonths));
			//format the date
			//var displayDate = newDate.format("dd mmm yyyy");
			//display it in the field that you added it to.
			//document.getElementById(displayField).value = displayDate;
			addScheduledAndGraceMonths()
			totalLoanDurationMonths()
			}
			else
			{
			showAlert(284,1);
			document.getElementById(months).value = "";
			setTimeout(function(){document.getElementById(months).focus();}, 10);
			return false;
			}
		}
		else
		{
			showAlert(285,1);
			setTimeout(function(){document.getElementById('loan_start_date').focus();}, 10);
			document.getElementById(months).value = "";
			return false;
		}
	}
	else
	{
		
			showAlert(286,1);
			setTimeout(function(){document.getElementById('loan_years').focus();}, 10);
			document.getElementById(months).value = "";
			return false;
	}
	
	
}

function addScheduledAndGraceMonths()
{
	var graceInterest = Number(document.getElementById('grace_period').value);
	var gracePrincipal = Number(document.getElementById('grace_prinicipal').value);
	var scheduledPaymentPeriod = Number(document.getElementById('grace_scheduled_payments').value);
	
	var loanYears = Number(document.getElementById('loan_years').value);
	var yearsToMonths = loanYears * 12;
	var total = yearsToMonths - graceInterest - gracePrincipal - scheduledPaymentPeriod;
	document.getElementById('scheduled_pay_grace_period').value = total;
	
}

function totalLoanDurationMonths()
{
	var graceInterest = Number(document.getElementById('grace_period').value);
	var gracePrincipal = Number(document.getElementById('grace_prinicipal').value);
	var scheduledPaymentPeriod = Number(document.getElementById('grace_scheduled_payments').value);
	var scheduledPayGracePeriod = Number(document.getElementById('scheduled_pay_grace_period').value);
	
	var total = graceInterest + gracePrincipal + scheduledPaymentPeriod + scheduledPayGracePeriod;
	document.getElementById('total_loan_dur').value = total;
}
	
<!----------------------------------------------------------------END-------------------------------------------------------------->