// JavaScript Document


// SG JavaScript Form Validator © 2007 Studio Gecko Interactive. All Rights Reserved.
// Version 1.1
// Type: Temporary Generic Contact Form
// Warning: Bloated Script. Created in a hurry.
// Designed for use with the XHTML 1.0 Strict DTD
// Author: Andrei Gonzales and Jenn Huang
// Studio Gecko: http://www.studio-gecko.com
// This script is covered under the Creative Common's licence.
// Feel free to use and share this script on your website(s),
// but please do not resell nor claim this script as your own.
// If you are to modify this script, please do not state that
// it is still the work of Studio Gecko Interactive.
// However, we would appreciate a link back to the original file

//JavaScript Begins Here

//var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;
var noNumbers = /[0-9]/;
var noSpecChars = /[^-A-Za-z]/;
var noChars = /[^+0-9]/

// Step 1: Select Your Package
// Script controls the behavior of the package choices

// Form Validation Script
function validate()
{
	valid = true; 
	
	// Generic Text Fields
	// Empty Field Checker
	// Empty Fields Are Given A Message Value To Display, Informing The User Which Data Are Missing
	// Required Empty Fields Are Recolored

	// Checks The Surname Field
	if ((document.forms['contact_form'].surname.value == "") || (document.forms['contact_form'].surname.value == "Please Enter a Surname"))
	{
		document.forms['contact_form'].surname.value = "Please Enter a Surname"; 		// To change the message, simply replace the text within the quotation marks**
		document.forms['contact_form'].surname.style.backgroundColor = "#FEFFEC"; 		// To change the background, simply replace the hexadecimal value
		valid = false; 																	// *Note: If the message is changed, the one stated in the "if" line should be				
																						// changed to match as well
	}  else {																			// The code below filters out numbers from the surname field
	if (document.forms['contact_form'].surname.value.match(noNumbers && noSpecChars))
		{
			document.forms['contact_form'].surname.value = "Please Enter a Surname";
			document.forms['contact_form'].surname.style.backgroundColor = "#FEFFEC";
			valid = false;
		}
	}
	
	// Checks The Name(First Name) Field
	if ((document.forms['contact_form'].name.value == "") || (document.forms['contact_form'].name.value == "Please Enter a Name"))
	{
		document.forms['contact_form'].name.value = "Please Enter a Name";
		document.forms['contact_form'].name.style.backgroundColor = "#FEFFEC";
		valid = false;
	}  else { 																			// The code below filters out numbers from the name field
	if (document.forms['contact_form'].name.value.match(noNumbers && noSpecChars))
		{
			document.forms['contact_form'].name.value = "Please Enter a Name";
			document.forms['contact_form'].name.style.backgroundColor = "#FEFFEC";
			valid = false;
		}
	}
	
	
	// Checks the Message Text Box Field
	if ((document.forms['contact_form'].message.value == "") || (document.forms['contact_form'].message.value == "Please Enter a Message"))
	{
		document.forms['contact_form'].message.value = "Please Enter a Message";
		document.forms['contact_form'].message.style.backgroundColor = "#FEFFEC";
		valid = false;

	}


	// Checks if the Email is in a valid email format ("characters_and_numbers@characters_and_numbers.characters_and_numbers" and so on)
	if ((document.forms['contact_form'].email.value == "") || (document.forms['contact_form'].email.value == "Please Enter an Email"))
		{
			document.forms['contact_form'].email.value = "Please Enter an Email";
			document.forms['contact_form'].email.style.backgroundColor = "#FEFFEC";
			valid = false;
		} else {
		
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.forms['contact_form'].email.value))) 
		{
			document.forms['contact_form'].email.value = "Please Enter a Valid Email";
			document.forms['contact_form'].email.style.backgroundColor = "#FEFFEC";
			valid = false;
		}
	}
 
	return valid;
}



// Returns Empty Required Fields To Their Original Background Color When The Field Is Clicked
// Field Name Requires "onfocus" event
// Example: <name="fieldset_name" onfocus="baseStyle('fieldset_name');" />
function baseStyle(passValue)
{
	switch(passValue) {																	// Command which activates the case switches
	// Resets The Surname Field															// case switches are named according to the fields which they apply to
	case 'surname': if (document.forms['contact_form'].surname.value == "Please Enter a Surname")	
	{																					// The above code activates when the surname field is clicked, and when
		document.forms['contact_form'].surname.style.backgroundColor="#FFFFFF";			// it contains the text value "Please Enter a Surname"
		document.forms['contact_form'].surname.value = "";								// The code does not apply when the text value in the field is valid
	}; break;

	// Resets The Name Field
	case 'name': if (document.forms['contact_form'].name.value == "Please Enter a Name")
	{
		document.forms['contact_form'].name.style.backgroundColor="#FFFFFF";
		document.forms['contact_form'].name.value = "";
	}; break;

	// Resets The Message Field
	case 'message': if (document.forms['contact_form'].message.value == "Please Enter a Message")
	{
		document.forms['contact_form'].message.style.backgroundColor="#FFFFFF";
		document.forms['contact_form'].message.value = "";
	}; break;
	
	// Resets The Email Field
	case 'email': if ((document.forms['contact_form'].email.value == "Please Enter an Email") || (document.forms['contact_form'].email.value == "Please Enter a Valid Email"))
	{
		document.forms['contact_form'].email.style.backgroundColor="#FFFFFF";
		document.forms['contact_form'].email.value = "";
	}; break;
	}
}


// Resets All Fields
// Returns All Field Styles To The Original Base Style
// This funcition must be activated by the a JavaScript attribute of the "reset" button in the XHTML document
// Example using the onclick command: <input type="reset" name="CLEAR" value="CLEAR" onclick="clearFields();"/>

function clearFields()
	{
		document.forms['contact_form'].surname.style.backgroundColor="#FFFFFF";
		document.forms['contact_form'].name.style.backgroundColor="#FFFFFF";
/*		document.forms['contact_form'].subject.style.backgroundColor="#FFFFFF";*/
		document.forms['contact_form'].estamount.style.backgroundColor="#FFFFFF";
		document.forms['contact_form'].message.style.backgroundColor="#FFFFFF";
		document.forms['contact_form'].email.style.backgroundColor="#FFFFFF";
	}