//Validation
var newline = '<br>';

//Check for Integer
function ValidateNumber(txt,
						txtError,
						sFieldName)
{
	var validateBool;
	
	if (isNaN(removeCommas(txt.value)) && txt.value != "")
	{
		validateBool = false;
	}
	else
	{
		validateBool = true;
	}
	
	if (!validateBool)
	{
		txtError.innerHTML += "* " + sFieldName + " must be a number." + newline;
	}
	
	return validateBool;
}

function ValidateGreaterThan(txt,
							 txtError,
							 sFieldName,
							 limit)
{
	var validateBool;
	
	if (isNaN(removeCommas(txt.value)))
	{
		//Already rendered false - this error message will not appear
		validateBool = true;
	}
	else
	{
		var tempInt;
		tempInt = parseFloat(removeCommas(txt.value))
		if(tempInt > limit)
		{
			validateBool = true;
		}
		else
		{
			validateBool = false;
		}
		
	}
	
	if (!validateBool)
	{
		txtError.innerHTML += "* " + sFieldName + " must be greater than " 
								  + addCommas(limit) + "." + newline;
	}
	
	return validateBool;
}

function ValidateLessThan(txt, 
						  txtError,
						  sFieldName,
						  limit)
{
	var validateBool;
	
	if (isNaN(removeCommas(txt.value)))
	{
		//Already rendered false - this error message will not appear
		validateBool = true;
	}
	else
	{
		var tempInt;
		tempInt = parseFloat(txt.value)
		if(tempInt < limit)
		{
			validateBool = true;
		}
		else
		{
			validateBool = false;
		}
		
	}
	
	if (!validateBool)
	{
		txtError.innerHTML += "* " + sFieldName + " must be less than " 
								  + addCommas(limit) + "." + newline;
	}
	
	return validateBool;
}

function ValidatePositive(txt,
						  txtError,
						  sFieldName)
{
	var validateBool;
	
	if (isNaN(removeCommas(txt.value)))
	{
		//Already rendered false - this error message will not appear
		validateBool = true;
	}
	else
	{
		var tempInt;
		tempInt = parseFloat(removeCommas(txt.value))
		if(tempInt >= 0)
		{
			validateBool = true;
		}
		else
		{
			validateBool = false;
		}
		
	}
	
	if (!validateBool)
	{
		txtError.innerHTML += "* " + sFieldName + " cannot be negative." + newline;
	}
	
	return validateBool;
}

function ValidateRequired(txt,
						  txtError,
						  sFieldName)
{
	var validateBool;
		
	if (txt.value == "")
	{
		validateBool = false;
	}
	else
	{
		validateBool = true;
	}
	
	if (!validateBool)
	{
		txtError.innerHTML += "* Must enter a value for " + sFieldName + "." + newline;
	}
	
	return validateBool;
}

function ValidateRange(txt,
					   txtError,
					   sFieldName,
					   lowerLimit, 
					   upperLimit)
{
	var validateBool = true;
	
	if (isNaN(removeCommas(txt.value)))
	{
		//Already rendered false - this error message will not appear
	}
	else
	{
		var tempInt;
		tempInt = parseFloat(removeCommas(txt.value))
		
		if(isNaN(upperLimit))
		{
			//	Only a lower limit exists
			if(tempInt < lowerLimit)
				validateBool = false;
		}
		else if(isNaN(lowerLimit))
		{
			//	Only a lower limit exists
			if(tempInt > upperLimit)
				validateBool = false;
		}
		else if (isNaN(upperLimit) && isNaN(lowerLimit))
			validateBool = false;
		else if(tempInt < lowerLimit || tempInt > upperLimit)
			validateBool = false;
	}
	
	if (!validateBool)
	{
		if (isNaN(upperLimit) && isNaN(lowerLimit))
			txtError.innerHTML += "* ERROR!" + newline;
		else if(isNaN(upperLimit))
			txtError.innerHTML += "* " + sFieldName + " cannot be less than " + addCommas(lowerLimit) + "." + newline;
		else if(isNaN(lowerLimit))
			txtError.innerHTML += "* " + sFieldName + " cannot be greater than " + addCommas(upperLimit) + "." + newline;
		else
			txtError.innerHTML += "* " + sFieldName + " must be a value from " + addCommas(lowerLimit) + " to " + addCommas(upperLimit) + "." + newline;
	}
	
	return validateBool;
}

function ValidateCompareFieldsLessThan(txt,
									   txtCompare,
									   txtError,
									   sFieldName,
									   sCompareFieldName)
{
	var validateBool;
	
	if (isNaN(removeCommas(txt.value)))
	{
		//Already rendered false - this error message will not appear
		validateBool = true;
	}
	else
	{
		var tempInt1, tempInt2;
		tempInt1 = parseFloat(removeCommas(txt.value))
		tempInt2 = parseFloat(removeCommas(txtCompare.value))
		if(tempInt1 < tempInt2)
		{
			validateBool = true;
		}
		else
		{
			validateBool = false;
		}
		
	}
	
	if (!validateBool)
	{
		txtError.innerHTML += "* " + sFieldName + " must be less than " 
								  + sCompareFieldName + "." + newline;
	}
	
	return validateBool;
}

