var reqField = "";
var emailValid = true;

function validateWeddingContact()
{
	//alert("found function");
	var numOfProblems = '0';
	var stringOfProblems = "";
	var cForm = document.forms[0];	
	
	//var alertStmnt = (cForm.firstName.value + "\n" + cForm.lastName.value + "\n" + cForm.email.value);
	//alert(alertStmnt);
		
	// require first and last name
	//alert("first name: " + cForm.firstName.value);
	str = checkForBlanks(cForm.firstName);
	if(cForm.firstName.value == "" || str == "")
	{
		numOfProblems++;
		stringOfProblems += "First Name is required\n";
	}
	//alert("last name: " + cForm.lastName.value);
	str = checkForBlanks(cForm.lastName);
	if(cForm.lastName.value == "" || str == "")
	{
		numOfProblems++;
		stringOfProblems += "Last Name is required\n";
	}
	//alert("email: " + cForm.email.value);
	str = checkForBlanks(cForm.emailAdd);
	// require email address
	if(cForm.email.value == "" || str == "")
	{
		numOfProblems++;
		stringOfProblems += "Valid Email Address is required\n";
	}
	else
	{
		echeck(cForm.emailAdd.value);
		//alert("emailValid = " + emailValid);
		if(emailValid == false)
		{
			numOfProblems++;
			stringOfProblems += "Valid Email Address is required\n";
		}
	}
	
	
	// throw alert and prevent form submit if problems
	if(numOfProblems > 0)
	{
		alert("The following error(s) occurred with your submittion. Please correct them and try again\n\n" + stringOfProblems);
		return false;
	}
	else
	{
		//testing with alert message. Replace this with form.submit() function...
		//alert("No problems with the form");
		cForm.submit();
	}
}

function checkForBlanks(field)
{
	//alert("checkForBlanks: " + field.name);
	var newValue = field.value.replace(/ /g,"");
	if (newValue == ''){field.value = '';}
	return newValue;
}

function echeck(str) 
{
	//alert("entered echeck");
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1)
	{
	   emailValid = false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
	   emailValid = false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
	   emailValid = false;
	}
	if (str.indexOf(at,(lat+1))!=-1)
	{
	   emailValid = false;
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
	   emailValid = false;
	}
	if (str.indexOf(dot,(lat+2))==-1)
	{
	   emailValid = false;
	 }
	 if (str.indexOf(" ")!=-1)
	 {
	   emailValid = false;
	 }					
}
