/*
	Used for formatting Phone Numbers to (123) 123-1234 
	Designed by Alex Hoover (BCS) Copyright 2004 by KDOT.
	Updated by Rob Reynolds (BCS).
	
	TO USE: Include below in your textbox or in a span element surrounding your asp:TextBox.	
	onkeyup="formatPhone('fieldID');"
	
	If you experience problems with this script try adding this BELOW your call to it.
	<script>
		lastPhoneLength=0;
		formatPhone('fieldID');
	</script>
	
*/

var lastPhoneLength;

function formatPhone(field) {
	strPhoneField = document.getElementById(field).value;
	strPhone = "";

	if (strPhoneField.length > lastPhoneLength) {
		for (x = 0; x < strPhoneField.length; x++) {
			strPhoneInt = strPhoneField.charCodeAt(x);
			if ((strPhoneInt > 47) && (strPhoneInt < 58)) {
				strPhone += strPhoneField.substr(x, 1);
			}
		}

		if (strPhone.substring(0, 1) != "(") {
			strPhone = "(" + strPhone;
		}

		if ((strPhone.length > 4) && (strPhone.substr(4, 2) != ") ")) {
			strPhone = strPhone.substr(0, 4) + ") " + strPhone.substr(4, (strPhone.length - 4));
		}

		if ((strPhone.length > 9) && (strPhone.substr(9, 1) != "-")) {
			strPhone = strPhone.substr(0, 9) + "-" + strPhone.substr(9, (strPhone.length - 9));
		}
	} else {
		strPhone = strPhoneField;
	}

	if (strPhone.length > 14) {
	strPhone = strPhone.substr(0, 14);
	}
	document.getElementById(field).value = strPhone;
	lastPhoneLength = strPhone.length;
}