/************************************************************
* Function: phoneMask()													*
* Input: a text field													*
* Return: nothing															*
* Purpose: incorporates an input mask for a phone number.	*
*			  Needs to be called on the onKeyUp event of			*
*			  the text field.												*
* Example : <input type="text" onkeyup="phoneMask(this);">  *
************************************************************/

intPrevLength = 0;
function phoneMask(txtPhone)
{
	var strAreaCode = ""; //variable to hold the area code
	var strPrefix = ""; //variable to hold the prefix
	var strEnd = ""; //variable to hold the ending numbers
	var chHolder = ""; //variable to hold characters

	//is the length of the value the previous one?
	//alert(intPrevLength + ', ' + txtPhone.value.length)
	if(txtPhone.value.length > intPrevLength) //Yes
	{
		for (i=0; i < txtPhone.value.length; i++)
		{
			//is this character a number?
			if(!isNaN(txtPhone.value.charAt(i)) && txtPhone.value.charAt(i) != ' ') //Yes
			{
				//which part of the phone number is this number?
				//alert('AreaCode Length = ' + strAreaCode.length)
				if(strAreaCode.length < 3) //Area Code
					strAreaCode += txtPhone.value.charAt(i);
				else if(strPrefix.length < 3) //Prefix
					strPrefix += txtPhone.value.charAt(i);
				else //Last four digits
					strEnd += txtPhone.value.charAt(i);
			}
			else
			{
				chHolder = txtPhone.value.charAt(i);
			}
		}

		if(strAreaCode.length < 3)
			txtPhone.value = "(" + strAreaCode;
		else if(strPrefix.length < 3)
			txtPhone.value = "(" + strAreaCode + ") " + strPrefix;
		else
			txtPhone.value = "(" + strAreaCode + ") " + strPrefix + "-" + strEnd;

		intPrevLength += 1
	}
	else //No, the length is less than the previous one
		intPrevLength = txtPhone.value.length - 1
}
