/* this function accepts field name and string. 
it strips the typical phone number characters and tests for a valid number. 
if the number is valid, function attempts formatting American style if the number is 10 digits (i.e. area code + number) 

-- this function is included by /Orders/dsp_JavaScript.cfm for new orders 
	and by /profiles.cfm for the address profiles pop-up
*/
/*
	returnString loop borrowed from: 
	<!-- Original:  Ryan A. Somma (ryan@waygate.com) -->
	<!-- Web Site:  http://www.waygate.com -->
*/
function phoneFormat(field, phone) {
	//copy the string for manipulating
	var theField = field;
	var s = phone;
	var filteredValues = " -()ext.";     // Characters stripped out
	var returnString = "";
	for (i = 0; i < s.length; i++) {  // Search through string and append unfiltered values to returnString.
		var c = s.charAt(i);
		if (filteredValues.indexOf(c) == -1) returnString += c;
	}
	p = returnString;
	/*
	//first strip ( and ) and -
	p=p.replace("(","");
	p=p.replace(")","");
	// temp dupe lines below until I remember the regEx for "all" - ted 8/14/02
	//p=p.replace("([\-]*)" ,"");
	p=p.replace("-","");
	p=p.replace("-","");
	p=p.replace(" ","");
	p=p.replace(" ","");
	//alert(p);
	*/
	//then test that this is a number
	if (isNaN(p)==true){
		alert("Phone number is not valid. Please enter the number only with no special characters.");
		return false;
	} else if (p.length == 10) {
		//now, it is a number, if it is 10 digits format it American style, otherwise leave it alone [int'l]
		p1 = p.slice(0,3);
		p2 = p.slice(3,6);
		p3 = p.slice(6);
		pFormatted = "(" + p1 + ")" + " " + p2 + "-" + p3;
		setField = eval("theForm." + theField);
		setField.value = pFormatted;
		//alert(pFormatted);
	} //end if 
}