<!--
// INCLUDE FILE FOR JAVASCRIPT VALIDATION

/**
* variable for error messages
*/
  var errorMessage = "";


/**
* check if a value has been set for a field
*
* @param string s field value
*
* @return boolean - false if no value
*/
  function IsSet(s) {
    var len=s.length
    if(len>1) return true;
    errorMessage = " must have a value entered.";
    return false;


  } // end isSet


/**
* check if a field is blank
*
* @param string s field value
*
* @return boolean - false if not blank
*/
  function IsBlank(s) {
    var len=s.length
    var i
    for(i=0;i<len;++i) {
      if(s.charAt(i)!=" ") return false;
    }
    errorMessage = " must have a value entered.";
    return true;


  } // end isBlank


/**
* check if a select box has a value selected
*
* @param string s field value
*
* @return boolean - false if no vlaue selected
*/
  function IsSelected(s) {
    if((s == "null") || (s.substring(0,4) == "null") ){
      errorMessage = " must have a value selected";
      return false;
    }
    return true;


  } // end isSelected


/**
* Start check date functions
*/

/**
* strip characters from a string
*
* @param string s field value
* @param string bag characters to be stripped from string
*
* @return string
*/
  function stripCharsInBag(s, bag){
	  var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
      var c = s.charAt(i);
      if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;


  } // end stripCharsInBag


/**
* find the number of days in february for a given year
*
* @param integer year
*
* @return integer
*/
  function daysInFebruary (year){
	  // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
   return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );


  } // end daysInFebruary


/**
* set the number of days in a month
*
* @param integer month
*
* @return integer
*/
  function DaysArray(n) {
	  for (var i = 1; i <= n; i++) {
		  this[i] = 31
		  if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		  if (i==2) {this[i] = 29}
    }
    return this


  } // end daysArray


/**
* Check if a field is a valid date.
* If required is false then field may be blank or a valid date
* If required is false then field may only be a valid date
*
* @param string s field value
* @param boolean required
*
* @return boolean - false if blank or not a valid email address
*/
  function IsDate(s,required){
    if (required == true && s==""){
      errorMessage = " must have a value entered.";
      return false;
    } else if (s!=""){
      var divider= "/";
      var minYear=1900;
      var maxYear=2100;
      var daysInMonth = DaysArray(12)
      var pos1=s.indexOf(divider)
      var pos2=s.indexOf(divider,pos1+1)
      var strDay=s.substring(0,pos1)
      var strMonth=s.substring(pos1+1,pos2)
      var strYear=s.substring(pos2+1)
      strYr=strYear
      if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
      if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
      for (var i = 1; i <= 3; i++) {
        if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
      }
      month=parseInt(strMonth)
      day=parseInt(strDay)
	    year=parseInt(strYr)
	    if (pos1==-1 || pos2==-1){
		    errorMessage = ": The date format should be : dd/mm/yyyy";
		    return false
      }
      if (strMonth.length<1 || month<1 || month>12){
        errorMessage = ": Please enter a valid month";
        return false
      }
	    if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		    errorMessage = ": Please enter a valid day";
        return false
      }
      if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		    errorMessage = ": Please enter a valid 4 digit year between "+minYear+" and "+maxYear;
		    return false
      }
	    if (s.indexOf(divider,pos2+1)!=-1 || IsNumbers(stripCharsInBag(s, divider))==false){
		    errorMessage = ": Please enter a valid date";
		    return false
	    }
    }
    return true


  } // end isDate


/**
* End check date functions
*/


/**
* Check if a field value is a minimum length.
* If required is false then field may be blank
*
* @param string s field value
* @param integer minLength
* @param boolean required
*
* @return boolean - false if blank or less than length permitted
*/
  function IsMinLength(s, minLength,required){
    if (required==true && s==""){
      errorMessage = " must have a value entered.";
      return false
    } else if (s!=""){
      if(s.length < minLength) {
        errorMessage = " is less than the minimum length permitted.";
        return false;
      }
    }
    return true;


  } // end isMinLength


/**
* Check if a field value is a maximum length.
* If required is false then field may be blank
*
* @param string s field value
* @param integer maxLength
* @param boolean required
*
* @return boolean - false if blank or greater than length permitted
*/
  function IsMaxLength(s, maxLength,required){
    if (required==true && s==""){
      errorMessage = " must have a value entered.";
      return false
    } else if (s!=""){
      if(s.length > maxLength) {
        errorMessage = " is greater than the maximum length permitted.";
        return false;
      }
    }
    return true;


  } // end isMaxLength


/**
* Check if a field matches a regular expression.
* If required is false then field may be blank
*
* @param string s field value
* @param string userRegExp
* @param boolean required
*
* @return boolean - false if blank or doesn't match regular expression
*/
  function IsRegExp(s, userRegExp,required){
    if (required==true && s==""){
      errorMessage = " must have a value entered.";
      return false
    } else if (s!=""){
      if (s.search(userRegExp) != -1){
        return true;
      } else {
        errorMessage = " is not in the correct format.";
        return false;
      }
    }
    
    
  } // end isRegExp


