// JavaScript Document

/* 
Start of FORM CHECKER code
*/
function checkForm(formID) {
	
	var fieldnames = Array();
	fieldnames['email1'] = 'Your email address';
	fieldnames['name1'] = 'Your name';
	fieldnames['email2'] = 'Your email address';
	fieldnames['name2'] = 'Your name';
	fieldnames['email3'] = 'Your email address';
	fieldnames['name3'] = 'Your name';

	
	missingFields = ""
	badEmail = 0
	submitForm = 1
	if (document.forms) {
		
		// Iterate Element
		elementsCollection = document.getElementById(formID).elements;
		for(j=0; j<elementsCollection.length; j++)
		{
			node = elementsCollection[j];
			
			// Check if any required fields are missing
			if (/required/.test(node.className) && (node.value == "" || node.value == "..."))
			{
				var text = node.id;
				if (fieldnames[node.id]) text = fieldnames[node.id];
				
				if (missingFields == "")
				{
					missingFields = text;
				}
				else
				{
					missingFields = missingFields + ", " + text;
				}
				// Don't submit the form
				submitForm = 0
			}
			
			// Check to see if required email addresses have been filled in AND are correctly formatted
			else if (/email-req/.test(node.className)){
				/* Check if email exists */
				if (node.value == "" || node.value == "...")
				{
					var text = node.id;
					if (fieldnames[node.id]) text = fieldnames[node.id];
					
					if (missingFields == "")
					{
						missingFields = text;
					}
					else
					{
						missingFields = missingFields + ", " + text;
					}
					// Don't submit the form
					submitForm = 0
				}
				else
				{
					var str = node.value; // email string
					
					var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
					var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{1,4}|[0-9]{1,4})(\]?)$/; // valid
					
					if (!reg1.test(str) && reg2.test(str)) 
					{
						// Email is valid, so do nothing
					}
					else
					{
						// Email is invalid
						badEmail = 1
						// Don't submit the form
						submitForm = 0
					}
				}
			}
			
			// Check to see if non-required email addresses are correctly formatted
			else if (/email-nonreq/.test(node.className)){
				/* Check if email field has been filled in */
				if (node.value != "" && node.value != "...")
				{
					var str = node.value; // email string
					
					var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
					var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{1,4}|[0-9]{1,4})(\]?)$/; // valid
					
					if (!reg1.test(str) && reg2.test(str)) 
					{
						// Email is valid, so do nothing
					}
					else
					{
						// Email is invalid
						badEmail = 1
						// Don't submit the form
						submitForm = 0
					}
				}
			}
			
			// Check to see if required country has been filled in
			else if (/country-req/.test(node.className)){
				/* Check if a country has been selected */
				if (node.options[node.selectedIndex].value == "" || node.options[node.selectedIndex].value == "...")
				{
					var text = node.id;
					if (fieldnames[node.id]) text = fieldnames[node.id];
					
					if (missingFields == "")
					{
						missingFields = text;
					}
					else
					{
						missingFields = missingFields + ", " + text;
					}
					// Don't submit the form
					submitForm = 0
				}
			}
		}
		
		// Create the error message
		
		if (missingFields != "")
		{
			missingFields = "Sorry, you have missed one or more required fields.\n\nThe fields you missed are: " + missingFields
		}
		
		if (missingFields == "" && badEmail != 0)
		{
			missingFields = "Sorry, you appear to have entered an incorrect email address."
		}
		else if (missingFields != "" && badEmail != 0)
		{
			missingFields = missingFields + "\n\nYou also appear to have entered an incorrect email address."
		}
		
		if (missingFields != "")
		{
			alert (missingFields)	
		}
	}
	if (submitForm != 1)
	{
		// don't submit the form if there are missing or invalid elements
		return false;
	}
}
/*
End of FORM CHECKER code
*/
