/*
################################################################################
#
# Program	: phone.js
#
# Description	: provide client-side telephone entry formatting and
#		: validation
#
# Author	: Krys Wallbank
# 		: Groovy Web Services
#		: krys@groovy-web.com
#
# Version	: 1.0.1 - 29Oct2003
#		:
#		: Copyright (c) 2003, All Rights Reserved
#
################################################################################

################################################################################
#
# Example usage:
# <html>
# <body>
# <script language="javascript" src="/scripts/phone.js"></script>
# <form name='phone'>
# (<input type="text" name="phone_area_code" size="5" maxlength="3" onKeyUp="checkFieldContent(document.phone.phone_area_code);" onBlur="checkFieldLength(document.phone.phone_area_code, 3)">) -
# <input type="text" name="phone_prefix" size="5" maxlength="3" onKeyUp="checkFieldContent(document.phone.phone_prefix);" onBlur="checkFieldLength(document.phone.phone_prefix, 3)"> &nbsp;
# <input type="text" name="phone_body" size="7" maxlength="4" onKeyUp="checkFieldContent(document.phone.phone_body);" onBlur="checkFieldLength(document.phone.phone_body, 4)">
# </form>
# </body>
# </html>
#
# TO BE TIED IN WITH SERVER-SIDE VALIADATION
#
################################################################################

################################################################################
# MAIN PROGRAM
################################################################################
*/

function checkFieldLength(textObj, numCharsReq) {

	if (textObj.value.length > numCharsReq) {

		textObj.value = textObj.value.substr(0, numCharsReq );

	}

	if ( (textObj.value.length < numCharsReq)  && (textObj.value.length > 0) ) {

		alert("Please enter " + numCharsReq + " digits");

		textObj.focus();

		return false;

	}



}

function checkFieldContent(textObj) {

	for (var i = 0; i < textObj.value.length; i++) {

		if ( (textObj.value.charAt(i) > '9') || (textObj.value.charAt(i) < '0') ) {

			if (textObj.value.length <= 1) {

				textObj.value = '';

			} if (i == 0) {

				textObj.value = textObj.value.substring(1, textObj.value.length);

			} else {

				textObj.value = textObj.value.substr(0, i) + textObj.value.substr( i + 1, textObj.value.length - i);

			}

		}
	}

}

