// Copyright © 2002 Australasian Management and Technology (AMAT) PTY LTD, Level 7, 47 York Street, Sydney NSW 2000, Australia. All rights reserved. 
// Warning: This computer program is protected by copyright law and international treaties. Unauthorized reproduction or distribution of this program

// used to populate the fields in the loan simulator

var sLoanAmount = '100000';
var sTerm = '30';
var sIR = '6';

setValues();
	
function showRadio5()
{
	if (typeof radiobuttons5 != "undefined")
	{
		document.getElementById("radiobuttons5").style.visibility = "visible";
	}
}

function hideRadio5()
{
	if (typeof radiobuttons5 != "undefined")
	{
		document.getElementById("radiobuttons5").style.visibility = "hidden";
	}
}

function showRadio51()
{
	if (typeof radiobuttons51 != "undefined")
	{
		document.getElementById("radiobuttons51").style.visibility = "visible";
	}
}

function hideRadio51()
{
	if (typeof radiobuttons51 != "undefined")
	{
		document.getElementById("radiobuttons51").style.visibility = "hidden";
	}
}

function initForm()
{
	if (window.focus != null) window.focus();
	document.mainform.elements[0].focus();
}

function numval(val,digits,minval,maxval)
{
	if (val == "" || isNaN(val)) val = 0;
	val = parseFloat(val);
	if (digits == null) digits = 0;
	var dec = Math.pow(10,digits);
	val = (Math.round(val * dec))/dec;
	if (minval != null && val < minval) val = minval;
	if (maxval != null && val > maxval) val = maxval;
	return parseFloat(val);
}

function zeroBlanks(formname)
{
	var i, ctrl;
	for (i = 0; i < formname.elements.length; i++)
	{
		ctrl = formname.elements[i];
		if (ctrl.type == "text")
		{
			if (ctrl.value == "" || isNaN(ctrl.value))
				ctrl.value = "0";
		}
	}
}

function highLight(n)
{		
	clearAll(document.mainform);
	
	for (var i = intStartCalculator; i <= (intStartCalculator + intTotalCalculators - 1); i++)
	{
		var strOption = "option" + i;
		var strArrow = "arrow" + i;
		
		if (i == n)
		{
			document.getElementById(strOption).bgColor = clrHighlighted;
			document.getElementById(strArrow).innerHTML = '>';
		}
		else
		{
			document.getElementById(strOption).bgColor = clrUnhighlighted;
			document.getElementById(strArrow).innerHTML = '';
		}
	}
	
	if (n == 5)
	{
		if (debug == 1) { alert ("Show Radio5"); }
		showRadio5();
		showRadio51();
	}
	else
	{
		if (debug == 1) { alert ("Hide Radio5"); }
		hideRadio5();
		hideRadio51();
	}	
}

function drawForm(n)
{
	if (debug == 1) { alert ("drawForm") }
	highLight(n);
	
	for (var i = intStartCalculator; i <= (intStartCalculator + intTotalCalculators - 1); i++)
	{
		var strLayerQ = "LayerQ" + i;
		
		if (i == n) 
		{
			document.getElementById(strLayerQ).style.visibility = "visible";
		}
		else
		{
			document.getElementById(strLayerQ).style.visibility = "hidden";
		}
	}
}

function changeButtons(n)
{
	clearAll(document.mainform);

	if (n == 1) document.getElementById("dynbuttons").innerHTML = '<input type="button" value="Calculate" onClick="doLoanRepaymentCalculations(4)" class="sbttn"> <input type="reset" value="Reset Form" class="sbttn"  onClick="clearAll(document.mainform)">  <input type="button" value="Print" class="sbttn" onClick="javascript:print()">';

	if (n == 2) document.getElementById("dynbuttons").innerHTML = '<input type="button" value="Calculate" onClick="doLoanRepaymentCalculations(5)" class="sbttn"> <input type="reset" value="Reset Form" class="sbttn"  onClick="clearAll(document.mainform)">  <input type="button" value="Print" class="sbttn" onClick="javascript:print()">';
}

function mod(divisee,base)
{
	return Math.round(divisee - (Math.floor(divisee/base)*base));
}
	
function clearAll(formname)
{
//	zeroBlanks(formname);
	document.getElementById("spacer").innerHTML = '';
	document.getElementById("message").innerHTML = '';
	document.getElementById("table1").innerHTML = '';
	document.getElementById("table2").innerHTML = '';
	document.getElementById("table3").innerHTML = '';
	document.getElementById("disclaimer").innerHTML = '';
}

// The following functions are used to output the input boxes

function outputLoanAmount () {
	sHTML = "\<input type='TEXT' name='principal' size='10' value='" + sLoanAmount + "' class='textinput' onChange='value=numval(value,2,0)' maxlength='10'\>";
	document.write(sHTML);
}

function outputPropertyValue () {
	sHTML = "\<input type='TEXT' name='propertyValue' size='10' value='" + sLoanAmount + "' class='textinput' onChange='value=numval(value,2,0)' maxlength='10'\>";
	document.write(sHTML);
}

function outputInterestRate() {
	sHTML = "\<input type='TEXT' name='interest' size='5' value='" + sIR + "' class='textinput' onChange='value=numval(value,2,0)' maxlength='5'\>"
	document.write(sHTML);
}

function outputTerm() {
	sHTML = "\<input type='TEXT' name='year' size='3' value='" + sTerm + "' class='textinput' onChange='value=numval(value,2,1)' maxlength='3'\>"
	document.write(sHTML);
}

function outputState() {
	sHTML = "";
	sHTML = sHTML + "\<option value='QLD'\>QLD\<\/option\>";
	sHTML = sHTML + "\<option value='NSW'\>NSW\<\/option\>";
	sHTML = sHTML + "\<option value='ACT'\>ACT\<\/option\>";
	sHTML = sHTML + "\<option value='VIC'\>VIC\<\/option\>";
	sHTML = sHTML + "\<option value='SA'\>SA\<\/option\>";
	sHTML = sHTML + "\<option value='WA'\>WA\<\/option\>";
	sHTML = sHTML + "\<option value='NT'\>NT\<\/option\>";
	sHTML = sHTML + "\<option value='TAS'\>TAS\<\/option\>";
	sHTML = sHTML + "";
					
	document.write(sHTML);		
}

function outputFrequency() {
	sHTML = "\<select name='frequency' class='drpdwn' onChange='clearAll(document.mainform)'\>";
	sHTML = sHTML + "\<option value='m'\>Monthly\<\/option\>";
	sHTML = sHTML + "\<option value='f'\>Fortnightly\<\/option\>";
	sHTML = sHTML + "\<option value='w'\>Weekly\<\/option\>";
	sHTML = sHTML + "\<\/select\>";
					
	document.write(sHTML);		
}
