<!--

var reAlphabetic = /^[a-zA-Z \-\/\&\.\,\']+$/					// Alphabetic - [a to z, A - Z, space, - , /] matches the preceding characters 1 or more times
var reAlphanumeric = /^[a-zA-Z 0-9\-\/\,\n\r\&\.\']+$/			// Alphanumeric - [a to z, A - Z, space, 0 to 9, -, /] matches the preceding characters 1 or more times
var reNumeric = /^[0-9 ]+$/								// Numeric - [0 to 9, space] matches the preceding characters 1 or more times
var reInteger = /^[0-9]+$/								// Integer - [0 to 9] matches the preceding characters 1 or more times
var reEmail = /^.+\@.+\..+$/
var reEmailName = /^((([a-zA-Z\-0-9\.]{1,})|\.\-)+([\w\'\"])+)$/
//'
var reEmailDomain = /^([a-zA-Z0-9_-])+((([a-zA-Z\-0-9]{1,})+\.)+([a-zA-Z]{2,})+)$/
var rePostCode = /^(GIR\s?0AA|[A-Za-z]{1,2}(\d{1,2}|\d[A-Za-z])\s?\d[A-Za-z][A-Za-z])$/ //Matches purely on letter and number locations with/without a space, not excluded characters. Allowed formats are : GIR 0AA, AN NAA, ANN NAA, AAN NAA, AANN NAA, ANA NAA, AANA NAA (where A = alpha n = number)
var reYear = /^[1-2]?[089]?[0-9][0-9]$/					// Year - beginning with 1 or 2, second digit 0, 8 or 9 and then any two digits
var dteNow = new Date();
var theYear = dteNow.getFullYear();

function trim(string) {
	// This function trims out any spaces
    for (var i=0, output='', valid=" "; i<string.length; i++)
       if (valid.indexOf(string.charAt(i)) == -1)
         output += string.charAt(i)
    return output;
}

function callAlert()	{
	if (bitFncRequired == true)	{
		if (strValue.length == 0)	{
			alert(strFncAlert + " cannot be left blank.")
			objFncToValidate.focus()
			return false;
		}
	}
	
	if (bitExpTest == false && (bitFncRequired == true || (bitFncRequired == false && strValue.length > 0)))	{
		alert(strAlertMsg)
		objFncToValidate.focus()
		return false;
	}
	return true
}

// isIntegerInRange tests whether an integer passed into the procedure as 's' is between the range
// greater than a and less than b. If 's' is not an integer the procedure returns false, if the value
// of 's' is preceded by 0 it is stripped from the value before testing if it lies between a and b.
function isIntegerInRange(s, a, b) {
	if (!reInteger.test(s)) return false;
		if ((s.length == 2) && (s.substring(0, 1) == '0')) {
			s = s.substring(1);
		}
	var num = parseInt (s);
	return ((num >= a) && (num <= b));
}

// isDate checks that the three values strDay, strMonth and strYear will form a valid date.
// If any of these values are preceded by 0 then it is first stripped from the value before
// the test is carried out.
function isDate(strDay, strMonth, strYear) {
	switch(strMonth)
	{
		case 'January':
			strMonth = '1';
			break;
		case 'February':
			strMonth = '2';
			break;
		case 'March':
			strMonth = '3';
			break;			
		case 'April':
			strMonth = '4';
			break;			
		case 'May':
			strMonth = '5';
			break;			
		case 'June':
			strMonth = '6';
			break;			
		case 'July':
			strMonth = '7';
			break;			
		case 'August':
			strMonth = '8';
			break;			
		case 'September':
			strMonth = '9';
			break;			
		case 'October':
			strMonth = '10';
			break;			
		case 'November':
			strMonth = '11';
			break;			
		case 'December':
			strMonth = '12';
			break;			
	}
	if (strDay.substring(0, 1) == '0') {
		strDay = strDay.substring(1);
	}
	if (strMonth.substring(0, 1) == '0') {
		strMonth = strMonth.substring(1);
	}
	var CheckDate = strDay;
	var CheckMonth = strMonth;
	var CheckYear = strYear;

	if (((isNaN(CheckDate)) || (isNaN(CheckMonth))) || (isNaN(CheckYear))) {
		return false;
	}
	var mydate = new Date(parseInt(CheckYear), (parseInt(CheckMonth) - 1), parseInt(CheckDate));

	if (mydate.getFullYear() != parseInt(CheckYear)) {
		return false;
	}
	if (mydate.getMonth() != (parseInt(CheckMonth) - 1)) {
		return false;
	}
	if (mydate.getDate() != parseInt(CheckDate)) {
		return false;
	}
	return true;
}

function isFieldValidAndFocus(objToValidate, strType, bitRequired, bitTrim, strAlert)	{
	if (bitTrim == true)	{ objToValidate.value = trim(objToValidate.value)	}
	
	strValue = objToValidate.value
	strAlertMsg = ""
	bitFncRequired = bitRequired
	strFncAlert = strAlert	 
	objFncToValidate = objToValidate
	switch(strType)
	{
		case "numeric":
			bitExpTest = reNumeric.test(objToValidate.value);
			strAlertMsg = strFncAlert + " must be a numeric value."
			return callAlert();
			break;
			
		case "alpha":
			bitExpTest = reAlphabetic.test(objToValidate.value);
			strAlertMsg = strFncAlert + " must only contain alphabetic characters."
			return callAlert();
			break;
			
		case "email":
			bitExpTest = reEmail.test(objToValidate.value);
			strAlertMsg = strFncAlert + " must be a valid email address."
			return callAlert();
			break;
			
		case "EmailName":	
			bitExpTest = reEmailName.test(objToValidate.value);
			strAlertMsg = strFncAlert + " must be valid eg. Joe.Bloggs"
			return callAlert();
			break;
		
		case "EmailDomain":	
			bitExpTest = reEmailDomain.test(objToValidate.value);
			strAlertMsg = strFncAlert + " must be valid. eg myDomain.co.uk"
			return callAlert();
			break;
		case 'Day':
			bitExpTest = (isIntegerInRange(objToValidate.value, 1, 31) == true);
			strAlertMsg = strFncAlert + " must be a valid day of the month.";
			return callAlert();
			break;
		case 'Month':
			bitExpTest = (isIntegerInRange(objToValidate.value, 1, 12) == true);
			strAlertMsg = strFncAlert + " must be a valid month in the year.";
			return callAlert();
           	break;
		case 'Year':
 			if (objToValidate.value.length == 2)
           {
				if ((objToValidate.value.substring(0, 1) == '0') || (objToValidate.value.substring(0, 1) == '1'))
             	{
					objToValidate.value = '20' + objToValidate.value;
				}
 				else
             	{
  					objToValidate.value = '19' + objToValidate.value;
	  			}
			}
			
			if ((reYear.test(objToValidate.value) == true) && (objToValidate.value.length != '3') && (isIntegerInRange(objToValidate.value, 1900, parseInt(theYear) + 5)))
           {
				var k = 0;
 				while ((objToValidate.name != objToValidate.form.elements[k].name) && (k < objToValidate.form.elements.length)) 
             	{
  					++k;
				}
 				if (isDate(objToValidate.form.elements[k-2].value, objToValidate.form.elements[k-1].value, objToValidate.value)) 
             	{
	  				bitExpTest = true;
				}
				else 
             	{
	  				bitExpTest = false;
			  		strAlertMsg = strFncAlert + " must be a valid date.";
 				}	
  			}
	  		else 
           {
				bitExpTest = false;
		  		strAlertMsg = strFncAlert + " must be a valid four digit year.";
 			}
			return callAlert();
			break;
		case 'Postcode':
            bitExpTest = (rePostCode.test(objToValidate.value) == true);
			strAlertMsg = strFncAlert + " must be a valid postcode.";
            return callAlert();
			break;		
	}
	
	return true;
}

//-->
