function editPhone(evt) {
IE = document.all;

	if (IE) {
	var keycode = window.event.keyCode;
	var shift = window.event.shiftKey;
	var ctrl = window.event.ctrlKey;
	var alt = window.event.altKey;
	var pos = this.value.length + 1;
	var lparen = (shift  && !ctrl && !alt && keycode == 40);
	var rparen = (shift  && !ctrl && !alt && keycode == 41);
	var space = (!shift && !ctrl && !alt && keycode == 32);
	var dash = (!shift && !ctrl && !alt && keycode == 45);
	var slash = (!shift && !ctrl && !alt && keycode == 47);
	var digit = (!shift && !ctrl && !alt && keycode >= 48 && keycode <= 57);
	
		if (keycode == 13) checkForm();
		if (!lparen && !rparen && !space && !dash && !digit) return false;
		if (pos == 1  && lparen) return true;
		if (pos == 1  && digit) { this.value = '('; return true; }
		if (pos == 2  && digit) return true;
		if (pos == 3  && digit) return true;
		if (pos == 4  && digit) return true;
		if (pos == 5  && rparen) { this.value += ') '; return false; }
		if (pos == 5  && dash)   { this.value += ') '; return false; }
		if (pos == 5  && slash)  { this.value += ') '; return false; }
		if (pos == 5  && space)  { this.value += ') '; return false; }
		if (pos == 5  && digit)  { this.value += ') '; return true; }
		if (pos == 6  && space) return true;
		if (pos == 6  && digit) { this.value += ' '; return true; }
		if (pos == 7  && digit) return true;
		if (pos == 8  && digit) return true;
		if (pos == 9  && digit) return true;
		if (pos == 10 && dash) return true;
		if (pos == 10 && space) { this.value += '-'; return false; }
		if (pos == 10 && digit) { this.value += '-'; return true; }
		if (pos == 11 && digit) return true;
		if (pos == 12 && digit) return true;
		if (pos == 13 && digit) return true;
		if (pos == 14 && digit) return true;
	} else {
	keycode = evt.which;
	var bs = String.fromCharCode(evt.which);
		if (keycode >= 48 && keycode <= 57) return true;
		if (keycode == 0) return true;
		if ((keycode == 32) || (keycode >= 40 && keycode <= 41) || (keycode >= 45 && keycode <= 46)) return true;
		if (bs == "\b") return true;
		if (keycode == 13) checkForm(); return false;
	}
return false;
}

function isPhone() {
var phone;
var prefix;
var exchange;
var bad_no;

	if (!this.value) return(this.xerror());
phone = this.value.replace(/\D/g, "");
prefix = phone.slice(0,3);
exchange = phone.slice(3,6);
bad_no = phone.slice(3,4);

	if (prefix == 000) return(alert("Please enter a valid phone number."));this.focus();
	if (prefix == 555) return(alert("Please enter a valid phone number."));this.focus();
	if (prefix == 700) return(alert("Please enter a valid phone number."));this.focus();
	if (prefix == 800) return(alert("Please enter a valid phone number."));this.focus();
	if (prefix == 888) return(alert("Please enter a valid phone number."));this.focus();
	if (prefix == 900) return(alert("Please enter a valid phone number."));this.focus();
	if (exchange == 555) return(alert("Please enter a valid phone number."));this.focus();
	if (exchange == 000) return(alert("Please enter a valid phone number."));this.focus();
	if (exchange == 976) return(alert("Please enter a valid phone number."));this.focus();
	if (bad_no == 0) return(alert("Please enter a valid phone number."));this.focus();
	if (phone.length < this.xmin) return(this.xerror());
	if (phone.length > this.xeditmax) return(this.xerror());

this.value = phone.replace(/^(\d\d\d)(\d\d\d)(\d\d\d\d)$/, "($1) $2-$3");
return true;
}

function isPostal () {
code = this.value.replace(/\D/g, "");
	if (!code) return(this.xerror());
	if (code.length < this.xmin) return(this.xerror());
	if (code.length > this.xeditmax) return(this.xerror());
return true;
}

function showError(msgpat) {
alert(this.xerrmsg);
this.focus();
return false;
}

function setup_fields(form) {
	with (form) {
		if (typeof(dsl_phone) == "object") {
		dsl_phone.xmin = 10;
		dsl_phone.xeditmax = 10;
		dsl_phone.maxLength = 14;
		dsl_phone.xlabel = "DSL Phone";
		dsl_phone.onkeypress = editPhone;
		dsl_phone.xvalidate = isPhone;
		dsl_phone.xerror = showError;
		dsl_phone.xerrmsg = "The Phone Number you entered is incomplete.  Please enter your full Phone Number -- including area code.";
		}
		if (typeof(phone) == "object") {
		phone.xmin = 10;
		phone.xeditmax = 10;
		phone.maxLength = 14;
		phone.xlabel = "DSL Phone";
		phone.onkeypress = editPhone;
		phone.xvalidate = isPhone;
		phone.xerror = showError;
		phone.xerrmsg = "The Phone Number you entered is incomplete.  Please enter your full Phone Number -- including area code.";
		}
		if (typeof(zip) == "object") {
		zip.xmin = 5;
		zip.xeditmax = 5;
		zip.maxLength = 5;
		zip.xlabel = "Zip";
		zip.onkeypress = hitReturn;
		zip.xvalidate = isPostal;
		zip.xerror = showError;
		zip.xerrmsg = "Please make sure the Zip Code you entered is 5 digits long.";
		}
	}
return;
}

IE = document.all;
redo = false;
NextButtonUp = new Image();
NextButtonDown = new Image();
NextButtonUp.src = "/static/id55/1/button_continue.gif";
NextButtonDown.src = "/static/id55/1/button_processing.gif";

function hitReturn(evt) {
	if (IE) {
	keycode = window.event.keyCode;
	} else {
	keycode = evt.which;
	}
	if (keycode == 13) {
	checkForm(); return false;
	}
return;
}

function validate_form() {
var elm, i;
form = document.forms[0];
	for (i = 0; i < form.elements.length; i++) {
	elm = form.elements[i];
		if (elm.type == "hidden") continue;
		if (typeof(elm.xvalidate) == "function" && !elm.xvalidate()) return false;
	}
return true;
}

function checkForm() {
	if (validate_form()) {
	redo = true;
	document.images['btnNext'].src = "/static/id55/1/button_processing.gif";
	setTimeout('submitForm()',300);
	}
}

function submitForm() {
document.forms[0].submit();
}
