function findEdit( bodyText, searchTerm, wIndex ) 
{
    var startTag  = "<a href='http://www.word-bank.com/words_output.php?fns=viewWord&wordID="; 
    startTag += wIndex;
    startTag += "'";
    startTag += 'class="wordBankLinks"';   
    startTag += 'target="page"';
    startTag += 'onClick="window.open(';
    startTag += "'','page','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=505,height=400,left=50,top=50,titlebar=yes')"; 
    startTag += '">';    
    var endTag    = "</a>";
    
    var newHTML = "";
    var i = -1;
    var lcSearchTerm = searchTerm.toLowerCase();
    var lcBodyText   = bodyText.toLowerCase();
        
    while (bodyText.length > 0) 
    {
        i = lcBodyText.indexOf(lcSearchTerm, i+1);
        if (i < 0) {
            newHTML += bodyText;
            bodyText = "";
        } 
        else 
        {
            // skip anything inside an HTML tag
            if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) 
            {
                // skip anything inside a <script> block
                 if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
		      var extendedMatch = bodyText.substr(i-1,searchTerm.length+2);
		      var exp = "\\b" + searchTerm + "\\b";
		      var re = new RegExp(exp,"i");
		      if (extendedMatch.match(re)) {
                          newHTML   += bodyText.substring(0, i) + startTag + bodyText.substr(i, searchTerm.length) + endTag;
		      }
	              else {
                          newHTML   += bodyText.substring(0, i) + bodyText.substr(i, searchTerm.length);
		      }
                      // newHTML   += bodyText.substring(0, i) + startTag + bodyText.substr(i, searchTerm.length) + endTag + "COMPARE:" + bodyText.substr(i-1,searchTerm.length+2) + ":COMPARE MATCHES:" + exp + ":MATCHES";
                      bodyText   = bodyText.substr(i + searchTerm.length);
                      lcBodyText = bodyText.toLowerCase();
                        i = -1;
                  };
            };
        };
    };
    
    return newHTML;
}

function wordLinker( searchText, wIndex )
{
    if ( !document.body || typeof( document.body.innerHTML) == "undefined" ) 
    {
        if ( warnOnFailure ) 
        {
            alert("Sorry, there was an error while linking the Word");
        }
        return false;
    }
  
    var bodyText = document.body.innerHTML;
    bodyText     = findEdit( bodyText, searchText, wIndex );  
    document.body.innerHTML = bodyText;
    return true;
}

function populateLinks()
{
    for(i = 1; i < wordData.length; i ++)
    {
        if ( wordData[i] != null ) 
        {
            wordLinker( wordData[i], i );
        };
    };
}
