// --- Form validation
function validateForm(formobj) {
revivecontent();
var checkSum = 0;
var fieldNames = Array("phone","email","billingname","serviceaddress","city","state","zip");
for (var j = 0; j < fieldNames.length; j++) {
	var obj = formobj.elements[fieldNames[j]];
	if (obj.name == "phone") {
		if (checkNationalPhone(obj.value)==false) {
			checkSum += 1;
			expandcontent('phone_error2'); }
		else if ((obj.value == "") || (obj.value == null)) {
			checkSum += 1;
			expandcontent('phone_error'); }
		}
	if ((obj.name == "email") && (obj.value != "")) {
		//if (obj.value != null) {
			if (emailTest(obj.value) == "invalid") {
				checkSum += 1;
				expandcontent('email_error'); }
			else if (emailTest(obj.value) == "multiple") {
				checkSum += 1;
				expandcontent('email_error2'); }
			//}
		}
	if (obj.name == "billingname") {
		if ((obj.value == "") || (obj.value == null)) {
			checkSum += 1;
			expandcontent('billingname_error'); }
		}
	if (obj.name == "serviceaddress") {
		if ((obj.value == "") || (obj.value == null)) {
			checkSum += 1;
			expandcontent('serviceaddress_error'); }
		}
	if (obj.name == "city") {
		if ((obj.value == "") || (obj.value == null)) {
			checkSum += 1;
			expandcontent('city_error'); }
		}
	if (obj.name == "state") {
		if ((obj.value == "") || (obj.value == null)) {
			checkSum += 1;
			expandcontent('state_error'); }
		}
	if (obj.name == "zip") {
		if ((obj.value == "") || (obj.value == null)) {
			checkSum += 1;
			expandcontent('zip_error'); }
		}
	}
if (checkSum == 0) {
	document.form.location.value = "639";
	return true; }
else {
	expandcontent('errors_occurred');
	document.location.href="#top";
	return false; }
}

// Validate Phone Numbers
// declare required variables
var digits = "0123456789";
// non-digit characters allowed
var phoneNumberDelimiters = "";
// required no of digits
var DigitsInPhoneNumber = 10;

function isInteger(s) {
var i;
for (i = 0; i < s.length; i++) {
	// Check that current character is number.
	var c = s.charAt(i);
    if (((c < "0") || (c > "9"))) return false; }
return true; }

function stripCharsInBag(s, bag) {
var i;
var returnString = "";
// Search through string's characters one by one.
// If character is not in bag, append to returnString.
for (i = 0; i < s.length; i++) {
	// Check that current character isn't whitespace.
	var c = s.charAt(i);
    if (bag.indexOf(c) == -1) returnString += c; }
return returnString; }

function checkNationalPhone(strPhone) {
s=stripCharsInBag(strPhone,phoneNumberDelimiters);
return (s.length >= DigitsInPhoneNumber || s.length == 0); }

function checkCSRPhone() {
revivecontent();
var Phone=document.form.csrphone;
if (checkNationalPhone(Phone.value)==false) {
	expandcontent('csrphone_error2');
	document.form.csrphone.focus();
	return false; }
else { return checkHostPhone(); }
}

// --- Check Required Fields
// Show/Hide Errors for required fields
function formCheck(formobj){
revivecontent();
// Enter name of mandatory fields
var fieldRequired = Array("phone","billingname","serviceaddress","city","state","zip");
// Enter field description to appear in the dialog box
var fieldDescription = Array("Phone","Account Name","Service Address","City","State","Zip");
// dialog message
var alertMsg = "Errors occurred in form submission.  Please correct the form and resubmit.";
var l_Msg = alertMsg.length;

for (var i = 0; i < fieldRequired.length; i++) {
	var obj = formobj.elements[fieldRequired[i]];
	if (obj) {
		switch(obj.type) {
		case "select-one":
			if (obj.selectedIndex == 0 || obj.options[obj.selectedIndex].text == "") {
				alertMsg += " - " + fieldDescription[i] + "\n";
				expandcontent(fieldRequired[i] + '_error'); }
			break;
		case "select-multiple":
			if (obj.selectedIndex == 0) {
				alertMsg += " - " + fieldDescription[i] + "\n";
				expandcontent(fieldRequired[i] + '_error'); }
			break;
		case "text":
		case "textarea":
			if (obj.value == "" || obj.value == null) {
				alertMsg += " - " + fieldDescription[i] + "\n";
				expandcontent(fieldRequired[i] + '_error'); }
			break;
		default: 
		}
		if (obj.type == undefined) {
			var blnchecked = false;
			for (var j = 0; j < obj.length; j++) {
				if (obj[j].checked) {
					blnchecked = true; }
			}
			if (!blnchecked) {
				alertMsg += " - " + fieldDescription[i] + "\n";
				expandcontent(fieldRequired[i] + '_error'); }
		}
	}
}

if (alertMsg.length == l_Msg) {
	return true; }
else {
	document.location.href="#top";
	return false; }
}
// --- End Check Required Fields

