// JSScripts - Validation
// Useful javascript functions.
// This particular one is for validating forms.
//
// Added by Neil Gibbons, 23.01.03

	// Check a given string against an email pattern
	function isEmailString (InString)
	{
		if(InString.length==0) return (false);
		var filter 	= /^.+@.+\..{2,3}$/;
	
		return (filter.test(InString));
	}

	// Check a string for any spaces, tabs, or newline chars
	function IsBlank(s)
	{
		// For each characer in the string
		for(var i = 0; i < s.length; i ++)
		{
			// Get the character
			var c = s.charAt(i);
			
			// If the characters empty, a newline or a tab, return false
			if ((c != ' ') && (c != '\n') && (c != '\t'))
			{
				return false;
			}
		}
		return true;
	}
	
	// Validates given check boxes
	function validateForm()
	{
		// Initialise vars
		var strMsg = "";
		var dodgyFields = "";
		var temp_one = "";
		var temp_two = "";
		var temp_three = "";
		var temp_four = "";
		var tempPassword = "";
				
		// Get passed parameters
		var passed_args = validateForm.arguments; 
		
		// For each element/passed parameter
		for(i = 0; i < passed_args.length; i++)
		{
		
			// Check for text boxes
			if (passed_args[i].type == "text" || passed_args[i].type == "password" || passed_args[i].type == "textarea")
			{
				// Add a new line to the message if the value of the element is empty, null or starts with a blank character 
				if ((passed_args[i].value == null) || (passed_args[i].value == "") || IsBlank(passed_args[i].value))
				{
						dodgyFields += "\n" + passed_args[i].name;
				}	
				
				// Check the email field for validity
				if (passed_args[i].name == "email")
				{
					var EmailVar = isEmailString (passed_args[i].value);
					if (EmailVar == 0)
					{	
						dodgyFields += "\nEmail address appears incorrect";
					}	
				}
			}
			
			if (passed_args[i].name == "password")
			{
				tempPassword = passed_args[i].value;
			}
			
			
			// check if the password field matches the passwordConfirm field
			if (passed_args[i].name == "passwordConfirm")
			{
				if (passed_args[i].value != tempPassword)
				{
					dodgyFields += "\nYour password and password confirmation fields do not match";				
				}
			
			}

			// Check the the agree radio button has been checked to Yes
			/*if (typeof(passed_args[i]) == "object" && passed_args[i].length > 0)
			{
				if (!passed_args[i][0].checked)
				{
					dodgyFields += "\n\nPlease confirm that the information is correct and you\nhave read and agree to the Terms of Business.";
				}
			}*/
			
			// Store date if there
			var tempString = ""
			var tempString = passed_args[i].name
			if (tempString != null)
			{
				if (tempString.indexOf("Mediatype") != -1)
				{
					temp_three += passed_args[i].options[passed_args[i].selectedIndex].value;					
				}
				
				if (tempString.indexOf("country") != -1)
				{
					temp_four += passed_args[i].options[passed_args[i].selectedIndex].value;					
				}				
			}			
		}
			
		
		// Check country
		if (temp_four == "0")
		{
			dodgyFields += "\n\nPlease make sure you have chosen\nyour appropriate country.";
		}
		
		// Display message if theres anything in it and return false
		if (dodgyFields) 
		{			
			strMsg = "_________________________________________________\n\n";
			strMsg += "The form was not submitted due to error(s).\n";
			strMsg += "Please check that you provided the correct information\nfor the following and re-submit.\n";				
			strMsg += "_________________________________________________\n";						
			strMsg += dodgyFields;
			alert(strMsg);
			return false;
		} else {
			// Otherwise return true
			return true;
		}
	}
