// requires commoncalc.js
function calculate() {
	var attributes = new Array();
	attributes ["fsf"] = $("fsf").value;
	attributes ["Pf"] = $("Pf").value;
	attributes ["pid"] = $("pid").value;
	attributes ["dp"] = $("dp").value;
	attributes ["oef"] = $("oef").value;
	attributes["pef"] = $("pef").value;
	attributes ["sg"] = $("sg").value;
	attributes ["viscosity"] = $("viscosity").value;
	attributes ["Tb"] = $("Tb").value;
	attributes ["Tf"] = $("Tf").value;
	attributes ["Zb"] = $("Zb").value;
	attributes ["Zf"] = $("Zf").value;
	
	$("message").innerHTML = "&nbsp;";
	
	// make sure all values are numeric
	var valid = true;
	for (key in attributes)
	{
		if (attributes[key] == "")
		{
			valid = false;
			break;
		}
		else if (!isNumber(attributes[key]))
		{
			var labelID = "lbl" + key;
			$("message").innerHTML = $(labelID).innerHTML + " must be numeric!";
			valid = false;
			break;
		}
		else if (attributes[key] <= 0)
		{
			var labelID = "lbl" + key;
			$("message").innerHTML = $(labelID).innerHTML + " must be > 0!";
			valid = false;
			break;
		}
	}
	
	if (valid)
	{
		var fsfeu = $("fsfeu").value;
		var pideu = $("pideu").value;
		var dpeu = $("dpeu").value;
		var obeu = $("obeu").value;
		var Pfeu = $("Pfeu").value;
	
		var queryString = "fsf=" + attributes["fsf"] + "&fsfeu=" + fsfeu + "&Pf=" + attributes ["Pf"] + "&Pfeu=" + Pfeu + "&pid=" + attributes ["pid"] + "&pideu=" + pideu + "&dp=" + attributes ["dp"] + "&dpeu=" + dpeu + "&oef=" + attributes ["oef"] + "&pef=" + attributes ["pef"] + "&sg=" + attributes ["sg"]  + "&viscosity=" + attributes ["viscosity"] + "&Tb=" + attributes ["Tb"] + "&Tf=" + attributes ["Tf"] + "&Zb=" + attributes ["Zb"] + "&Zf=" + attributes ["Zf"] + "&obeu=" + obeu;
	
		xmlhttpPost("http://dexautomation.com/ajax/gob.php", queryString);
		$("message").innerHTML = "thinking..."; //<img src='images/loading.gif' alt='loading' border='0'>;
	}
}

function updatepage(code){
	eval(code);
	if (data['error'])
	{
		$("br").innerHTML = "*****";
		$("ob").innerHTML = "*****";
	}
	else
	{
		$("br").innerHTML = data['br'];
		$("ob").innerHTML = data['ob'];
	}
	$("message").innerHTML = data['message'];
}

function showTable(table, targetID) {
	var t = $(targetID);
	 
	 switch (table)
	 {
		 case "ref":
			 t.innerHTML = refGas();
		 break;
		 
		 case "pipe":
			 t.innerHTML = pipeTable();
		 break;
		 
		case "tef":
			 t.innerHTML = "<img src='images/tef.gif' ALT='Expansion Factor' border='0'>";
		 break;
		 
		case "comp":
			t.innerHTML = "<span style='font-weight: 700;'>Procedure for determining 'Compressability Factor Z'</span><br />When dealing with gases at relatively low pressure, it is generally acceptable to use the Compressibility Factor Z obtained from <a href=\"JavaScript:showTable('cf', 'innerTarget')\">this table.</a> Otherwise, obtain and note the Critical Pressure (Pc) and Critical Temperature (Tc) from a reference such as <a href=\"JavaScript:showTable('cc', 'innerTarget')\">this one.</a> Then, use <a href=\"JavaScript:showTable('z', 'innerTarget')\">this utility</a> to calculate the Reduced Pressure (Pr) and Reduced Temperature (Tr).<br /><div id='innerTarget' style='width: 100%'></div>";
		break;
		 
		case "gsg":
			t.innerHTML = gsgTable();
		break;
		 
		case "cf":
			t.innerHTML = cfTable();
		break;
		 
		case "cc":
			t.innerHTML = ccTable();
		break;
		 
		case "z":
			t.innerHTML = zTable();
		break;

		 
		default:
			t.innerHTML = "";
    }
}