<!--
/* contains methods for the mortgage calculator */
var Fields;
var Prefix;
var Component;

function isblank(field_value)
		{
			var len=field_value.length;
			var i;
			for (i=0;i<len;++i)
				{
					if( field_value.charAt(i) != " " )
					return false ;

				}
			return true;
		}
		
function IsNumeric(description,id)
{
	var amount = document.getElementById(id);
	var msgFld = document.getElementById("Lb"+id+"Error");
	
	msgFld.innerHTML = "";
	
	if ( isblank(amount.value))
	{
		amount.value = '0.00';
	}
	if (isNaN(amount.value) | amount.value < 0 )
	{
		// asume the error message label has the same name as the field, preceded by "Lb" and with a suffix of "error"
		if (description == "")
			msgFld.innerHTML = "Invalid Amount";
		else
			msgFld.innerHTML = description + " is not valid";
		
		var valid =  document.getElementById("isValid");
		valid.value = "false";	
		setTimeout(function(){amount.focus()}, 10);
		
		return false;
 	}
 	var theAmount = Number(amount.value);
 	amount.value = theAmount.toFixed(2);
 	
 	return true;
}

function CalculateTotal(description,id)
{
	var valid =  document.getElementById("isValid");
	if ( ! IsNumeric(description,id))
	{
		valid.value = "false";
		return false;
	}
	else
	{
		var i=0;
		var total = 0;
		var fieldId = "";
		// total is server side so need to prefix with component name
		var totalFld = document.getElementById(Component + "_" + Prefix+"Total");
		try
		{
			for (i = 1; i<=Fields ; i++)
			{	
				fieldId = Prefix+"Amount" + i;
				var field = document.getElementById(fieldId);
				total += Number(field.value);
			}
		}
		catch(e)
		{
			if (i>0)
				IsNumeric("",fieldID);
				
			totalFld.value = '0.00';
			valid.value = "false";	
			return false;	
		}
		totalFld.value = total.toFixed(2);
		valid.value = "true";
	}
		
}	
	

function CalculateTotalIncome(description,id)
{
	/* set fields needed by script */
	Fields = 3;
	Prefix = "I";
	Component = "Income";
	CalculateTotal(description,id)
}


function CalculateTotalExpenditure(description,id)
{
	/* set fields needed by script */
	Fields = 22;
	Prefix = "E";
	Component = "Expenditure";
	CalculateTotal(description,id)
}
function SetExpenditure(formname)
{
	var amount = document.getElementById('TotalExp');
	var errorMsg = document.getElementById('LbTotalExpError');
	errorMsg.innerHTML = "";
	if (isNaN(amount.value) | amount.value < 0  )
	{
		errorMsg.innerHTML = "Total Expenditure is not valid";
		//set focus only works with a delay on firefox
		setTimeout(function(){amount.focus()}, 10);
		return;
	}
	if ( isblank(amount.value) )
	{
		amount.value = '0.00';
		var mortgage = document.getElementById('EAmount1');
		setTimeout(function(){mortgage.focus()},10);
		return;
	}
//amount.focus();
//setTimeout(function(){amount.focus()},10);
var theAmount = Number(amount.value);
 	amount.value = theAmount.toFixed(2);
 	
 	// set the big total
 	var amount2 = document.getElementById('Expenditure_ETotal');
	amount2.value = theAmount.toFixed(2);
	if (theAmount > 0)
		DisableEnableExpenditure(true);
	else
		DisableEnableExpenditure(false);
}
function DisableEnableExpenditure(state)
{	
	Fields=22;
	Prefix="E";
	for (i = 1; i<=Fields ; i++)
	{	
		fieldId = Prefix+"Amount" + i;
		var field = document.getElementById(fieldId);
		field.disabled = state;
		if (state == true)
			field.value='0.00';
		
	}
}
function totatafforamt()
{
	var income = document.getElementById('Income_ITotal').value;
	var expense = document.getElementById('Expenditure_ETotal').value;
	var totalaff = income-expense;
	document.getElementById('affordamt').value= totalaff.toFixed(2);
}
//-->
