﻿// Valiates the Regular Expression that it is fed
function validateRegEx(regex, input, ErrorText, ErrorMsg) {
	// see if the input data is valid
	if (!regex.test(input)) {
		// data is invalid. Set Error Message and return false
		if (ErrorText != null) {
			ErrorText.innerHTML = ErrorMsg;
			return false;
		}
	}
	else
	{
		// data is ok. Clear Error Message and return true
		if (ErrorText != null)
			ErrorText.innerHTML = "";
		return true;
	}
}

// Checks to see if the field is empty
function validateNonEmpty(inputField, ErrorText) {
	// See if the input value contains any text
	return validateRegEx(/.+/, inputField.value, ErrorText, "*Required");
}

/////////////////////////////////////////////////
// Form Validation Elements for individual fields
////////////////////////////////////////////////
function validateBusinessName(inputField, ErrorText) {
	// See if the First name field is empty
	if (!validateNonEmpty(inputField, ErrorText))
		return false;
	
	// Check to see if this is a valid name
	return validateRegEx(/^\s*\w+(\s)*/, inputField.value, ErrorText, "* Invalid Name");
}

function validateContactPerson(inputField, ErrorText) {
	// See if the Last name field is empty
	if (!validateNonEmpty(inputField, ErrorText))
		return false;
	
	// Check to see if this is a valid name
	return validateRegEx(/^\D+$/, inputField.value, ErrorText, "* Invalid Name");
}

function validatePersonPosition(inputField, ErrorText) {
	// See if the Last name field is empty
	if (!validateNonEmpty(inputField, ErrorText))
		return false;
	
	// Check to see if this is a valid name
	return validateRegEx(/^\D+$/, inputField.value, ErrorText, "* Invalid Position, character only");
}

function validateAddress(inputField, ErrorText) {
	// See if the Address field is empty
	return validateNonEmpty(inputField, ErrorText);
}

function validateCity(inputField, ErrorText) {
	// See if the City field is empty
	if (!validateNonEmpty(inputField, ErrorText))
		return false;
	
	// Check to see if this is a valid city
	return validateRegEx(/^\D+$/, inputField.value, ErrorText, "* Invalid City");
}

function validateZip(inputField, ErrorText) {
	// See if the Zip code field is empty
	if (!validateNonEmpty(inputField, ErrorText))
		return false;
	
	// Check to see if this is a valid zip code
	return validateRegEx(/^\d{5}$/, inputField.value, ErrorText, "* Invalid Zip");
}

function validateEmail(inputField, ErrorText) {
	// See if the Email Address field is empty
	if (!validateNonEmpty(inputField, ErrorText))
		return false;
	
	// Check to see if this is a valid email address
	return validateRegEx(/^[\w\.-_\+]+@[\w-]+(\.\w{2,4})+$/, inputField.value, ErrorText, "* Invalid Email");
}

function validateBusinessPhone(inputField, ErrorText) {
	// See if the Home Phone field is empty
	if (!validateNonEmpty(inputField, ErrorText))
		return false;
	
	// Check to see if this is a valid phone number
	return validateRegEx(/^\d{3}[\.-]\d{3}[\.-]\d{4}$/, inputField.value, ErrorText, "* Enter as (###-###-####)");
}

function validateBusinessPhoneExt(inputField, ErrorText) {
	// Check to see if this is a valid phone number
	return validateRegEx(/^(\d+)?$/, inputField.value, ErrorText, "* Invalid Ext");
}

function validateOtherPhone(inputField, ErrorText) {
	// Check to see if this is a valid phone number
	return validateRegEx(/^(\d{3}[\.-]\d{3}[\.-]\d{4})?$/, inputField.value, ErrorText, "* Enter as (###-###-####)");
}

function validateEmployeeNumber(inputField, ErrorText) {
	// See if the Home Phone field is empty
	if (!validateNonEmpty(inputField, ErrorText))
		return false;
	
	// Check to see if this is a valid phone number
	return validateRegEx(/^\d+$/, inputField.value, ErrorText, "* Numbers only");
}

//function validateDOB(inputField, ErrorText) {
//	// See if the date of birth field is empty
//	if (!validateNonEmpty(inputField, ErrorText))
//		return false;
//
//	// See if it's in the right format
//	return validateRegEx(/^\d{1,2}[\/-]\d{1,2}[\/-]\d{4}$/, inputField.value, ErrorText, "* Enter as MM/DD/YYYY");
//}	

//////////////////////////////////////////////
// Functions for sumbmiting and reseting the form
/////////////////////////////////////////////
function resetForm()
{
	document.myform.business_name.value="";
	document.myform.contact_person.value="";
	document.myform.person_position.value="";
	document.myform.address_1.value="";
	document.myform.address_2.value="";
	document.myform.city.value="";
	document.myform.state.value="OH";
	document.myform.zip.value="";
	document.myform.email.value="";
	document.myform.business_phone.value="";
	document.myform.business_phone_ext.value="";
	document.myform.other_phone.value="";
	document.myform.employeeNumber.value="";
	document.getElementById('errorBusinessName').innerHTML="";
	document.getElementById('errorContactPerson').innerHTML="";
	document.getElementById('errorPersonPosition').innerHTML="";
	document.getElementById('errorAddress').innerHTML="";
	document.getElementById('errorCity').innerHTML="";
	document.getElementById('errorZip').innerHTML="";
	document.getElementById('errorEmail').innerHTML="";
	document.getElementById('errorBusinessPhone').innerHTML="";
	document.getElementById('errorBusinessPhoneExt').innerHTML="";
	document.getElementById('errorOtherPhone').innerHTML="";
	document.getElementById('errorEmployeeNumber').innerHTML="";
}

function emailForm(form)
{
	if (validateBusinessName(form["txtBusinessName"], form["errorBusinessName"]) && 
		validateContactPerson(form["txtContactPerson"], form["errorContactPerson"]) && 
		validatePersonPosition(form["txtPersonPosition"], form["errorPersonPosition"]) &&
		validateAddress(form["txtAddress"], form["errorAddress"]) && 
		validateCity(form["txtCity"], form["errorCity"]) && 
		validateZip(form["txtZip"], form["errorZip"]) && 
		validateEmail(form["txtEmail"], form["errorEmail"]) && 
		validateBusinessPhone(form["txtBusinessPhone"], form["errorBusinessPhone"]) && 
		validateEmployeeNumber(form["txtEmployeeNumber"], form["errorEmployeeNumber"]))
	{
		// submit the form to the server.
		// server side script will send out the email.
		form.submit();
	}
	else
	{
			alert("Please fill in the required fields.");
	}
}















