// JavaScript Document
//-----------------------------------------------------------------------------
// eventhandler for inputfields and selectfields
	var foc = function ()
	{
		this.style.backgroundColor = bg_activ;
	}

	var keyup = function (ev)
	{
		this.changed = true;
		if('txtInterest' != this.id)
		{
			calc();
		}
	}

	var leave = function ()
	{
		if (false == this.changed)
		{
			this.style.backgroundColor = bg_inaktiv;
		}
		else
		{
			this.style.backgroundColor = bg_changed;
		}
		calc();
	}
	
//-----------------------------------------------------------------------------	
// eventhandler for selectfields
	var change = function ()
	{
		this.changed = true;
		calc();
	}
	
//-----------------------------------------------------------------------------
// set eventhandler of forms
	function evHdl_input(el)
	{
		el.onfocus = 	foc;
		el.onkeyup = 	keyup;
		el.onblur = 	leave;

		el.getNum = getNumeric;
		el.sD = setDots_input;
		el.rD = removeDots;

		el.changed = false;
		el.style.backgroundColor = bg_inaktiv;
		el.value = 0;
	}
	
	function evHdl_select(el)
	{
		el.onfocus = 	foc;
		el.onchange = 	change;
		el.onblur = 	leave;
		
		el.changed = false;
	}
	
//-----------------------------------------------------------------------------
// start
	function init()
	{
		for (key in setting)
		{
			if (key != 'length' && $(key))
			{
				if ($(key).type == 'text')
				{
					evHdl_input($(key));
				}
				else
				{
					evHdl_select($(key));
				}
			}
		}

		$('txtPrice').value = setting['txtPrice'];
		// calculate
		calc();
	}
	
	
	
//-----------------------------------------------------------------------------
// get real number
	var getNumeric = function ()
	{
		if('txtInterest' == this.id)
		{
			var tmp = parseFloat(this.value);
			if (''==tmp||isNaN(tmp))tmp = 0;
			return tmp;
		}
		return forceNumber(this.value);
	}
	
	var forceNumber = function(nval , valid)
	{
		var tmpval = "";
		var vchars = (valid)? vldChars+valid : vldChars;
		if (null == nval.length)
		{
			return nval;
		}
		for(var i = 0; i < nval.length; i+=1)
		{
			tmpchar = nval.charAt(i);
			if(vchars.indexOf(tmpchar) != -1)
			{
				tmpval += tmpchar;
			}
		}
		if (''==tmpval)tmpval = 0;
		return parseInt(tmpval);
	}

//-----------------------------------------------------------------------------
// set dots into string
	var setDots_input = function () 
	{
		var tmp = setDots(this.getNum());
		this.value = tmp;
	}
	
	var setDots = function (val) 
	{
		strValue = '' + val;
		var objRegExp  = new RegExp('(-?[0-9]+)([0-9]{3})');

		//check for match to search criteria
		while(objRegExp.test(strValue)) 
		{
			//replace original string with first group match,
			//a dot, then second group match
			strValue = strValue.replace(objRegExp, '$1.$2');
		}
		//this.value = strValue;
		
		return strValue;
	}

	var removeDots = function() 
	{
		var objRegExp = /\./g; 
		return this.value.replace(objRegExp,'');
	}


//-----------------------------------------------------------------------------
// calculate
	function calc()
	{
		$('txtPrice').sD();
		$('txtCosts').value = calc_cost();
		$('txtCosts').sD();
		$('txtMoney').value = calc_deposit();
		$('txtMoney').sD();
		$('calcTotalMortgage').firstChild.data = setDots(calc_morgage());
		$('txtInterest').value = calc_interest();
		$('calcPayment').firstChild.data = setDots(calc_estimate());
	}
	
	function calc_cost()
	{
		var tmp = 0;
		if ($('txtCosts').changed)
		{
			tmp = $('txtCosts').getNum();
		}
		else
		{
			
			tmp = ($('txtPrice').getNum()/100);
			tmp = tmp * forceNumber(setting['txtCosts']);
		}
		return Math.round(tmp);
	}
	
	function calc_deposit()
	{
		var tmp = 0;
		if ($('txtMoney').changed)
		{
			tmp = $('txtMoney').getNum();
		}
		else
		{
			tmp = (($('txtPrice').getNum() + $('txtCosts').getNum())/100);
			tmp = tmp * setting['txtMoney'];
		}
		return Math.round(tmp);
	}
	
	function calc_morgage()
	{
		var tmp = $('txtPrice').getNum() + $('txtCosts').getNum()-$('txtMoney').getNum();
		return Math.round(tmp);
	}
	
	function calc_interest()
	{
		var tmp = setting['txtInterest'];
		if ($('txtInterest').changed)
		{
			tmp = $('txtInterest').getNum();
		}
		return tmp;
	}
	
	function calc_estimate()
	{
		var tmp = 0;
		var morgage = calc_morgage();
	
		var tmp = (morgage/100*$('txtInterest').getNum())/12;
		return Math.round(tmp);
	}