/**
* Check if a field is a valid UK postcode.
* If required is false then field may be blank
*
* @param string s field value
* @param boolean required
*
* @return boolean - false if blank or not a valid postcode
*/
  function IsPostcode(s,required) {
    if (required==true && s==""){
      errorMessage = " must have a value entered.";
      return false;
    } else if (s!=""){
      RegExp1 = /^[A-Za-z]{1,2}[1-9]{1}(\d{0,1}|[A-Za-z]{0,1}) \d{1}[A-Za-z]{2}$/;
      if ( s.search(RegExp1) != -1 ) {
        return true;
      } else {
        errorMessage = " is not a valid postcode.";
        return false;
      }
    }
    return true;


  } // end isPostcode


/**
* Check if a field value contains only *.
*
* @param string s field value
*
* @return boolean - true if field value contain's only *
*/
  function IsStars(s) {
    var len=s.length
    var i
    for(i=0;i<len;++i) {
      if(s.charAt(i)!="*") return false;
    }
    errorMessage = " is not permitted to be all stars.";
    return true;


  } // end isStars

/**
* Check if a field value contains all the same character.
*
* @param string s field value
* @param boolean required
*
* @return boolean - true if field value blank or contains all the same character
*/
  function IsSameChar(s,required) {
    if (required==true && s==""){
      errorMessage = " must have a value entered.";
      return true;
    } else if (s!=""){
      var FirstChar = s.substr(0,1)
      var len=s.length
      var i
      if(len == 0) return false;
      for(i=0;i<len;++i) {
        if(s.charAt(i)!= FirstChar) return false;
      }
      errorMessage = " is not permitted to be all the same character.";
      return true;
    }
    return false;


  } // end isSameChar


/**
* Check if a field value contains only permitted values.
*
* @param string s field value
* @param string type type of values permitted
*
* @return boolean - false if field value contains restricted characters.
*/
  function IsChars(s,type) {

    if (type == "alphaOnly"){
      var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
    } else if (type == "alphaNumeric"){
      var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
    } else if (type == "alphaNumericUnderScore"){
      var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
    } else if (type == "alphaNumericWhitespace"){
      var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 \t\r\n\f";
    } else {
      var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-()'*&,. \t\r\n\f";
    }
    var checkStr = s;
    var allValid = true;
    for (i = 0;  i < checkStr.length;  i++) {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j)) {
	 			break;
	 		}
      if (j == checkOK.length) {
        allValid = false;
        break;
      }
    }
    if (!allValid) {
      errorMessage = ": an illegal character has been entered";
      return false;
    }
    return true;


  } // end isChar


/**
* Check if a field value contains only numbers
*
* @param string s field value
*
* @return boolean - false if field value contains non-numeric characters.
*/
  function IsNumbers(s) {
    var checkOK = "0123456789";
    var checkStr = s;
    var allValid = true;
    for (i = 0;  i < checkStr.length;  i++) {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
      if (j == checkOK.length) {
        allValid = false;
        break;
      }
    }
    if (!allValid) {
      errorMessage = ": non-numeric value entered";
      return false;
    }
    return true;


  }// end is Numbers


/**
* Check if a field has a value selected. Used for radio and checkboxes
*
* @param string TheField field name
*
* @return boolean - false if no value selected for field.
*/
  function IsChecked(TheField) {
    var radioSelected = false;
    for (i = 0;  i < TheField.length;  i++) {
      if (TheField[i].checked)
        radioSelected = true;
    }
    if (!radioSelected) {
      errorMessage = " must have a value selected.";
      return false;
    }
    return true;


  } // end isChecked


