var digitsOnly="1234567890";

function formatText(obj){
	var sText = obj.value;
	
	sText=c_TrimText(sText);
	obj.value = sText.toUpperCase();
}
function c_TrimText(sText){
//remove leading & trailing spaces and inner multiple spaces
	var i;
	var sTmp="";
	var bIsLeading=true;
	var bIsKeep;
	var bIsLastWasSpace=false;
	for(i=0;i<sText.length;i++){
		bIsKeep=true;
		if(sText.charAt(i)==" "){
			if(bIsLastWasSpace)
				bIsKeep=false;
			if(bIsLeading==true)
				bIsKeep=false;
			if(bIsKeep)
				bIsLastWasSpace=true;
		}
		else{
			bIsLastWasSpace=false;
			bIsLeading=false;
		}
		if(bIsKeep)
			sTmp=sTmp+sText.charAt(i);
	}
	if(sTmp.charAt(sTmp.length-1)==" ")
		sTmp=sTmp.substr(0,sTmp.length-1);
	return(sTmp);

}

function c_IsValidText(sValid,sText){
//true=contains only characters found in given valid string
	var i;
	for(i=0;i<sText.length;i++)
		if(sValid.indexOf(sText.charAt(i))==-1)
			return(false);
	return(true);
}

function c_TrimValidText(sValid,sText){
//convert string to contain only characters found in given valid string
	var i;
	var sTmp="";
	//alert("c_TrimValidText"+sText+":" +sValid);
	for(i=0;i<sText.length;i++)
		if(c_IsValidText(sValid,sText.charAt(i)))
			sTmp=sTmp+sText.charAt(i);
	return(sTmp);	
}

function c_TrimText(sText){
//remove leading & trailing spaces and inner multiple spaces
	var i;
	var sTmp="";
	var bIsLeading=true;
	var bIsKeep;
	var bIsLastWasSpace=false;
	for(i=0;i<sText.length;i++){
		bIsKeep=true;
		if(sText.charAt(i)==" "){
			if(bIsLastWasSpace)
				bIsKeep=false;
			if(bIsLeading==true)
				bIsKeep=false;
			if(bIsKeep)
				bIsLastWasSpace=true;
		}
		else{
			bIsLastWasSpace=false;
			bIsLeading=false;
		}
		if(bIsKeep)
			sTmp=sTmp+sText.charAt(i);
	}
	if(sTmp.charAt(sTmp.length-1)==" ")
		sTmp=sTmp.substr(0,sTmp.length-1);
	return(sTmp);

}

function isAllNumeric(stringValue) {
	var stringLen	= stringValue.length;
	var retValue	= true;
	var digits		= "0123456789";
	var i			= 0;

	for (i=0; i<stringLen; i++) {
		if (digits.indexOf(stringValue.substr(i, 1)) == -1) {
			retValue = false;
			i = stringLen;
		}
	}
	
	return retValue;
}

function formatPhone(obj) {
	var oldString = obj.value;
	var oldLength;
	oldString = c_TrimValidText(digitsOnly,oldString);
	oldString = c_TrimText(oldString);
	oldLength = oldString.length;
	/*if(oldLength <10){
		alert("Home Phone invalid. Please correct.");
	}*/
	if ((oldLength==10) && (isAllNumeric(oldString))) {
		oldString = oldString.substr(0, 3) + "-" + oldString.substr(3, 3) + "-" + oldString.substr(6, 4);
	}
	obj.value = oldString;
}

/*******************************************************************/
/*************  Added for Year Formatting **************************/ /*******************************************************************/
function formatYear(obj){
	var oldString = obj.value;
	var oldLength = oldString.length;
	oldString = c_TrimValidText(digitsOnly,oldString);
	oldString = c_TrimText(oldString);
	oldLength = oldString.length;
	//format 4 digit year (converts 2 digit year to 4 digit if necessary)
	var today;
	var thisYear;
	var thatYear;

	//convert 2-digit year to 4-digit year
	//it uses 19xx if 2 digit year is > thisYear 2 digit year + 10
	thatYear=new String(oldString);
	
	if(thatYear.length>0 && thatYear.length<3){
		today=new Date();
		thisYear=today.getFullYear();
		thisYear=thisYear-1990;
		if(thatYear.length==1)
			thatYear="0"+thatYear;
		if(thatYear>thisYear)
			thatYear="19"+thatYear;
		else
			thatYear="20"+thatYear;
	}
	obj.value=thatYear;
}

