<!--// JavaScript Document - validation functions
///////////////////////////////////////////BASIC VALIDATION CODE///////////////////////////////////////////
var msg = [['Please enter your '], ['Please enter a valid ', 'Please enter a valid email address'], ['Illegal character sequence in ', 'Illegal character in ']];
var space = ' ';  var sQuote = "'";  var message = '';

function noSQL(theValue, fieldName)//Checks that a field has no obvious SQL code or spam characters.
{
 var illegalChars = '¬¦`!""“”£$€%^&*()=+[]{};:^#~\|<>?¼½¾'
 for (var i = 0; i < theValue.length; i++)
 {for (var j=0; j < illegalChars.length; j++)
  {if (theValue.charAt(i) == illegalChars.charAt(j)) {message = msg[2][1] + fieldName + ': ' + theValue.charAt(i);}
	 if ((theValue.charAt(i) == sQuote)&&(theValue.charAt(i+1) == sQuote)){message = msg[2][0] + fieldName + ': ' + sQuote + ' followed by ' + sQuote;}
	 if ((theValue.charAt(i) == sQuote)&&(theValue.charAt(i+1) == space)){message = msg[2][0] + fieldName + ': ' + "'" + ' followed by space';}
	 if (theValue.charAt(theValue.length-1) == sQuote){message = msg[2][1] + fieldName + ': ' + sQuote + ' at end of theValue.';}
  }
 }
}

function isTelephone(theValue, fieldName, minimum)//Checks that a phone number is numerical and at least minimum length defined.
{
 var illegal = 0; var legal = '0123456789 ';
 for (var i = 0; i < theValue.length; i++) {if (legal.indexOf(theValue.charAt(i)) == -1) {illegal = illegal + 1;}}
 if (illegal != 0) {message = msg[1][0] + fieldName;}
 if (theValue.length < minimum) {message = msg[1][0] + fieldName;}
}

function isEmail(email)//checks that an email address has an @ and a . in the right place.
{
 var legal = 0;
 if ((email.charAt(email.length - 3) != '.') && (email.charAt(email.length - 4) != '.')) {message = msg[1][1]}
 for (var i = 0; i < email.length; i++) {if (email.charAt(i) == '@') {legal = legal + 1;}}
 if (legal != 1) {message = msg[1][1];}
}

function maxLength(txtArea, theLength)//Ensures that a text area only takes the correct character maximum.
{if (txtArea.value.length > theLength) {txtArea.value = txtArea.value.substring(0, theLength);}}

///////////////////////////////////////////DATABASE VALIDATION CODE///////////////////////////////////////////
function doReplace(theElement)//Ensures that entries are database safe.
{
 var theElement1, theElement2;
 //Replaces any allowed but database unrecognised/reserved characters with a safe version.
 theElement.value = theElement.value.replace(/&/g, '&amp;').replace(/‘|’|'/g, '&#39;').replace(/“|”|"/g, '&quot;').replace(/£/g, '&pound;').replace(/€/g, '&euro;');
 for (j = 0; j < theElement.value.length; j++) 
 {if (theElement.value.charAt(j) == '+'){theElement1 = theElement.value.slice(0,j); theElement2 = theElement.value.slice(j+1); theElement1 = theElement1+'&#43;'; theElement.value = theElement1 + theElement2; theElement1 = ''; theElement2 = '';}}
 //Makes sure any html tags are stored correctly.
 for (k = 0; k < theElement.value.length; k++)
 {if (theElement.value.charAt(k) == '<')
  {theElement1 = theElement.value.slice(0,k); theElement2 = '<'+theElement.value.slice(k+1).replace(/&quot;/g, '"').replace(/&#39;/g, "'"); theElement.value = theElement1 + theElement2; theElement1 = ''; theElement2 = '';}
  else if (theElement.value.charAt(k) == '>')
  {theElement1 = theElement.value.slice(0,k); theElement2 = '>'+theElement.value.slice(k+1).replace(/"/g, '&quot;').replace(/‘|’|'/g, '&#39;'); theElement.value = theElement1 + theElement2; theElement1 = ''; theElement2 = '';}
 }
}
-->