<!--
function isblank(s)
{
for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;;
        }
return true;
}

//check validity of email address
function validEmail(email) {
        invalidChars = " /:,;"
        if (email == "") {
                return false
        }
        for (i=0; i<invalidChars.length; i++) {
                badChar=invalidChars.charAt(i)
                if (email.indexOf(badChar,0) != -1) {
                        return false
                }
        }
        atPos = email.indexOf("@",1)
        if (atPos == -1) {
                return false
        }
        if (email.indexOf("@",atPos+1) != -1) {
                return false
        }
        periodPos = email.indexOf(".",atPos)
        if (periodPos == -1) {
                return false
        }
        if (periodPos+3 > email.length) {
                return false
        }
        return true
}

//truncate field name
function replaceString(oldS,newS,fullS) {
	for (var i=0; i<fullS.length; i++) {      
		if (fullS.substring(i,i+oldS.length) == oldS){
			fullS = fullS.substring(0,i)+newS+fullS.substring(i+oldS.length,fullS.length)
		}
	}   
return fullS;
}

//fill billing info if same
function fillBill() {
	var f = document.forms[0];
	if (f.same.checked) {
		f.x_b_company.value = f.x_company.value;
		f.x_b_contact.value = f.x_contact.value;
		f.x_b_billing.value = f.x_billing.value;
		f.x_b_suite.value = f.x_suite.value;
		f.x_b_city.value = f.x_city.value;
		f.x_b_state.value = f.x_state.value;
		f.x_b_zip.value = f.x_zip.value;
		f.x_b_phone.value = f.x_phone.value;
		f.x_b_ext.value = f.x_ext.value;
		f.x_b_fax.value = f.x_fax.value;
		f.x_b_email.value = f.x_email.value;
	} else {
		f.x_b_company.value = "";
		f.x_b_contact.value = "";
		f.x_b_billing.value = "";
		f.x_b_suite.value = "";
		f.x_b_city.value = "";
		f.x_b_state.value = "";
		f.x_b_zip.value = "";
		f.x_b_phone.value = "";
		f.x_b_ext.value = "";
		f.x_b_fax.value = "";
		f.x_b_email.value = "";
	}
}

function formAction(){
	var f = document.forms[0];
	if ((f.x_service1.checked) && (f.x_service2.checked)) {
		f.x_template_key.value = "open_an_account";
		f.x_subject.value = "Open Account: Full Service";
	} else if (f.x_service1.checked) {
		f.x_template_key.value = "open_an_account_sameday_overnight";
		f.x_subject.value = "Open Account: SameDay/Overnight";
	} else if (f.x_service2.checked) {
		f.x_template_key.value = "open_an_account_scheduled";
		f.x_subject.value = "Open Account: Scheduled Delivery";
	}	
}

//called from the onSubmit() event handler.
function verify(f) {
	var msg = "";
	var empty_fields = "";
	var no_state = "";
	var no_b_state = "";
	var bad_email = "";

	for(var i=0; i < f.length; i++) {
			var e = f.elements[i];
			
		if ((e.type=="text") && !e.optional) {
			if ((e.value == null) || (e.value == "") || isblank(e.value)){				
				var fieldName = replaceString("x_","",e.name);
				var finalName = replaceString("b_","billing ",fieldName);
				empty_fields += "\n" + finalName;
			}
		}
	}

	if (f.x_state.selectedIndex == 0) {
		empty_fields += "\ncompany state"
	}
	if (f.x_b_state.selectedIndex == 0) {
		empty_fields += "\nbilling state"
	}
	if ((f.x_email.value == null) || (f.x_email.value == "") || isblank(f.x_email.value)){
	} else {
		if (!validEmail(f.x_email.value)) {
			bad_email = "invalid email address"
		}
	}
	if ((f.x_b_email.value == null) || (f.x_b_email.value == "") || isblank(f.x_b_email.value)){
	} else {
		if (!validEmail(f.x_b_email.value)) {
			bad_email += "\ninvalid billing email address"
		}
	}

	if (!f.x_service1.checked && !f.x_service2.checked)  {
		empty_fields += "\ntype of service";
	}

	if (empty_fields) {	
		msg = "Please complete all required fields:\n";
		msg += empty_fields;
	}

	if (bad_email) {
		msg += "\n" + bad_email;
	}		

	if (msg) {
		alert(msg);
		return(false);
	}

formAction();
return(true);
}
//-->