EquityCalculator = function()
{
	this.dom = new newsi.HTML.DOM();
	this._execComputeEquity();
}
	EquityCalculator.prototype._execComputeEquity = function()
	{
		this["doCalculation"] = new newsi.Event();
		this["doCalculation"].addListener("onclick" , "calc_butt" , this , "checkEquityBounds" , "false" );
	}
	
	EquityCalculator.prototype.checkEquityBounds = function()
	{	
		this.checkForEmptyFields();
		if ( !ec.checkNumber( document.equity_form.property , 1 , 9999999 , "Property value" ) ||
			!ec.checkNumber( document.equity_form.owing , .001 , 9999999 , "Mortgage amount" ) ||
			!ec.checkNumber( document.equity_form.growth , 1 , 1000 , "Annual growth" ) ) 
		{
			//return true;
		}
	}
	
	EquityCalculator.prototype.computeEquity = function(form) 
	{
		this.HouseValueCalculations();
		this.EquityCalculations();
	}
	
	EquityCalculator.prototype.extractValues = function()
	{
		var pv, mv
		pv = document.equity_form.property.value;
		pv = pv.split(",");
		pv = pv.join("");
		this.property = pv;
		mv = document.equity_form.owing.value;
		mv = mv.split(",");
		mv = mv.join("");
		this.mortgage = mv;
		
		this.PV = parseFloat(pv);
		this.MV = parseFloat(mv);
		this.Rt = parseFloat(document.equity_form.growth.value);
		this.change = parseFloat(document.equity_form.rtt.value);
		this.R = (this.Rt/100)*this.change;
		document.equity_form.property.value = ec.poundsPence( this.property );
		document.equity_form.owing.value = ec.poundsPence( this.mortgage );
	}
	
	EquityCalculator.prototype.checkForEmptyFields = function()
	{
		this.extractValues();
		
		if(this.PV == null || this.PV.length == 0 || isNaN(this.PV))
		{
			document.equity_form.property.focus();
		}
		else if(this.MV == null || this.MV.length == 0 || isNaN(this.MV))
		{
			document.equity_form.owing.focus(); 
		}
		else if(this.Rt == null || this.Rt == 0 || isNaN(this.Rt)) 
		{
			document.equity_form.growth.focus();
		}
		else{
			this.computeEquity();
		}
	}
	
	EquityCalculator.prototype.HouseValueCalculations = function()
	{		
		this.v1 = (( this.PV * this.R ) + this.PV ).toFixed(0); 
		document.equity_form.y1.value = ec.poundsPence( this.v1 );
		this.v2 = (( this.v1 * this.R ) + parseFloat( this.v1 )); 
		this.v3 = (( this.v2 * this.R ) + this.v2 ).toFixed(0); 
		document.equity_form.y3.value = ec.poundsPence( this.v3 );
	}
	
	EquityCalculator.prototype.EquityCalculations = function()
	{
		this.e1 = ( this.v1 - this.MV ).toFixed(0);
		document.equity_form.te.value = ec.poundsPence( this.e1 );
		this.e3 = ( this.v3 - this.MV ).toFixed(0);
		document.equity_form.te3.value = ec.poundsPence(this.e3);	
		this.greenAndPink();
	}
	
	EquityCalculator.prototype.greenAndPink = function()
	{
		var inputsArray = this.dom.gebtn( { parent : this.dom.e_gebid( "equity" ), el : "input" } );
		
		for( var x = 9 ; x < inputsArray.length ; x++)
		{
			if(inputsArray[x].value <= 0 )
			{
				this.dom.setClass({el:inputsArray[x], className:"negate"});
			}else
				{
					this.dom.setClass({el:inputsArray[x], className:"posate"});
				}
			
		}		
	}
	
	
	EquityCalculator.prototype.checkNumber = function( input , min , max , msg ) 
	{
		msg = msg + " has invalid data: " + input.value;
		var str = input.value;
		for ( var i = 0 ; i < str.length ; i++ ) 
		{
			var ch = str.substring( i , i + 1 )
			if ( ( ch < "0" || "9" < ch ) && ch != '.' ) 
			{
				alert(msg);
				return false;
			}
		}
		var number = 0 + input.value
		if ( number < min || max < number ) {
			alert( msg + ". Value should be in range: " + min + " - " + max );
			return false;
		}
		input.value = str;
		return true;
	}	

	EquityCalculator.prototype.poundsPence = function( N ) 
	{
		S = new String( N );
		var i = S.indexOf( '.' );
		if ( i != -1 ) {
			S = S.substr( 0, i+3 );
			if ( S.length-i < 3 )
				S = S + '0';
		}
		return S;
	}