// Checks For JPG
function IsJpg(cFormField)
{

	var cSuffix = cFormField.value.substr((cFormField.value.length-4),4);

	if(cSuffix.toLowerCase() != '.jpg') 
	{
		alert("You can only upload images with a 'jpg' extension"); 
		return false;
	}

	return true;

}


// Check For Data Inputed
function IsEmpty(aTextField) {

   if ((aTextField.value.length==0) || (aTextField.value==null)) {
      return true;
   }
   else { return false; }

}


// Check if Null
function IsNull(aTextField) {

   if ((aTextField.value.length==0) || (aTextField.value==null)) {
      return false;
   }
   else { return true; }

}

// Check For numeric Chars only.
function IsNumeric(sText)
{

	var IsOK = true;
	var sValidChars = "0123456789. ";
	IsOK = isNaN(sText);
	return IsOK;

}

// Check For Decimal Chars only.
function IsDecimalInteger(sText)
{
	var IsOK = true;
	var sValidChars = "0123456789.";
	IsOK = searchCharacterList(sText, sValidChars);
	return IsOK;
}

// Check For Capital Alphanumeric Chars.
function IsCapAlphanumericOrSpace(sText)
{
	var IsOK = true;
	var sValidChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
	IsOK = searchCharacterList(sText, sValidChars);
	return IsOK;
}

// Check for all Alpha Chars.
function IsAlphanumericOrSpace(sText)
{
	var IsOK = true;
	var sValidChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
	IsOK = searchCharacterList(sText, sValidChars);
	return IsOK;
}


// Check if Chars valid for Username
function IsUsername(sText)
{
	var IsOK = true;
	var sValidChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_";
	IsOK = searchCharacterList(sText, sValidChars);
	return IsOK;
}

// Password Chars.
function IsPassword(sText)
{
	var IsOK = true;
	var sValidChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_#";
	IsOK = searchCharacterList(sText, sValidChars);
	return IsOK;
}

// Valid Date
function IsDateDDMMYYYY(sText)
{

	if (sText.substr(2, 1) != '/') { return false; }
	if (sText.substr(5, 1) != '/') { return false; }
	if (sText.length != 10) { return false; }

	var sValidDayChars = "0123456789";
	if (!searchCharacterList(sText.substr(0, 2), sValidDayChars)) { return false; }

	var sValidMonthChars = "0123456789";
	if (!searchCharacterList(sText.substr(3, 2), sValidMonthChars)) { return false; }

	var sValidYearChars = "0123456789";
	if (!searchCharacterList(sText.substr(6, 4), sValidYearChars)) { return false; }

	return true;

}

// Check for Rude Words.
function IsRudeText(sText)
{

	var IsOK = false;
	var aPartialWord = new Array('fuck', 'shit', 'arse');
	// Words in aFullWord must be standalone words
	var aFullWord = new Array('cunt');
	var j;

	sText = ' '+ sText.toLowerCase() +' ';	// Need to find words at start & end of text

	for (j = 0; j < aPartialWord.length && IsOK == false; j++) 
	{
		if (sText.search(aPartialWord[j]) > 0) { IsOK = true; }
	}

	for (j = 0; j < aFullWord.length && IsOK == false; j++) 
	{
		if (sText.search(aFullWord[j] + ' ') > 0) { IsOK = true; }
	}

	return IsOK;

}

// Search a specific Chars List.
function searchCharacterList(sText, sValidChars)
{

   var IsOK = true;
   var Char, i;

   for (i = 0; i < sText.length && IsOK == true; i++) 
   {

      Char = sText.charAt(i); 

      if (sValidChars.indexOf(Char) == -1) 
      {
         IsOK = false;
      }

   }

   return IsOK;

}


