/*******************************************************************************/
/*                                                                             */
/* kx_validation - Various Form Validation Routines                            */
/*                                                                             */
/* (C) Copyright 2004, Keropac.  All right reserved.                           */
/* Last updated: 29th January 2004                                             */
/*                                                                             */
/* Provides various form validation routine.                                   */
/*                                                                             */
/* The isNull function returns true if the value is null.                      */
/*                                                                             */
/* The isDigit function returns true if the value is a one character numeric.  */
/*                                                                             */
/* The isBlank function returns true if the fields contains spaces.            */
/*                                                                             */
/* The validEmail function returns true if the email address passed to it is   */
/* valid, or false if invalid.                                                 */
/*                                                                             */
/* The validNumber function returns true if the value is numeric               */
/* (i.e. postive or negative number with or without decimal places).           */
/*                                                                             */
/* The validInteger function returns true if the value is an integer           */
/* (i.e. postive number with no decimal places).                               */
/*                                                                             */
/* The validPostCode function returns true if the post code passed to it is    */
/* valid.  (See http://www.govtalk.gov.uk/gdsc/html/frames/PostCode.htm)       */                 
/*                                                                             */
/*******************************************************************************/

<!-- Begin

function trim(val)

{
	return trimr(triml(val));
}

function triml(val)

{
	var x1 = 0;
	
	while(x1 < val.length && val[x1] == ' ')
	  x1++;

	return val.substring(x1, val.length);
}

function trimr(val)

{
	var x1 = val.length -1;
	
	while(x1 > 0 && val[x1] == ' ')
	  x1--;

	return val.substring(0, x1 + 1);
}

//-------------------------------------------------------------------
// Check if a field is null.
//-------------------------------------------------------------------

function isNull(val)

{
  return(val == null);
}

// ---------------------------------------------------------------------------
// Check if a field contains a digit.
// ---------------------------------------------------------------------------

function isDigit(num) 

{
  if (num.length > 1)
    return false;

  var string = "1234567890";
  
  if (string.indexOf(num) != -1)
    return true;

  return false;
}

//-------------------------------------------------------------------
// Check if a field is empty (i.e. contains only spaces).
//-------------------------------------------------------------------

function isBlank(val)

{
  if(val == null)
    return true;

  for(var i=0; i<val.length; i++) 
  {
    if ((val.charAt(i) != ' ') && (val.charAt(i) != "\t") && (val.charAt(i) != "\n") && (val.charAt(i) != "\r"))
      return false;
  }

  return true;
}

// ---------------------------------------------------------------------------
// Validate value is an email address.
// ---------------------------------------------------------------------------

function validEmail(emailAddress) 

{
  at = "@";
  dot = ".";
  atPos = emailAddress.indexOf(at);
  emailLen = emailAddress.length;
  dotPos = emailAddress.indexOf(dot);

  // The must be an @ in the address, it must not be at the end and there must not be two.

  if (emailAddress.indexOf(at) == -1 || 
      emailAddress.indexOf(at) == 0 ||
      emailAddress.indexOf(at) == emailLen ||
      emailAddress.indexOf(at,(atPos + 1)) != -1)
    return false;

  // The must be an . in the address, it must not be at the end but must be after the @.

  if (emailAddress.indexOf(dot) == -1 ||
      emailAddress.indexOf(dot) == 0 ||
      emailAddress.indexOf(dot) == emailLen ||
      emailAddress.indexOf(dot,(atPos + 2)) == -1)
    return false;

  // There must not be a . just before or just after the @.

  if (emailAddress.substring(atPos - 1, atPos) == dot || 
      emailAddress.substring(atPos + 1, atPos + 2) == dot)
    return false;

  // There must be no spaces.
		
  if (emailAddress.indexOf(" ") != -1)
    return false;

  // Finished, the email address is valid.

  return true;

}

// ---------------------------------------------------------------------------
// Validate value is a post code.
// ---------------------------------------------------------------------------

function validPostCode(postCode) 

{
  var firstPos = "ABCDEFGHIJKLMNOPRSTUWYZ";
  var secondPos = "ABCDEFGHKLMNOPQRSTUVWXY";

  var thirdPos = "ABCDEFGHJKSTUW";
  var forthPos = "ABEHMNPRVWXY";

  var lastPos = "ABDEFGHJLNPQRSTUWXYZ";

  // Trim the post code, and heck the length.

  postCode = trim(postCode);
  
  pcLen = postCode.length;
   
  if (pcLen < 6 || pcLen > 8)
    return false;

  // There must be a space, but only one.

  x = postCode.indexOf(" ");

  if (x == -1)
    return false;

  if (postCode.indexOf(" ",(x + 1)) != -1)
    return false;

  // Make all the characters uppercase.

  postCode = postCode.toUpperCase();

  // Validate the first character is alpha.
  // The letters Q, V and X are not used in the first position.

  xpa = postCode.charAt(0);

  if (firstPos.indexOf(xpa) == -1)
    return false;
  
  // Validate the second character is alphanumeric.
  // The letters I, J and Z are not used in the second position.

  xpb = postCode.charAt(1);

  if (secondPos.indexOf(xpb) == -1 && !isDigit(xpb))
    return false;
  
  // Validate the third character (if specified) is numeric if the 2nd character was alpha, or alphanumeric 
  // if the 2nd character was numeric.
  // The only letters to appear in the third position are A, B, C, D, E, F, G, H, J, K, S, T, U and W.

  if (pcLen >= 7)
  {
    xpc = postCode.charAt(2);

    if (secondPos.indexOf(xpb) != -1)  // 2nd character is alpha.
    {
      if (!isDigit(xpc))
        return false;
    }
    else
    {
      if (thirdPos.indexOf(xpc) == -1 && !isDigit(xpc))
        return false;
    }
  }

  // Validate the forth character (if specified) is alphanumeric, and if specified the third character
  // must be numeric.
  // The only letters to appear in the fourth position are A, B, E, H, M, N, P, R, V, W, X and Y.

  if (pcLen == 8)
  {
    xpd = postCode.charAt(3);

    if (forthPos.indexOf(xpd) == -1 && !isDigit(xpd))
      return false;

    if (!isDigit(xpc))
      return false;
  }

  // Validate the last three characters.
  // The second half of the Postcode is always consistent numeric, alpha, alpha format.
  // The letters C, I, K, M, O and V are never used.

    xpl = postCode.charAt(pcLen - 3);

  if (!isDigit(xpl))
    return false;

  xpl = postCode.charAt(pcLen - 2);

  if (lastPos.indexOf(xpl) == -1)
    return false;

  xpl = postCode.charAt(pcLen - 1);

  if (lastPos.indexOf(xpl) == -1)
    return false;

  // Finished, the post code is valid.

  return true;
}

// ---------------------------------------------------------------------------
// Validate value is numeric (i.e. postive, negative and optional decimal 
// places)
// ---------------------------------------------------------------------------

function validNumber(val) 

{
  return(parseFloat(val,10) == (val*1));
}

//-------------------------------------------------------------------
// Validate value is integer (i.e. positive and no decimal places).
//-------------------------------------------------------------------

function validInteger(val)

{
  if (isBlank(val))
    return false;

  for(var i=0;i<val.length;i++)
  {
    if(!isDigit(val.charAt(i)))
      return false;
  }

  return true;
}

// End -->
