
<!--
/*------------------------------------------------------------------------------
 *
 * File Name:   javautil.inc
 *
 * Description: This include file contains a library of javascript functions
 *
 * Notes:
 *
 *------------------------------------------------------------------------------
 *
 * History
 *
 * Date      Author          Description
 * --------  --------------  ---------------------------------------------------
 * 10/06/97  J McGraw        New
 * 10/10/97  J McGraw        Added banner
 * 10/16/97  J McGraw        Add new common functions; changed nSearch function
 * 10/17/97  J McGraw        Added script tags
 * 11/03/97  P Cardillo      Rewrote trim function; modified bNumbers to be more
 *                           flexible; added function sCharStrip
 * 11/06/97  J McGraw        Added bAlphNumeric and bcheckIfAlpha
 * 11/14/97  P Cardillo      Added function bIsAnInteger; fixed a bug in the
 *                           trim function; added function bIntInRange; added
 *                           function nStringToInt
 * 11/16/97  P Cardillo      Added bIsDate
 * 11/17/97  P Cardillo      Completed bIsDate - added year and leap year
 *                           check; added function bIsDecimal, nCountChars;
 *                           renamed bIsAnInteger to bIsInteger
 * 11/18/97  P Cardillo      Added function bIsAlphaNumeric
 * 11/18/97  P Cardillo      Added function bIsAlphaNumSpaces
 * 11/26/97  P Cardillo      Added functions showHelp and getCurrentPath;
 *                           reformatted this file, cleaned comments, function
 *                           headers, etc.; added application-level "smarts"
 *                           to the help facility
 * 12/15/97  P Cardillo      Removed showHelp and getCurrentPath
 * 01/30/98  P Cardillo      Fixed bug in bIsDecimal - was handling the
 *                           precision incorrectly
 * 08/21/98  P Cardillo      Changed nStringToInt to eliminate leading zeroes
 *                           which were being interpreted as octal values
 * 10/08/98  P Cardillo      Fixed bug in nStringToInt - it was stripping all
 *                           zeroes from integer strings rather than just the
 *                           leading zeroes
 * 11/24/99  P Cardillo      Fixed bug in trim function - apparently, in 
 *                           IE4-sp2 if the end index becomes negative, the 
 *                           function still thinks it has valid character - this 
 *                           was causing IE4-sp2 to hang
 *
 *----------------------------------------------------------------------------*/


/*------------------------------------------------------------------------------
 * sCharStrip - removes all occurrences of a specified character from a string
 *----------------------------------------------------------------------------*/

function sCharStrip( sSource, sChar ) {
  var sDest = "";
  var sDestIdx = 0;
  var sSourceIdx = 0;

  for ( ; sSourceIdx < sSource.length; sSourceIdx++ )
    if ( sSource.charAt( sSourceIdx ) != sChar ) {
      sDest += sSource.charAt( sSourceIdx );
      sDestIdx++;
      }

  return sDest;
  }


/*------------------------------------------------------------------------------
 * trim
 *----------------------------------------------------------------------------*/

function trim( target ) {
  var sidx = 0;
  var eidx = target.length - 1;

  while ( (target.charAt( sidx ) == ' ') && (sidx < target.length) ) sidx++;

  while ( (target.charAt( eidx ) == ' ') && (eidx >= 0) ) eidx--;

  // If eidx became negative, it means the whole string is blanks; if this is
  // the case, the substring function will switch sidx and eidx (go figure), so
  // we check for this condition and return an empty string if necessary

  target = (eidx < 0) ? "" : target.substring(sidx, eidx+1);

  return target;
  }


/*------------------------------------------------------------------------------
 * nSearch - RETURNS LOCATION OF TERM IN TARGET STRING; IF NOT FOUND, RETURNS -1
 *----------------------------------------------------------------------------*/

function nSearch(target,term)
{
  var nLocator=0

  if (target.length == 0)
     return -1;

  nLocator=target.indexOf(term,0)

  if (nLocator >= 0)
     return nLocator
  else
     return -1;
}


