var Calculators = Calculators || {};
Calculators.loan = Calculators.loan || {};

Calculators.loan.MonthlyToBiWeekly = function MonthlyToBiweekly(loanAmount, annualRate, monthlyPayment, compounded) {
	var formula = new Calculators.math.Formulas();
	this.loanAmount = new Calculators.type.Money(loanAmount);
	this.annualRate = new Calculators.type.Rate(annualRate);
	this.monthlyPayment = new Calculators.type.Money(monthlyPayment);
	this.compounded = new Number(compounded);

	this.isValidMonthlyPayment = function() {
		var rate = this.getMonthlyRate();
		var pmt = -1 * formula.pmt(rate / 12, 600, this.loanAmount.getAmount(), 0);
		return (pmt <= this.monthlyPayment.getAmount());
	}

	this.getLoanAmount = function() {
		return this.loanAmount;
	}
	
	this.getAnnualRate = function() {
		return this.annualRate;
	}
	
	this.getMonthlyPayment = function() {
		return this.monthlyPayment;
	}
	
	this.getCompounded = function() {
		return this.compounded;
	}
	
	this.getEffectiveRate = function() {
		var rate = new Calculators.type.Rate(formula.effect(this.annualRate.getAmount(), this.compounded))
		return rate;
	}
	
	this.getBiWeeklyPayment = function() {
		return new Calculators.type.Money(this.monthlyPayment.getAmount() / 2);
	}
	
	this.getMonthlyRemainingTerm = function() {
		var rate = this.getMonthlyRate();
		var nper = formula.nper(rate / 12, this.monthlyPayment.getAmount(), -1* this.loanAmount.getAmount(), 0);
		return (nper / 12);
	}
	
	this.getBiWeeklyRemainingTerm = function() {
		var rate = this.getBiWeeklyRate();
		var nper = formula.nper(rate / 26, this.getBiWeeklyPayment().getAmount() , -1* this.loanAmount.getAmount(), 0);
		return (nper / 26);
	}		
	
	this.getMonthlyRate = function() {
		var rate = 0;
		if(this.compounded == 365) {
			rate = (formula.effect(this.annualRate.getAmount(), 365) - formula.effect(this.annualRate.getAmount(), 12) + this.annualRate.getAmount());
		} else {
			rate = this.annualRate.getAmount();
		}	
		return rate;
	}
	
	this.getBiWeeklyRate = function() {
		var rate = 0;
		if(this.compounded == 365) {
			rate = (formula.effect(this.annualRate.getAmount(), 365) - formula.effect(this.annualRate.getAmount(), 26) + this.annualRate.getAmount());
		} else {
			rate = this.annualRate.getAmount();
		}	
		return rate;
	}
	
	this.getMonthlyRemainingTermFormatted = function() {
		return Math.round(this.getMonthlyRemainingTerm() * 100) / 100;
	}
	
	this.getBiWeeklyRemainingTermFormatted = function() {
		return Math.round(this.getBiWeeklyRemainingTerm() * 100) / 100;
	}	
	
	this.getMonthlyTotalExpense = function() {
		var expense = (this.monthlyPayment.getAmount() * (this.getMonthlyRemainingTerm() * 12)) - this.getLoanAmount().getAmount();
		return new Calculators.type.Money(expense);
	}
	
	this.getBiweeklyTotalExpense = function() {
		var expense = (this.getBiWeeklyPayment().getAmount() * (this.getBiWeeklyRemainingTerm() * 26)) - this.getLoanAmount().getAmount();
		return new Calculators.type.Money(expense);
	}	
	
	this.getDifferenceOfPayments = function() {
		return Math.round((this.getMonthlyRemainingTermFormatted() - this.getBiWeeklyRemainingTermFormatted()) * 100) / 100;
	}
	
	this.getDifferenceInInterest = function() {
		return new Calculators.type.Money(this.getMonthlyTotalExpense().getAmount() - this.getBiweeklyTotalExpense().getAmount());
	}
	
	/*
	this.monthlyLoan = new Calculators.loan.Loan(this.getMonthlyRate(), 12 * this.getMonthlyRemainingTerm(), this.loanAmount);
	this.biWeeklyLoan = new Calculators.loan.Loan(this.getBiWeeklyRate(), 26 * this.getBiWeeklyRemainingTerm(), this.loanAmount);
	this.biWeeklyLoan.setPaymentsPerYear(26);
	
	this.getMonthlyLoan = function() {
		return this.monthlyLoan;
	}
	
	this.getBiWeeklyLoan = function() {
		return this.biWeeklyLoan;
	}*/
	
}
