// The Loan Input Class
function CBL_LoanInput(customerTypeObject, typeObject, currencyObject, termObject, totalAmountObject, installmentAmountObject)
{
	this.CustomerTypeObject = customerTypeObject;	
	this.TypeObject = typeObject;
	this.CurrencyObject = currencyObject;
	this.TermObject = termObject;
	this.TotalAmountObject = totalAmountObject;
	this.InstallmentAmountObject = installmentAmountObject;
}

// The Loan Output Class
function CBL_LoanOutput(customerTypeObject, typeObject, amountObject, rateObject, monthlyPaymentObject)
{
	this.CustomerTypeObject = customerTypeObject;	
	this.TypeObject = typeObject;
	this.AmountObject = amountObject;
	this.RateObject = rateObject;
	this.MonthlyPaymentObject = monthlyPaymentObject;
}

// The Loan Term Currency Rate Class
function CBL_LoanTermCurrencyRate(currencyCode, terms, rates)
{
	this.CurrencyCode = currencyCode;
	var tempString = new String(terms);
	this.Terms = tempString.split(",");
	tempString = String(rates);
	this.Rates = tempString.split(",");
}

// The Loan Taxes Class
function CBL_LoanTaxes(personalBSMV, personalKKDF, corporateBSMV, corporateKKDF)
{
	this.KKDF = new Array(personalKKDF, corporateKKDF);
	this.BSMV = new Array(personalBSMV, corporateBSMV);
}

// The Loan Class
function CBL_Loan(name, loanTaxesObject)
{
	// Private 
	this.currs = new Array();
	this.ltcrs = new Array();
	
	// Properties
	this.LoanTaxes = loanTaxesObject;
	this.Name = name;
	
	// Methods
	this.AddTermCurrencyRate = CBL_Loan_AddTermCurrencyRate;
}

function CBL_Loan_AddTermCurrencyRate(LTCRObject)
{
	this.ltcrs.push(LTCRObject);
	for(currencyIndex=0;currencyIndex<this.currs.length;currencyIndex++)
	{
		if(this.currs[currencyIndex] == LTCRObject.CurrencyCode)
			return;
	}
	this.currs.push(LTCRObject.CurrencyCode)
}

// The Loan Calculator Class
function CBL_LoanCalculator(inputObject, outputObject, alerts)
{
	// Private
	this.loans = new Array();
	
	// Properties
	this.Output = outputObject;
	this.Input = inputObject;
	this.Alerts = alerts;
	
	// Methods
	this.AddLoan = CBL_LoanCalculator_AddLoan;
	this.ReCalculate = CBL_LoanCalculator_ReCalculate;
	this.Initialize = CBL_LoanCalculator_Initialize;
	this.RestoreCurrencyCodes = CBL_LoanCalculator_RestoreCurrencyCodes;
	this.RestoreTerms = CBL_LoanCalculator_RestoreTerms;
	this.GetSelectedLTCR = CBL_LoanCalculator_GetSelectedLTCR;
	this.GetSelectedLoan = CBL_LoanCalculator_GetSelectedLoan;
}

function CBL_LoanCalculator_Initialize()
{
	var obj = this.Input.TypeObject;
	CBL_ClearOptions(obj);
	for(var loanIndex = 0; loanIndex < this.loans.length; loanIndex++)
	{
		CBL_AddOption(obj, loanIndex, this.loans[loanIndex].Name);
	}
	this.RestoreCurrencyCodes();
	
}

function CBL_LoanCalculator_RestoreTerms()
{
	var LTCRObject = this.GetSelectedLTCR();
	var termObject = this.Input.TermObject;
	CBL_ClearOptions(termObject);
	for(var termIndex = 0; termIndex < LTCRObject.Terms.length; termIndex++)
	{
		CBL_AddOption(termObject, termIndex, LTCRObject.Terms[termIndex]);
	}
}

