function validatefixture(thisform) {
  var error="";
  error=validateDate(thisform.date.value);
  if (error!="") {
    alert(error);
    thisform.date.focus();
    thisform.date.select();
    return false;
  }
  error=validatePresent(thisform.competition.value);
  if (error!="") {
    alert(error);
    thisform.competition.focus();
    thisform.competition.select();
    return false;
  }
  error=validatePresent(thisform.opposition.value);
  if (error!="") {
    alert(error);
    thisform.opposition.focus();
    thisform.opposition.select();
    return false;
  }
  error=validateNumericIfPresent(thisform.sufcscore.value);
  if (error!="") {
    alert(error);
    thisform.sufcscore.focus();
    thisform.sufcscore.select();
    return false;
  }
  error=validateNumericIfPresent(thisform.oppscore.value);
  if (error!="") {
    alert(error);
    thisform.oppscore.focus();
    thisform.oppscore.select();
    return false;
  }
  if (error=="")
  {
    return true;
  }
}

function validatetableentry(thisform) {
  var error="";
  error=validatePresent(thisform.team.value);
  if (error!="") {
    alert(error);
    thisform.team.focus();
    thisform.team.select();
    return false;
  }
  error=validateNumeric(thisform.matches.value);
  if (error!="") {
    alert(error);
    thisform.matches.focus();
    thisform.matches.select();
    return false;
  }
  error=validateNumeric(thisform.won.value);
  if (error!="") {
    alert(error);
    thisform.won.focus();
    thisform.won.select();
    return false;
  }
  error=validateNumeric(thisform.lost.value);
  if (error!="") {
    alert(error);
    thisform.lost.focus();
    thisform.lost.select();
    return false;
  }
  error=validateNumeric(thisform.legsfor.value);
  if (error!="") {
    alert(error);
    thisform.legsfor.focus();
    thisform.legsfor.select();
    return false;
  }
  error=validateNumeric(thisform.legsagainst.value);
  if (error!="") {
    alert(error);
    thisform.legsagainst.focus();
    thisform.legsagainst.select();
    return false;
  }
  if (error=="")
  {
    return true;
  }
}

function validateaverageentry(thisform) {
  var error="";
  error=validatePresent(thisform.name.value);
  if (error!="") {
    alert(error);
    thisform.name.focus();
    thisform.name.select();
    return false;
  }
  error=validateNumeric(thisform.matches.value);
  if (error!="") {
    alert(error);
    thisform.matches.focus();
    thisform.matches.select();
    return false;
  }
  error=validateNumeric(thisform.legs.value);
  if (error!="") {
    alert(error);
    thisform.legs.focus();
    thisform.legs.select();
    return false;
  }
  error=validateNumeric(thisform.visits.value);
  if (error!="") {
    alert(error);
    thisform.visits.focus();
    thisform.visits.select();
    return false;
  }
  error=validateNumeric(thisform.total.value);
  if (error!="") {
    alert(error);
    thisform.total.focus();
    thisform.total.select();
    return false;
  }
  error=validateNumericIfPresent(thisform.hundreds.value);
  if (error!="") {
    alert(error);
    thisform.hundreds.focus();
    thisform.hundreds.select();
    return false;
  }
  error=validateNumericIfPresent(thisform.oneforties.value);
  if (error!="") {
    alert(error);
    thisform.oneforties.focus();
    thisform.oneforties.select();
    return false;
  }
  error=validateNumericIfPresent(thisform.oneeighties.value);
  if (error!="") {
    alert(error);
    thisform.oneeighties.focus();
    thisform.oneeighties.select();
    return false;
  }
  error=validateNumeric(thisform.finishes.value);
  if (error!="") {
    alert(error);
    thisform.finishes.focus();
    thisform.finishes.select();
    return false;
  }
  error=validateNumericIfPresent(thisform.hf.value);
  if (error!="") {
    alert(error);
    thisform.hf.focus();
    thisform.hf.select();
    return false;
  }
  if (error=="")
  {
    return true;
  }
}

function validatePresent(strng) {
  var error="";
  if (strng == "") {
    error = "Invalid entry - data missing";
  }
  return error;
}

function validateNumeric(num) {
  var error="";
  if (parseInt(num)!=num-0)
  {
    error="Invalid entry - number needed";
  }
  return error;
}

function validateNumericIfPresent(num) {
  var error="";
  if (num!="")
  {
    if (parseInt(num)!=num-0)
    {
      error="Invalid entry - number needed";
    }
  }
  return error;
}

function validateDate(strng) {
  var error="";
  var validformat=/^\d{4}\-\d{2}\-\d{2}$/;
  size=strng.length;
  if (strng=="TBA")
  {
    return error;
  }
  else
  {
    if (!(validformat.test(strng))) {
      error="Invalid date format - must be either YYYY-MM-DD or TBA";
    } else {
      var yearfield=strng.value.split("-")[0];
      var monthfield=strng.value.split("-")[1];
      var dayfield=strng.value.split("-")[2];
      var dayobj = new Date(yearfield, monthfield-1, dayfield);
      if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
      {
        error="Invalid date format - must be either YYYY-MM-DD or TBA";
      }
    }
  }
  return error;
}


