/**
 * This function gets called by the form upon being submitted.
 */

function reg_validate()
{

	// build an array of all the fields we want to be required.
	var required_fields = new Array();
	var required_labels = new Array();

	// list the fields - all arrays start at 0 as the key.
	// Field ID's
	required_fields[0] = "RestaurantRestaurantName";
	required_fields[1] = "RestaurantContactName";
	required_fields[2] = "RestaurantPhone";
	required_fields[3] = "RestaurantAddress";
	required_fields[4] = "RestaurantCity";
	required_fields[5] = "RestaurantState";
	required_fields[6] = "RestaurantZip";
	required_fields[7] = "mt1";
	required_fields[8] = "RestaurantEmail";
	required_fields[9] = "county";

	// Field Labels
	required_labels[0] = "Restaurants Name";
	required_labels[1] = "Restaurants Contact Name";
	required_labels[2] = "Restaurants Phone Number";
	required_labels[3] = "Restaurants Address";
	required_labels[4] = "Restaurants City";
	required_labels[5] = "Restaurants State";
	required_labels[6] = "Restaurants Zip Code";
	required_labels[7] = "Restaurants Menu Style";
	required_labels[8] = "Restaurants Email Address";
	required_labels[9] = "Restaurants County";


	var total_fields = required_fields.length; // total amount of elements in our array (8)

	// loop through the array to validate the fields
	for(var i=0;i<total_fields;i++)
	{
		if(document.getElementById(required_fields[i]).value == "")
		{
			alert("Please be sure to fill in your "+required_labels[i]); // alert the user
			document.getElementById(required_fields[i]).focus(); // put focus on the element
			return false; // tell the form not to submit the information
		}
	}

	// if we get to this point, that means that all the above required fields where entered in.
	// at this point we should validate that the State is "CT".
	if(document.getElementById('RestaurantState').value != "CT")
	{
		alert("Please make sure that you enter in CT for your Restaurants State.");
		document.getElementById('RestaurantState').focus();
		return false;
	}

	// lets do a quick check on the email address to make sure its not from .ru
	// an easy way to do this is to pull the last three elements off the email address
	// and if those last three elements are .ru, well we have a problem.
	var emailAddress = document.getElementById('RestaurantEmail').value; // get the email
	var threeElementsBack = (emailAddress.length)-3; // the total length of the email minus 3 characters.
	var check_email = emailAddress.substring(threeElementsBack); // pull the last three elements.
	
	// time to check, now if its a .ru email it will not pass.
	// If someone enters a .com, .net, .org, etc... The check email variable will have "com", "net", "org".
	// so those emails will not get held up by this check.
	if(check_email == ".ru")
	{
		alert("Please enter a valid email address.");
		document.getElementById('RestaurantEmail').value = ''; // remove the email that is there now
		document.getElementById('RestaurantEmail').focus(); // put focus on the element
		return false; // still don't want to submit that form.
	}

	// okay, let the form get passed to php.
	return true;
}

