var oldHTML;


function GetLoan()
{
	var amount = document.getElementById("loan-amount").value;
	var term   = document.getElementById("loan-term").value;
	var affid  = document.getElementById("affid").value;
	var loan   = CalculateLoan(new Number(amount),new Number(term));
	

	if(loan!=null)
	{
		var newHTML = "<fieldset><ul><li>Loan Amount: &pound;"
			+ loan._BorrowedAmount
			+ "</li><li>Term: "
			+ loan._Term
			+ " weeks</li><li>Weekly Repayment: &pound;"
			+ loan.GetWeeklyPayment()
			+ "</li><li>Total Payable: &pound;"
			+ loan.GetTotalPayment()
			+ "</li></ul><p><strong>"
			+ loan._APR
			+ "% <abbr title=\"Annual Percentage Rate\">APR</abbr></strong></p>"
			+ "<br /><a style=\"color:white\" href=\""+affid+"\">"
			+ "<img src=\"http://www.providentaffiliates.co.uk/4499.006/apply-01.gif\" alt=\"Apply"			
			+ "for your loan...\"  border=\"0\" /></a>"
			+ "<p><a onclick=\"Reset()\" href=\"#\" title=\"Recalculate your loan\">Recalculate</a></p></fieldset>";
	
		oldHTML = document.getElementById("calc").innerHTML;
		document.getElementById("calc").innerHTML = newHTML;
	}
	
}

function Reset()
{
	document.getElementById("calc").innerHTML      = oldHTML;
	document.getElementById("loan-term").innerHTML = "";
}


//returns an array of terms for the loan amount specified
function GetTerms(loanAmount)
{
    var terms;
    if(!isNaN(loanAmount))
    {
        if(loanAmount>=50 && loanAmount<100)
        {
        	terms    = new Array(1);
			terms[0] = 31;
        }
        else if(loanAmount<=500)
        {
        	terms    = new Array(2);
			terms[0] = 56;
			terms[1] = 31;
        }

	var selectListHTML="<select id=\"loan-term\" name=\"loan-term\">";
	if(terms!=null)
	{		
		for(var i=0;i<terms.length;i++)
		{
			selectListHTML+="<option value=\"";
			selectListHTML+=terms[i];
			selectListHTML+="\">";
			selectListHTML+=terms[i];
			selectListHTML+=" weeks</option>";
		}
	}

	selectListHTML+="</select>";

	document.getElementById("term").innerHTML = selectListHTML;
	return;
    }
    //Could not determine loan terms from input
    return Number.NaN
}

//returns a Loan object based on the selected loan amount and term
function CalculateLoan(amount,term)
{
    if(!isNaN(amount)&&!isNaN(term))
    {
        switch(term.toString())
		{
			case "31":
				return new Loan(365.1,0.05,amount,term);

			case "56":
				return new Loan(183.2,0.03,amount,term);

		}
    }
    return null;
}

//Loan Class
function Loan (apr,multiplier,borrowedAmount,term)
{
    this._APR = apr;
    this._Multiplier=multiplier;
    this._BorrowedAmount = borrowedAmount;
    this._Term=term;
}

Loan.prototype._APR;
Loan.prototype._Multiplier;
Loan.prototype._BorrowedAmount;
Loan.prototype._Term;

Loan.prototype.GetTotalPayment = function() {
    return (this._BorrowedAmount * this._Multiplier * this._Term).toFixed(2);
}

Loan.prototype.GetWeeklyPayment = function() {
    return (this._BorrowedAmount * this._Multiplier).toFixed(2);
}



