/************************/
/* Global Object LabCalc    */
/**********************/
var LabCalc = {};

/************************************/
function Throw(msg) {
// error handler function
	alert(msg);
}

/************************************/
LabCalc.initialize = function() {
	// set focus to first input field
	document.calculator.input1.focus();
}

/************************************/
LabCalc.applyUnits = function(number,units) {
// This functions applies "units" to "number"
	switch(units) {
		case "mega" : return number*1000000;
		case "kilo" : return number*1000;
		case "deca"	: return number*10;
		case "milli" : return number/1000;
		case "micro" : return number/1e6;
		case "nano" : return number/1e9;
		case "pico" : return number/1e12;
		default : return number;
	}
}

/*****************************************************/
/* Update the <textarea> defined by the "Ticker" id 		*/
/* can pass one parameter or two					*/
/* 1 param just adds as a string to the ticker			*/
/* two params, treats as a left side, right side of an equation	*/
/* **************************************************	 */
LabCalc.updateTicker = function (leftSide, rightSide) {
	var e = document.getElementById("ticker");
	var oldContent = e.value;
	if (!oldContent) oldContent="";
	
	var newContent = leftSide;
	if (arguments.length==2) newContent += " = " + rightSide;
	newContent += "\n";
	e.value = newContent + oldContent;
	
	// need to somehow cause textbox e to be fully scrolled down
	document.calculator.input1.focus(); // set focus to first input field
	document.calculator.input1.select();
}

/********************************************************/
LabCalc.printView = function(textToPrint) {
	var now = new Date();
	var t = textToPrint.replace(/\n/g,"<br>");	// replace newlines with <br> tags
	
	// Hide divs that are not to be printed
	document.getElementById("searchDiv").style.display = "none";
	document.getElementById("bodyDiv").style.display = "none";
	document.getElementById("footerDiv").style.display = "none";
		
	//Layout the Print View div
	document.getElementById("printViewDiv").style.display = "inline";

	// put content into the Print View div
	document.getElementById("printViewP").innerHTML = t;

	
	/* OLD VERSION 
	var scriptlet = "javascript: document.write('<html><head><title>Lab Calculator</title></head><body>Laboratory Calculator: " + now + "<br><br>";
	scriptlet += t + "</body></html>');";
	document.location = scriptlet; */
}

LabCalc.normalView = function() {
	// Show the normal divs
	document.getElementById("searchDiv").style.display = "block";
	document.getElementById("bodyDiv").style.display = "block";
	document.getElementById("footerDiv").style.display = "block";
	
	//Hide the Print View div
	document.getElementById("printViewDiv").style.display = "none";
}

LabCalc.insertInputValue = function(v,e) {
	// insert value v into element e 
	if (!e.value) e.value="";
	e.value+=v;
}
