/*

 Highlight fields failing the validation by providing
 the element id and setting the class to orange.

*/
function highlightValidationError(idName) {
  element = document.getElementById(idName);
  element.className = 'orange';
}

/*
 
 Clear error fields by providing the element id
 and removing the red class.

*/
function clearError(idName) {
  element = document.getElementById(idName);
  element.className = '';
}

/*

 A valid UK postcode validator. The validation process is based on
 information from the http://www.govtalk.gov.uk/gdsc/html/frames/PostCode.htm
 Government Data Standards Catalogue. Adapted from the post code validator
 supplied by Nick Efford (nde) in SE20.

*/
function validatePostCode(p) {

  var firstPart = p.slice(0, p.indexOf(" "));
  var secondPart = p.slice(p.indexOf(" ")+1, p.length);

  /*

  AN/ANN/AAN/AANN/ANA/AANA represents the patterns available for the first part.
  The aim of the check is to see if any pattern yields a 0 i.e. everything is
  valid. Invalid characters will return 1.

  */

  // Test if Post Office anomaly
  if (p == "GIR 0AA") {
    return true;
  }

  if (firstPart.length < 2 || firstPart.length > 4) { // if first part is too small or too big
    return false;
  }
  else if (firstPart.length == 2) { // if first part is two characters long
    // AN is valid
    if (checkFirstCharacter(firstPart.charAt(0)) == 0 && isNumber(firstPart.charAt(1)) == 0) {
      //alert('OK');
    }
    else {
      return false;
    }
  }
  else if (firstPart.length == 3) {
    // ANN is valid
    if (checkFirstCharacter(firstPart.charAt(0)) == 0 && isNumber(firstPart.charAt(1)) == 0 && isNumber(firstPart.charAt(2)) == 0) {
      //alert('OK');
    }
    else {
      // AAN is valid
      if (checkFirstCharacter(firstPart.charAt(0)) == 0 && checkSecondCharacter(firstPart.charAt(1)) == 0 && isNumber(firstPart.charAt(2)) == 0) {
        //alert('OK');
      }
      else {
        // ANA is valid
        if (checkFirstCharacter(firstPart.charAt(0)) == 0 && isNumber(firstPart.charAt(1)) == 0 && checkSecondCharacter(firstPart.charAt(2)) == 0) {
          //alert('OK');
        }
        else {
          return false;
        }
      }
    }
  }
  else if (firstPart.length == 4) {
    // AANN is valid
    if (checkFirstCharacter(firstPart.charAt(0)) == 0 && checkSecondCharacter(firstPart.charAt(1)) == 0 && isNumber(firstPart.charAt(2)) == 0 && isNumber(firstPart.charAt(3)) == 0) {
      //alert('OK');
    }
    else {
      // AANA is valid
      if (checkFirstCharacter(firstPart.charAt(0)) == 0 && checkSecondCharacter(firstPart.charAt(1)) == 0 && isNumber(firstPart.charAt(2)) == 0 && checkThirdCharacter(firstPart.charAt(3)) == 0) {
        //alert('OK');
      }
      else {
        return false;
      }
    }
  }

  if (secondPart.charAt(0).search(/[^0-9]/i) == false ||
      secondPart.charAt(1).search(/[^a-z]/i) == false ||
      secondPart.charAt(2).search(/[^a-z]/i) == false ||
      secondPart.charAt(1).search(/[c|i|k|m|o|v]/i) == false ||
      secondPart.charAt(2).search(/[c|i|k|m|o|v]/i) == false || secondPart.length != 3) {
    return false;
  }

  return true;

}

// Check first character of a post code
function checkFirstCharacter(c) {

  if (c.search(/[^a-z]/i) == false || c.search(/[q|v|x]/i) == false) {
    return 1;
  }
  return 0;

}

// Check second character of a post code
function checkSecondCharacter(c) {

  if (c.search(/[^a-z]/i) == false || c.search(/[i|j|z]/i) == false) {
    return 1;
  }
  return 0;

}

// Check third character of a post code
function checkThirdCharacter(c) {

  if (c.search(/[^a-z]/i) == false || c.search(/[^a|b|c|d|e|f|g|h|j|k|s|t|u|w]/i) == false) {
    return 1;
  }
  return 0;

}

// Check fourth character of a post code
function checkFourthCharacter(c) {

  if (c.search(/[^a-z]/i) == false || c.search(/[^a|b|e|h|m|n|p|r|v|w|x|y]/i) == false) {
    return 1;
  }
  return 0;

}

// Check if character is a number
function isNumber(c) {

  if (c.search(/[^0-9]/) == true) {
    return 1;
  }
  return 0;

}

// Check if there are any numbers in a string
function hasNumbers(c) {

  for (i=0; i < c.length+1; i++) {
    if (c.charAt(i).search(/[0-9]/) == false) {
      return true;
    }
  }

  return false;

}

// Check if there are any characters in a string
function hasCharacters(c) {

  for (i=0; i < c.length+1; i++) {
    if (c.charAt(i).search(/[a-z]/i) == false) {
      return true;
    }
  }

  return false;

}

