// version 1.0 -- Three Text Boxes

function isPhoneDone(inObj, nextObj) {
	var CEFColor="#FFCC66";
	var inMaxLength=3;
	inObj.style.backgroundColor="#FFFFFF";
	if ((window.event.keyCode != 9) && (window.event.keyCode != 16))  {		// If Tab or Shift-Tab wasn't pressed
		if (inObj.value.length >= inMaxLength) {	// If the textbox is at the most it can handle, then ...
			nextObj.focus();		// Focus to the next textbox
		}
	} else if (inObj.value != "") {
		if (inObj.value.length != inMaxLength) {
			inObj.style.backgroundColor=CEFColor;
			inObj.focus();
		}
	}
}

// version 2.0 -- Single Text Box

var phoneNumberCheckFailed=false;
	
function phoneNumberCheckBlur(inPhNumber) {
	if (phoneNumberCheckFailed) {
		inPhNumber.focus();
	}
	phoneNumberCheckFailed=false;
}
	
	
	
function phoneNumberCheck(inPhNumber,inPrefixFlag,inAreaCode,inAllowExt, inFormat) {
	var CEFColor="#FFCC66";
	var x=0;
	inPhNumber.style.backgroundColor="#FFFFFF";
	if ((!inAllowExt) && (inAllowExt != 1)) {
		var inAllowExt=0;
	}
	if ((!inPrefixFlag) && (inPrefixFlag != 0)) { 
		var inPrefixFlag=1;
	}
	if ((!inAreaCode) && (inAreaCode != 0)) { 
		var inAreaCode=1;
	}
	var useFormat=false;
	if (inFormat && (inFormat.toString().search(/^format:/i) != -1)) {
		useFormat=true;
		inFormat=inFormat.toString().replace(/^format:\s*(.+)$/i,"$1");
	}
	var inString=inPhNumber.value;
	var inStringCpy=inString;		// A copy of inString (used for Mexico Phone numbers)
	var phoneStr=new String("");
	if (inString.length == 0) { 
		return false; 
	}
	if (inString.search(/^8\-/)  != -1) {
		inAreaCode=0;
		inPrefix=0;
		phoneStr+="8-";
	}
	inString=inString.replace(/^8\-/,"");		// Remove leading "8-" if there
	inString=inString.replace(/[^0-9]/g,"");    // Remove all non-digits
	inString=inString.replace(/^1/,"");			// Remove leading "1" if there
	if (inString.substring(0,3) == "011" ) {	// Is it a Mexician Phone Number?
		var cbmex=inStringCpy.replace(/^.*011-?(.*)$/,"$1");
		if (inPrefixFlag == 1) {
			phoneStr="1-";
		}
		phoneStr+=inString.substring(0,3) + "-" + cbmex;
	} else {									// Not a Mexician Phone Number
		if ((inString.length == 7) && (inAreaCode == 1)) {
			alert("The number " + inPhNumber.value + " does not specify an area code.\nPlease include an area code with the phone number.")
			inPhNumber.style.backgroundColor=CEFColor;
			phoneNumberCheckFailed=true;
			return false;
		} else if ((inString.length < 10) && (inAreaCode == 1)) {
			alert("The number " + inPhNumber.value + " contains only " + inString.length + " digits.\nPlease specify the full 10-digit phone number (areacode-number).")
			inPhNumber.style.backgroundColor=CEFColor;
			phoneNumberCheckFailed=true;
			return false;
		} else if ((inString.length < 7) && (inAreaCode == 0)) {
			alert("The number " + inPhNumber.value + " contains only " + inString.length + " digits.\nPlease specify the 7-digit phone number.")
			inPhNumber.style.backgroundColor=CEFColor;
			phoneNumberCheckFailed=true;
			return false;
		} else if ((inString.length > 10) && (inAllowExt == 0) && (inAreaCode == 1)) { 
			alert("The number " + inPhNumber.value + " contains too many digits (" + inString.length + ").\nPlease specify the 10-digit phone number.")
			inPhNumber.style.backgroundColor=CEFColor;
			phoneNumberCheckFailed=true;
			return false;
		} else if ((inString.length > 7) && (inAllowExt == 0) && (inAreaCode == 0)) { 
			alert("The number " + inPhNumber.value + " contains too many digits (" + inString.length + ").\nPlease specify the 7-digit phone number.")
			inPhNumber.style.backgroundColor=CEFColor;
			phoneNumberCheckFailed=true;
			return false;
		}
		if (useFormat) {
			var i=0;
			for (i=0; i < inString.length; i++) {
				inFormat=inFormat.replace(/#/, inString.charAt(i));
			}
			inPhNumber.value=inFormat;
			return true;
		}
		if ((inPrefixFlag == 1) && (inAreaCode != 0)) {
			phoneStr="1-";
		}
		if (inAreaCode != 0) {
			phoneStr+=inString.substring(0,3) + "-";
		} else if ((inAreaCode == 0) && (inPhNumber.value.substring(0,1) == "1")) {
			inString=inString.substring(3);
		}
		if (inString.length == 7) {
			phoneStr+=inString.substring(0,3) + "-" + inString.substring(3,7);
		} else if ((inString.length > 7) && (inAreaCode == 0) && (inAllowExt == 1)) {
			phoneStr+=inString.substring(0,3) + "-" + inString.substring(3,7) + " Ext.#" + inString.substring(7);
		} else {
			phoneStr+=inString.substring(3,6) + "-" + inString.substring(6,10);
		}
		if ((inString.length > 10) && (inAllowExt == 1) && (inAreaCode == 1)) {
			phoneStr+= " Ext.#" + inString.substring(10);
		}
	}		
	inString=phoneStr;
	inPhNumber.value=inString;
}