// --- Dynamic Content - error messages

var enablepersist="off" //Enable saving state of content structure using session cookies? (on/off)
var collapseprevious="no" //Collapse previously open content when opening present? (yes/no)

if (document.getElementById) {
document.write('<style type="text/css">')
document.write('.required{display:none;}')
document.write('</style>')
}

function getElementbyClass(classname) {
ccollect=new Array()
var inc=0
var alltags=document.all? document.all : document.getElementsByTagName("*")
for (i=0; i<alltags.length; i++) {
	if (alltags[i].className==classname)
	ccollect[inc++]=alltags[i] }
}

function contractcontent(omit) {
var inc=0
while (ccollect[inc]) {
	if (ccollect[inc].id!=omit)
	ccollect[inc].style.display="none"
	inc++ }
}

function expandcontent(cid) {
if (typeof ccollect!="undefined") {
	if (collapseprevious=="yes")
	contractcontent(cid)
	document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="block")? "block" : "none" }
}

function revivecontent() {
contractcontent("omitnothing")
selectedItem=getselectedItem()
selectedComponents=selectedItem.split("|")
for (i=0; i<selectedComponents.length-1; i++)
document.getElementById(selectedComponents[i]).style.display="block"
}

function get_cookie(Name) { 
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
	offset = document.cookie.indexOf(search)
	if (offset != -1) {
		offset += search.length
		end = document.cookie.indexOf(";", offset);
		if (end == -1) end = document.cookie.length;
		returnvalue=unescape(document.cookie.substring(offset, end)) }
	}
return returnvalue;
}

function getselectedItem() {
if (get_cookie(window.location.pathname) != "") {
	selectedItem=get_cookie(window.location.pathname)
	return selectedItem }
else
return ""
}

function saveswitchstate() {
var inc=0, selectedItem=""
while (ccollect[inc]) {
	if (ccollect[inc].style.display=="block")
	selectedItem+=ccollect[inc].id+"|"
	inc++ }
document.cookie=window.location.pathname+"="+selectedItem
}

function do_onload() {
uniqueidn=window.location.pathname+"firsttimeload"
getElementbyClass("required")
if (enablepersist=="on" && typeof ccollect!="undefined") {
	document.cookie=(get_cookie(uniqueidn)=="")? uniqueidn+"=1" : uniqueidn+"=0" 
	firsttimeload=(get_cookie(uniqueidn)==1)? 1 : 0 //check if this is 1st page load
	if (!firsttimeload)
	revivecontent() }
}

if (window.addEventListener)
window.addEventListener("load", do_onload, false)
else if (window.attachEvent)
window.attachEvent("onload", do_onload)
else if (document.getElementById)
window.onload=do_onload

if (enablepersist=="on" && document.getElementById)
window.onunload=saveswitchstate

// --- End form validation

// Validate email address
function emailTest(email_address) {
var email_pattern = /^(\w|-)+\@([a-zA-Z0-9]|-)+\.(\w|\.)+/;
var email_pattern2 = /^(\w|-)+\.(\w|\.)+\@([a-zA-Z0-9]|-)+\.(\w|\.)+/;
// var email_address = document.form.email.value;
var start = email_address.indexOf("@") + 1;
var checkString = email_address.substring(start,email_address.length);
var duplicate = checkString.indexOf("@");
	if (duplicate >= 0) {
		return email_address = "multiple";
		// alert("You may only enter one email address.");
	}
var email_result;
email_result = email_pattern.exec(email_address);
var email_result2;
email_result2 = email_pattern2.exec(email_address);
	if (email_result) {
	}
	else if (email_result2) {
	}
	else  {
		return email_address = "invalid";
		// alert("Please enter a valid email address.");
	}
}
// end email address validation

function populatePricing() {
if ((document.form.plan.options[document.form.plan.selectedIndex].value) == "Dial-Up Starter") {
	document.form.pricing.value = "$9.95 per month"; }
if ((document.form.plan.options[document.form.plan.selectedIndex].value) == "Dial-Up Standard") {
	document.form.pricing.value = "$21.95 per month"; }
if ((document.form.plan.options[document.form.plan.selectedIndex].value) == "Dial-Up Enhanced") {
	document.form.pricing.value = "$24.95 per month"; }
if ((document.form.plan.options[document.form.plan.selectedIndex].value) == "DSL Starter") {
	document.form.pricing.value = "$34.95 per month"; }
if ((document.form.plan.options[document.form.plan.selectedIndex].value) == "DSL Standard") {
	document.form.pricing.value = "$39.95 per month"; }
if ((document.form.plan.options[document.form.plan.selectedIndex].value) == "DSL Premium") {
	document.form.pricing.value = "$64.95 per month"; }
if ((document.form.plan.options[document.form.plan.selectedIndex].value) == "DSL Business") {
	document.form.pricing.value = "$94.95 per month"; }
if ((document.form.plan.options[document.form.plan.selectedIndex].value) == "Wireless Internet") {
	document.form.pricing.value = "$39.95 per month"; }
}