/**
* Check if a field value is a valid email address.
*
* @param string emailStr field value
* @param boolean required
*
* @return boolean - false if blank or invalid email address
*/
  function IsEmail(emailStr,required) {  	
    if (required==true && emailStr.length==0){
      errorMessage = " must have a value entered.";
      return false;
    } else if (emailStr!=""){
    	
      /* The following variable tells the rest of the function whether or not
      to verify that the address ends in a two-letter country or well-known
      TLD.  1 means check it, 0 means don't. */

      var checkTLD=1;

      /* The following is the list of known TLDs that an e-mail address must end with. */

      var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|uk|nhs.uk)$/;

      /* The following pattern is used to check if the entered e-mail address
      fits the user@domain format.  It also is used to separate the username
      from the domain. */

      var emailPat=/^(.+)@(.+)$/;

      /* The following string represents the pattern for matching all special
      characters.  We don't want to allow special characters in the address.
      These characters include ( ) < > @ , ; : \ " . [ ] */

      var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

      /* The following string represents the range of characters allowed in a
      username or domainname.  It really states which chars aren't allowed.*/

      var validChars="\[^\\s" + specialChars + "\]";

      /* The following pattern applies if the "user" is a quoted string (in
      which case, there are no rules about which characters are allowed
      and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
      is a legal e-mail address. */

      var quotedUser="(\"[^\"]*\")";

      /* The following pattern applies for domains that are IP addresses,
      rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
      e-mail address. NOTE: The square brackets are required. */

      var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

      /* The following string represents an atom (basically a series of non-special characters.) */

      var atom=validChars + '+';

      /* The following string represents one word in the typical username.
      For example, in john.doe@somewhere.com, john and doe are words.
      Basically, a word is either an atom or quoted string. */

      var word="(" + atom + "|" + quotedUser + ")";

      // The following pattern describes the structure of the user

      var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

      /* The following pattern describes the structure of a normal symbolic
      domain, as opposed to ipDomainPat, shown above. */

      var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

      /* Finally, let's start trying to figure out if the supplied address is valid. */

      /* Begin with the coarse pattern to simply break up user@domain into
      different pieces that are easy to analyze. */

      var matchArray=emailStr.match(emailPat);

      if (matchArray==null) {

        /* Too many/few @'s or something; basically, this address doesn't
        even fit the general mould of a valid e-mail address. */
        errorMessage = ": email address seems incorrect (check @ and .'s)";
        return false;
      }
      var user=matchArray[1];
      var domain=matchArray[2];

      // Start by checking that only basic ASCII characters are in the strings (0-127).

      for (i=0; i<user.length; i++) {
        if (user.charCodeAt(i)>127) {
          errorMessage = ": this username contains invalid characters.";
          return false;
        }
      }
      for (i=0; i<domain.length; i++) {
        if (domain.charCodeAt(i)>127) {
          errorMessage = ": this domain name contains invalid characters.";
          return false;
        }
      }

      // See if "user" is valid

      if (user.match(userPat)==null) {

        // user is not valid
        errorMessage = ": the username doesn't seem to be valid.";
        return false;
      }

      /* if the e-mail address is at an IP address (as opposed to a symbolic
      host name) make sure the IP address is valid. */

      var IPArray=domain.match(ipDomainPat);
      if (IPArray!=null) {
        // this is an IP address

        for (var i=1;i<=4;i++) {
          if (IPArray[i]>255) {
            errorMessage = ": the destination IP address is invalid!";
            return false;
          }
        }
        return true;
      }

      // Domain is symbolic name.  Check if it's valid.

      var atomPat=new RegExp("^" + atom + "$");
      var domArr=domain.split(".");
      var len=domArr.length;
      for (i=0;i<len;i++) {
        if (domArr[i].search(atomPat)==-1) {
          errorMessage = ": the domain name does not seem to be valid.";
          return false;
        }
      }

      /* domain name seems valid, but now make sure that it ends in a
      known top-level domain (like com, edu, gov) or a two-letter word,
      representing country (uk, nl), and that there's a hostname preceding
      the domain or country. */

      if (checkTLD && domArr[domArr.length-1].length!=2 &&
      domArr[domArr.length-1].search(knownDomsPat)==-1) {
        errorMessage = ": the address must end in a well-known domain or two letter " + "country.";
        return false;
      }

      // Make sure there's a host name preceding the domain.

      if (len<2) {
        errorMessage = ": this address is missing a hostname!";
        return false;
      }
    }
    // If we've gotten this far, everything's valid!
    return true;


  } // end isEmail


/**
* Check if a field value is a valid URL
*
* @param string s field value
* @param boolean required
*
* @return boolean - false if field value not a valid URL
*/
  function IsURL(s,required) {
    if (required==true && s==""){
      errorMessage = " must have a value entered.";
      return false;
    } else if (s!=""){
      errorMessage = ": is an invalid web address.";
      if (s.indexOf(" ") != -1){
        return false;
      } else if (s.indexOf("http://") == -1) {
        return false;
      } else if (s == "http://") {
        return false;
      } else if (s.indexOf("http://") > 0){
        return false;
      }
      s = s.substring(7, s.length);
      if (s.indexOf(".") == -1) {
        return false;
      } else if (s.indexOf(".") == 0) {
        return false;
      } else if (s.charAt(s.length - 1) == "."){
        return false;
      }
      if (s.indexOf("/") != -1) {
        s = s.substring(0, s.indexOf("/"));
        if (s.charAt(s.length - 1) == ".")
          return false;
      }

      if (s.indexOf(":") != -1) {
        if (s.indexOf(":") == (s.length - 1)) {
          return false;
        } else if (s.charAt(s.indexOf(":") + 1) == "."){
          return false;
        }
        s = s.substring(0, s.indexOf(":"));
        if (s.charAt(s.length - 1) == ".")
          return false;
      }

    }
    return true;


  }// end isURL
-->