function ValidateCompareFieldsLessThanEqual(txt,
											txtCompare,
											txtError,
											sFieldName,
											sCompareFieldName)
{
	var validateBool;
	
	if (isNaN(removeCommas(txt.value)))
	{
		//Already rendered false - this error message will not appear
		validateBool = true;
	}
	else
	{
		var tempInt1, tempInt2;
		tempInt1 = parseFloat(removeCommas(txt.value))
		tempInt2 = parseFloat(removeCommas(txtCompare.value))
		if(tempInt1 <= tempInt2)
		{
			validateBool = true;
		}
		else
		{
			validateBool = false;
		}
		
	}
	
	if (!validateBool)
	{
		txtError.innerHTML += "* " + sFieldName + " must be less than or equal to " 
								  + sCompareFieldName + "." + newline;
	}
	
	return validateBool;
}

/*
function setAsterisk(inputBool, textbox)
{
	if (inputBool)
	{
		txt.value = ""
	}
	else
	{
		txt.value = "*"
	}
}
*/

function removeCommas(input)
{
	var tempNumber = input;
	var inputLength = input.length;
	var commaPosition;
	var decimalPosition = input.indexOf(".");
	var tempString1, tempString2;
	var numberLength;
	
	if (decimalPosition > -1)
	{
		tempNumber = input.substring(0, decimalPosition);
	}
	numberLength = tempNumber.toString().length;
	
	commaPosition = tempNumber.indexOf(",");

	while (commaPosition > -1)
	{
		if (commaPosition <= numberLength - 4)
		{
			tempString1 = tempNumber.substring(0, commaPosition);
			tempString2 = tempNumber.substring(commaPosition + 1, numberLength);
			tempNumber = tempString1 + tempString2;
			numberLength = tempNumber.toString().length;
			commaPosition += 3;
		}
		else
		{
			commaPosition = -1
		}

		if (tempNumber.charAt(commaPosition) != ",")
		{
			commaPosition = -1
		}
	}

	if (decimalPosition < inputLength && decimalPosition >= 0)
	{
		tempNumber = tempNumber + input.substring(decimalPosition, input.toString().length);
	}
	
	return tempNumber;
}

function addCommas(input)
{
	var tempNumber = "" + input;
	var tempInput;
		
	var commaTracker;
	var tempString1, tempString2;
	var numberLength;
	var negativeBool;


	var decimalPosition = input.toString().indexOf(".");
	var inputLength = input.toString().length;

	
	
	if (decimalPosition > -1)
	{
		tempNumber = input.toString().substring(0, decimalPosition);
	}
	
	numberLength = tempNumber.length;
	
	if (numberLength > 3)
	{
		commaTracker = numberLength - 3;
	}
		
	while (commaTracker > 0)
	{
		tempString1 = tempNumber.substring(0, commaTracker);
		tempString2 = tempNumber.substring(commaTracker, numberLength);
		tempNumber = tempString1 + "," + tempString2;
		numberLength = tempNumber.toString().length;
		commaTracker -= 3;
	}

	if (decimalPosition < inputLength && decimalPosition >= 0)
	{
		tempNumber = tempNumber + input.toString().substring(decimalPosition, inputLength);
	}

	return tempNumber;
}

function roundToDecimal(input, place)
{
	if (!isNaN(input) && input.toString().indexOf(".") > -1 && input > 0)
	{
		var tempString, tempInt, tempDouble, tempStringArray;
		tempStringArray = input.toString().split(".");

		if (tempStringArray[1].length < place)
		{
			var i;
			for (i = tempStringArray[1].length; i < place; i++)
			{
				tempStringArray[1] = tempStringArray[1] + 0;
			}
			tempInt = parseInt(tempStringArray[1]);
		}		
		else if (tempStringArray[1].length == place)
		{
			tempInt = parseInt(tempStringArray[1]);
		}
		else if (parseInt(tempStringArray[1].charAt(place)) < 5)
		{
			tempInt = parseInt(tempStringArray[1].toString().substr(0, place))
		}
		else
		{
			tempInt = parseInt(tempStringArray[1].toString().substr(0, place)) + 1;
			if (tempInt == 10)
			{
				tempInt = 0;
				tempStringArray[0] = parseInt(tempStringArray[0]) + 1;
			}
 
		}
		

		tempString = tempStringArray[0] + "." + tempInt;
		
		tempDouble = parseFloat(tempString);
		return tempString;

	}
	else
	{
		return input;
	}
}