
function save_hilite_text( form_id, return_count, search_text ) {
 form_id.result.value = return_count + 'x ' + search_text;
 return false;
}

function hilite( search_text, color_text, color_background, object_id ) {
  if( !search_text ){
    return 0;
  }
  /* fails if first or last char is space... needed to fix this */
  search_text = search_text.replace( /^ /, '' ); 
  search_text = search_text.replace( / $/, '' ); 
  search_text = search_text.replace( /([\\|^$()[\]{}.*+?])/g, '\\$1' ); 
  if( /^\s*$/.test( search_text ) ){
    return 0;
  }
  search_text = search_text.split( /\s+/ ).join( '|' );
  var doc = window.document;
  object_id = [ object_id || doc.documentElement || doc.body ];
  var reg = new RegExp( search_text, 'gi' );
  var newspan = doc.createElement( 'span' );
  var i = 0;
  var j;
  var k;
  var l;
  var m;
  var span_count = 0;
  var t;
  newspan.style.color = '#'+color_text;
  newspan.style.backgroundColor = '#'+color_background;
  do {
    m = object_id[i];
    if( m.nodeType===3 ){
      reg.lastIndex = 0;
      l = reg.exec(m.nodeValue);
      if( l !== null ){
        k = l[0].length;
        if( reg.lastIndex > k ){
          m.splitText( reg.lastIndex - k );
          m = m.nextSibling;
        }
        if( m.nodeValue.length > k ){
          m.splitText(k);
          object_id[i++] = m.nextSibling;
        }
        t = newspan.cloneNode( true );
        t.appendChild( doc.createTextNode( l[0] ) );span_count++;
        m.parentNode.replaceChild( t, m );
      }
    }else{
      j = m.childNodes.length;
      while ( j ){
        object_id[i++] = m.childNodes.item( --j );
      }
    }
  } while( i-- );
  return span_count;
}


function unhighlight( search_text, object_id ){
  var doc = window.document;
  search_text = search_text.replace( /([\\|^$()[\]{}.*+?])/g, '\\$1' ).split( /\s+/ ).join( '|' );
  object_id = object_id || doc.documentElement || doc.body;
  var span_list = object_id.getElementsByTagName( 'span' );
  var span_index = span_list.length;
  var span_item;
  var re = new RegExp( '^' + search_text + '$', 'i' );
  while( span_index-- ){
    span_item = span_list[span_index].firstChild;
    if( span_item ){
      if( span_item.nodeType===3 && re.test( span_item.nodeValue ) ){ 
        span_list[span_index].parentNode.replaceChild( doc.createTextNode( span_item.nodeValue ), span_list[span_index] );
      }
    }
  }
  return false;
}

function hex( n ) { return ( n<16 ? '0' : '' ) + n.toString(16); }


