/*Programming: 	Human Element
				Gregg Milligan
				March 2, 2009*/
				
//indicates what input field currently has focus
function setThisFocus(elementName)
{
	fieldWithFocus = elementName;
}

//check to see if a value is an integer
function validIfInteger(elementName)
{
	var valid = false;
	var errElementName = elementName + "_err";
	var elementValue = document.getElementById(elementName).value;
	
	//if the text box is empty (no input)
	if (elementValue != "")
	{
		//if the value is a number and an integer
		if (!isNaN(elementValue) && elementValue.indexOf('.') == -1)
		{
			//make sure the error message is hidden
			document.getElementById(errElementName).style.display = "none";
			
			valid = true;
		}
		else
		{
			whenInvalid(elementName);
		}
	}
	else
	{
		//if the text box does not have the focus and it is empty
		if (fieldWithFocus != elementName)
		{
			whenInvalid(elementName);
		}
	}
	
	return valid;
}

//check to see if a value is a number
function validIfNumber(elementName)
{
	var valid = false;
	var errElementName = elementName + "_err";
	var elementValue = document.getElementById(elementName).value;
	
	//if the text box is empty (no input)
	if (elementValue != "")
	{
		//if the value is a number
		if (!isNaN(elementValue))
		{
			//make sure the error message is hidden
			document.getElementById(errElementName).style.display = "none";
			
			valid = true;
		}
		else
		{
			whenInvalid(elementName);
		}
	}
	else
	{
		//if the text box does not have the focus and it is empty
		if (fieldWithFocus != elementName)
		{
			whenInvalid(elementName);
		}
	}
	
	return valid;
}

//what happens when a field is invalid?
function whenInvalid(elementName)
{
	var errElementName = elementName + "_err";
	
	//show the error message
	document.getElementById(errElementName).style.display = "block";
		
	document.getElementById(elementName).focus();
}

//trim leading spaces
String.prototype.trim = function() 
{
	return this.replace(/^\s+|\s+$/g,"");
}

//function for rounding to the nearest decimal place, 1, 10, 100, 1000, etc...
function roundNumToNearestPlace(num,place)
{
	num *= place;
	num = Math.round(num);
	num /= place;
	
	return num;
}
