// set of javascript functions that mimic the coldfusion list functions.

/*
* http://scripts.e-linewebsolutions.nl/scr_jslist.js
*
* @author Rens van Leeuwen | rens@e-linewebsolutions.nl
* @author Auke van Leeuwen | auke@e-linewebsolutions.nl
* @author Mingo Hagen      | mingo@e-linewebsolutions.nl
* @version 0.1.1 - 11/15/2001 11:58:00 PM
*/

/**
* function retrieves an element form a list
*
* @param lTheList   | string
* @param nIndex     | integer
* @param sSeparator | character ( default value is a comma. )
* 
* i am very well aware that this is not the most elegant way to retrieve 
* elements from a list. however, this was done quick 'n dirty, and it does
* seem to work right now ..
*/
function listGetAt( lTheList, nIndex )
{
  lTheList = trim( lTheList );
  sSeparator = ( listGetAt.arguments.length == 2 ) ? ',' : listGetAt.arguments[2];

  var sCurrentElement = "";
  var sCurrentChar    = "";
  var nCurrentElement = 0;
  var i;
  
  for( i = 0; i < lTheList.length; i++ )
  {
    sCurrentChar = lTheList.charAt( i );

    if(( nCurrentElement == nIndex ) &&
       ( sCurrentChar != sSeparator ))
    {
      sCurrentElement += sCurrentChar;
    }
    
    if( sCurrentChar == sSeparator )
    {
      if( nCurrentElement == nIndex )
      {
        return( sCurrentElement );
      }
      else
      {
        nCurrentElement++;
      }
    }
  }
  
  if( nCurrentElement == nIndex )
  {
    return( sCurrentElement );
  }
 
  return( "" );
}

/**
* listFind
*/
function listFind( lTheList, sValue )
{
  sSeparator = ( listFind.arguments.length == 2 ) ? ',' : listFind.arguments[2];
  
  var i;
  var nListLen = listLen( lTheList, sSeparator );
  
  for( i = 0; i < nListLen; i++ )
  {
    if( listGetAt( lTheList, i, sSeparator ) == sValue )
    {
      return( i );
    }
  }
  
  return( -1 );
}

/**
* listAppend
*/
function listAppend( lTheList, sValue )
{
  sSeparator = ( listAppend.arguments.length == 2 ) ? ',' : listAppend.arguments[2];
  
  if( listLen( lTheList, sSeparator ) == 0 )
  {
    return( sValue );
  }
  
  return( lTheList + sSeparator + sValue );
}

/**
* listPrepend
*/
function listPrepend( lTheList, sValue )
{
  sSeparator = ( listPrepend.arguments.length == 2 ) ? ',' : listPrepend.arguments[2];
  
  if( listLen( lTheList, sSeparator ) == 0 )
  {
    return( sValue );
  }
  
  return( sValue + sSeparator + lTheList );
}

/*
* returns the number of elements in the list
*/
function listLen( lTheList )
{
  sSeparator = ( listLen.arguments.length == 1 ) ? ',' : listLen.arguments[1];
  
  lTheList = trim( lTheList );
  
  var nIndex   = 0;
  var nCounter = ( lTheList.length == 0 ) ? 0 : 1;
  
  while( lTheList.indexOf( sSeparator, nIndex ) != -1 )
  {
    nIndex = lTheList.indexOf( sSeparator, nIndex ) + 1;
    nCounter++;
  }
  
  return( nCounter );
}

/*
* self written trim function, since this one is not part of the basic 
* functions that JavaScript supports for the String object.
*/
function trim( sString )
{
  var _nStartIndex = 0;
  var _nEndIndex = sString.length - 1;
  
  /* get the index of the first char that is not a space */
  while(( sString.length != 0 ) && 
        ( _nStartIndex < sString.length ) &&
        (( sString.charAt( _nStartIndex ) == ' ' ) ||
         ( sString.charCodeAt( _nStartIndex ) == 13 ) ||
         ( sString.charCodeAt( _nStartIndex ) == 10 )))
  {
    _nStartIndex++;
  }

  /* get the index of the last char that is not a space */
  while(( sString.length != 0 ) && 
        ( _nEndIndex >= 0 ) &&
        (( sString.charAt( _nStartIndex ) == ' ' ) ||
         ( sString.charCodeAt( _nStartIndex ) == 13 ) ||
         ( sString.charCodeAt( _nStartIndex ) == 10 )))
  {
    _nEndIndex--;
  }

  return( sString.substring( _nStartIndex, _nEndIndex + 1 ));
}
