//===============================================================================
// System        : Cinnamon Systems Web
// File          : Validation.js
// Description   : This document contains JavaScript routines which process
//                 client-side field validations.
//
// Copyright     : Copyright (C) 2004 Cinnamon Systems Ltd.
//                 All rights reserved.
// Author        : Graham Auty  : Mar 2004
// Changes       : Graham Auty  : n/a
//===============================================================================

//-------------------------------------------------------------------------------
// Constants 
//-------------------------------------------------------------------------------
var csCharSetNumericExcZero= /[1-9]/;
var csCharSetNumeric       = /[0-9]/;
var csCharSetDecimal       = /[0-9\.]/;
var csCharSetNumericSpace  = /[0-9 ]/;
var csCharSetAlpha         = /[a-z]/i;
var csCharSetAlphaNum      = /[A-Za-z0-9]/;
var csCharSetAlphaNumSpace = /[A-Za-z0-9 ]/;
var csCharSetEmail         = /[A-Za-z0-9\-\_\@\.]/;
var csCharSetEmailWild     = /[A-Za-z0-9\-\_\@\.\*]/;
var csCharSetInvalidFree   = /[#<>]/;
var csCharSetInvalidFree2  = /[\!\"\£\$\€\%\^\&\(\)\_\+\=\{\}\[\]\:\;\@\~\#\?\<\>\|\¬\`\¦\\\/]/;
var csCharSetInvalidFree3  = /[0-9\!\"\£\$\€\%\^\&\(\)\_\+\=\{\}\[\]\:\;\@\~\#\?\<\>\.\,\|\¬\`\¦\\\/]/;
var csCharSetInvalidFree4  = /[0-9\!\"\£\$\€\%\^\&\*\(\)\_\+\=\{\}\[\]\:\;\@\~\#\?\<\>\.\,\|\¬\`\¦\\\/]/;
var csCharSetInvalidFree5  = /[\']/;
var csCharSetCapitalAfter  = /( |\'|\-)[a-z]/;
var csDateSeparator        = "/";
var csDateFormatLong       = "Long";
var cnDecimalRadix         = 10;
var csTimeSeparator        = ":";
//-------------------------------------------------------------------------------
var csInvCharSetAlphaNumSpace = /[^A-Za-z0-9 ]/g;


//-------------------------------------------------------------------------------
// Function      : FilterCharacterSet
// Description   : This function is used to prevent the user from typing invalid
//                 characters into a field. The character set supplied is a
//                 Regular Expression value which contains either allowable
//                 characters (when bInvert is absent or false) or dis-allowed
//                 characters (when bInvert is true).
// Parameters IN : nCharCode - ANSI code of the character typed into the field
//                 sCharSet  - Regular Expression containing valid characters
//                 bInvert   - Boolean indicating if sCharSet is allowed (false)
//                             or disallowed (true) characters
//           OUT : None
//        IN/OUT : None
// Return Value  : FilterCharacterSet - if the character was in the set, it is echoed
//                                      back to the caller, otherwise 0 is returned.
//-------------------------------------------------------------------------------
function FilterCharacterSet(nCharCode, sCharSet, bInvert) {
  var sChar;
  var bCharFound;
  
  if (nCharCode != 13) {
    if (bInvert == null) bInvert = false;
  
    sChar = String.fromCharCode(nCharCode);
    bCharFound = (sChar.search(sCharSet) != -1);
  
    if (!bInvert) {
      if (!bCharFound)
        nCharCode = 0; }
    else {
      if (bCharFound)
        nCharCode = 0; }
  }
  
  return nCharCode;
}

//-------------------------------------------------------------------------------
// Function      : FilterNumericExcZero
// Description   : This function is used to prevent the user from typing non-numeric
//                 characters into a text field.
// Parameters IN : nCharCode  - ANSI code of the character typed into the field
//                 bDecimal   - Boolean indicating if decimal points are allowed (true)
//           OUT : None
//        IN/OUT : None
// Return Value  : FilterNumeric - if character was allowable, it is echoed back
//                              to the caller, otherwise 0 is returned.
//-------------------------------------------------------------------------------
function FilterNumericExcZero(nCharCode, bDecimal) {
  var sRegDecimal = /\./;
  var nANSICode;
  var sFieldValue;
 
  if (bDecimal == null) bDecimal = false;
  
  if (bDecimal) {
    sFieldValue = event.srcElement.value;
    nANSICode = FilterCharacterSet(nCharCode, csCharSetDecimal);
    if (nCharCode == ".".charCodeAt(0))
      if (sFieldValue.search(sRegDecimal) != -1)
        nANSICode = 0;
  }
  else {
    sFieldValue = event.srcElement.value;
    if (sFieldValue == "")
      nANSICode =  FilterCharacterSet(nCharCode, csCharSetNumericExcZero);
    else 
      nANSICode =  FilterCharacterSet(nCharCode, csCharSetNumeric);
  }
  
  return nANSICode;
}

//-------------------------------------------------------------------------------
// Function      : FilterNumeric
// Description   : This function is used to prevent the user from typing non-numeric
//                 characters into a text field.
// Parameters IN : nCharCode  - ANSI code of the character typed into the field
//                 bDecimal   - Boolean indicating if decimal points are allowed (true)
//           OUT : None
//        IN/OUT : None
// Return Value  : FilterNumeric - if character was allowable, it is echoed back
//                              to the caller, otherwise 0 is returned.
//-------------------------------------------------------------------------------
function FilterNumeric(nCharCode, bDecimal) {
  var sRegDecimal = /\./;
  var nANSICode;
  var sFieldValue;
 
  if (bDecimal == null) bDecimal = false;
  
  if (bDecimal) {
    sFieldValue = event.srcElement.value;
    nANSICode = FilterCharacterSet(nCharCode, csCharSetDecimal);
    if (nCharCode == ".".charCodeAt(0))
      if (sFieldValue.search(sRegDecimal) != -1)
        nANSICode = 0;
  }
  else {
    nANSICode =  FilterCharacterSet(nCharCode, csCharSetNumeric);
  }
  
  return nANSICode;
}

//-------------------------------------------------------------------------------
// Function      : FilterAlpha
// Description   : This function is used to prevent the user from typing numeric
//                 characters into a text field.
// Parameters IN : nCharCode  - ANSI code of the character typed into the field
//           OUT : None
//        IN/OUT : None
// Return Value  : FilterAlpha - if character was allowable, it is echoed back
//                              to the caller, otherwise 0 is returned.
//-------------------------------------------------------------------------------
function FilterAlpha(nCharCode) {
  var nANSICode;
 
  nANSICode =  FilterCharacterSet(nCharCode, csCharSetNumeric);
  
  if (nANSICode != 0)
    nANSICode = 0;
  else
    nANSICode = nCharCode; 

  return nANSICode;
}

//-------------------------------------------------------------------------------
// Function      : FilterDate
// Description   : This function is used to prevent the user from typing invalid
//                 characters into a date field.
// Parameters IN : nCharCode  - ANSI code of the character typed into the field
//           OUT : None
//        IN/OUT : None
// Return Value  : FilterDate - if character was allowable, it is echoed back
//                              to the caller, otherwise 0 is returned.
//-------------------------------------------------------------------------------
function FilterDate(nCharCode) {
  var nANSICode;
  
  nANSICode = FilterCharacterSet(nCharCode, csCharSetNumeric);
  if (nANSICode == 0)
    nANSICode = FilterLegal(nCharCode, csDateSeparator);
    
  return nANSICode;
}

//-------------------------------------------------------------------------------
// Function      : FilterTime
// Description   : This function is used to prevent the user from typing invalid
//                 characters into a time field.
// Parameters IN : nCharCode  - ANSI code of the character typed into the field
//           OUT : None
//        IN/OUT : None
// Return Value  : FilterTime - if character was allowable, it is echoed back
//                              to the caller, otherwise 0 is returned.
//-------------------------------------------------------------------------------
function FilterTime(nCharCode) {
  var nANSICode;
  
  nANSICode = FilterCharacterSet(nCharCode, csCharSetNumeric);
  if (nANSICode == 0)
    nANSICode = FilterLegal(nCharCode, csTimeSeparator);
    
  return nANSICode;
}

//-------------------------------------------------------------------------------
// Function      : FilterSearch
// Description   : This function is used to prevent the user from typing invalid
//                 characters into a keyword search field.
// Parameters IN : nCharCode  - ANSI code of the character typed into the field
//           OUT : None
//        IN/OUT : None
// Return Value  : FilterSearch - if character was allowable, it is echoed back
//                              to the caller, otherwise 0 is returned.
//-------------------------------------------------------------------------------
function FilterSearch(nCharCode) {
  var nANSICode;
  
  nANSICode = nCharCode;
  //nANSICode = FilterCharacterSet(nCharCode, csCharSetAlphaNumSpace);
  //if (nANSICode == 0)
  //  nANSICode = FilterLegal(nCharCode, "\"");
  //  if (nANSICode == 0)
  //    nANSICode = FilterLegal(nCharCode, "*");
    
  return nANSICode;
}

//-------------------------------------------------------------------------------
// Function      : FilterLegal
// Description   : This function is used to prevent the user from typing invalid
//                 characters into a data field.
// Parameters IN : nCharCode   - ANSI code of the character typed into the field
//                 sLegalChars - String containing set of characters considered legal
//           OUT : None
//        IN/OUT : None
// Return Value  : FilterLegal - if character was allowable, it is echoed back
//                               to the caller, otherwise 0 is returned.
//-------------------------------------------------------------------------------
function FilterLegal(nCharCode, sLegalChars) {
  var sChar;
  
  sChar = String.fromCharCode(nCharCode);
  if (!CharInString(sLegalChars, sChar))
    nCharCode = 0;
 
  return nCharCode;
}

//-------------------------------------------------------------------------------
// Function      : FilterUpper
// Description   : This function is used to convert input to uppercase.
// Parameters IN : nCharCode   - ANSI code of the character typed into the field
//                 sLegalChars - String containing set of characters considered legal
//           OUT : None
//        IN/OUT : None
// Return Value  : Character   - if character was allowable, it is echoed back to
//                               the caller but capitalised, otherwise 0 is returned.
//-------------------------------------------------------------------------------
function FilterUpper(nCharCode, sLegalChars) {
  nCharCode = FilterCharacterSet(nCharCode, sLegalChars, false);
  
  if (nCharCode != 0)
    nCharCode = String.fromCharCode(nCharCode).toUpperCase().charCodeAt(0);

  return nCharCode;
}

//-------------------------------------------------------------------------------
// Function      : CapitaliseFieldChars
// Description   : This function capitalises characters in a field when they are
//                 preceded by one of a number of characters, as defined by
//                 the character set passed in sCharSetRules. The first character
//                 in the field is also capitalised if it is a letter.
// Parameters IN : sSourceField  - Field to be processed and initial capitalised
//                 sCharSetRules - Regular expression containing capitalisation rules:
//                                 each char in set causes capitalisation of next
//                                 character.
//           OUT : None
//        IN/OUT : None
// Return Value  : CapitaliseFieldChars - sSourceField after capitalisation rules applied.
//-------------------------------------------------------------------------------
function CapitaliseFieldChars(sSourceField, sCharSetRules) {
  var objMatch;                             //Match object containing capitalisation string
  var sChar;                                //Character to be capitalised
  
  while ((objMatch = sSourceField.match(sCharSetRules)) != null) {
    sChar = sSourceField.charAt(objMatch.lastIndex-1).toUpperCase();
    sSourceField = sSourceField.slice(0, objMatch.index + 1) + sChar + 
                   sSourceField.slice(objMatch.lastIndex);
  }
  if (sSourceField.length > 1)
    sSourceField = sSourceField.charAt(0).toUpperCase() + sSourceField.slice(1);
  else
    sSourceField = sSourceField.toUpperCase();

  return sSourceField;
}

//-------------------------------------------------------------------------------
// Function      : FormatDate
// Description   : This function is used to change a supplied character string
//                 representing a date into a standard display representation, as
//                 indicated by the input parameter sDateFormat.
// Parameters IN : sDate       - String containing date in any of these formats:
//                               d/m/yyyy, d/mm/yyyy, dd/m/yyyy, dd/mm/yyyy, ddmmyyyy
//                 sDateFormat - String indicating required output format for sDate:
//                               "Long" gives dd/mm/yyyy
//           OUT : None
//        IN/OUT : None
// Return Value  : FormatDate  - String containing the date formatted as required
//-------------------------------------------------------------------------------
function FormatDate(sDate, sDateFormat) {
  var sDay;
  var sMonth;
  var sYear;
  var nDateSeparator1;                      //Position of 1st date separator in input date
  var nDateSeparator2;                      //Position of 2nd date separator in input date
  var sTempDate;
  var sFormatDisplayDate;
  var sDateSepRE;                           //Date separator regular expression
  
  sDateSepRE = new RegExp("[" + csDateSeparator + "]");
  sDate = TrimString(sDate);

  //First, determine which of the valid formats is in sDate
  nDateSeparator1 = sDate.search(sDateSepRE);
  if (nDateSeparator1 == -1) {
    //Format must be ddmmyyyy
    sDay   = sDate.slice(0, 2);
    sMonth = sDate.slice(2, 4);
    sYear  = sDate.slice(4, sDate.length); }
  else {
    //Get info between separators
    sTempDate = sDate.slice(nDateSeparator1 + 1, sDate.length);
    nDateSeparator2 = sTempDate.search(sDateSepRE);
    if (nDateSeparator2 == -1) {
      //Only one separator indicates incorrect date - return blank date
      sFormatDisplayDate = ""; }
    else {
      sDay   = sDate.slice(0, nDateSeparator1);
      sMonth = sTempDate.slice(0, nDateSeparator2);
      sYear  = sTempDate.slice(nDateSeparator2 + 1, sTempDate.length);
    }
  }
      
  if (sFormatDisplayDate != "") {
    sFormatDisplayDate = "";
    if (ValidNumeric(sDay) && ValidNumeric(sMonth) && ValidNumeric(sYear)) {  
      if (sDay.length == 1) 
        sDay = "0" + sDay;
      if (sMonth.length == 1)
        sMonth = "0" + sMonth;
      if ((sDay.length = 2) && (sMonth.length = 2)) {
        if (sYear.length == 4) {
          sFormatDisplayDate = sDay + csDateSeparator + sMonth + csDateSeparator + sYear;
        }
      }
    }
  }
  
  return sFormatDisplayDate;
}

//-------------------------------------------------------------------------------
// Function      : ValidNumeric
// Description   : This function checks that a supplied string is a valid number.
// Parameters IN : sTestItem - String containing text to be validated
//           OUT : None
//        IN/OUT : None
// Return Value  : ValidNumeric - true if sTestItem contains just numbers,
//                                false otherwise
//-------------------------------------------------------------------------------
function ValidNumeric(sTestItem) {
  var bValid;

  bValid = ((sTestItem.search(/\D+/) == -1) && (sTestItem.length != 0));
  
  return bValid;
}

//-------------------------------------------------------------------------------
// Function      : CharInString
// Description   : This function checks if a supplied character is present in the
//                 input string sTestItem, returning true if so.
// Parameters IN : sTestItem - String containing text to be searched
//                 sFindChar - Character to search for in sTestItem
//           OUT : None
//        IN/OUT : None
// Return Value  : CharInString - true if sTestItem contains search character,
//                                false otherwise
//-------------------------------------------------------------------------------
function CharInString(sTestItem, sFindChar) {
  var bCharFound;                           //Boolean indicating success of search
  var nPos;                                 //Position of character in test string
  var sCharRE;                              //Regular expression for search character
  
  sCharRE = new RegExp("[\\" + sFindChar + "]");
  nPos = sTestItem.search(sCharRE);
  bCharFound = (nPos != -1);

  return bCharFound;
}

//-------------------------------------------------------------------------------
// Function      : AnyCharInString
// Description   : This function checks if any of the characters in a supplied
//                 string is present in the input string sTestItem, 
//                 returning true if so.
// Parameters IN : sTestItem  - String containing text to be searched
//                 sFindChars - Characters to search for in sTestItem
//           OUT : None
//        IN/OUT : None
// Return Value  : AnyCharInString - true if sTestItem contains any search
//                                    character, false otherwise
//-------------------------------------------------------------------------------
function AnyCharInString(sTestItem, sFindChars) {
  var bCharFound;                           //Boolean indicating success of search
  var i;                                    //Index counter
  
  bCharFound = false;
  for (i = 0; i < sFindChars.length; i++) {
    bCharFound = CharInString(sTestItem, sFindChars.charAt(i));
    if (bCharFound) break;
  }

  return bCharFound;
}

//-------------------------------------------------------------------------------
// Function      : TrimString
// Description   : This function removes extraneous whitespace at the start or
//                 end of a supplied string.
// Parameters IN : sInString - String containing text to be trimmed
//           OUT : None
//        IN/OUT : None
// Return Value  : TrimString - String containing the trimmed version of sInString
//                                false otherwise
//-------------------------------------------------------------------------------
function TrimString(sInString) {
  var nPos;
  
  if (sInString.length != 0) {
    for(nPos = 0; nPos < sInString.length; nPos++) {
      if (sInString.charAt(nPos) != " ")
        break;
    }

    if (nPos == sInString.length)
      sInString = "";
    else {
      if (nPos != 0)
        sInString = sInString.slice(nPos, sInString.length);    
            
      for(nPos = sInString.length - 1; nPos >= 0; nPos--) {
        if (sInString.charAt(nPos) != " ")
          break;
      }

      if (nPos != sInString.length-1)
        sInString = sInString.slice(0, nPos + 1);          
    }
  }
    
  return sInString;
}

//-------------------------------------------------------------------------------
// Function      : ValidDate
// Description   : Given an input date string this routine checks that it is a
//                 valid date, returning true if so.
// Parameters IN : objDate - Field containing string date to be validated
//                 nCenturyType - Value used to determine bahaviour when the
//                                input date does not contain a century:
//                                null = no assumptions are made, yyyy required
//                                0    = assume century is 1900s
//                                1,2  = assume century is current one
//           OUT : None
//        IN/OUT : objDate   - String in field is manipulated and passed out of function
// Return Value  : ValidDate - Boolean containing true if sInDate is valid
//                             false otherwise
//-------------------------------------------------------------------------------
function ValidDate(objDate, nCenturyType) {
  var nDay;
  var nMonth;
  var nYear;
  var nMaxDaysInMonth;
  var bValidDate;
  var asDateBits;
  var sCentury;
  var sInDate;
  
  sInDate = objDate.value;
  objDate.bCenturyPresent = true;
  
  switch (nCenturyType) {
    case 0: {
      sCentury = "19";
      break;
    }
    case 1:
    case 2: {
      sCentury = new Date();
      sCentury = "" + sCentury.getFullYear();
      sCentury = sCentury.slice(0, 2);
      break;
    }
    default: {
      sCentury = "";
      break;
    }
  }

  asDateBits = sInDate.split("/");
  if (asDateBits.length == 3) {
    asDateBits[0] = "00" + asDateBits[0];
    asDateBits[1] = "00" + asDateBits[1];
    asDateBits[2] = "0000" + asDateBits[2];
    asDateBits[0] = asDateBits[0].slice(asDateBits[0].length - 2);
    asDateBits[1] = asDateBits[1].slice(asDateBits[1].length - 2);
    asDateBits[2] = asDateBits[2].slice(asDateBits[2].length - 4);
    if (asDateBits[2].slice(0, 2) == "00") {
      objDate.bCenturyPresent = false;
      asDateBits[2] = sCentury + asDateBits[2].slice(2, 4);
    }
    sInDate = asDateBits[0] + "/" + asDateBits[1] + "/" + asDateBits[2];
  }
  else {
    //Date format is ddmmyy
    if (sInDate.length == 6) {
      objDate.bCenturyPresent = false;
      sInDate = sInDate.slice(0, 4) + sCentury + sInDate.slice(4);
    }
  }
  
  sInDate = FormatDate(sInDate, csDateFormatLong);
  if (sInDate == "")
    bValidDate = false;
  else { 
    nDay   = parseInt(sInDate.slice(0, 2), cnDecimalRadix);
    nMonth = parseInt(sInDate.slice(3, 5), cnDecimalRadix);
    nYear  = parseInt(sInDate.slice(6, sInDate.length), cnDecimalRadix);
    if (nMonth > 0 && nMonth <= 12) {
      nMaxDaysInMonth = 31;
      switch(nMonth) {
        case 4:
        case 6:
        case 9:
        case 11: {
          nMaxDaysInMonth = 30;
          break;
        }
        case 2: {
          nMaxDaysInMonth = 28;
          //Check for leap year
          if ((nYear % 4 == 0) && (nYear % 100 != 0) || (nYear % 400 == 0))
            nMaxDaysInMonth = 29;
          break;
        }
      }
      bValidDate = ((nDay >= 1) && (nDay <= nMaxDaysInMonth)) }   
    else
      bValidDate = false;
  }
  
  if (bValidDate)
    objDate.value = sInDate;
  else
    objDate.value = "";
  return bValidDate;
}

//-------------------------------------------------------------------------------
// Function      : ValidTime
// Description   : Given an input time string this routine checks that it is a
//                 valid time, returning true if so.
// Parameters IN : sInTime - String containing time to be validated.
//           OUT : None
//        IN/OUT : None
// Return Value  : ValidTime - Boolean containing true if sInTime is valid
//                             false otherwise
//-------------------------------------------------------------------------------
function ValidTime(sInTime) {
  
  var nHours;
  var nMinutes;
  var nMaxMinutesInHours;
  var bValidTime;
  
  nMaxMinutesInHour = 60;
  
  sInTime = FormatTime(sInTime);
  if (sInTime == "")
    bValidTime = false;
  else { 
    nHours = parseInt(sInTime.slice(0, 2), cnDecimalRadix);
    nMinutes  = parseInt(sInTime.slice(3, sInTime.length), cnDecimalRadix);
  
    if (nHours > 0 && nHours <= 24) {
      if (nMinutes <  nMaxMinutesInHour){
          bValidTime = true;
       }   
      else
          bValidTime = false;
      }
  }
  
  return bValidTime;
}

//-------------------------------------------------------------------------------
// Function      : FormatTime
// Description   : This function is used to change a supplied character string
//                 representing a time into a standard display representation, as
//                 indicated by the input parameter sDateFormat.
// Parameters IN : sTime       - String containing time in any of these formats:
//                               h:mm, hh:mm, hhmm
//                 sTimeFormat - String indicating required output format for sTime:
//                               hh:mm
//           OUT : None
//        IN/OUT : None
// Return Value  : FormatTime  - String containing the date formatted as required
//-------------------------------------------------------------------------------
function FormatTime(sTime, sTimeFormat) {
  
  var sHours;
  var sMinutes;
  var nTimeSeparator1;                      //Position of 1st date separator in input date
  var sFormatDisplayTime;
  var sTimeSepRE;                           //Date separator regular expression
  
  sTimeSepRE = new RegExp("[" + csTimeSeparator + "]");
  sTime = TrimString(sTime);

  //First, determine which of the valid formats is in sDate
  nTimeSeparator1 = sTime.search(sTimeSepRE);
  if (nTimeSeparator1 == -1) {
    //Format must be hhmm
    sHours = sTime.slice(0, 2);
    sMinutes = sTime.slice(2, sTime.length);
    
    if (sMinutes.length > 2)
       sFormatDisplayTime = "";
   }
  else {
    //Get info between separators
    sHours   = sTime.slice(0, nTimeSeparator1);
    sMinutes  = sTime.slice(nTimeSeparator1 + 1, sTime.length);
  }
      
    sFormatDisplayTime = "";
    if (ValidNumeric(sHours) && ValidNumeric(sMinutes))
     {  
      if (sHours.length == 1) 
        sHours = "0" + sHours;
      if (sMinutes.length == 1)
        sMinutes = "0" + sMinutes;
     
      sFormatDisplayTime = sHours + csTimeSeparator + sMinutes;
    }
   
  return sFormatDisplayTime;
}

//--------------------------------------------------------------------------------
// Function      : CheckDate
// Description   : Check that date value input to supplied object is valid. If it
//                 is, reformat it to dd/mm/ccyy.
// Parameters IN : objDate - Field containing string date to be validated
//                 nCenturyType - Value used to determine bahaviour when the
//                                input date does not contain a century:
//                                null = no assumptions are made, yyyy required
//                                0    = assume century is 1900s
//                                1    = assume century is current one, unless resulting
//                                       date is in future, in which case use previous
//                                       century.
//                                2    = assume century is current one
//           OUT : None
//        IN/OUT : None
// Return Value  : None
//--------------------------------------------------------------------------------
function CheckDate(objDate, nCenturyType) {
  var sCentury;
  var bCenturyPresent;
  
  if (nCenturyType == null)
    nCenturyType = 2;
  
  objDate.bCenturyPresent = true;
  if (ValidDate(objDate, nCenturyType)) {
    if (!objDate.bCenturyPresent) {
      switch (nCenturyType) {
        case 1: {
          if (DateTriState(objDate.value, "Today") > 0) {
            //Set century back by 1
            sCentury = parseInt(objDate.value.slice(6, 8) - 1) + "";
            objDate.value = objDate.value.slice(0, 6) + sCentury + objDate.value.slice(8);
          }
          break;
        }
      }
    }
  }
}

//--------------------------------------------------------------------------------
// Function      : CheckTime
// Description   : Check that time value input to supplied object is valid. If it
//                 is, reformat it to hh:mm
// Parameters IN : objTime - Field containing string date to be validated
//           OUT : None
//        IN/OUT : None
// Return Value  : None
//--------------------------------------------------------------------------------
function CheckTime(objTime) {
  if (ValidTime(objTime.value))
    objTime.value = FormatTime(objTime.value);
  else
    objTime.value = "";
}

//--------------------------------------------------------------------------------
// Function      : CheckMaxLength
// Description   : Limits the length of an elements content
// Parameters IN : objTextArea - The text area element
// 		   objEvent - The event object
//                 nMaxLength - the length to limit the field to
//           OUT : None
//        IN/OUT : None
// Return Value  : None
//--------------------------------------------------------------------------------
function CheckMaxLength(objTextArea, objEvent, nMaxLength) {
  if (objEvent.keyCode==13 || objTextArea.value.length < nMaxLength)
    return true;
  else
    return false;
}

//--------------------------------------------------------------------------------
// Function      : LimitLength
// Description   : Limits the length of an elements content
// Parameters IN : objTextArea - The text area element
//                 nMaxLength - the length to limit the field to
//           OUT : None
//        IN/OUT : None
// Return Value  : None
//--------------------------------------------------------------------------------
function LimitLength(objTextArea, nMaxLength) {
  if (objTextArea.value.length > nMaxLength)
    objTextArea.value = objTextArea.value.substr(0, nMaxLength);
}

//-------------------------------------------------------------------------------
// Function      : FormatToDecimalPlaces
// Description   : This function is used to format a character string representing
//                 a number into a specificed number of decimal places.
// Parameters IN : sNumber            - String containing the number to format
//                 nRequiredNoOfDecimalPlaces - The number of decimal places to format to
//           OUT : None
//        IN/OUT : None
// Return Value  : The formatted number
//-------------------------------------------------------------------------------
function FormatToDecimalPlaces(sNumber, nRequiredNoOfDecimalPlaces) {
  
  var nDecimalPos;
  var nCurrentNoOfDecimalPlaces;
  
  // See if there is currently a decimal place in the number
  sNumber += "";
  nDecimalPos = sNumber.search("\\.");
  
  if (nDecimalPos == -1) // No decimal place
  {
    sNumber += "."
    
    for (var i = 0; i < nRequiredNoOfDecimalPlaces; i++) 
    {
      sNumber += "0";
    }
  }
  else // There is a decimal place
  {
    nCurrentNoOfDecimalPlaces = sNumber.length - (nDecimalPos + 1);
    
    if (nCurrentNoOfDecimalPlaces < nRequiredNoOfDecimalPlaces)
    {
      // Add additional decimal places
      for (; nCurrentNoOfDecimalPlaces < nRequiredNoOfDecimalPlaces; nCurrentNoOfDecimalPlaces++) 
      {
        sNumber += "0";
      }
    }
    else
    {
      sNumber = sNumber.slice(0, sNumber.length - (nCurrentNoOfDecimalPlaces - nRequiredNoOfDecimalPlaces))
    }
  }
  
  return sNumber;
}

//-------------------------------------------------------------------------------
// Function      : stripChars
// Description   : This function is used to strip out all illegal characters.
//		      Attach to any fields that need characters removing.
// Parameters IN : pattern - pattern to be stripped from string. Pattern will be one
//				  of the above constants.
//                 str - string to be validated
//
//           OUT : None
//        IN/OUT : None
// Return Value  : The validated string
//-------------------------------------------------------------------------------

function stripChars(pattern, str) 
{
  if(str != null)
    return str.replace(pattern,"");
  else
    return str;
}