function validateform() {
  var error="";
  error=validateUsername(document.insertuser.username.value);
  if (error!="") {
    alert(error);
    document.insertuser.username.focus();
    document.insertuser.username.select();
    return false;
  }
  error=validatePassword(document.insertuser.password.value,document.insertuser.passwordcheck.value);
  if (error!="") {
    alert(error);
    document.insertuser.password.focus();
    document.insertuser.password.select();
    return false;
  }
  error=validateName(document.insertuser.first_name.value);
  if (error!="") {
    alert(error);
    document.insertuser.first_name.focus();
    document.insertuser.first_name.select();
    return false;
  }
  error=validateName(document.insertuser.last_name.value);
  if (error!="") {
    alert(error);
    document.insertuser.last_name.focus();
    document.insertuser.last_name.select();
    return false;
  }
  error=validateEmail(document.insertuser.email.value);
  if (error!="") {
    alert(error);
    document.insertuser.email.focus();
    document.insertuser.email.select();
    return false;
  }
  error=validateTelephone(document.insertuser.phone.value);
  if (error!="") {
    alert(error);
    document.insertuser.phone.focus();
    document.insertuser.phone.select();
    return false;
  }
  error=validateAddress(document.insertuser.address1.value);
  if (error!="") {
    alert(error);
    document.insertuser.address1.focus();
    document.insertuser.address1.select();
    return false;
  }
  error=validateAddress(document.insertuser.address2.value);
  if (error!="") {
    alert(error);
    document.insertuser.address2.focus();
    document.insertuser.address2.select();
    return false;
  }
  error=validatePostcode(document.insertuser.postcode.value);
  if (error!="") {
    alert(error);
    document.insertuser.postcode.focus();
    document.insertuser.postcode.select();
    return false;
  }
  if (error=="") {
    return true;
  }
}

function validateUsername(strng) {
  var error = "";
  if (strng == "") {
    error = "Please enter a username.";
  }
  return error;
}

function validatePassword(strng,strngChk) {
  var error = "";
  if (strng == "") {
    error = "Please enter a password.";
  }
  else
  {
    if (strng!=strngChk) {
      error = "Passwords do not match.";
    }
  }
  return error;
}

function validateName(strng) {
  var error = "";
  if (strng == "") {
    error = "Please enter a name.";
  }
  return error;
}

function validateEmail(strng) {
  var error="";
  var emailFilter=/^.+@.+\..{2,3}$/;
  if (!(emailFilter.test(strng))) { 
    error = "Please enter a valid email address.";
  }
  else {
    var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
    if (strng.match(illegalChars)) {
      error = "The email address contains illegal characters.";
    }
  }
  return error;
}

function validateTelephone(strng) {
  var error="";
  var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
  //strip out acceptable non-numeric characters
  if (isNaN(parseInt(stripped))) {
    error = "The phone number contains illegal characters.";
  }
  return error;
}

function validateAddress(strng) {
  var error = "";
  if (strng == "") {
    error = "Please enter an address.";
  }
  return error;
}

function validatePostcode(strng) {
  var error="";
  size=strng.length;
  strng=strng.toUpperCase();
  while (strng.slice(0,1) == " ") 
  {
    strng = strng.substr(1,size-1);
    size = strng.length;
  }
  while(strng.slice(size-1,size)== " ") 
  {
    strng = strng.substr(0,size-1);
    size = strng.length;
  }
  if (!(strng.charAt(size-4) == " ")){  	
    if (size==6 || size==7) {
      strng=strng.substring(0,size-3)+" "+strng.substring(size-3,size);
      size = strng.length;
    }
  }
  document.insertuser.postcode.value = strng;
  if (size < 6 || size > 8){
    error=strng;
    error+=" is not a valid postcode - wrong length";
    return error;
  }
  if (!(isNaN(strng.charAt(0)))){
    error=strng;
    error+=" is not a valid postcode - cannot start with a number";
    return error;
  }
  if (isNaN(strng.charAt(size-3))){
    error=strng;
    error+=" is not a valid postcode - alpha character in wrong position";
    return error;
  }
  if (!(isNaN(strng.charAt(size-2)))){
    error=strng;
    error+=" is not a valid postcode - number in wrong position";
    return error;
  }
  if (!(isNaN(strng.charAt(size-1)))){
    error=strng;
    error+=" is not a valid postcode - number in wrong position";
    return error;
  }
  if (!(strng.charAt(size-4) == " ")){
    error=strng;
    error+=" is not a valid postcode - no space or space in wrong position";
    return error;
  }
  count1 = strng.indexOf(" ");
  count2 = strng.lastIndexOf(" ");
  if (count1 != count2){
    error=strng;
    error+=" is not a valid postcode - only one space allowed";
  }
  return error;
}

function validateCountry(strng) {
  var error = "";
  if (strng == "") {
    error = "Please select a country.";
  }
  return error;
}


function validatebid() {
  var poundbid=document.enterbid.poundbid.value;
  var pencebid=document.enterbid.pencebid.value;
  if (pencebid=="")
  {
    pencebid="00";
    document.enterbid.pencebid.value=pencebid;
  }
  if ( document.enterbid.username.value == '' )
  {
    alert('Please enter a username');
    document.enterbid.username.focus();
    document.enterbid.username.select();
    return false;
  }
  else
  {
    if ( document.enterbid.password.value == '' )
    {
      alert('Please enter a password');
      document.enterbid.password.focus();
      document.enterbid.password.select();
      return false;
    }
    else
    {
      if ( isNaN(parseInt(poundbid)) || isNaN(parseInt(pencebid)) )
      {
        alert('Please enter a valid bid.');
        document.enterbid.poundbid.focus();
        document.enterbid.poundbid.select();
        return false;
      }
      else
      {
      	return true;
      }
    }
  }
}
