// Quanterion Solutions 
// © Copyright 2008

					// This function allows multiple functions to be called when an
					// event occurs.  For example, if the object called is window,
					// the event_type is load and the function is
					// alert("Hello World!"), then when the page is loaded an alert
					// box will pop-up, displaying the text "Hello World!"
function addEvent(objct, event_type, functn)
{
	if (objct.addEventListener)
	{
		objct.addEventListener(event_type, functn, false);
		return true;
	}
	else if (objct.attachEvent)
	{
		var r = objct.attachEvent('on' + event_type, functn);
		return r;
	}
	else
	{
		return false;
	}
}

					// This function opens up a new window.  The url and name of the
					// new window are passed as arguments.
function newWindow(url, windowName)
{
	var newWindow;
	newWindow = window.open(url, windowName);
	if (window.focus)
	{
		newWindow.focus();
	}
}

function printPage()
{
	var name_of_the_tool;
	name_of_the_tool = parent.getNameOfTool();
	document.getElementById("printing label").innerHTML = name_of_the_tool;
	print();
}

					// This function replaces the HTML within the element with the id
					// divID.  The new HTML is loaded from a file from location
					// fileURL.  This has been tested for most browsers, but may not
					// work in some.
function include(fileURL, divID)
{
	var element = document.getElementById(divID);
	if (!element)
	{
		alert('Bad element id');
		return;
	}
	else
	{
		element.innerHTML = load(fileURL);
	}
}

					// This function loads a file from the location fileURL.  This
					// has been tested for most browsers, but may not work in some.
function load(fileURL)
{
	fileURL = replaceSubString(fileURL, " ", "%20");
	var req = false;
					// Should work with most modern browsers.  However most WML and
					// WAP browsers will not support this at all.
	if (window.XMLHttpRequest)
	{
    	try
		{
			req = new XMLHttpRequest();
		}
		catch (e)
		{
			req = false;
		}
	}
	else
	{
		req = false;
	}
	if (req)
	{
		if (fileURL != null)
		{
						// The false is important because it ensures that the file
						// will be completely read before a the program moves on.
						// This will also mean that the program could lock up if the
						// connection times out.
			req.open('GET', fileURL, false);
			req.send(null);
			if (req.readyState == 4)
			{
				// if "OK"
				if (req.status == 200)
				{
					return req.responseText;
				}
				else
				{
					alert("Problem retrieving data:" + req.statusText);
					return;
				}
			}
			else
			{
				alert("I NEED MORE TIME!");
				return;
			}
			alert("only get's here if something is horribly wrong.");
		}
		else
		{
			return 'No such file';
		}
	}
	else
	{
		return ('Sorry, your browser does not support XMLHTTPRequest ' +
			'objects.  This page works with Internet Explorer 7, Firefox 2, ' +
			'Safari 3, Opera 9, and Netscape Navigator 9 or better.  Please update ' +
			'your browser.');
	}
}

					// This function takes in a string with the format:
					// [element #1 id]=[element #1.value]
					// [element #2 id]=[element #2.value]
					// ...
					// And updates the current tool accordingly.
function loadContent(content)
{
	if (content == "NotFound")
	{
//		alert("Error - Content not found");
	}
	else
	{
		var content_index = 0;
		var elementId = '';
		var variable_value = '';
		if (content != "")
		{
			for (content_index; content.substring(content_index, content_index + 1) != '='; content_index++)
			{
				elementId = elementId + content.substring(content_index, content_index + 1);
			}
			content_index++;
			for (content_index; content.substring(content_index, content_index + 1) != ','; content_index++)
			{
				variable_value = variable_value + content.substring(content_index, content_index + 1);
			}
			content_index = content_index + 2;
			if (document.getElementById(elementId).tagName.toUpperCase() == "SELECT")
			{
				document.getElementById(elementId).selectedIndex = variable_value;
			}
			else if (document.getElementById(elementId).tagName.toUpperCase() == "INPUT")
			{
				if (document.getElementById(elementId).getAttribute('type').toUpperCase() == "RADIO")
				{
					document.getElementById(elementId).checked = variable_value;
				}
				else if (document.getElementById(elementId).getAttribute('type').toUpperCase() == "CHECKBOX")
				{
					if (variable_value == "true")
					{
						document.getElementById(elementId).checked = true;
					}
					else
					{
						document.getElementById(elementId).checked = false;
					}
				}
				else
				{
					document.getElementById(elementId).value = variable_value;
				}
			}
			if (content.substring(content_index))
			{
				loadContent(content.substring(content_index));
			}
	//		settimeout("loadContent(" + content.substring(content_index) + ")", 1);
		}
	}
}

					// This function identifies all the elements to be saved and
					// returns them in a string with the format:
					// [element #1 id]=[element #1.value]
					// [element #2 id]=[element #2.value]
					// ...
