/*********************************************
If this variable (iPhoneShowErrorMessage) is 
set to true in a page, then it will show the
error message if the phone number appears to 
be incorrect.
**********************************************/
var iPhoneShowErrorMessage = false;

/*********************************************
Call this to strip out a string to numbers only
In: (206) 555-1212  Out: 2065551212
In: 206-555-1212    Out: 2065551212
In: 206-555-aaaa    Out: 206555
**********************************************/
function strip_phone(sInput)
{
	var sReturn = '';
	for (var i = 0; i < sInput.length; i++)
	{
		if (sInput.charAt(i) >= "0" && sInput.charAt(i) <= "9")
		{
			sReturn += sInput.charAt(i);
		}
	}
	
	return sReturn;
}


/*********************************************
Call this to format a string into
either 333-4444 format or (333) 333-4444
format.  It will call strip_phone first
to make sure the input string is only
numeric.

If the phone number is not either 7 or 10 
characters, it will return the string that
was passed to it exactly
In: 5551212    Out: 555-1212
In: 2065551212 Out: (206) 555-1212
In: 206555121  Out: 206555121 (not right format)
**********************************************/
function format_phone(sInput)
{
	var sReturn = '';
	var iErrorFlag = true;
	
	sInput = strip_phone(sInput);
	if (sInput.length != 10 && sInput.length != 7)
	{
		sReturn = sInput;
		if (iPhoneShowErrorMessage == true && sInput != '')
		{
			alert ('It appears that the phone number you have just entered is incorrect.')
		}
	}
	
	if (sInput.length == 7)
	{
		sReturn = sInput.substring(0,3) + '-' + sInput.substring(3,7);
	}
	if (sInput.length == 10)
	{
		sReturn = '(' + sInput.substring(0,3) + ') ' + sInput.substring(3,6) + '-' + sInput.substring(6,10)
	}
	return sReturn;
}
