// --------------------------------------------------------------
// This file is copyright Mona M. Everett, Ph.D., 1998-2006
// Permission to use this include file is granted as long as 
// this copyright notice stays intact.  Permission to use 
// individual functions is granted as long as this copyright 
// notice is included.
// This file contains functions for manipulating strings
// or functions which are accomplished with string manipulation
// Created      :     17 Nov 98
// Creator      :     Mona M. Everett
// Requires     :
// Version      :     1.0
// Changes      :
// Functions    :     isEmpty(what)
//                    nbsp(what)
//										trim(what, how)
//                    getNumeric(what,how,exceptions)
//                    strip(what,ofwhat)
//										cleanStr(astr)
//										replaceit(astr,findseq,replsec,limit)
// --------------------------------------------------------------

function isEmpty(what)
{
    // pass it a string; if the string is empty, it passes back true
    // otherwise, it returns false
    // requires trim function
    what = '' + what
    if ( trim(what,'both') == '' ) { return true }
    else { return false }
}


function nbsp(what)
{
    // pass it a string; if the string is empty, it passes back a non-breaking space
    // requires trim function
    what = '' + what
    if ( trim(what,'both') == '' ) { what = '&nbsp;' }
    return what
}

function trim(what,how)
{
//  Trims blank spaces from [left,right,both] ends of the string 
//  How can be 'left', 'right', or 'both'                    
    //what = what.toString()
    what = ' ' + what
    if ( what == ' undefined') { what = '' }
    if ( what == '' ) { what = '&nbsp' ; return '' }     
    // how is left,right,both
    //alert('-' + what + '-')
    var achar,astr
    how = how + ''
    if ( (how == '') || ( how == 'undefined' ) )
    { how = 'both' }
    how = how.substring(0,1)
    how.toLowerCase()
    astr = what
    var lstr = astr.length
    if ( how != 'r' )
    {
       achar = ''
       do 
       {
            achar = astr.substring(0,1)
            //alert('#' + achar + '#')
            if (achar != ' ' ) { break }
            else { astr = astr.substring(1,lstr) }
            //document.write(astr + '<BR>' )
       }  
       while ( astr != '' )                                             
    }  // end if how not equal to right
    
    if ( how != 'l' )
    {
       achar = ''
       do 
       {
            lstr = astr.length
            achar = astr.substring(lstr - 1,lstr)
            //alert('-' + achar + '-' )
            if (achar != ' ' ) { break }
            else { astr = astr.substring(0,lstr - 1 ) }
            //document.write(astr + '<BR>' )
       }  
       while ( astr != '' )
    }  // end if how not equal to right
    
    
    return astr
} // end function

function getNumeric(what,how,exceptions)
{
//  Returns true/false if how = 'check'
//  Returns a string with characters which are
//     either numberic or in the exception string.
//  Exceptions is a string of characters which
//     should be allowed in addition to the digits 0-9
    var exstr = '0123456789' + exceptions
    var hchar = how.toLowerCase()
    hchar = how.substring(0,1)
    if ( (hchar != 'c') && (hchar != 's') ) { hchar = 's' }
    what = '' + what
    var whatlen = what.length
    var newstr = ''
    var ii = 0
    var isNum = true
    var achar = ''
    var xx = 0
    for (ii = 0 ; ii < whatlen ; ii++)
    {
       achar = what.substring(ii,ii + 1  )
       //alert('achar is ' + achar)
       xx = exstr.indexOf(achar)
       if ( xx < 0 )
       { 
         if ( hchar == 'c' )
         {
            isNum = false
            return isNum
            break
         }
       }
       else
       {
          if ( hchar == 's' )
          {
            newstr += achar
          }
       }  // end achar found or not
         
    } // end for loop
    if ( hchar == 'c' ) { return isNum }
    else if ( hchar == 's' ) {return newstr}
} // end function

function strip(what,ofwhat)
{
 	what = trim(what)
	var l = ofwhat.length
	var achar = ''
		for ( var ii = 0 ; ii< l-1 ; ii++ )
		{
		 	achar = ofwhat.substr(ii,1)
			re = '/' + achar + '/gi'
			what = what.replace(re,'')  
		} 	
}

      function cleanStr(astr)
      {
        var newstr = ""
        var len = astr.length
        var achar
        var acode
        for( var ii = 0 ; ii < len ; ii++ )
        {
          acode = astr.charCodeAt(ii)
          if ( acode >= 32 )
          {
            newstr += astr.substr(ii,1)
          }
        }
        return newstr
      }

function replaceit(astr,findseq,replsec,limit)
{
	var x = 0
	var newstr = ''
	var counter = 0 
	while( x != -1 )
	{
		var x = astr.indexOf(findseq)
		if ( x != -1 )
		{
			//alert(astr.charCodeAt(x) )
			newstr += astr.substring(0,x) + replsec
			astr = astr.substr(x + 1)
		}
		else
		{
			newstr += astr
		}
		counter ++
		if ( counter > limit ) { break }
	}
  //alert( counter )
  return newstr
}   

