//Validate data and Build Error Message
function validate(registerform) {
	var errorMsg = "";
	
	if (!registerform.name.value || !registerform.name.value.match(/^\D{1,80}$/)) { 
		errorMsg += "Please enter your name (no numeric characters).\n"; 
	}
	
	errorMsg += ValidateEmail(registerform.email.value);
	
	registerform.phone.value = registerform.area.value + registerform.prefix.value + registerform.suffix.value;
	
	if (!registerform.phone.value || !registerform.phone.value.match(/^\d{10}$/)) {
		errorMsg += "Please enter your phone number correctly.\n";
	}
		
	if (errorMsg) { 
		dispErrorMsg("We're sorry.  We could not send your data as it was submitted.\n", errorMsg);
		return false;
	}
	else {  
		return true; 
	}
}

function ValidateEmail(email){
	var errorMsg = "";
	if((email == '')||(email == null))
		{errorMsg += "Email cannot be empty, please type in your email address\n";}
	//CHECK @
	else if(email.indexOf('@')==-1)
		{errorMsg += "Email Address cannot contain @\n";}
	else if (email.indexOf('@')!=email.lastIndexOf('@'))
		{errorMsg += "Email Address cannot have more than one @\n";}
	else if ((email.indexOf('@')==0)||(email.lastIndexOf('@')==email.length-1))
		{errorMsg += "Email Address cannot start or end with @\n";}
		
	//CHECK PERIOD
	else if(email.indexOf('.')==-1)
		{errorMsg += "Email Address must contain periods '.'\n";}
	else if ((email.indexOf('.')==0)||(email.lastIndexOf('.')==email.length-1))
		{errorMsg += "Email Address cannot start or end with a period\n";}
	else if (email.indexOf("..")!=-1)
		{errorMsg += "Email Address cannot contain consecutive periods\n";}
		
	//CHECK LOCAL PART AND DOMAIN PART SEPERATELY
	else{
		var local = email.substring(0,email.indexOf('@'));
		var domain = email.substring(email.indexOf('@')+1);
		var last = domain.substring(domain.lastIndexOf('.')+1);	
	
		//CHECK LENGTH
		if(local.length>64)
			{errorMsg += "Local part (the part before @) cannot be longer than 64 characters\n";}
		if(domain.length>255)
			{errorMsg += "Domain part (the part after @) cannot be longer than 255 characters\n";}
		else{
			var sections = domain.split('.');
			for(i=0; i< sections.length; i++){
				if(sections[i].length>63)
					{errorMsg += "Domain sections (separated by periods) cannot be longer than 63 characters\n";}
			}
		}
		//CHECK LOCAL PART ILLEGAL CHARACTERS
		var regex1 = /[^A-Za-z0-9\.\!\#\$\%\&\*\+\-\/\=\?\^\_\`\{\|\}\~]/g;
		var regex2 = /^[^A-Za-z0-9]/g;
		if(regex1.test(local))
			{errorMsg += "Local part (the part before @) contains illegal characters such as space, comma or semicolon\n";}
		else if(regex2.test(local))
			{errorMsg += "Local part (the part before @) cannot start with a non word character\n";}
		
		//CHECK DOMAIN PART ILLEGAL CHARACTERS
		var regex3 = /[^a-zA-Z0-9\-\.]/g;
		var regex4 = /^[^a-zA-Z0-9]/g;
		var regex5 = /[a-zA-Z]/g;		
		var regex6 = /[a-zA-Z]/g;	
		if(regex3.test(domain))
			{errorMsg += "Domain part (the part after @) contains illegal characters such as comma, semicolon, etc\n";}
		else{
			if(regex4.test(domain))
				{errorMsg += "Domain part (the part after @) cannot start with a nonword character\n";}
			else if(!regex5.test(domain))			
				{errorMsg += "Domain part (the part after @) cannot be numbers only\n";}
			else if(!regex6.test(last))
				{errorMsg += "The last section of domain part (the part after @) cannot be numbers only\n";}
		}
	}
	
	return errorMsg;
}

//Alert of invalid data
function dispErrorMsg(head, errorMsg) {
	var line = "________________________________________________________\n\n";
	alert(line + head + line + errorMsg);
	return false;
}
