// Validation script for the trial application form for Gold's Gym
// Written By Dale Fyfe for Quoth Design - 3/5/07

function validateApp()
{
	//collect variables to use during validation
	var name = document.application.name.value;
	var address = document.application.address.value;
	var contact = document.application.contact.value;
	var email = document.application.email.value;
	var day = document.application.day.value;
	var month = document.application.month.value;
	var errorMsg = "";
	
	//validation algorithm. Mostly goes through and adds to the error message at the end.
	// if there is no error message, or it is blank, then the form returns true and it can be proccessed by the php
	// which sends the email.
	validateName();
	validateContact();
	if(! validateInterests())
	{
		errorMsg = errorMsg + "You must select at least one interest\n";	
	}
	validateDate();
	
	// generate error message if the user has done something we dont want them to, otherwise return true and process the form with php.
	if( errorMsg == "")
	{
		return true;	
	}
	else
	{
		alert(""+errorMsg);
		return false;
	}
	
	//Ensure the user has specified a name for the trial membership
	function validateName()
	{
		if(name == "")
		{
			errorMsg = errorMsg + "You must specify a name\n";	
		}
	}
	
	//ensure that the user has specified an address or an email address so that we can contact them.
	function validateContact()
	{
		//if the user has not specified a mailing address, then make sure they have left an email address
		if (address == "")
		{
			if (contact == "")
			{
				if(email == "")
				{
					errorMsg = errorMsg + "You must specify either an email address, a contact number or a mailing address for us to contact you and send your Trial Membership to\n";	
				}
				//if there is an email address then we need to make sure it is of a valid syntax of text@moretext.moretext
				else
				{
					var first = email.charAt(0);//first char of the email address
					var last = email.charAt(email.length -1);//Last char of the email address
					var foundSymbol = email.indexOf('@');// Where abouts the @ symbol is in the email address, returns -1 if not there
					var foundDot = email.indexOf('.');// Where the dot is in the email address.
					
					//if the @ symbol is not there, is at the start, or at the end, then the email is not valid
					if ((foundSymbol == -1) || (foundSymbol == 0) || (foundSymbol == email.length -1))
					{
						errorMsg = errorMsg + "Email address is not a valid syntax.\n";
						document.application.email.value="";	
					}
					//if the dot is not there, is at the start, or at the end, then the email is not valid
					if ((foundDot == -1) || (foundDot == 0) || (foundDot == email.length -1))
					{
						errorMsg = errorMsg + "Email address is not a valid syntax.\n";
						document.application.email.value="";	
					}
				}
			}
		}
	}
	
	//Ensure the user has ticked at least one of the interests checkboxes. If not then the PHP processing shits itself and throws an error
	function validateInterests()
	{
		var id1 = document.getElementById("weights").checked;
		var id2 = document.getElementById("weightloss").checked;
		var id3 = document.getElementById("cardio").checked;
		var id4 = document.getElementById("classes").checked;
		var id5 = document.getElementById("personaltraining").checked;
		var id6 = document.getElementById("fitness").checked;
		var id7 = document.getElementById("rehab").checked;
		var interests = new Array(id1, id2, id3, id4, id5, id6, id7);
		var validCheckbox = false;
		
		for(i=0;i<interests.length;i++)
		{
			if(interests[i]==true)
			{
				
				validCheckbox = true;	
			}
		}
		return validCheckbox;
	}
	
	// Make sure the user has specified a day and a month for their trial membership
	function validateDate()
	{
		if (day == "Day")
		{
			errorMsg = errorMsg + "You must specify a day for your Membership Trial.\n";	
		}
		if (month == "Month")
		{
			errorMsg = errorMsg + "You must specify a month for your Membership Trial.\n";	
		}
	}
}