// JavaScript Document 
// almig.de //
Calculator = function(){
	this.init();
}
o = Calculator.prototype;
o.init = function(){
	this.vars = [];
	this.resultContainer = null;
	this.form = null;
	this.roundPrecision = 2;
	this.language = this.getTypo3Language();
}
o.errorMessages = {
	notAllFieldsFilled: [
						 	"Bitte füllen Sie alle Felder aus!",
						 	"Veuillez remplir toutes les cases !",
						 	"Rumanisch: Bitte füllen Sie alle Felder aus!",
						 	"We verzoeken u alle velden in te vullen!",
						 	"Please fill in all of the fields!",
						 ]
}
o.getErrorMessage = function(str){
	return this.errorMessages[str][this.language];
}
o.getTypo3Language = function(){
	var s = window.location.search;
	if(!s) return 0;
	var g = s.split("&");
	if(!g.length) return 0;
	var l = 0;
	for(var i=0;i<g.length;i++){
		var tmp = g[i].split("=");
		if(tmp[0] == "L") l = tmp[1];
	}
	return l;
}
o.setForm = function(formId){
	var el = document.getElementById(formId);
	if(!el) return false;
	this.form = el;
	el.ref = this;
	el.onsubmit = function(){
		this.ref.outputResult();
		return false;
	}
}
o.setResultContainer = function(containerId,prop){
	var el = document.getElementById(containerId);
	if(!el) return false;
	this.resultContainer = {el:el,prop:prop};
}
o.setVar = function(inputId,prop){
	var el = document.getElementById(inputId);
	if(!el) return false;
	var v = {el:el,prop:prop};
	this.vars[inputId] = v;
}
o.setRoundPrecision = function(amountOfCharsAfterComma){
	this.roundPrecision = amountOfCharsAfterComma;
}
o.allInputFilled = function(){
	var v = this.vars;
	var l = v.length;
	for(var i in v){
		var e = v[i];
		if(typeof(e) == "function")continue; // bugfix after prototype lite was included
		if(!e || !e.el || !e.el[e.prop]) return false;
	}
	return true;
}
o.getVars = function(){
	var r = {};
	var v = this.vars;
	var l = v.length;
	for(var i in v){
		var e = v[i];
		if(typeof(e) == "function")continue; // bugfix after prototype lite was included
		r[i] = Number(this.commaToDot(e.el[e.prop]));
	}
	return r;
}
o.setFormula = function(formular){
	this.formula = formular;
}
o.getResult = function(){
	if(!this.allInputFilled()){
		alert(this.getErrorMessage("notAllFieldsFilled"));
		return false;
	}
	return this.formula();
}
o.outputResult = function(){
	this.resultContainer.el[this.resultContainer.prop] = this.dotToComma(this.roundTo(this.getResult(),this.roundPrecision));
	//*
	// zusÃ¤tzliche Funktionen um auch den Prozentualen Anteil des Ergebnisses auszugeben:
	if(this.percentResultContainer && this.percentOfVar){
		this.percentResultContainer.el[this.percentResultContainer.prop] = this.dotToComma(this.roundTo(this.getPercentResult(),this.roundPercentPrecision));
	}
	//*/
}
o.animate = function(){
	window.resultanim = this.timeline = new JsTimeLine("resultanim");
	var el = new JsTimeLineElement("resultbox");
	console.log(el);
	this.timeline.setElementColorTweenEvent(el,100,500,100,"backgroundColor","#FFFFFF","#CCE2EF");
	this.timeline.loop = false;
	this.timeline.play();
}
o.roundTo = function(x, n) {
	if (n < 0 || n > 14) return false;
	var e = Math.pow(10, n);
	var k = (Math.round(x * e) / e).toString();
	if (k.indexOf('.') == -1) k += '.';
	k += e.toString().substring(1);
	var r = k.substring(0, k.indexOf('.') + n+1);
	if(r.substring(r.length-1,r.length) == "."){
		r = r.substring(0,r.length-1);
	}
	return r;
}
o.dotToComma = function(num){
	var str = num.toString();
	var tmp = str.split(".");
	return tmp.join(",");
}
o.commaToDot = function(num){
	var str = num.toString();
	var tmp = str.split(",");
	return tmp.join(".");	
}
o.log10 = function(x) {
   //return Math.log(x) / Math.LOG10E;
    return Math.log(x,10);
}

o.trace = function(msg){	
	if(window.traceWindow){
		window.traceWindow.trace(msg+"<br />\n");
	}
	if(window.console){
		console.log(msg);
	}
}
//*
// zusatzliche Funktionen um auch den Prozentualen Anteil des Ergebnisses auszugeben:
o.roundPercentPrecision = 0;
o.setPercentResultContainer = function(containerId,prop){
	var el = document.getElementById(containerId);
	if(!el) return false;
	this.percentResultContainer = {el:el,prop:prop};
}
o.setPercentOfVar = function(inputId){
	this.percentOfVar = inputId;
}
o.getPercentResult = function(){	
	var v = this.getVars();
	return ((100/v[this.percentOfVar])*this.getResult());
}
//*/