/*==============================================================================

Application:   Utility Function
Author:        John Gardner

Version:       V1.0
Date:          23rd May 2005
Description:   Used to check the validity of a UK National Insurance Number

Version:       V1.1
Date:          25th March 2008
Description:   Allows lowercase but converts to uppercase on return.
  
Parameters:    toCheck - National insurance number to be checked. 

This function checks the validity of the supplied National Insurance Number. 

If the number is found to be in a valid format, the function returns the 
National Insurance Number converted to uppercase, otherwise a value of false.

See http://www.govtalk.gov.uk/gdsc/html/frames/NationalInsuranceNumber-2-1-Release.htm
for a formal specification.
  
Example call:
  
  function testNINO () {
    var myNINO = document.getElementById('nino').value;
    if (checkNINO (myNINO)) {
      document.getElementById('nino').value = checkNINO (myNINO);
      alert ("National Insurance Number has a valid format");
    } 
    else alert ("National Insurance Number has invalid format");
  }   
                  
------------------------------------------------------------------------------*/

function checkNINO (toCheck) {

  var exp1 = /^[A-CEGHJ-NOPR-TW-Z]{1}[A-CEGHJ-NPR-TW-Z]{1}[0-9]{6}[A-D\s]{1}/i;
  var exp2 = /(^GB)|(^BG)|(^NK)|(^KN)|(^TN)|(^NT)|(^ZZ).+/i;
	
  if (toCheck.match(exp1) && !toCheck.match(exp2)) 
	  return toCheck.toUpperCase() 
	else
	  return false;
}

