function validDate(y, m, d) { 
// m = 0..11 ; y m d integers, y!=0
  with (new Date(y, m, d))
    return (getMonth()==m && getDate()==d);
}

function getKeyChar(theKeyPress)
{
  var key = window.event ? window.event.keyCode : theKeyPress.which;
  return String.fromCharCode(key);
}

function digitEntered(keychar)
{
  reg = /\d/;
  return (reg.test(keychar) || keychar =='\t')?"":"Only digits are valid in this field.";
}

function phoneEntered(keychar)
{
  return (digitEntered(keychar)=="" || keychar=="-" || keychar==" ")?"":"Only digits, hyphens and spaces are valid in this field.";
}

function ucAlphaEntered(keychar)
{
  return (keychar>='A' && keychar <='Z' || keychar =='\t')?"":"Only uppercase alphabetic characters are valid in this field.";
}

function lcAlphaEntered(keychar)
{
  return (keychar>='a' && keychar <='z' || keychar =='\t')?"":"Only lowercase alphabetic characters are valid in this field.";
}

function alphaEntered(keychar)
{
  return (lcAlphaEntered(keychar)=="" || ucAlphaEntered(keychar)=="")?"":"Only alphabetic characters are valid in this field.";
}

function postCodeEntered(keychar)
{
  return (ucAlphaEntered(keychar)=="" || digitEntered(keychar)=="" || keychar==" ")?"":"Only uppercase letters, digits and spaces are valid in this field.";
}

function nameCharEntered(keychar)
{
  return (alphaEntered(keychar)=="" || keychar=="." || keychar=="," || keychar=="-" || keychar=="'" || keychar==" ")?"":"Only alphabetic characters, plus \".\", \"'\", \" \" and \"-\" are valid in this field.";
}

function schoolNameCharEntered(keychar)
{
  return (nameCharEntered(keychar)=="" || keychar=="+" || keychar=="&" || keychar==",")?"":"Only alphabetic characters, plus \".\", \"'\", \" \", \"+\", \",\", \"&\" and \"-\" are valid in this field.";
}

function addressCharEntered(keychar)
{
  return (schoolNameCharEntered(keychar)=="" || digitEntered(keychar)=="" || keychar=="#" || keychar=="/")?"":"Only alphabetic and numeric characters, plus \".\", \"'\", \" \", \"+\", \"&\", \"#\", \"/\" and \"-\" are valid in this field.";
}

function backspaceEntered(keychar)
// are there other keys that should be included? e.g. Del?
{
  var key = window.event ? window.event.keyCode : keychar.which;
  return key==8 || key==9 || key==127;
}
 
function preventKeyPress(theEvent, validationFunction) {
  var keychar = getKeyChar(theEvent);
  var errorMessage = validationFunction(keychar)
  if (errorMessage=="" || backspaceEntered(theEvent)) {
    return true;
  } else {
    alert(errorMessage);
    return false;
  }
}

function validate_email(emailAddress) {
// suggested pattern if desired is: [A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}
  strEmail = String(emailAddress);
  strInvalid = '';
  if (strEmail.length==0) {
    strInvalid = "empty";
  } else if (strEmail.indexOf("@")<0) {
    strInvalid = "does not contain '@'";
  } else if (strEmail.indexOf('"')>-1) {
    strInvalid = "should not contain a double-quote";
  } else if (strEmail.indexOf("'")>0) {
    strInvalid = "should not contain an apostrophe";
  } else if (strEmail.indexOf("@")==0) {
    strInvalid = "cannot begin with '@'";
  } else if (strEmail.indexOf("@")==strEmail.length - 1) {
    strInvalid = "cannot end with '@'";
  } else if (strEmail.indexOf(".")<0) {
    strInvalid = "does not contain '.'";
  } else if (strEmail.indexOf(".")==0) {
    strInvalid = "cannot begin with '.'";
  } else if (strEmail.lastIndexOf(".")==strEmail.length - 1) {
    strInvalid = "cannot end with '.'";
  } else if (strEmail.substring(strEmail.indexOf("@")+1).indexOf("@")>-1) {
    strInvalid = "cannot contain more than one '@'";
  } else if (strEmail.indexOf(".@")>-1) {
    strInvalid = "cannot contain '.@'";
  } else if (strEmail.indexOf("@.")>-1) {
    strInvalid = "cannot contain '@.'";
  } else if (strEmail.substring(strEmail.indexOf("@")+1).indexOf(".")<0) {
    strInvalid = "domain hierarchy has no delimiter ('.')";
  } else if (strEmail.indexOf(" ")>-1) {
    strInvalid = "contains a space";
  }
  return strInvalid;
}

function validateField(fieldName, oFunction) {
//  if (document.getElementById(fieldName) == null) { alert("Fieldname: " + fieldName + " not found."); }
  var response2 = "";
  strValue = String(document.getElementById(fieldName).value);
  for (i=0; i<strValue.length; i++) {
    if (oFunction(strValue.charAt(i))!="") {
      response2 = response2 + "There is an invalid character " + strValue.charAt(i) + " in field " + String(fieldName).replace("txt", "") + ". " + oFunction(strValue.charAt(i)) + "\n";
    }
  }
  return response2;
}

function validateFieldList(fieldNameList, oFunction) {
  for (j=0; j<fieldNameList.length; j++) {
    var response1 = validateField(fieldNameList[j], oFunction);
    if (response1!="") {
       alert(response1);
       break;
    }
  }
  return response1=="";
}

function validateCompositeDate(cboDayName, cboMonthName, txtYearName) {
cboDay = document.getElementById(cboDayName)
cboMonth = document.getElementById(cboMonthName)
txtYear = document.getElementById(txtYearName)
if (parseInt(txtYear.value)>0 && 
  cboMonth.selectedIndex>0 && 
  cboDay.selectedIndex>0 && 
  !validDate(parseInt(txtYear.value), cboMonth.selectedIndex-1, cboDay.selectedIndex)) {
    alert(Days[cboDay.selectedIndex] + "/" + Months[cboMonth.selectedIndex] + "/" + txtYear.value + " is not a valid date. You will not be able to submit the form until this is corrected.");
    return false;
  } else {
    return true;
  }
}

function checkYear(textBoxName, oFunction) {
  var textLength = document.getElementById(textBoxName).value.length;
  if (textLength!=4 && textLength!=0) {
    alert("The year value just entered must be given as four digits");
    return false;
  }
  if (textLength==4) {
    return oFunction();
  } else {
    return true;
  }
}

function checkYear_2params(textBoxName, oFunction, param1, param2) {
  var textLength = document.getElementById(textBoxName).value.length;
  if (textLength!=4 && textLength!=0) {
    alert("The year value just entered must be given as four digits");
    return false;
  }
  if (textLength==4) {
    return oFunction(param1, param2);
  } else {
    return true;
  }
}
