//Bond Repayment Calculator
function compute(obj) {
	obj.payments.value = "";
	if (setnum(obj)){
		fprice = parseFloat(obj.price.value)
		fdeposit = parseFloat(obj.deposit.value)
		fmonths =  parseFloat(get_selection(obj.period).value);
		finterest = parseFloat(obj.interest.value)
		ffinal_payment = parseFloat(obj.final_payment.value)
		fpayments = parseFloat(obj.payments.value)
		var intermediate = new MakeArray(fmonths);
		var i
		var fdaily_rate = finterest/36500;
		var famount = fprice - fdeposit;
		var ftotal = 0;
		for (i = 0; i <= fmonths -1; i++) {
			intermediate[i] =1+30.42*fdaily_rate
		}
	
		for (i = fmonths - 2; i >= 0; i--) {
			intermediate[i] = intermediate[i] * intermediate[i+1];
		}
	
		for (i = 0; i <= fmonths-1; i++) {
			ftotal = ftotal  + intermediate[i];
		}
		if (fpayments == 0) {
			fpayments = ((famount*intermediate[0]-ffinal_payment)/(ftotal+1-intermediate[0]));
			obj.payments.value = Math.round(fpayments)
		}else if (fprice == 0) {
			famount =((fpayments*(ftotal+1-intermediate[0]))+ffinal_payment)/intermediate[0];
			fprice = famount + fdeposit;
			obj.price.value = Math.round(fprice)
		}else if (fdeposit == 0) {
			famount =((fpayments*(ftotal+1-intermediate[0]))+ffinal_payment)/intermediate[0];
			fdeposit = fprice - famount;
			obj.deposit.value = Math.round(fdeposit)
		}else if (ffinal_payment == 0) {
			ffinal_payment = (famount*intermediate[0])-(fpayments*(ftotal+1-intermediate[0]));
			obj.final_payment.value = Math.round(ffinal_payment)
		}else{
			fpayments = ((famount*intermediate[0]-ffinal_payment)/(ftotal+1-intermediate[0]));
			obj.payments.value = Math.round(fpayments)
		}
	}
}

function MakeArray(n) {
	this.length = n;
	for (var i = 1; i <= n; i++) { 
		this[i] = 0 
	}
	return this;
}

function select_item(name, value) {
	this.name = name;
	this.value = value;
}

function get_selection(select_object) {
	contents = new select_item("w","w");
	for(var i=0;i<select_object.options.length;i++)
		if(select_object.options[i].selected == true) {
			contents.name = select_object.options[i].text;
			contents.value = select_object.options[i].value;
		}
	return contents;
}



function setnum(obj) {
	if (obj.payments.value == ""){
		obj.payments.value = "0"
	}
	if (obj.price.value == ""){
		obj.price.value = "0"
	}
	if (obj.deposit.value == ""){
		obj.deposit.value = "0"
	}
	if (obj.interest.value == ""){
		obj.interest.value = "0"
	}
	if (obj.final_payment.value == ""){
		obj.final_payment.value = "0"
	}
	if (obj.payments.value == 0 && obj.price.value == 0){
		alert("Please enter either the price or the monthly payments.")
		return false;
	}
	if (isnum(obj.payments.value,"payments") && isnum(obj.price.value,"price") && isnum(obj.deposit.value,"deposit") && isnum(obj.interest.value,"interest") && isnum(obj.final_payment.value,"final payment")){
		return true;
	}else{
		return false;
	}
}

function isnum(str,strname) {
	for (var i = 0; i < str.length; i++) {
	var ch = str.substring(0,1)
		if (ch < "0" || ch > "9") {
			alert("Try a number for "+strname+", please.");
			return false;
		}
	}
	return true;
}
//Max Home Loan
//find out what version of a net-browser we are using	
ns4 = (document.layers)? true:false;
ie4 = (document.all)? true:false;

function ValidNumber(input) 
{
	// check for a length greater than 0
	if (input.length < 1)
		return false;
		
	// check every position and validate it against the allowed characters
	sCheckStr = "0123456789.";	// allowed characters
	iCnt=0;						// counter for the number of "." characters
	for(iPos=0; iPos < input.length; iPos++)
	{
		// check that the current position in the string is a valid character
		if (sCheckStr.indexOf(input.charAt(iPos)) < 0)
			return false;
		else
		{
			// ensure not more than 1 "." character
			if (input.charAt(iPos) == ".")
			{
				iCnt++;
				if (iCnt>1)
					return false;
			}
		}
	}
	
	// the number is valid ... return true
	return true;
}

function ValidateMessage(input, fieldname)
{
	// check the number and if it is invalid display the message and insert the name as the 
	// incorrect field name in the alert box
	if (!(ValidNumber(input)))
	{
		alert("Please enter a valid number for the " + fieldname + " field. Please use decimal points as opposed to commas, and omit all spaces.");
		return false;
	}
	
	// the number is valid ... return true
	return true;
}

