if (document.layers) navigator.family = "nn4";
if (document.all) navigator.family = "ie4";
if (window.navigator.userAgent.toLowerCase().match("gecko")) navigator.family = "gecko";

function checkRequestForm(thisForm)
{
	alertDisplay = "";
	notEmpty = new Array("first_name","last_name","email");
	spans = new Array();
	errors = new Array();
	for (i = 0; i < notEmpty.length; ++i) {
		if (thisForm[notEmpty[i]].value.length < 2) {
			spans[spans.length] = "span" + notEmpty[i]
			errors[errors.length] = "\n - " + splitName("_",notEmpty[i], true);
		} else {
			if (navigator.family == "nn4")
				document["span" + notEmpty[i]].style.color = "green";
			else if (navigator.family == "ie4")
				document.all["span" + notEmpty[i]].style.color = "green";
			else if (navigator.family == "gecko")
				document.getElementById("span" + notEmpty[i]).style.color = "green";
		}
	}
	if (errors.length > 0) {
		for (i = 0; i < spans.length; ++i) {
			if (navigator.family == "nn4")
				document[spans[i]].style.color = "red";
			else if (navigator.family == "ie4")
				document.all[spans[i]].style.color = "red";
			else if (navigator.family == "gecko")
				document.getElementById(spans[i]).style.color = "red";
		}
				
		alertDisplay += "The following fields need to be filled in:\n" + errors;
		if (validEmail(thisForm.email.value) != "") {
			alertDisplay += "\n\nE-mail Address errors: \n" + validEmail(thisForm.email.value);
			if (navigator.family == "nn4")
				document["spanemail"].style.color = "red";
			else if (navigator.family == "ie4")
				document.all["spanemail"].style.color = "red";
			else if (navigator.family == "gecko")
				document.getElementById("spanemail").style.color = "red";
		}
	}
	
	if (alertDisplay != "") {
		alert(alertDisplay);
		return false;
	}
}		

function splitName(delim, value, upper)
{
	valueArray = value.split(delim);
	finalName = "";
	for (x = 0; x < valueArray.length; ++x) {
		if (upper)
			valueArray[x] = toTitleCase(valueArray[x]);
		finalName += valueArray[x] + " ";
	}
	finalName = finalName.substring(0,finalName.length -1);

	return finalName;
}

function toTitleCase(word)
{
	firstLetter = word.charAt(0);
	firstLetter = firstLetter.toUpperCase();
	word = word.substring(1,word.length);
	word = firstLetter + word;
	return word;
}


/*******************************************************

 * ADVANCED FORM VALIDATION:
 *
 * usage: <FORM onSubmit="isOK(this, 'name; email','Please fill in your name; Please enter a valid E-Mail Address')">
 *
 * NOTE: each form element must have a corresponding label with an id that is equal to the first two letters of the element name,
 *			eg, <SPAN ID="em">E-Mail: </SPAN><INPUT TYPE="text" NAME="email">
 *
 *	If one of the fields' name is "email", then the script will automatically call validEmail(String email) for errors.
 *
 * @author justin.beere@macsteel.co.za
  
********************************************************/
function isOK(thisForm, fields, errors) {
	
	fields = fields.split("; ");
	errors = errors.split("; ");
	
	//var set = setStatus("There are " + fields.length + " fields, and " + error.length + " error messages");
	
	messages = new Array();
	formelements = new Array();
	spanelements = new Array();
	
	for (i = 0; i < fields.length; ++i) {
		formelements[formelements.length] = addFormElement(thisForm, fields[i]);
		spanelements[spanelements.length] = addSpanElement(fields[i]);
	}
	
	for (i = 0; i < spanelements.length; ++i)
		spanelements[i].style.color = "green";
	
	for (i = 0; i < formelements.length; ++i) {
		if (formelements[i].value.length == 0 && errors[i]) {
			messages[messages.length] = "\n - " + errors[i];
			spanelements[i].style.color = "red";
		} else if (formelements[i].name == "email") {
			formelements[i].value = formelements[i].value.toLowerCase();
			emailstuffups = validEmail(formelements[i].value);
			if (emailstuffups != "") {
				messages[messages.length] = "\nYour email address is invalid for the following reason(s) : " + emailstuffups;
//				spanelements[i].style.color = "red";
			}
		}
	}
	
	if (messages.length > 0) {
		alert("Please correct the following errors" + messages);
		return false;
	} else {
		return true;
	}
	return false;
}

function addFormElement(formName, fieldName) {
	
	if (formName[fieldName]) {
		return formName[fieldName];
	} else {
		return false;
	}
}

function addSpanElement(fieldName) {
	provSpanName = fieldName.substring(0, 2);
	if (navigator.family == "nn4")
		return document.layers[provSpanName];
	else if (navigator.family == "ie4")
		return document.all[provSpanName];
	else if (navigator.family == "gecko")
		return document.getElementById(provSpanName);
}

function validEmail(emailString) {

	// this should only happen if there's something written in the email field
	if (emailString != "") {
		mailerrors = new Array();
		// these are the characters that may not be in an email string
		invalidChars = " /:,;"

		//search for invalid characters in the string
		for (i = 0; i <invalidChars.length; i++) {
			badChar = invalidChars.charAt(i)

			// place any errors into a string
			if (emailString.indexOf(badChar, 0) > -1) {
				mailerrors[mailerrors.length] = "\n - You have used invalid characters [ /:,;] in your e-mail address";
			}
		}
	
		// search for an @ symbol
		atPos = emailString.indexOf("@",1)
		if (atPos == -1) {
			mailerrors[mailerrors.length] = "\n - You have no @ symbol in your e-mail address";
		}
	
		// search for a second @ symbol
		if (emailString.indexOf("@",atPos+1) > -1) {
			mailerrors[mailerrors.length] = "\n - You have too many @ symbols in your e-mail address";
		}

		// search for a period (.) after the @ symbol
		periodPos = emailString.indexOf(".",atPos) 
		if (periodPos == -1) {
			mailerrors[mailerrors.length] = "\n - An e-mail address should have a period (full-stop) somewhere after the @ symbol";
		}

		//make sure the first period after the @ symbol is not less than 2 characters away from the end of the address
		if (periodPos + 3 > emailString.length) {
			mailerrors[mailerrors.length] = "\n - An e-mail address will always have a period more than 1 letter away from the end of the address";
		}
		return mailerrors;
	} else {
		setStatus("Mail Fine");
		return "";
	}
}

function setStatus(message) {
	window.status = message;
	return true;
}

