function isInvalidChars(myString, StringType) 
{
	if (typeof myString != "string") { return myString; }
	var boolIsBad = false
	var intPeriod = 0
	//Remove leading and trailing spaces.
	myString = trim(myString);
	for (var i = 0; i < myString.length; i++)
    {
	    A_char = myString.charCodeAt(i)
	    //We are validating for numbers.
	    if (StringType == "numbers")
		{
			if (A_char > 47 && A_char < 58)
			{
				// It's a number
				continue;
			}
			else if(A_char == 46)
			{
				// It's a period, 
				//but you can only have one
				intPeriod++;
				if (intPeriod > 1)
				{
					boolIsBad = true;
					break;
				}
			}
			else
			{     
				boolIsBad = true;
				break;
			}
		}
		else if(StringType == "letters")
		{
			//We are validating for letters.
			if (A_char > 64 && A_char < 91)
			{
				// Upper case letter
				continue;
			}     
			else
			{
				if (A_char > 96 && A_char < 123)
				{
					// Lower case letter
					continue;
				}
				else
				{
					if (A_char == 32)
					{
						//If it's ok to have spaces.
				        continue;
					}
					else
					{
						boolIsBad = true;
						break;
					}
				}
			}
		}
	}
	return boolIsBad;
}

function removeInvalidChars(formElement, myString, StringType) 
{
	var intPeriod = 0
	if (typeof myString != "string") { return myString; }
	//Remove leading and trailing spaces.
	myString = trim(myString);
	for (var i = 0; i < myString.length; i++)
    {
	    A_char = myString.charCodeAt(i)
	    //We are validating for numbers.
	    if (StringType == "numbers")
		{
			if (A_char > 47 && A_char < 58)
			{
				// It's a number
				continue;
			}
			else if(A_char == 46)
			{
				// It's a period, 
				//but you can only have one
				intPeriod++;
				if (intPeriod > 1)
				{
					myString = myString.substring(0,i)
				}
			}
			else
			{
				myString = myString.substring(0,i)
			}
		}
		else if(StringType == "letters")
		{
			//We are validating for letters.
			if (A_char > 64 && A_char < 91)
			{
				// Upper case letter
				continue;
			}     
			else
			{
				if (A_char > 96 && A_char < 123)
				{
					// Lower case letter
					continue;
				}
				else
				{
					myString = myString.substring(0,i)
				}
			}
		}
	}
	formElement.value = myString;
}

function trim(myString)
{
	//Thank you http://www.breakingpar.com/
   	// Removes leading and trailing spaces from the passed string. Also removes
   	// consecutive spaces and replaces it with one space. If something besides
   	// a string is passed in (null, custom object, etc.) then return the input.
   	if (typeof myString != "string")
	{
		return myString;
	}
   	var retValue = myString;
   	var ch = retValue.substring(0, 1);
   	while (ch == " ")
	{
		// Check for spaces at the beginning of the string
	      	retValue = retValue.substring(1, retValue.length);
      		ch = retValue.substring(0, 1);
   	}
   	ch = retValue.substring(retValue.length-1, retValue.length);
   	while (ch == " ")
	{
		// Check for spaces at the end of the string
      		retValue = retValue.substring(0, retValue.length-1);
      		ch = retValue.substring(retValue.length-1, retValue.length);
   	}
   	while (retValue.indexOf("  ") != -1)
	{
		// Note that there are two spaces in the string - look for multiple spaces within the string
      		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
		// Again, there are two spaces in each of the strings
   	}
   	return retValue; // Return the trimmed string back to the user 
}

function FormatNumber()
{
	//You can pass these in as parameters	
	var num = document.myForm.txtComma.value
	var decimalNum = 2 //How far to go after the decimal point
	
	//! My addition to the function	
	var bolEnforceDecimalNum = true //Above we said how far to
	//Carry out the decimal place to. Set this variable to enforce 
	//that the number goes that many decimal places.
	
	var bolLeadingZero = true //Remove the leading zero
	var bolParens = false //Show parantheses for negative numbers
	var bolCommas = true //Show commas
    if (isNaN(parseInt(num))) return "NaN";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number

	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign; // Readjust for sign


	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);

	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," +
						tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";
		
	//I added this code to make sure we carry out
	//the number of decimal places if bolEnforceDecimalNum = true
	if (bolEnforceDecimalNum)
	{
		var intLocPeriod = tmpNumStr.indexOf('.');
		var intLenNumStr = tmpNumStr.length;
		if(intLocPeriod < 1)
		{
			tmpNumStr += '.';
			for (i=0;i < decimalNum;i++)
			{
			tmpNumStr += '0';
			}
		}
		else
		{
			var intAfterDecimal = tmpNumStr.substring(intLocPeriod,intLenNumStr).length - 1;
			if(intAfterDecimal < decimalNum)
			{
				for(i=intAfterDecimal;i < decimalNum;i++)
				{
					tmpNumStr += '0';
				}
			}
		}
	}

	//return tmpNumStr;// Return our formatted string!
	document.myForm.txtComma.value = tmpNumStr;
}