/*------------------------------------------------------------------------------
 * sDeletePreceedingTerms - RETURNS SWORK WITH ALL PRECEEDING TERM STRINGS
 *                          DELETED
 *----------------------------------------------------------------------------*/

function sDeletePreceedingTerms(sWork,term)
{
   while (nSearch(sWork,term)==0) {
      sWork=(sWork.substring(sWork.length,term.length));
   }
   return sWork;
}


/*------------------------------------------------------------------------------
 * sDeleteFollowingTerms - RETURNS SWORK WITH ALL TRAILING TERM STRINGS DELETED
 *----------------------------------------------------------------------------*/

function sDeleteFollowingTerms(sWork,term)
{
   while (sWork.lastIndexOf(term,sWork.length) >= 0) {
      if (sWork.lastIndexOf(term,sWork.length) < (sWork.length - term.length))
          break;
      sWork = sWork.substring(0,(sWork.length-term.length));
   }
    return sWork;
}


/*------------------------------------------------------------------------------
 * sGetNextString - RETURNS NEXT STRING NOT EQUAL TO TERM; DELETES STRING
 *                  RETURNED AND TERM FROM TARGET MUST ASSIGN A PAGE LEVEL
 *                  VARIABLE sNewWorkString
 *----------------------------------------------------------------------------*/

function sGetNextString(target,term)
{
  var sCleanString="";
  var nNewLocator=0;

  target = sDeletePreceedingTerms(target,term);
  target = sDeleteFollowingTerms(target,term);

  //MAKE SURE ANY DATA LEFT
  if (target.length == 0)  {
     sNewWorkString="";
     sCleanString="";
     return sCleanString;
  }

  //DATA AVAILABLE SO CONTINUE

  nNewLocator=nSearch(target,term)

  //CASE 1:  NO MORE TERM STRINGS
  //THEREFOR, AT LAST STRING

  if (nNewLocator == -1)  {
     sNewWorkString="";
     return target;
  }

  //CASE 2:  AT LEAST ONE MORE TERM STRING IN TARGET

  sCleanString=target.substring(nNewLocator,0);
  sNewWorkString=target.substring(target.length,nNewLocator+term.length);
  return sCleanString;

}


/*------------------------------------------------------------------------------
 * bcheckIfNumber - RETURNS TRUE IF DIGIT PASSED IS NUMERIC; OTHERWISE, RETURNS
 *                  FALSE
 *----------------------------------------------------------------------------*/

function bcheckIfNumber(digit) {
  var nNums="0123456789";
  var j;

  for ( j=0; j<=9; j++ )
    if(digit==nNums.charAt(j))
      return true;

  return false;
}


/*------------------------------------------------------------------------------
 * bNumbers - returns true if each digit in target is numeric; otherwise,
 *            returns false
 *----------------------------------------------------------------------------*/

function bNumbers( target ) {
  var i;

  for ( i=0; i < target.length; i++ )
    if ( ! bcheckIfNumber( target.charAt(i) ) )
      return false;

  return true;
  }


/*------------------------------------------------------------------------------
 * RETURNS TRUE IF DIGIT PASSED IS ALPHA; OTHERWISE, RETURNS FALSE
 *----------------------------------------------------------------------------*/

function bcheckIfAlpha(digit) {
  var sAlph="abcdefghijklmnopqrstuvwxyz";
  var j;

  for ( j=0; j<=25; j++ )
    if ( digit == sAlph.charAt(j) )
       return true;

  return false;
}


/*------------------------------------------------------------------------------
 * BALPHANUMERIC - RETURNS TRUE IF EACH DIGIT IN TARGET IS ALPHANUMERIC;
 * OTHERWISE, RETURNS FALSE
 *----------------------------------------------------------------------------*/

function bAlphaNumeric( target ) {
  var i;

  for ( i=0; i < target.length; i++ )
    if ( ! bcheckIfNumber( target.charAt(i) ) && ! bcheckIfAlpha( target.charAt(i) ) )
      return false;

  return true;
  }


/*------------------------------------------------------------------------------
 * bIsInteger - Determines if the value passed is an integer
 *----------------------------------------------------------------------------*/