// Valid Email.
function IsValidEmail(str) {

	var IsOK = true;
	var atpos=str.indexOf("@");
	var dotpos=str.lastIndexOf(".");
	var space=str.indexOf(" ");

	if (atpos<1 || dotpos<atpos+2 || dotpos+2>=str.length || space>0)
 	{
 		//	 alert("Not a valid e-mail address");
 	 //	IsOK = false;
 	
  	}

	var emailPattern = /^.+\@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
 	var testa=emailPattern.test(str);

   return testa;

}

// Valid Postcode.
function IsPostcode(str) {

	var IsOK = true;

	test = str; size = test.length
	test = test.toUpperCase(); //Change to uppercase

	while (test.slice(0,1) == " ") //Strip leading spaces
	{
		test = test.substr(1,size-1);size = test.length
	}
	while(test.slice(size-1,size)== " ") //Strip trailing spaces
 	{
		test = test.substr(0,size-1);size = test.length
	}

	// document.details.pcode.value = test; //write back to form field
	if (size < 5 || size > 8){ //Code length rule
 		// alert(test + " is not a valid postcode - wrong length");
		IsOK = false;
  	}
	if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
  	 	//alert(test + " is not a valid postcode - cannot start with a number");
		IsOK = false;

	}
	if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
 		//alert(test + " is not a valid postcode - alpha character in wrong position");
		IsOK = false;

	}
	if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
  		//alert(test + " is not a valid postcode - number in wrong position");
		IsOK = false;

	}
	if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
		// alert(test + " is not a valid postcode - number in wrong position");
		IsOK = false;

	}

	count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
	if (count1 != count2){//only one space rule
 		 // alert(test + " is not a valid postcode - only one space allowed");
		IsOK = false;

	}

	return IsOK;

}

// Valid UK Telephone
function checkUKTelephone (telephoneNumber) {

	// Convert into a string and check that we were provided with something
	var telnum = telephoneNumber + " ";
	if (telnum.length == 1)  {
		telNumberErrorNo = 1;
     	return false
  	}
  	telnum.length = telnum.length - 1;
  
  	// Don't allow country codes to be included (assumes a leading "+")
  	var exp = /^(\+)[\s]*(.*)$/;
  	if (exp.test(telnum) == true) {
  	   telNumberErrorNo = 2;
  	   return false;
  	}
  
  	// Remove spaces from the telephone number to help validation
  	while (telnum.indexOf(" ")!= -1)  {
  	  telnum = telnum.slice (0,telnum.indexOf(" ")) + telnum.slice (telnum.indexOf(" ")+1)
  	}
  
 	 // Remove hyphens from the telephone number to help validation
  	while (telnum.indexOf("-")!= -1)  {
  	  telnum = telnum.slice (0,telnum.indexOf("-")) + telnum.slice (telnum.indexOf("-")+1)
  	}  
  
  	// Now check that all the characters are digits
  	exp = /^[0-9]{10,11}$/;
  	if (exp.test(telnum) != true) {
  	   telNumberErrorNo = 3;
  	   return false;
  	}
  
  	// Now check that the first digit is 0
  	exp = /^0[0-9]{9,10}$/;
  	if (exp.test(telnum) != true) {
   	  telNumberErrorNo = 4;
   	  return false;
  	}
	
	// Disallow numbers allocated for dramas.
	 
  	// Array holds the regular expressions for the drama telephone numbers
  	var tnexp = new Array ();
	tnexp.push (/^(0113|0114|0115|0116|0117|0118|0121|0131|0141|0151|0161)(4960)[0-9]{3}$/);
	tnexp.push (/^02079460[0-9]{3}$/);
	tnexp.push (/^01914980[0-9]{3}$/);
	tnexp.push (/^02890180[0-9]{3}$/);
	tnexp.push (/^02920180[0-9]{3}$/);
	tnexp.push (/^01632960[0-9]{3}$/);
	tnexp.push (/^07700900[0-9]{3}$/);
	tnexp.push (/^08081570[0-9]{3}$/);
	tnexp.push (/^09098790[0-9]{3}$/);
	tnexp.push (/^03069990[0-9]{3}$/);
	
	for (var i=0; i<tnexp.length; i++) {
    	if ( tnexp[i].test(telnum) ) {
     		telNumberErrorNo = 5;
      		return false;
    	}
	}
  
  	// Finally check that the telephone number is appropriate.
  	exp = (/^(01|02|03|05|070|071|072|073|074|075|07624|077|078|079)[0-9]+$/);
		if (exp.test(telnum) != true) {
   		  	 telNumberErrorNo = 5;
    		 return false;
  		}
  
  	// Telephone number seems to be valid - return the stripped telehone number  
  	return telnum;

}

