// JavaScript Document
function check_field_numeric(theField)
{
	if (IsNumeric(theField.value)) {
		theField.style.border = "1px solid #5E7DB1";
		return 0;
	} else {
		theField.style.border = "2px solid #a50101";
		return 1;	
	}
}

function IsNumeric(strString)
//  check for valid numeric strings	
{
   var strValidChars = "0123456789.-,";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

function check_field_empty(theField)
{
	if (theField.value.length == 0) {		
		theField.style.border = "2px solid #a50101";
		return 1;
	} else {
		theField.style.border = "1px solid #5E7DB1";
		return 0;
	}	
}

function check_field_notselected(theField)
{
	if (theField.value == 0) {
		theField.style.border = "2px solid #a50101";
		return 1;
	} else {
		theField.style.border = "1px solid #5E7DB1";
		return 0;
	}	
}

/*
 * Function edited by Kevin Dew June 2007
 */ 
function check_invalid_email(theField)
{
	if (!testEmail(theField.value)) {
		theField.style.border = "2px solid #a50101";
		return 1;
	} else {
		theField.style.border = "1px solid #5E7DB1";
		return 0;
	}		
}

//function to test an email address
//string input of the email address
function testEmail(email)
{
	var regExp = /^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]{1,64}@([a-z0-9.!\#$%&\'*+-/=?^_`{|}~]){1,255}$/i;
	if(!regExp.test(email)) return false;
	var emailParts = email.split("@");
	//check email doesn't have dots at the start of domain bit
	regExp = /^[^\.](.+)[^\.]$/;
	if(!regExp.test(emailParts[1])) return false;
	//check we have atleast two parts (like yahoo.com)
	var domainParts = emailParts[1].split(".");
	if(domainParts.length < 2) return false;
	return true;
}

/*
 * Function to test a field is of a length between the min and max values provided as arguments
 */
function check_field_length(theField, minLength, maxLength)
{
	if((theField.value.length < minLength || minLength == 0) || (theField.value.length > maxLength || maxLength == 0))
	{
		theField.style.border = "2px solid #a50101";
		return 1;
	}
	else
	{
		theField.style.border = "1px solid #5E7DB1";
		return 0;
	}
		
}

/*
 * function to check the postcode field and call the testPostcode function
 */
function check_invalid_postcode(theField)
{
	if (!testPostcode(theField.value)) {
		theField.style.border = "2px solid #a50101";
		return 1;
	} else {
		theField.style.border = "1px solid #5E7DB1";
		return 0;
	}		
}

/*
 * Function to test a postcode to check it's valid
 */
function testPostcode(postcode)
{
	postcode = postcode.toUpperCase();

	//weird one
	if(postcode == "GIR 0AA" || postcode == "GIR0AA" || postcode == "GIR-0AA") return true;

	//reg exp test
	var regExp = /^([A-Z]{1,2}[0-9]{1,2}|[A-Z]{1,2}[0-9][A-Z])( |-)?[0-9][A-Z]{2}$/i;
	return regExp.test(postcode);
}

/*
 * Function to display error messages 
 * Creates a list of errors and displays them with a p explaining in the provided div
 * Argument array of string error messages
 * Returns true if it's ok false if the preliminary tests fail
 */
function addErrors(divId, errorArr)
{
	if(!document.getElementsByTagName || !document.createElement || !document.getElementById(divId)) return false;
	//make sure array isn't blank
	if(!errorArr.length) return false;

	//make error message
	var p = document.createElement("p");
	p.appendChild(document.createTextNode("The form could not be submit because there are errors. Please amend these and resubmit."));

	var lis = [];

	//build list items
	for(var i=0; i<errorArr.length; i++)
	{
		var li = document.createElement("li");
		li.appendChild(document.createTextNode(errorArr[i]));
		lis.push(li);
	}
	
	//make a list and append li's
	var ul = document.createElement("ul");
	for(i=0; i<lis.length; i++)
	{
		ul.appendChild(lis[i]);
	}
	
	//add elements to doc
	if(document.getElementById(divId))
	{
		document.getElementById(divId).appendChild(p);
		document.getElementById(divId).appendChild(ul);
	}
	return true;
}

/*
 * Function to remove the error messages of form failing
 * Removes all p and ul elements 
 * Returns false if preliminary tests fail returns true on completion
 */
function removeErrors(divId)
{
	if(!document.getElementsByTagName || !document.createElement || !document.getElementById(divId)) return false;
	
	var errorDiv = document.getElementById(divId);

	var ps = errorDiv.getElementsByTagName('p');
	var uls = errorDiv.getElementsByTagName('ul');

	//if there are elements remove them
	if(ps)
	{
		for(var i=0; i<ps.length; i++)
			errorDiv.removeChild(ps[i]);
	}
	if(uls)
	{
		for(var i=0; i<uls.length; i++)
			errorDiv.removeChild(uls[i]);
	}

	return true;
}