function trim( str ) {
  while( str.indexOf( " " ) == 0 ) {
    if( str.length > 1 ) {
      str = str.substr( 1, str.length-1 );
    } else {
      str = "";
    }
  }
  while( str.length > 0 && str.lastIndexOf( " " ) == str.length-1 ) {
    if( str.length > 1 ) {
      str = str.substr( 0, str.length-1 );
    } else {
      str = "";
    }
  }
  return( str );
}

function encode( str ) {
  app = navigator.appName.charAt(0);
  ver = navigator.appVersion.charAt(0);
  if (app == "N" || ver < 4) {
    str = escape(str);
  } else {
    // IE ver4 or greater can't use escape function, so encode "%","&"
    frpnt = -3;
    com = "";
    while( (frpnt = str.indexOf( "%", frpnt+3 )) >= 0 ) {
      str = str.substr( 0, frpnt ) + "%25" +
            str.substr( frpnt+1, str.length-(frpnt+1) );
    }
    while( str.indexOf( "&" ) >= 0 ) {
      str = str.replace( "&", "%26" );
    }
  }
  // the escape function can't encode "+"
  while( str.indexOf( "+" ) >= 0 ) {
    str = str.replace( "+", "%2B" );
  }
  // encode " " to "+" (replace the space with the plus symbol)

  while( str.indexOf( " " ) >= 0 ) {
    str = str.replace( " ", "+" );
  }
  return( str );
}
