function doFormatPhone(phoneobj)
{
	// set form field where phone format should take place
	var gotphone = document.getElementById(phoneobj);
	var store = window;

	gotphone.onblur = function()
	{
		formatPhone(this);
	}
}

/* Format phone number function */
function formatPhone(curPhone)
{
	curPhone.value = formatPhoneStr(curPhone.value);
}

/* Returns a formatted phone number */
function formatPhoneStr(phoneNumber) {

	var tempPhone = phoneNumber.replace(/[^0-9xX]/g,"");

	tempPhone = tempPhone.replace(/[xX]/g,"x");

	var extension = "";
	
	if(tempPhone.length != 10) {

				//Alert the user that the phone number entered was invalid.
			
				alert('Please enter a valid, 10-digit phone number (including area code).');	
				tempPhone = "";		
				return tempPhone;						
	 } else {
 
		if(tempPhone.indexOf("x") > -1)
		{
			extension = " "+tempPhone.substr(tempPhone.indexOf("x"));

			tempPhone = tempPhone.substr(0,tempPhone.indexOf("x"));
		}

		switch(tempPhone.length)
		{
			case(10):
				return tempPhone.replace(/(...)(...)(....)/g,"($1) $2-$3")+extension;
			case(11):
				if(tempPhone.substr(0,1) == "1")
				{
					return tempPhone.substr(1).replace(/(...)(...)(....)/g,"($1) $2-$3")+extension;
				}
				break;
			default:
		}

		return phoneNumber;
	}
}