function bIsInteger( sValue ) {
  var i = 0;

  // Check the length of the value passed in; if it's zero, that's not a number

  if ( (sValue = trim(sValue)).length == 0 ) return false;

  // Now check that each character in the string is a digit

  for ( ; i < sValue.length; i++ )
    if ( (sValue.charAt(i) < "0") || (sValue.charAt(i) > "9") )
      return false;

  return true;
  }


/*------------------------------------------------------------------------------
 * bIntInRange - Determines if the value passed is within the specified range
 *----------------------------------------------------------------------------*/

function bIntInRange( nValue, nLlimit, nUlimit ) {

  if ( (nValue < nLlimit) || (nValue > nUlimit) )
    return false;

  return true;
  }


/*------------------------------------------------------------------------------
 * nStringToInt - Converts the string representation of an integer to an integer
 *----------------------------------------------------------------------------*/

function nStringToInt( sValue ) {

  var nInt;
  var sIntNoZero = '';

  // convert sValue to a non-zero leading value since this is interpreted as
  // octal by Jscript

  for ( i = 0; i < sValue.length; i++ )
    if ( sValue.substring( i, i+1 ) != '0' ) {
      sIntNoZero = sIntNoZero + sValue.substring( i, sValue.length + 1 );
      break;
      }

  // convert sIntNoZero to integer if the value is a valid integer value

  if ( bIsInteger( sIntNoZero) )
    nInt = parseInt( sIntNoZero );
  else
    nInt = 0;

  return nInt
  }


/*------------------------------------------------------------------------------
 * bIsDate - Checks the passed value to determine if it is a valid date in the
 *           form mm/dd/yyyy
 *----------------------------------------------------------------------------*/

function bIsDate( sDate ) {
  var i = 0;
  var nPos = 0;
  var nPrevPos = 0;
  var sMonth;
  var nMonth;
  var sDay;
  var nDay;
  var sYear;
  var nYear;
  var bDateVvalid;
  var nLength;
  var anDaysInMonth = new Array(13);

  anDaysInMonth[1] = 31;
  anDaysInMonth[2] = 28;
  anDaysInMonth[3] = 31;
  anDaysInMonth[4] = 30;
  anDaysInMonth[5] = 31;
  anDaysInMonth[6] = 30;
  anDaysInMonth[7] = 31;
  anDaysInMonth[8] = 31;
  anDaysInMonth[9] = 30;
  anDaysInMonth[10] = 31;
  anDaysInMonth[11] = 30;
  anDaysInMonth[12] = 31;

  // If a null string passed, return false now

  if ( (nLength = sDate.length) == 0 ) return false;

  //----------------------------------------------------------------------------
  // Locate the first "/" and pick off the month value
  //----------------------------------------------------------------------------

  nPos = sDate.indexOf( "/", nPrevPos );

  // nPos must be greater than nPrevPos for there to have been at least one
  // character before the "/"

  bDateValid = false;

  if ( nPos > nPrevPos ) {
    sMonth = sDate.substring( nPrevPos, nPos );
    nMonth = nStringToInt( sMonth );
    if ( (nMonth >= 1) && (nMonth <= 12) ) {
      nPrevPos = nPos + 1;

      // We shouldn't be at the end of the string yet

      if ( nPrevPos < nLength ) bDateValid = true;

      }
    }

  if ( ! bDateValid ) return false;

  //----------------------------------------------------------------------------
  // Locate the next "/" and pick off the day value
  //----------------------------------------------------------------------------

  nPos = sDate.indexOf( "/", nPrevPos );

  // nPos must be greater than nPrevPos for there to have been at least one
  // character before the "/"

  bDateValid = false;

  if ( nPos > nPrevPos ) {
    sDay = sDate.substring( nPrevPos, nPos );
    nDay = nStringToInt( sDay );
    if ( (nDay >= 1) && (nDay <= 31) ) {

      nPrevPos = nPos + 1;

      // We shouldn't be at the end of the string yet

      if ( nPrevPos < nLength ) bDateValid = true;

      }
    }

  if ( ! bDateValid ) return false;

  //----------------------------------------------------------------------------
  // Get the remaining characters in the date string and check these for a
  // valid year
  //----------------------------------------------------------------------------

  sYear = sDate.substring( nPrevPos, nLength );

  nYear = nStringToInt( sYear );

  if ( (nYear < 1900) || (nYear > 2037) )           // SQL server limitations
    return false;

  //----------------------------------------------------------------------------
  // Now that we have some sort of valid date, we need to check that the date
  // is real.  i.e that it exists in a non leap year, that the day is correct
  // for the month etc.
  //
  // First we'll determine if the year is a leap year and set february's days
  // Next, we'll check the day value against the anDaysInMonth array
  //----------------------------------------------------------------------------

  if ( (((nYear % 4) == 0) && ((nYear % 100) != 0)) || ((nYear % 400) == 0) )
    anDaysInMonth[2] = 29;

  if ( nDay <= anDaysInMonth[nMonth] ) return true;               // Valid date

  return false;
  }


