
/***********************************************
* Required field(s) validation v1.10- By NavSurf
* Visit Nav Surf at http://navsurf.com
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function formCheck(formobj)
{

	var required = document.getElementsByClassName("required");	// The class name that this script looks for
	
	var alert_message_text = "Please complete the following fields:\n";		// This message is displayed before the list of problematic fields
	var fieldRequired = Array();								// List of mandatory fields
	var fieldDescription = Array();								// List of field descriptions/labels (to appear in error message)
	var mesg = "";												// The list of problematic fields
	
	// Create a list of all the form fields that are marked as "required",
	// and another list to contain their label (for use in the error message)
	
	for (var i=0; i< required.length; i++)
	{
		var field = getElementsByTagNames("input,select,textarea",required[i]);
		var label = required[i].getElementsByTagName("LABEL");
		
		if (field.length > 0 && label.length > 0)
		{
			fieldRequired.push(field[0]);
			fieldDescription.push(stripHtml(label[0].innerHTML));
		}
	}
	
	// Iterate through all the required fields, checking which fields are empty
	for (var i=0; i<fieldRequired.length; i++)
	{
		var obj = fieldRequired[i];
		var text = "";
		
		if (obj != "")
		{
			if (fieldIsEmpty(obj))
			{
				mesg += " - " + fieldDescription[i] + "\n";
			}
		}
	}
	
	// Otherwise, if the spam check has been passed and there is no message, then it is okay
	if (mesg == "")
	{
		if (spamCheck(formobj))
			return true;
	}
	else
	{
		mesg = alert_message_text + mesg;
		alert(mesg);
		return false;
	}
	
	return false;
}

// Function that checks whether fields are empty

function fieldIsEmpty(obj)
{
	switch(obj.type)
	{
		case "select-one":
			if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "" || obj.value == "none")
				return true;
			break;
		case "select-multiple":
			if (obj.selectedIndex == -1)
				return true;
			break;
		case "text":
		case "textarea":
			if (obj.value == "" || obj.value == null)
				return true;
			break;
		default:
	}
	
	return false;
}


function spamCheck(formobj) 
{

	var spamfield = "antispam";
	
	var obj = formobj.elements[spamfield];
	
	if (obj) 
	{
		
		if (obj.value == "")
		{
			alert("Please enter the word 'yes' into the 'Help us fight spam!' field to submit your enquiry.");
			return false;
		}
		
		else if (obj.value.indexOf("yes") >= 0 || obj.value.indexOf("YES") >= 0)
		{
			formobj.elements["spamaddress"].value = "false";
			return true;
		}
		else
		{	
			formobj.elements["spamaddress"].value = "true";
			return true;
		}	
	}
	else {
		return true;
	}

}



/*********************************
* getElementsByTagNames
*
* Nicked from Quirksmode
* http://www.quirksmode.org/dom/getElementsByTagNames.html
*********************************/


function getElementsByTagNames(list,obj) {

	if (!obj) var obj = document;
	var tagNames = list.split(',');
	var resultArray = new Array();
	for (var i=0;i<tagNames.length;i++) {
		var tags = obj.getElementsByTagName(tagNames[i]);
		for (var j=0;j<tags.length;j++) {
			resultArray.push(tags[j]);
		}
	}
	var testNode = resultArray[0];
	if (!testNode) return [];
	if (testNode.sourceIndex) {
		resultArray.sort(function (a,b) {
				return a.sourceIndex - b.sourceIndex;
		});
	}
	else if (testNode.compareDocumentPosition) {
		resultArray.sort(function (a,b) {
				return 3 - (a.compareDocumentPosition(b) & 6);
		});
	}
	return resultArray;
}



/*********************************
* Strip HTML Tags from String

*********************************/


function stripHtml(s) 
{
     s = s.replace(/(<([^>]+)>)/ig,""); 
     s = s.replace(/\*/,""); 
     s = s.replace(/:/,""); 
     return s;
}



