  function calculateAge(day, month, year)
  { // Calculates the age, given a date
    var thisDate=new Date();
    var thisYear=thisDate.getFullYear();
    var thisMonth=Number(thisDate.getMonth()) + 1;
    var thisDay=Number(thisDate.getDate());
    month=Number(month);
    day=Number(day);
    var age=thisYear-year;
    if (thisMonth>month) {return age;}
    if (thisMonth<month) {return age-1;}
    if (thisDay>=day) {return age;}
    else {return age-1;}
  }

  function checkDate(d,m,y)
  { // Checks for valid date in range 1/1/1900 to 31/12/2099
    if (!isNumeric(d) || !isNumeric(m) || !isNumeric(y)) {return false;}
        if (m<1 || m>12) {return false;}
        if (y<1900 || y>2099) {return false;}
    var daysInMonth=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
        if (y%1000==0 ||(y%4==0 && y%100!=0)) {daysInMonth[1]=29;}
        if (d<1 || d>daysInMonth[m-1]) {return false;}
        return true;
  }


  function checkEmail(email)
  { // this checks for a valid format email address
    var pattern=/^[a-zA-Z0-9]+([.|_|-][a-zA-Z0-9]+)*[@]{1}[a-zA-Z0-9]+([.|_|-][a-zA-Z0-9]+)*[.]{1}[a-zA-Z0-9]{2,3}$/;
    var result=email.match(pattern);
    if (result==null)
      {
       return false;
      }
      else  return true;
  }


  function checkForm(f)
  {
//  alert ("Here in checkForm");
//  alert ("Name=" + f.name);

/*   To test the chosen option in a Select statement:
              var opt=f.selectname.selectedIndex;
                  var field=f.selectname.options[opt].text;

 (Can use value instead of text if the option statements have a value clause)
*/
    if (f.buttonPressed.value=="Cancel") {return true;}
    if (f.buttonPressed.value=="Delete")
    {
      if (confirm("Are you really sure you want to delete this record?"))
      {return true;}
      else {return false;}
    }
    var errMsg="";
    switch (f.name)
    {
      case "contactForm":
        if (empty (f.company.value))
        {
           if (errMsg=="")  {f.company.focus();}
           errMsg+="Please enter your Company Name\n";
        }

        if (empty (f.address.value))
        {
           if (errMsg=="")  {f.address.focus();}
           errMsg+="Please enter your Address\n";
        }

        if (empty (f.contactName.value))
        {
           if (errMsg=="")  {f.contactName.focus();}
           errMsg+="Please enter your Contact Name\n";
        }

        if (empty (f.position.value))
        {
           if (errMsg=="")  {f.position.focus();}
           errMsg+="Please enter your Position\n";
        }

        if (empty (f.phone.value))
        {
           if (errMsg=="")  {f.phone.focus();}
           errMsg+="Please enter your Telephone number\n";
        }

        if (empty (f.email.value))
        {
           if (errMsg=="")  {f.email.focus();}
           errMsg+="Please enter your Email address\n";
        }
        else
        {
          if (!checkEmail (f.email.value))
          {
           if (errMsg=="")  {f.email.focus();}
           errMsg+="That Email address is not valid\n";
          }
        }

        /*
        if (empty (f.comments.value))
        {
           if (errMsg=="")  {f.comments.focus();}
           errMsg+="Please enter details of your Enquiry\n";
        }

        if (empty (f.auditAddresses.value))
        {
           if (errMsg=="")  {f.auditAddresses.focus();}
           errMsg+="Please enter your auditAddresses\n";
        }

        if (empty (f.fax.value))
        {
           if (errMsg=="")  {f.fax.focus();}
           errMsg+="Please enter your fax\n";
        }

        if (empty (f.spare4.value))
        {
           if (errMsg=="")  {f.spare4.focus();}
           errMsg+="Please enter your spare4\n";
        }
*/
      break;

      case "clientForm":
        if (empty(f.clientName.value))
        {
          if (errMsg=="") {f.clientName.focus();}
          errMsg+="Please enter the Client Name\n";
        }

        if (empty(f.address.value))
        {
          if (errMsg=="") {f.address.focus();}
          errMsg+="Please enter the Client Company Address\n";
        }

        if (empty(f.country.value))
        {
          if (errMsg=="") {f.country.focus();}
          errMsg+="Please enter the Country\n";
        }

        if (f.status.value=="Accredited" || f.status.value=="In process of Re-accreditation")
        {
          if (!validateDate(f, "accreditationDate"))
          {
            if (errMsg=="") {f.accreditationDateDay.focus();}
            errMsg+="That Accreditation Date is not a valid date\n";
          }
        }


        if (empty(f.contactName.value))
        {
          if (errMsg=="") {f.contactName.focus();}
          errMsg+="Please enter the Contact Name\n";
        }

        if (empty(f.contactPhone.value) && empty(f.contactEmail.value))
        {
          if (errMsg=="") {f.contactPhone.focus();}
          errMsg+="Please enter either the Contact Telephone Number or the Contact Email Address\n";
        }

        if (!empty(f.contactEmail.value) && !checkEmail(f.contactEmail.value))
        {
          if (errMsg=="") {f.contactEmail.focus();}
          errMsg+="The Contact Email is not a valid email address\n";
        }

        /*
        if (empty(f.field10.value))
        {
          if (errMsg=="") {f.field10.focus();}
          errMsg+="Please enter your field10\n";
        }
        */

      break;

      case "faqForm":
        if (empty(f.question.value))
        {
          if (errMsg=="") {f.question.focus();}
          errMsg+="Please enter the Question\n";
        }

        if (empty(f.answer.value))
        {
          if (errMsg=="") {f.answer.focus();}
          errMsg+="Please enter the Answer\n";
        }

        if (!empty(f.sequence.value) && !isNumeric(f.sequence.value))
        {
          if (errMsg=="") {f.sequence.focus();}
          errMsg+="If entered, the Sequence No. must be numeric\n";
        }

        /*
        if (empty(f.field05.value))
        {
          if (errMsg=="") {f.field05.focus();}
          errMsg+="Please enter the field05\n";
        }

        if (empty(f.field06.value))
        {
          if (errMsg=="") {f.field06.focus();}
          errMsg+="Please enter the field06\n";
        }
        */
      break;

      case "newsForm":
        if (empty(f.title.value))
        {
          if (errMsg=="") {f.title.focus();}
          errMsg+="Please enter the Title of the Item\n";
        }

        if (empty(f.newsText.value))
        {
          if (errMsg=="") {f.newsText.focus();}
          errMsg+="Please enter the Text of the Item\n";
        }

        if (!validateDate(f, "newsDate"))
        {
          if (errMsg=="") {f.newsDateDay.focus();}
          errMsg+="That Date is not a valid date\n";
        }

        if (!empty(f.sequence.value) && !isNumeric(f.sequence.value))
        {
          if (errMsg=="") {f.sequence.focus();}
          errMsg+="If entered, the sequence must be numeric\n";
        }

        /*
        if (empty(f.contact.value))
        {
          if (errMsg=="") {f.contact.focus();}
          errMsg+="Please enter the contact\n";
        }
        */

      break;

      default:
      return true;
    }

    if (errMsg!="")
    {
      alert(errMsg);
      return false;
    }
    else return true;
  }


  function checkPass(pass)
  { // This checks password is 6-12 chars, starts with a letter and contains
    // only alphanumerics

        if (pass.length<6 || pass.length>12) {return false;}
        var pattern=/^[a-zA-Z][a-zA-Z0-9]{0,11}$/;
    var result=pass.match(pattern);
    if (result==null)
      {
       return false;
      }
      else  return true;
  }

  function checkPhoneNo(phone)
  { // This checks a phone number contains at least 11 digits. It
    // ignores blanks and hyphens
        var p=phone.replace(/ |-/g,""); // Remove blanks or hyphens
        if (p.length<11 || !isNumeric(p)) {return false;}
        else {return true;}
  }

  function checkTime(h,m)
  {
    if (!isNumeric(h) || !isNumeric(m)) {return false;}
        if (h<0 || h>23) {return false;}
        if (m<0 || m>59) {return false;}
        return true;
  }

  function checkURL(URL)
  { // this checks for a valid format URL
    // NB this does not check anything beyond the basic address - ie no parameters
    var pattern=/^(http:\/\/)?[a-zA-Z0-9]+([.|_|-][a-zA-Z0-9]+)*[.]{1}[a-zA-Z0-9]{2,3}/;
    var result=URL.match(pattern);
    if (result==null)
      {
       return false;
      }
      else  return true;
  }

  function empty(s)
  {
    if (s=="" || s==null || isblank(s)) {return true;}
        else {return false;}
  }

  function formatDecimal(number)
  { // This takes a number and converts it to a string with 2 decimal places. If it has more than
    // 2 places to start with, it will not be changed
    var stringValue=String(number);
    var point=stringValue.search(/\./);
    if (point==-1) {stringValue+=".00";}
    else
    {
      if (point==stringValue.length-1) {stringValue+="00";}
      else
      {
        if (point==stringValue.length-2) {stringValue+="0";}
      }
    }
    return stringValue;
  }

  function generateRandomNo(x, y)
  { // This function generates a random integer between x & y
    var range = y - x + 1;
    return Math.floor(Math.random() * range) + x;
  }

  function getElement(id)
  {
    if (document.getElementById) {var elmt=document.getElementById(id);}
    else
    {
      if (document.all) {var elmt=document.all[id];}
          else
          {
            if (document.layers) {var elmt=document.layers[id];}
            else elmt=false;
          }
    }
    if (!elmt) {alert ("Not found");}
    return elmt;
  }

  function isblank (s)
  { // Returns false if field contains any non-blank chars other than new line
    for (var i=0; i<s.length; i++) {
      var c=s.charAt(i);
          if (c!=" " && c!="\n" && c!="") {
          return false;
          }
    }
    return true;
  }


  function isNumeric (number, decPlaces)
  { // This function checks for numerics with a maximum of decPlaces decimal places
    var pattern="/^[0-9]+$/";
    if (decPlaces!='0')
    { // Decimal places allowed
      var dPlaces=2;
      var strPlaces=String(decPlaces);
      var result=strPlaces.match(pattern);
      if (result)  {dPlaces=decPlaces;}
      pattern=eval("/^[0-9]+([.][0-9]{1," + dPlaces + "})?$/");
    }
    var result=number.match(pattern);
    return result;
  }


  function makeTime(f, timeName)
  { // Returns a string containng the time in hh:mm form
    var opt=eval("f." + timeName + "Hour.selectedIndex");
        var hour=eval("f." + timeName + "Hour.options[opt].text");
        opt=eval("f." + timeName + "AmPm.selectedIndex");
        var amPm=eval("f." + timeName + "AmPm.options[opt].text");
        opt=eval("f." + timeName + "Minutes.selectedIndex");
        var minutes=eval("f." + timeName + "Minutes.options[opt].text");
        if (amPm=="pm" && hour!=12) {hour=Number(hour)+12;}
        else
        {
          if (hour<10) {hour="0"+hour;}
        }
        if (minutes<10) {minutes="0" + minutes;}
        return hour+":"+minutes;
  }


  function newWindow(url, w, h)
  { // Opens URL w in a new window positioned top left and ensures it has the focus
    if (!w) {var wide=400;} else {wide=w;}
        if (!h) {var high=450;} else {high=h;}
        var paras="width=" + wide + ",height=" + high + "align=center scrollbars=yes, left=0, top=0, screenx=0, screeny=0";
    var x=window.open(url, "newWindow", paras);
        x.window.focus();
  }

  function validateDate(f, dateName)
  {
        var opt=eval("f." + dateName + "Day.selectedIndex");
        var day=eval("f." + dateName + "Day.options[opt].text");
        opt=eval("f." + dateName + "Month.selectedIndex");
        var month=eval("f." + dateName + "Month.options[opt].value");
        opt=eval("f." + dateName + "Year.selectedIndex");
        var year=eval("f." + dateName + "Year.options[opt].text");
        return checkDate(day, month, year);
  }

  function validateSlashDate(ddmmyy)
  {
    var dateArray=ddmmyy.split("/");
        var d=dateArray[0];
        var m=dateArray[1];
        var y=dateArray[2];
        if (empty(d) || empty(m) || empty(y)) {return false;}
        var valid=checkDate(d, m, y);
        return valid;
  }

  function showNewsDiv(divName)
  {
    var d=getElement(divName);
    d.className="showNewsDiv";
  }

  function hideNewsDiv(divName)
  {
    var d=getElement(divName);
    d.className="hideNewsDiv";
  }