function CBL_LoanCalculator_RestoreCurrencyCodes()
{
	var loanObject = this.GetSelectedLoan();
	var currObject = this.Input.CurrencyObject;
	currObject.options.length = 0;
	for(var currIndex = 0; currIndex < loanObject.currs.length; currIndex++)
	{
		CBL_AddOption(currObject, loanObject.currs[currIndex], loanObject.currs[currIndex]);
	}
	this.RestoreTerms();
}

function CBL_LoanCalculator_AddLoan(loanObject)
{
	this.loans.push(loanObject);
}

function CBL_LoanCalculator_GetSelectedLTCR()
{
	var loanObject = this.GetSelectedLoan();
	var currencyArray = loanObject.currs;
	var LTCRArray = loanObject.ltcrs;
	var selectedCurrency = this.Input.CurrencyObject.value;
	
	for(var LTCRIndex = 0; LTCRIndex < LTCRArray.length; LTCRIndex++)
	{
		var LTCRObject = LTCRArray[LTCRIndex];
		if((LTCRObject.CurrencyCode == selectedCurrency))
		{
			return LTCRObject;
		}
	}
	
	return null;
}

function CBL_LoanCalculator_GetSelectedLoan()
{
	return this.loans[this.Input.TypeObject.value];
}

function CBL_LoanCalculator_ReCalculate()
{
	var MUSTERI_TIPI =  this.Input.CustomerTypeObject.value;
	var KREDI_TUTARI = parseFloat(this.Input.TotalAmountObject.value);
	var TAKSIT_TUTARI = parseFloat(this.Input.InstallmentAmountObject.value);
	var GIRILEN_VADE = this.Input.TermObject.value;
	
	
	
	var LTCRObject = this.GetSelectedLTCR();
	var loanObject = this.GetSelectedLoan();
	
	var BRUT_AYLIK_FAIZ_ORANI = (parseFloat(LTCRObject.Rates[GIRILEN_VADE]) / 100) * (loanObject.LoanTaxes.KKDF[MUSTERI_TIPI] + loanObject.LoanTaxes.BSMV[MUSTERI_TIPI] + 1);
	
		
	var CARPAN = Math.pow(1 + BRUT_AYLIK_FAIZ_ORANI, LTCRObject.Terms[GIRILEN_VADE]);
	
	if(!isNaN(KREDI_TUTARI))
	{
		TAKSIT_TUTARI = (KREDI_TUTARI * CARPAN * BRUT_AYLIK_FAIZ_ORANI) / (CARPAN - 1);
	}
	else if(!isNaN(TAKSIT_TUTARI))
	{
		KREDI_TUTARI = TAKSIT_TUTARI * ((CARPAN - 1) / (CARPAN * BRUT_AYLIK_FAIZ_ORANI));
	}
	else
	{
		alert(this.Alerts[0]);
		return;
	}
	if(this.Output.CustomerTypeObject!=null) this.Output.CustomerTypeObject.innerHTML = this.Input.CustomerTypeObject.options[this.Input.CustomerTypeObject.selectedIndex].innerHTML;
	if(this.Output.TypeObject!=null) this.Output.TypeObject.innerHTML = loanObject.Name + " (" + LTCRObject.Terms[GIRILEN_VADE] + ")";
	if(this.Output.AmountObject!=null) this.Output.AmountObject.innerHTML = CBL_FormatAmount(KREDI_TUTARI) +" TL";
	if(this.Output.RateObject!=null) this.Output.RateObject.innerHTML = CBL_FormatAs2Digit(LTCRObject.Rates[GIRILEN_VADE]); //CBL_FormatAs2Digit(100 * BRUT_AYLIK_FAIZ_ORANI);
	if(this.Output.MonthlyPaymentObject!=null) this.Output.MonthlyPaymentObject.innerHTML = CBL_FormatAmount(TAKSIT_TUTARI) +" TL";
	return new Array(LTCRObject.Rates[GIRILEN_VADE],TAKSIT_TUTARI);
	
}