/*------------------------------------------------------------------------------
 * bIsDecimal - Determines if the value passed is decimal
 *----------------------------------------------------------------------------*/

function bIsDecimal( sValue, nPrec, nScale ) {
  var i = 0;
  var nCount;
  var nLength;
  var nPos;

  // Check the length of the value passed in; if it's zero, it's not a decimal

  if ( (nLength = (sValue = trim(sValue)).length) == 0 )
    return false;

  // Now check that each character in the string is a digit (or a period)

  for ( ; i < sValue.length; i++ )
    if ( (sValue.charAt(i) != ".") &&
         ((sValue.charAt(i) < "0") || (sValue.charAt(i) > "9")) )
      return false;

  // Now check if we have more than one decimal point

  if ( (nCount = nCountChars(sValue, ".")) > 1 )
    return false;

  // Now check the precision and scale

  if ( nCount == 1 ) {
    nPos = sValue.indexOf( "." );
    if ( (nPos > (nPrec - nScale)) || ((nLength - nPos - 1) > nScale) ) return false;
    }
  else
    if ( nLength > (nPrec - nScale) ) return false;

  return true;
  }


/*------------------------------------------------------------------------------
 * nCountChars - Counts the number of occurrences of a character within a given
 *               string
 *----------------------------------------------------------------------------*/

function nCountChars( sValue, sChar ) {
  var i = 0;
  var nCount = 0;

  // Search for each occurrence of sChar within sValue

  for ( ; i < sValue.length; i++ )
    if ( sValue.charAt(i) == sChar ) nCount++;

  return nCount;
  }



/*------------------------------------------------------------------------------
 * bIsAlphaNumeric - Determines if the value passed is alpha numeric
 *----------------------------------------------------------------------------*/

function bIsAlphaNumeric( sValue ) {
  var i = 0;

  // Check the length of the passed value; if it's zero, it's not a valid check

  if ( (sValue = trim(sValue)).length == 0 ) return false;

  // Now check that each character in the string is alpha numeric

  for ( ; i < sValue.length; i++ )
    if ( ((sValue.charAt(i) < "0") || (sValue.charAt(i) > "9")) &&
         ((sValue.charAt(i) < "a") || (sValue.charAt(i) > "z")) &&
         ((sValue.charAt(i) < "A") || (sValue.charAt(i) > "Z")) )
      return false;

  return true;
  }


/*------------------------------------------------------------------------------
 * bIsAlphaNumSpaces - Determines if the value passed is alpha numeric,
 *                     including spaces
 *----------------------------------------------------------------------------*/

function bIsAlphaNumSpaces( sValue ) {
  var i = 0;

  // Check the length of the passed value; if it's zero, it's not a valid check

  if ( (sValue = trim(sValue)).length == 0 ) return false;

  // First strip the spaces out, what remains should be pure alpha numeric

  sValue = sCharStrip( sValue, " " );

  if ( ! bIsAlphaNumeric(sValue) ) return false;

  return true;
  }


//-->


