var strErr = '';

// Email
function CheckEmail(str){
	var re = new RegExp("^[0-9a-zA-Z_\.\-]+\@[0-9a-zA-Z_\-]+[\.][0-9a-zA-Z_\.\-]+$","ig");
	var arr = re.exec(str);
	return (arr == null);
}

// Phone
function CheckPhoneNumber(str){
	var re = new RegExp("^[0-9]{10}$","ig");
	var arr = re.exec(str);
	return (arr == null);
}

// ZIP
function CheckZIP(str){
	var re = new RegExp("^[0-9]{5}(-[0-9]{4})?$","ig");
	var arr = re.exec(str);
	return (arr == null);
}

// Characters only
function CheckC(str){
	var re = new RegExp("[^a-zA-Z]","ig");
	var arr = re.exec(str);
	return (arr != null);
}

// Characters, dashes, spaces only
function CheckCDS(str){
	var re = new RegExp("[^ a-zA-Z-']","ig");
	var arr = re.exec(str);
	return (arr != null);
}

// Address
function CheckAddr(str){
	var re = new RegExp("[^ a-zA-Z0-9-\,\.#']","ig");
	var arr = re.exec(str);
	return (arr != null);
}

// Get value
function getV(strObj){
	var o=document.getElementById(strObj)
	o.value=trim(o.value);
	return o.value;
}

function CheckChecked(strObj){
	return document.getElementById(strObj).checked;
}

function trim(str){
	var i = 0;
	while (str.substr(i,1) == ' ') i++;
	str = str.substr(i, str.length - i)
	i = str.length - 1;
	while (str.substr(i,1) == ' ') i--;
	return str.substr(0,i+1);
}

function CheckPhone(strName,strFieldName){
	var str = getV(strName);
	var r = '';
	if (str == ''){
		r = '"'+strFieldName+'" is required information.\n';
	}
	else{
		if (CheckPhoneNumber(str)){
			r = '"'+strFieldName+'" must ONLY contain 10 integers (no dashes, spaces and brackets).\n';
		}
	}
	return r;
}

function ValidateAndSubmit(){
	var strErr = '';
	if (!CheckChecked("mr") && !CheckChecked("mrs") && !CheckChecked("ms")) strErr += "Salutation must be checked.\n";
	if (CheckCDS(getV("firstname"))) strErr += '"First Name" may ONLY contain characters, apostrophe, spaces or dashes.\n';
	if (getV("firstname").length >20) strErr += '"First Name" must contain less than 20 characters.\n';
	if (getV("firstname") == '') strErr += '"First Name" is required information.\n';
	if ((CheckC(getV("middleinitial"))) || (getV("middleinitial").length >1)) strErr += '"Middle Initial" may ONLY contain ONE character.\n';
	if (CheckCDS(getV("lastname"))) strErr += '"Last Name" may ONLY contain characters, apostrophe, spaces or dashes.\n';
	if (getV("lastname").length >20) strErr += '"Last Name" must contain less than 20 characters.\n';
	if (getV("lastname") == '') strErr += '"Last Name" is required information.\n';
	if (CheckCDS(getV("compname"))) strErr += '"Company Name" may ONLY contain characters, apostrophe, spaces or dashes.\n';
	if (getV("compname").length >50) strErr += '"Company Name" must contain less than 50 characters.\n';
	if (CheckAddr(getV("address"))) strErr += '"Address" may ONLY contain characters, apostrophe, spaces, dashes, commas, dots and #.\n';
	if (getV("address").length >50) strErr += '"Address" must contain less than 50 characters.\n';
	if (getV("address") == '') strErr += '"Address" is required information.\n';
	if (CheckCDS(getV("city"))) strErr += '"City" may ONLY contain characters, apostrophe, spaces or dashes.\n';
	if (getV("city").length >20) strErr += '"City" must contain less than 20 characters.\n';
	if (getV("city") == '') strErr += '"City" is required information.\n';
	strErr += CheckPhone("workphone","Work phone");
	if (document.getElementById("state").selectedIndex < 1) strErr += '"State" must be selected.\n';
	if (CheckZIP(getV("zip"))) strErr += 'Please enter a proper 5 digit Zip Code or enter your Zip + 4 code.\n';
	strErr += CheckPhone("cellphone","Wireless phone");
	if (CheckCDS(getV("industry"))) strErr += '"Industry" may ONLY contain characters, apostrophe, spaces or dashes.\n';
	if (getV("industry").length >20) strErr += '"Industry" must contain less than 20 characters.\n';
	strErr += CheckPhone("fax","Fax");
	if (CheckCDS(getV("proftitle"))) strErr += '"Professional Title" may ONLY contain characters, apostrophe, spaces or dashes.\n';
	if (getV("proftitle").length >20) strErr += '"Professional Title" must contain less than 20 characters.\n';
	if (CheckEmail(getV("email"))) strErr += 'Please enter your correct email address.\n';
	if (getV("qandc").length >750) strErr += '"Questions & Comments" must contain less than 750 characters.\n';

	if (strErr==''){
		document.getElementById("rsubmit").click();
	}
	else{
		alert(strErr);
	}
}
