﻿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;
	}
}

function validateNonEmpty(inputField, ErrorText) {
	// See if the input value contains any text
	return validateRegEx(/.+/, inputField.value, ErrorText, "*Required");
}

// Special function to try and check for the age
//function validateRegExAge(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 = "";
//	}
//}

//function validateNonEmptyAge(inputField, ErrorText) {
//	// See if the input value contains any text
//	return validateRegExAge(/.+/, inputField.value, ErrorText, "*Required");
//}

// Function to verify that the user is at least 18 is not working yet
//function checkAge(input, ErrorText, ErrorMsg) {
//	// Check to see if the user is older than 18
//	var year = input.substr(6,4);
//	alert("hello");
//	var age = 2009
//	if (year > 1991) {
//		if (ErrorText != null) {
//			ErrorText.innerHTML = ErrorMsg;
//			return false;
//		}
//	}
//	else {
//		// User is older than 18
//		if (ErrorText != null)
//			ErrorText.innerHTML = "";
//		return true;
//	}
//}

function validateFirstName(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(/^\D+$/, inputField.value, ErrorText, "* Invalid Name");
}

function validateLastName(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 Last Name");
}

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 validateHomePhone(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, "* Invalid Number");
}

//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");
//}	

////// Code trying to get the CheckAge() function to work	
//	// Check to see if this is a valid b-day number
//	else if (!validateRegExAge(/^\d{2}[\/-]\d{2}[\/-]\d{4}$/, inputField.value, ErrorText, "* Enter as MM/DD/YYYY"))
//		return false;
//	
//	// Check to see if this is a valid dob number
//	else if (!CheckAge(inputField.value, ErrorText, "* Must be 18 to participate"))
//		return false;
//	
//	else
//		return true;


//function resetForm()
//{
//	document.myform.first_name.value="";
//	document.myform.initial.value="";
//	document.myform.last_name.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.home_phone.value="";
//	document.myform.business_phone.value="";
//	document.myform.dob.value="";
//	document.getElementById('ErrorFirstName').innerHTML="";
//	document.getElementById('ErrorLastName').innerHTML="";
//	document.getElementById('ErrorAddress').innerHTML="";
//	document.getElementById('ErrorCity').innerHTML="";
//	document.getElementById('ErrorZip').innerHTML="";
//	document.getElementById('ErrorEmail').innerHTML="";
//	document.getElementById('ErrorHomePhone').innerHTML="";
//	document.getElementById('ErrorBusinessPhone').innerHTML="";
//	document.getElementById('ErrorDOB').innerHTML="";
//}

function resetForm(form)
{
	form["first_name"].value="";
	form["initial"].value="";
	form["last_name"].value="";
	form["address_1"].value="";
	form["city"].value="";
	form["state"].value="OH";
	form["zip"].value="";
	form["email"].value="";
	form["home_phone"].value="";
	form["business_phone"].value="";
	document.getElementById('ErrorFirstName').innerHTML="";
	document.getElementById('ErrorLastName').innerHTML="";
	document.getElementById('ErrorAddress').innerHTML="";
	document.getElementById('ErrorCity').innerHTML="";
	document.getElementById('ErrorZip').innerHTML="";
	document.getElementById('ErrorEmail').innerHTML="";
	document.getElementById('ErrorHomePhone').innerHTML="";
	document.getElementById('ErrorBusinessPhone').innerHTML="";
}

function emailForm(form)
{
	if (validateFirstName(form["txtFirstName"], form["ErrorFirstName"]) && 
		validateLastName(form["txtLastName"], form["ErrorLastName"]) && 
		validateAddress(form["txtAddress"], form["ErrorAddress"]) && 
		validateCity(form["txtCity"], form["ErrorCity"]) && 
		validateZip(form["txtZip"], form["ErrorZip"]) && 
		validateEmail(form["txtEmail"], form["ErrorEmail"]) && 
		validateHomePhone(form["txtHomePhone"], form["ErrorHomePhone"]))
		{
			// submit the form to the server.
			// server side script will send out the email.
			form.submit();
		}
		else
		{
			alert("Please fill in the required fields.");
		}
}















