
function calculate(){
	if(this.type=="text")
		this.form.activeinput = this;
		
	var activeinput = this.form.activeinput;
	var value = activeinput.value.replace(",","."); 
	var precision = activeinput.form.precision.value;
	
	try{
		value = eval(value);
		precision = eval(precision);
	}
	catch(e){
		alert("'" + activeinput.value + "' is not a number.");
		return;
	}
			
	var factor = activeinput.getAttribute("factor");
	value *= factor;
	
	var elements = activeinput.form.elements;
	
	for (e=0; e < elements.length; e++) {
		element = elements[e];
		
		if(element != activeinput){
			if(element.getAttribute("factor")){	
				var factor = element.getAttribute("factor");
				element.value = number_format(value/factor, precision);
			}
		}
	}
}

function number_format(value, precision){	
	tmpvalue = value;
	value = value.toString().toLowerCase();
	
	ePos = -1
	if (value.indexOf("e") != -1){
		ePos = value.indexOf("e")
	}
	
	if (ePos == -1){
		// er zit geen e-macht in de notatie van het getal
		if(value >=1){
			value = Math.round(value*Math.pow(10,precision))/Math.pow(10,precision);
		}
		else{
			aantalnullen = 0;			
			var length = value.length;
			for (i=2; i<length; i++){
				if (value.substring(i,i+1) != 0){
					aantalnullen = i-2;
					break
				}
			}
			value = Math.round(value*Math.pow(10,precision+aantalnullen+1))/Math.pow(10,precision) + "e-" + (aantalnullen+1);
		}
	}
	else{
		// er zit een macht in de notatie   Deze nog uitwerken
		if (value.indexOf(".") != -1){
		// het getal heeft getallen achter de comma en is > 1
			ePos = value.indexOf("e");
			eBeforepoint = value.substring(0,ePos);
			eAfterpoint  = value.substring(ePos+1);
			value = Math.round(eval(eBeforepoint)*Math.pow(10,precision-1))/Math.pow(10,precision-1);
			value = value + "e" + eAfterpoint;
		}
	}

	return value;
}


function isAlien(a)     { return isObject(a) && typeof a.constructor != 'function' }

function isArray(a)     { return isObject(a) && a.constructor == Array }

function isBoolean(a)   { return typeof a == 'boolean' }

function isFunction(a)  { return typeof a == 'function' }

function isNull(a)      { return typeof a == 'object' && !a }

function isNumber(a)    { return typeof a == 'number' && isFinite(a) }

function isObject(a)    { return (a && typeof a == 'object') || isFunction(a) }

function isRegexp(a)    { return a && a.constructor == RegExp }

function isString(a)    { return typeof a == 'string' }