// Valid UK Mobile Number.
function checkUKMobile (telephoneNumber) {

  	// Convert into a string and check that we were provided with something
  	var telnum = telephoneNumber + " ";
  	if (telnum.length == 1)  {
   	  telNumberErrorNo = 1;
  	   return false
  	}
  	telnum.length = telnum.length - 1;
  
  	// Don't allow country codes to be included (assumes a leading "+")
  	var exp = /^(\+)[\s]*(.*)$/;
  	if (exp.test(telnum) == true) {
  	   telNumberErrorNo = 2;
  	   return false;
  	}
  
  	// Remove spaces from the telephone number to help validation
  	while (telnum.indexOf(" ")!= -1)  {
  	  telnum = telnum.slice (0,telnum.indexOf(" ")) + telnum.slice (telnum.indexOf(" ")+1)
  	}
  
  	// Remove hyphens from the telephone number to help validation
  	while (telnum.indexOf("-")!= -1)  {
  		  telnum = telnum.slice (0,telnum.indexOf("-")) + telnum.slice (telnum.indexOf("-")+1)
  	}  
  
  	// Now check that all the characters are digits
  	exp = /^[0-9]{10,11}$/;
  	if (exp.test(telnum) != true) {
  	   telNumberErrorNo = 3;
  	   return false;
  	}
  
  	// Now check that the first digit is 0
  	exp = /^0[0-9]{9,10}$/;
  	if (exp.test(telnum) != true) {
  	   telNumberErrorNo = 4;
  	   return false;
  	}
	
	// Disallow numbers allocated for dramas.
	 
  	// Array holds the regular expressions for the drama telephone numbers
  	var tnexp = new Array ();
	tnexp.push (/^(0113|0114|0115|0116|0117|0118|0121|0131|0141|0151|0161)(4960)[0-9]{3}$/);
	tnexp.push (/^02079460[0-9]{3}$/);
	tnexp.push (/^01914980[0-9]{3}$/);
	tnexp.push (/^02890180[0-9]{3}$/);
	tnexp.push (/^02920180[0-9]{3}$/);
	tnexp.push (/^01632960[0-9]{3}$/);
	tnexp.push (/^07700900[0-9]{3}$/);
	tnexp.push (/^08081570[0-9]{3}$/);
	tnexp.push (/^09098790[0-9]{3}$/);
	tnexp.push (/^03069990[0-9]{3}$/);
	
	for (var i=0; i<tnexp.length; i++) {
   		if ( tnexp[i].test(telnum) ) {
      		telNumberErrorNo = 5;
      		return false;
    	}
	}
  
  	// Finally check that the telephone number is appropriate.
  	exp = (/^(070|071|072|073|074|075|07624|077|078|079)[0-9]+$/);
	if (exp.test(telnum) != true) {
     	telNumberErrorNo = 5;
     	return false;
  	}
  
  	// Telephone number seems to be valid - return the stripped telehone number  
  	return telnum;

}


// Possible Telephone Errors.
var telNumberErrorNo = 0;
var telNumberErrors = new Array ();
telNumberErrors[0] = "Valid UK telephone number";
telNumberErrors[1] = "Telephone number not provided";
telNumberErrors[2] = "UK telephone number without the country code, please";
telNumberErrors[3] = "UK telephone numbers should contain 10 or 11 digits";
telNumberErrors[4] = "The telephone number should start with a 0";
telNumberErrors[5] = "The telephone number is either invalid or inappropriate";