function formatZipcode(obj) {
	var oldString = obj.value;
	var oldLength;
	oldString = c_TrimValidText(digitsOnly,oldString);
	oldString = c_TrimText(oldString);
	oldLength = oldString.length;
	/*if(oldLength <5){
		alert("Zip Code too short. Minimum 5. Please correct.");
	}*/
	if ((oldLength==9) && (isAllNumeric(oldString))) {
		oldString = oldString.substr(0, 5) + "-" + oldString.substr(5, 9);
	}
	obj.value = oldString;
}
function formatField(obj)
			{
				var oldValue = obj.value;
				var newValue = "";
				var oldValueLength = oldValue.length;

				//alert("oldValue: " + oldValue.indexOf("."));
				//alert("oldValueLength: " + oldValueLength);

				oldValue = c_TrimText(oldValue);


				//Remove all "," if the user has entered any.
				while(oldValue.indexOf(",") != -1)
				{
					oldValue = oldValue.substr(0,oldValue.indexOf(",")) + oldValue.substr(oldValue.indexOf(",")+1 ,oldValue.length);
				}

				//Check if there is a "." in the string.
				if(oldValue.indexOf(".") >= 0)
				{
					//Check if user has put in a value after the ".". E.g. "123."
					//check to see is "." is the last character in the string
					if(oldValue.indexOf(".") == oldValueLength - 1 )
					{
						newValue = oldValue + "00";
					}
					else
					//check if user has entered one one number after the "." E.g. "123.4"
					//Pad a "0" to the string
					if(oldValue.indexOf(".") + 2 == oldValueLength)
					{
						newValue = oldValue + "0";
					}
					else
					//If user has entered more than 2 decimal places, then trim the
					//decimal places e.g.123.555568 is trimmed to "123.55"
					if(oldValue.indexOf(".") + 2 < oldValueLength)
					{
						newValue = oldValue.substr(0,oldValue.indexOf(".") + 3);
					}

					//If the length of string is between 7 and 9
					//Format should be as "xxx,xxx,xxx"
					if(newValue.indexOf(".") >=7 && newValue.indexOf(".") <= 9)
					{
						newValue = format2Commas(newValue.substr(0,newValue.indexOf(".")) , newValue.indexOf(".")) + newValue.substr(newValue.indexOf("."),3);
					}
					//If the length of string is between 4 and 6
					//Format should be as "xxx,xxx,xxx"
					else
					if(newValue.indexOf(".") >=4 && newValue.indexOf(".") <= 6)
					{
						newValue = format1Comma(newValue.substr(0,newValue.indexOf(".")) , newValue.indexOf(".")) + newValue.substr(newValue.indexOf("."),3);
					}
				}
				//If no decimal point is found then
				else
				{
					//If the length of string is between 7 and 9
					//Format should be as "xxx,xxx,xxx"
					if(oldValueLength >=7 && oldValueLength <= 9)
					{
						newValue = format2Commas(oldValue,oldValueLength) + ".00";
					}
					//If the length of string is between 4 and 6
					//Format should be as "xxx,xxx,xxx"
					else
					if(oldValueLength >=4 && oldValueLength <= 6)
					{
						newValue = format1Comma(oldValue,oldValueLength) + ".00";
					}
					//If length is between 1 and 3, just pad the ".00"
					else
					if(oldValueLength >=1 && oldValueLength <= 3)
					{
						newValue = oldValue + ".00";
					}
					else
					//If the user enters a value greater then 9
					if(oldValueLength > 9)
					{
						newValue = oldValue.substr(0,9);
						newValue = format2Commas(newValue,newValue.length) + ".00";
					}
				}

				obj.value = newValue;

			}

			function format2Commas(oldValue,oldValueLength)
			{
				var secondComma = oldValueLength - 3;
				var firstComma = secondComma - 3;
				var newValue = oldValue.substr(0,firstComma) +"," + oldValue.substr(firstComma, 3) + "," + oldValue.substr(secondComma,3);
				return newValue;
			}

			function format1Comma(oldValue,oldValueLength)
			{
				var firstComma = oldValueLength - 3;
				var newValue = oldValue.substr(0,firstComma) +"," + oldValue.substr(firstComma,3);
				return newValue;
			}
	
