var formulas = "";
			
			//write the note of the Formula
			function showNote(){
				var index = document.getElementById("formulaSelect").selectedIndex;
				$('#note').html("<table><tr><td>"+formulas.formulas[index].description+"</td></tr></table>");
				//$('#note').html(note);
			}
			
			//get the variables to the formula oid
			function getVariables(oidFormula){
				$('#results').html("");
				var url = 'client/client.php?subject=variable&oidFormula='+oidFormula;
				try{
					$.post(url, "", function(data){
						if(data != "{\"error\":"){
							variables(data);
						}else{
							var vars = eval('(' + data + ')');
							$('#variables').html(vars.error);
						}
					});
				}catch(Error){
					$('#variables').html("<br/>Could not connect to the variableClient");
				}
			}
			
			
			//validate the variables, the user entered
			//correct: call of function calculate
			//error: show value of wrong field and show allowed values
			function validateVariables(){
				var valuesCorrect = true;
				var falseValue = "";
				var varValues = "";
				for(i=0; i < document.getElementsByTagName("input").length -1; i++){
					try{
						varValues = document.getElementsByTagName("input")[i].value;
					}catch(Error){
					}
					if (!varValues.match("^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$")) {
						valuesCorrect = false;
						falseValue = varValues;
					}
				}
				if(valuesCorrect){
					calculate();  
				}else{
					$("#results").html("<br/><b>Please check the entry: "+falseValue+"</b><br/>"+
					"Allowed values are: \"-\"  \"+\"  \"0-9\"  \".\"  \",\"");
				}
			}
			
			//calculate the formula by:
			//param1: oidFormula
			//param2: varNames
			//param3: varValues
			function calculate(){
				var oidFormula = $("#formulaSelect").val();
				var varNames = "";
				var varValues = "";
				var amVar = "";
				var i=0;
				try{
					while($("#input"+i).attr("name") != null || $("#select"+i).attr("name") != null){
						if($("#input"+i).attr("name") != null){
							varNames += $("#input"+i).attr("name");
							varValues += $("#input"+i).val().replace(",",".");
						}else{
							varNames += $("#select"+i).attr("name");
							varValues += $("#select"+i).val().replace(",",".");
						}
						if($("#selectAmount"+i).val() != null){
							amVar += $("#selectAmount"+i).val()
						}
						i++;
						if($("#input"+i).attr("name") != null || $("#select"+i).attr("name") != null){
							varNames += "@";
							varValues += "@";
							amVar += "@";
						}
					}
				}catch(Error){}
				var url = 'client/client.php?subject=result&oidFormula='+oidFormula+'&varNames='+varNames+		
				'&varValues='+varValues+'&amVar='+amVar;
				try{
					$.post(url, "", function(data){
						if(data != "{\"error\":"){
							results(data);
						}else{
							var res = eval('(' + data + ')');
							$('#variables').html(res.error);
						}
					});
				}catch(Error){
					$('#results').html("<br/>Could not connect to the formulaResultClient");
				}
			}
			
			
			//------------------------------ START Handle the formulas ------------------------------------
			function dataInit() {
				var url = 'client/client.php?subject=formula';
				try{
					$.post(url, "", function(data){
						if(data != "{\"error\":"){
							var output = "<font size=\"4\"><b>Powered by "+
							"<a href=\"http://www.abacal.com/\" target=\"_blank\">www.abaCal.com</a></font>"+
							"<br/><br/>Choose your Formula:</b><br/>"+
							"<select id=\"formulaSelect\" onchange=\"javascript:getVariables(this.value);showNote()\">";
							formulas = eval('(' + data + ')');
							for(i=0; i < formulas.formulas.length; i++){
								output += "<option value=\""+formulas.formulas[i].oid+"\">"+formulas.formulas[i].name+"</option>";
							}
							output += "</select>";
							$('#formulas').html(output);
							getVariables(formulas.formulas[0].oid);
							showNote();
						}else{
							formulas = eval('(' + data + ')');
							$('#formulas').html(formulas.error);
						}
					});
				}catch(Error){
					$('#formulas').html("Could not connect to the formulaClient");
				}
			}
			//----------------------- FINISH Handle the formulas ------------------------------------------
			
			//----------------------- START Handle the Variables -------------------------------------------
			 
			//preparing the JSON of the variables
			function variables(data){
				//clear the widget up to the variables
				var vars = eval('(' + data + ')');
				var output = "<br/><table width='100%'>";
				if(vars.error == null){
					//set up the variables (variable name  variable description  variable value)
					for(i=0; i < vars.variables.length; i++){
						output += "<tr><td width='50%'>"+vars.variables[i].description+
						"</td>"+"<td width='50%'>";
						if(vars.variables[i].value.indexOf("@") >-1){
							output += "<select id=\"select"+i+"\" name=\""+vars.variables[i].name+"\">"
							var entries = vars.variables[i].value.split("@");
							for(j=0;j<entries.length;j++){
								output += "<option value=\""+entries[j].split(";")[0]+"\">"+entries[j].split(";")[1]+"</option>";
							}
							output += "</select>";
						}else{
						output += "<input size=\"10\" id=\"input"+i+"\" name=\""+vars.variables[i].name+"\" type=\"text\"value=\""+
						vars.variables[i].value+"\" />";
						}
						output += "</td><td>";
						if(vars.variables[i].units != ""){
							output += "<select id=\"selectAmount"+i+"\">";
							var units = vars.variables[i].units.split("@");
							for(j=0;j<units.length; j++){
								output += "<option value=\""+units[j]+"\">"+units[j]+"</option>";
							}
							output += "</select>";
						}
						output += "</td></tr>";
					}
					//add calculate button
					//first validateVariables
					//second calculate
					output += "</table><br /><input type=\"button\" value=\"calculate\" onclick=\"javascript:validateVariables()\">";
				}else{
					//fill in the error
					output += "<tr><td>"+vars.error+"</td></tr></table>";
				}
				
				//add the variables to the widget
				$('#variables').html(output);
			}
			//----------------------- FINISH Handle the Variables ------------------------------------------
			
			//----------------------- START Handle the Results -------------------------------------------
			
			//preparing the JSON of the results
			function results(data) {
				//clear the widget up to the result
				try{
					var output = "<br/><b>Results:</b><br/><table width='60%'>";
					var res = eval('(' + data + ')');
					if(res.error == null){
						//set up the results
						for(i=0; i < res.results.length; i++){
							output += "<tr><td width='10%'>"+res.results[i].name+"</td><td width='40%' align=\"right\">"
							+res.results[i].value+"</td><td width='10%'>&nbsp;&nbsp;";
							if(res.results[i].units != null){
								output += res.results[i].units
							}
							output += "</td></tr>";
						}
					}else{
						//fill in the error
						output += "<tr><td>"+res.error+"</td></tr>";
					}
					output += "</table>";
				}catch(Error){
					output = "<br/>Could not connect to the formulaResultClient";
				}
				
				//add the results to the widget
				$('#results').html(output);
			}
			//----------------------- FINISH Handle the Results ------------------------------------------