function storeContent()
{
	var content = '';
	var select_statments = document.getElementsByTagName("select");
	var inputs = document.getElementsByTagName("input");
	for(var j = 0; j < select_statments.length; j++)
	{
		content = content + select_statments[j].id + '=' + select_statments[j].selectedIndex + ', ';
	}
	for(var i = 0; i < inputs.length; i++)
	{
		if (inputs[i].getAttribute('type').toUpperCase() != "BUTTON")
		{
			if (inputs[i].getAttribute('type').toUpperCase() == "RADIO")
			{
				if (inputs[i].checked == true)
				{
					content = content + inputs[i].id + '=' + inputs[i].checked + ', ';
				}
			}
			else if (inputs[i].getAttribute('type').toUpperCase() == "CHECKBOX")
			{
				content = content + inputs[i].id + '=' + inputs[i].checked + ', ';
			}
			else
			{
// remove = signs and & symbols
				content = content + inputs[i].id + '=' + inputs[i].value + ', ';
			}
		}
	}
	return content;
}

					// This is used mainly for MouseOver activated image changes.  So if the
					// user has their mouse over an image, the image might become highlighted
					// and then when the mouse leaves the image it might revert back to its
					// normal state.  This is accomplished with this function.
function flipImage(image)
{
	if (document.getElementById(image))
	{
		var oldImage = document.getElementById(image).src;
		var newImage = oldImage.toString().substring(0, oldImage.toString().length - 5);;
		if (oldImage.toString().indexOf('1') == -1)
		{
			newImage = newImage + '1.png';
		}
		else
		{
			newImage = newImage + '2.png';
		}
		document.getElementById(image).src = newImage;
	}
}

					// This function handles adding units to a measurement.  The last argument
					// is there to keep the resulting string within the confines of a set
					// length if required (If not required then simply pass -1 for maxLength.)
					// The function returns the boolean value false if the maxLength does not
					// allow both the measurement and the units to fit within the result.
					// Note that this function does not do any rounding.  It simply drops
					// digits from the end until the measurement and units fit.  Perhaps that
					// should be added, though it currently involves completely reworking the
					// function.
function appendUnits(measurement, units, maxLength)
{
	var goodValue = isValidPositiveNumericalValue(measurement);
	units = ' ' + units;
	if (goodValue)
	{
		measurement = roundNumber(eval(measurement), maxLength);
		measurement = measurement.toString();
		measurement = measurement + units;
		return measurement;
	}
	else if (measurement == 0)
	{
		return '0' + units;
	}
	return '';
}

					// This function returns a String of the argument measurement without the
					// units at the end.  If you removes the units from the end of a
					// measurement.  If the units are displayed with a space in front of them,
					// the function takes that into account.
function removeUnits(measurement, units)
{
	measurement = measurement.toString().toUpperCase();
	units = units.toString().toUpperCase();
	var unitStart = measurement.indexOf(units);
	if (unitStart == -1)
	{
		return measurement;
	}
	if (measurement.indexOf(' ' + units) == -1)
	{
		return measurement.substring(0, unitStart);
	}
	return measurement.substring(0, unitStart - 1);
}

					// This function takes in a string as an argument and reutrns it with all
					// instances of the second string paramter replaced with the third string
					// parameter.
function replaceSubString(stringArg1, beingReplaced, withThis)
{
	var tempString = '';
	for (var i = 0; i < stringArg1.length; i++)
	{
		if (stringArg1.charAt(i) == beingReplaced.charAt(0))
		{
			for (var j = 0; j < beingReplaced.length && (j + i) < stringArg1.length; j++)
			{
				if (stringArg1.charAt(i + j) != beingReplaced.charAt(j))
				{
					j = beingReplaced.length;
					tempString = tempString + stringArg1.charAt(i);
				}
				else if ((j + 1) == beingReplaced.length)
				{
					i = i + beingReplaced.length - 1;
					tempString = tempString + withThis;
				}
			}
		}
		else
		{
			tempString = tempString + stringArg1.charAt(i);
		}
	}
	return tempString;
}

					// This function takes in a string as an argument and removes all the
					// commas from it.
