/*
	Mortgage calculation form
	
	@author James Pritchard <james.pritchard@totemic.co.uk>
	@copyright Totemic Ltd (c) 2007

*/

function	round2dec( x )
{
	x = "" + eval( x );
	num = parseFloat( x );
	if( isNaN( num ) )
		num = 0.0;
	num = Math.abs( num * 100 );
	decstr = Math.abs( Math.round( num ) );
	retval = (decstr / 100).toFixed(2);
	return retval;
}
/* Safely parse a float */
function	atof( x )
{
	if ( isNaN( parseFloat( x ) ) )
			return 0;
	else	return parseFloat( x );
}
/* Safely parse an Int */
function	atoi( x )
{
	if ( isNaN( parseInt( x ) ) )
			return 0;
	else	return parseInt( x );
}
function recalctotal( )
{
	if(errorCheck( ) === true)
		return;
	
	var loanamount		= atof( document.getElementById('mortamount').value );
	var	interestrate	= atof( document.getElementById('intrate').value );
	var loanterm		= atoi( document.getElementById('mortterm').value );
	var i = .0;

	i = loanamount * Math.pow( (1 + (interestrate / 100)),loanterm) * (interestrate / 100) * ( 1 / ( Math.pow( 1 + (interestrate / 100), loanterm) -1) ) * ( 1 / 12); 
	document.getElementById('repayment').value = "£ " + round2dec( i );

	j = interestrate * loanamount / 1200;
	document.getElementById('intrestonly').value = "£ " + round2dec( j );
	
	if(i > 0 && j > 0) {
		document.getElementById('results').style.display = 'block';
	} else {
		document.getElementById('results').style.display = 'none';
	}
}
function intrateInc( x )
{
	document.getElementById('results').style.display = 'none';

	var intrate_ele = document.getElementById('intrate');
	var intrate = parseFloat(intrate_ele.value);
	intrate += x;
	if(intrate < 0.1)	{
		intrate = 0.1;
	}
	if(intrate > 25.0)	{
		intrate = 25.0;
	}
	
	intrate_ele.value = intrate.toFixed(2);
}
function termInc( x )
{
	document.getElementById('results').style.display = 'none';

	var term_ele = document.getElementById('mortterm');
	var term = parseFloat(term_ele.value);
	term += x;
	if(term < 1)	{
		term = 1;
	}
	if(term > 99)	{
		term = 99;
	}
	
	term_ele.value = term.toFixed(0);
}

function errorCheck( ) {
	document.getElementById('results').style.display = 'none';

	// Errors have a className='error' on the label and a span appended to the label element as a child node.
	//<span>You need to enter an amount</span>
	var loanamount			= atof( document.getElementById('mortamount').value );
	var loanamount_label	= document.getElementById('mortamount_label');
	var loanamount_span		= document.getElementById('mortamount_span');
	var	interestrate		= atof( document.getElementById('intrate').value );
	var interestrate_label	= document.getElementById('intrate_label');
	var interestrate_span	= document.getElementById('intrate_span');
	var loanterm			= atoi( document.getElementById('mortterm').value );
	var loanterm_label		= document.getElementById('mortterm_label');
	var loanterm_span		= document.getElementById('mortterm_span');
	
	var errors				= false;

	loanamount_label.className = '';
	interestrate_label.className = '';
	loanterm_label.className = '';

	loanamount_span.display = 'none';
	interestrate_span.display = 'none';
	loanterm_span.display = 'none';
	
	if(parseInt(loanamount) < 1 || parseInt(loanamount) > 99999999) {
		// Loan amount isn't valid
		loanamount_label.className = 'error';
		loanamount_span.display = 'block';
		errors = true;
	}
	
	if(parseFloat(interestrate) < 0.1 || parseFloat(interestrate) > 25.0) {
		// Interest rate is out of range
		interestrate_label.className = 'error';
		interestrate_span.display = 'block';
		errors = true;
	}
	
	if(parseInt(loanterm) < 1 || parseInt(loanterm) > 99) {
		// Mortage term is out of range
		loanterm_label.className = 'error';
		loanterm_span.display = 'block';
		errors = true;
	}
	
	return errors;
}