function FormB_Opt()
{
	//This function is only avail. for IE4 and above!
	if (ie4)
	{
		document.all.SecA_INPUT.style.display = "NONE";
		document.all.SecB_INPUT.style.display = "";
		Hide_Obj(document.all.calc2); 
		document.Calc.TGMI.disabled = true;
	}
	document.Calc.IR.value  = "";	
	document.Calc.IR2.value = "";	
}

function Calc_Con()
{
	if (document.FormA.B_TGMI.value < 7000)
		document.FormA.B_Con.value = "0";

	if ((document.FormA.B_TGMI.value >= 7000) && (document.FormA.B_TGMI.value < 12000))
		document.FormA.B_Con.value = "0.25";

	if ((document.FormA.B_TGMI.value >= 12000) && (document.FormA.B_TGMI.value < 18000))
		document.FormA.B_Con.value = "0.50";						//5%

	if (document.FormA.B_TGMI.value >= 18000)
		document.FormA.B_Con.value = "1";						//1%
}	

function AuChar(sg,i)
{ 
	var l,r,sg1,sg2;
	
  	l = sg.length;
  	if((i<0)||(i>=l)) 
		return("");
  	r = sg.substring(i,i+1)
  	return(r);
}

function AuInStr(sg,k)
{ 
	var i,l,sg1,sg2;
  	
	l=sg.length; 
	for(i=0;i<l;++i)
  	{ 
		if(k==AuChar(sg,i)) 
			return(i);
	}
	return(-1);
}

function AuMoney(z)
{ 
	var m,r;
	m	= 100*z+0.5; 
	r	= m%1; 
	m  -= r; 
	m  /= 100;
	return(m);
}

function AuMoney0(z)
{ 
	var j,l,zz,mm;
	
	zz = AuMoney(z);
	document.Calc.Calc0Money.value=zz; 
	mm=document.Calc.Calc0Money.value;
	l = mm.length; 
	j = AuInStr(mm,".");
	if(j>0)
	{ 
		if(j==(l-2)) 
			mm += "0";
	}
	return(mm);
}
	
function AuFactor(p,n)
{ 
	var i,S,z;
	S=z=1; 
	for(i=1;i<n;++i)
	{ 
		z*=p; S+=z;
	} 
	z*=p;
	return(z/S);
}

//Calculate Customer Rate using Cons & HLR
function Calc_CR()
{
	
	temp								= document.FormA.B_Con.value;			
	document.FormA.B_CR.value 	= (document.FormA.B_HLR.value - temp);
	document.Calc.IR.value		= document.FormA.B_CR.value;
	document.Calc.IR2.value 	= document.FormA.B_CR.value;
}

function Calc_MaxLoan()
{
	income			= document.Calc.TGMI.value;
	RTI 			= document.Calc.IR.value;
	Repay			= (document.Calc.TGMI.value * RTI)*12;
	Rate			= document.Calc.IR.value;
	monthlyrate = 1 + (Rate / 12) * 0.01;
	mpay_time	= (document.Calc.Pay_Time.value)*12;	
	go				= true;
	
	if(go)
	{		
		if(ValidateMessage(document.Calc.Pay_Time.value, "Repayment time") == false)	
		{
			document.Calc.Pay_Time.focus();
			go = false;
		}
	}

	if(go)
	{
		if(ValidateMessage(document.Calc.TGMI.value, "Gross Monthly Income") == false)	
		{
			document.Calc.TGMI.focus();
			go = false;
		}
	}

	if (go)
	{
		if(ValidateMessage(document.Calc.IR.value, "Interest rate") == false)	
		{
			document.Calc.IR.focus();
			go = false;
		}
	}
	
	if(go)
	{
		if(document.Calc.IR.value == "")
		{
			alert("Please enter in the Interest rate value please");
			document.Calc.IR.focus();
			go = false;
		}
		if (go)
			document.Calc.IR2.value = document.Calc.IR.value;
	}
	
	if (go)
	{
		go2 = true;
		if (income < 3500)
		{
			alert("You have to earn at least R 3500 to use this calculator");
			go2 = false;
		}

		if ((income >= 3500) && (income <= 6999))
			maxInst 	= AuMoney(0.25 * income);		

		if ((income >= 7000) && (income <= 11999))
			maxInst 	= AuMoney(0.27 * income);		

		if (income >= 12000)
			maxInst 	= AuMoney(0.30 * income);		

		if (go2 == true)
		{
			Max_Loan    = maxInst / AuFactor(monthlyrate, mpay_time);
			document.Calc.BIA.value = AuMoney0(maxInst);
			document.Calc.QLA.value = AuMoney0(Max_Loan);
			document.Calc.Repaid_Over.value = document.Calc.Pay_Time.value;
		}
	}
}

function Go_Reset()
{
	document.Calc.TGMI.value 			= "";
	document.Calc.Pay_Time.value		= 20;
	document.Calc.QLA.value				= "";
	document.Calc.Repaid_Over.value	= "";
}

/*function Do_The_Hide_Thing()
{
	if (ie4)
		Hide_Obj(document.all.calc2);
}
*/