﻿function scrub_zip() {
	this.value = this.value.replace(/\D+/gi, "");
}

function rewrite_phone() {
	if (this.value.length < 10) return;
	this.value = this.value.replace(/\D+/gi, "");
	this.value = this.value.replace(/(\d{3})(\d{3})(\d{4})/gi, "($1) $2-$3");
}

function scrub_field() {
	this.value = this.value.replace(/<(.+?)>/gi, "");
}

function verify_email() {
	var f = document.forms["aspnetForm"].elements["contact_email"].value;
	if (f.length == 0) return (true);
	return (f.search(/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/gi) != -1);
}

var form_assistant = new FormAssistant("assistant");
form_assistant.addRange(
	new FormFieldAssistant("aspnetForm", "company_name", "Enter the legal name of your company."),
	new FormFieldAssistant("aspnetForm", "stid", "Enter your sales tax number as issued by your state tax commission."),
	new FormFieldAssistant("aspnetForm", "company_type", "Select the type of business that your company performs."),
	new FormFieldAssistant("aspnetForm", "contact_name", "Enter the name of the primary point of contact for your company."),
	new FormFieldAssistant("aspnetForm", "contact_email", "Enter the email address for communications with the primary point of contact."),
	new FormFieldAssistant("aspnetForm", "address_line1", "Enter the street address for the physical location of the company."),
	new FormFieldAssistant("aspnetForm", "address_line2", "Enter additional street address information for the physical location of the company, such as suite number or C/O label.  This information is optional."),
	new FormFieldAssistant("aspnetForm", "address_city", "Enter the city for the physical location of the company."),
	new FormFieldAssistant("aspnetForm", "address_state", "Select the state for the physical location of the company."),
	new FormFieldAssistant("aspnetForm", "address_zip", "Enter the ZIP code for the physical location of the company."),
	new FormFieldAssistant("aspnetForm", "phone1", "Enter the primary phone number for the company, including area code."),
	new FormFieldAssistant("aspnetForm", "phone2", "Enter the primary fax number for the company, including area code."),
	new FormFieldAssistant("aspnetForm", "brands", "Enter the brands your company carries, sells, or works with, separated by commas.")
);

form_assistant.messages[9].registerMethod(scrub_zip, false);
form_assistant.messages[10].registerMethod(rewrite_phone, false);
form_assistant.messages[11].registerMethod(rewrite_phone, false);
form_assistant.messages.for_each(function() { this.registerMethod(scrub_field, false); });				
form_assistant.bind();

var form_validator = new FormValidator("aspnetForm");
form_validator.addValidators(
	new RequiredField("aspnetForm", "company_name", "The legal name of the company is required."),
	new RequiredField("aspnetForm", "stid", "The Sales Tax ID number of the company is required."),
	new RequiredField("aspnetForm", "company_type", "You must select the type of business."),
	new RequiredField("aspnetForm", "contact_name", "You must provide the name of the primary point of contact."),
	new RequiredField("aspnetForm", "contact_email", "You must provide the email address of the primary point of contact."),
	new RequiredField("aspnetForm", "address_line1", "You must provide the street address for the physical location of the company."),
	new RequiredField("aspnetForm", "address_city", "You must provide the city for the physical location of the company."),
	new RequiredField("aspnetForm", "address_state", "You must select the state for the physical location of the company."),
	new RequiredField("aspnetForm", "address_zip", "You must provide the ZIP code for the physical location of the company."),
	new RequiredField("aspnetForm", "phone1", "You must provide the primary phone number of the company, including area code."),
	new RequiredField("aspnetForm", "phone2", "You must provide the primary fax number of the company, including area code."),
	new RequiredField("aspnetForm", "brands", "You must specify the brands your company works with, or specify \"None\"."),
	new FieldMethod("aspnetForm", "contact_email", verify_email, "The email address provided is not a recognized format or is incomplete.")
);
form_validator.bind();