function removeCommas(stringArg)
{
	var tempString = '';
	for (var i = 0; i < stringArg.length; i++)
	{
		if (stringArg.charAt(i)!= ',')
		{
			tempString = tempString + stringArg.charAt(i);
		}
	}
	return tempString;
}

					// Displays a number as the innerHTML of the given element, with the
					// number of decimal places displayed set by rounding to the value given
					// as the last argument.
function outputNumber(element_id, number, decimal_length)
{
	document.getElementById(element_id).innerHTML = roundDecimal(number, decimal_length);
}

					// This function uses the isValidPositiveNumericalValue() function to test
					// each value in an array (squentially by index) and replaces all
					// supposedly invalid entries with the value NULL.
function sanitizeNumericalArray(dirtyArray)
{
	for (var i = 0; i < dirtyArray.length; i++)
	{
		if (!(isValidPositiveNumericalValue(dirtyArray[i])))
		{
			dirtyArray[i] = '';
		}
	}
	return dirtyArray;
}

					// This function places leading zeros in front of a number.  It does so
					// until it is at least as long as the argument finalLength.  It then
					// returns the number as a String.
function appendLeadingZeros(number, finalLength)
{
	number = number.toString();
	while (number.length < finalLength)
	{
		number = '0' + number;
	}
	return number;
}

					// This function returns a string which is the code to create a table.
					// The table will have a number of rows and columns that is set by the
					// arguments and will use the id argument to give each cell in the table a
					// unique identifier that is that id followed by the row and the column.
					// (Example <td id="ID table row ROWS column COLUMNS"> where the
					// capitalized words will be replaced with the corresponding variables'
					// value.)
function createTable(id, rows, columns)
{
	var tableCreationText = '<table id = "' + id + ' table">';
	for (var r = 0; r < rows; r++)
	{
		tableCreationText = tableCreationText + '<tr id = "' + id + ' table row ' + r + '">';
		for (var c = 0; c < columns; c++)
		{
			tableCreationText = tableCreationText + '<td id = "' + id + ' table row ' + r + 
				' column ' + c + '"></td>';
		}
		tableCreationText = tableCreationText + '</tr>';
	}
	tableCreationText = tableCreationText + '</table>';
	return tableCreationText;
}

					// This function does exactly what its name implies.  The newText
					// arguement is the text or code that you want inserted into table 'ID
					// table' at location ROW, COLUMN.
function changeTable(id, row, column, newText)
{
	document.getElementById(id + ' table row ' + row + ' column ' + column).innerHTML = newText;
}

//
function removeElement(id)
{
	var element = document.getElementById(id);
	element.parentNode.removeChild(element);
}

					// This function takes in the id of an element and disables that element,
					// both in function and in class name.
function disable(id)
{
	document.getElementById(id).disabled = true;
	document.getElementById(id).className = "disabled";
}

					// This function basically undoes the disable function above.
function enable(id)
{
	document.getElementById(id).disabled = false;
	document.getElementById(id).className = "";
}

					// This function takes in a number and rounds it to a given decimal
					// length.  It does this by multiplying the number by 10^(deciamlLength)
					// and then calling the Math.round() function (which rounds a variable to
					// the nearest whole number.)  It is then reduced back by the same factor
					// of 10 with the new rounded last digit and no trailing numbers
					// afterwards (because they were cut off during the Math.round()
					// function.)  Note that this method can cause a problem if the increased
					// number causes an overflow by being too large or too small.
function roundDecimal(theNumber, decimalLength)
{
	var negative = false;
	var decimalPoint = theNumber.toString().indexOf('.');
//	var smallestValue = 1 / (Math.pow(10, eval(decimalLength)));
	if (eval(theNumber) < 0)
	{
		negative = true;
	}
	if (decimalPoint == -1)
	{
		return theNumber;
	}
	var integerSection = eval(theNumber.toString().substring(0,decimalPoint));
	var fractionSection = eval(theNumber.toString().substring(decimalPoint));
	fractionSection = (Math.round(eval(fractionSection) * Math.pow(10, decimalLength)) / Math.pow(10, decimalLength));

	if (fractionSection.toString().substring(0, 1) == "1")
	{
		integerSection = integerSection + 1;
		integerSection = roundDecimal(integerSection, 0);
		fractionSection = "";
	}
	else
	{
		fractionSection = fractionSection.toString().substring(1, decimalLength + 2);
	}
	if (negative)
	{
		return (-1) * (eval(integerSection.toString() + fractionSection.toString()));
	}
	return (eval(integerSection.toString() + fractionSection.toString()));
}