var hasSubmitted = false;
var errorMsg = "";

// Validate email address
function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){return "failed";}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){return "failed";}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){return "failed";}
	if (str.indexOf(at,(lat+1))!=-1){return "failed";}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){return "failed";}
	if (str.indexOf(dot,(lat+2))==-1){return "failed";}
	if (str.indexOf(" ")!=-1){return "failed";}
	return "passed";					
	}

// Prevent double submit and generate error messages as neccessary
function noDoubleSubmit(){
	var terms = document.getElementById("terms");
	var firstname = document.getElementById("firstname");
	var lastname = document.getElementById("lastname");
	var email = document.getElementById("email");
	 
 	if(terms.checked == false){errorMsg += "<li>You must agree to the Terms and Conditions.</li>";}			
  if(firstname.value == 'First name'){errorMsg += "<li>You must provide your First name</li>";}			
  if(lastname.value == 'Last name'){errorMsg += "<li>You must provide your Last name</li>";}			
  if(email.value == 'Email address'){errorMsg += "<li>You must provide your Email address</li>";}
		
	var email_status = echeck(email.value);
	if(email_status == "failed" && email.value != 'Email address'){errorMsg += "<li>Invalid email address specified for 'Email' field. Please use the format 'aaa@bbb.ddd'</li>";}

	if(errorMsg.length > 0){
		var errorDiv = document.getElementById("xhtmlError");
		errorDiv.innerHTML = "<h3>Please fix the following errors:</h3><ol>"+errorMsg+"</ol>";
		errorDiv.style.display = "block";
		errorMsg = "";
		return false;
	}	
	if(hasSubmitted){
		return false;
	  }else{
			hasSubmitted = true;
			return true;
		 }
	}
	
	
	
	

