function addOnOff(){
	var name="onOff";
	//alert("LOCATION = " + document.location.href);
	var start=location.href.indexOf("?"+name+"=");
	if (start<0) start=location.href.indexOf("&"+name+"=");
	//alert("START = " + start + " .. CHAR AT = " + location.href.charAt(start));
	var end=location.href.indexOf("&",start+1)-1;
	if (end<0) end=location.href.length;
	var result='';
	var str 
	if (start <0){
		str = location.href.substring(0, location.href.length);
	}
	else {
		str = location.href.substring(0, start+1);
	}
	//alert("STR BEFORE =" + str);
	if (str.indexOf("?") < 0) str += "?";
	//alert("STR BEFORE 1 =" + str);
	if (end+2 < location.href.length) str += location.href.substring(end+2, location.href.length); 
	if ((str.charAt(str.length-1) != "?") && (str.charAt(str.length-1) != "&")) str += "&";
	//alert("STR AFTER =" + str);
	//alert("OPERA " + document.f1.onOff.value);
	document.write("<font face='arial' size='2'>Word Bank <img src='http://www.word-bank.com/images/external.png' width='1' height='1' alt='Turn Word Bank On'><a href='" + str + "onOff=ON'>ON</a> | <img src='http://www.word-bank.com/images/external.png' width='1' height='1' alt='Turn Word Bank Off'><a href='" + str + "onOff=OFF'>OFF</a></font>");
}

function findEdit( bodyText, searchTerm, wIndex )
{
//var startTag  = "<img src='http://www.word-bank.com/images/external.png' width='1px' height='1px' alt='Definition of: '><a href='http://www.word-bank.com/words_outputEBB.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,resizable=yes,menubar=0,width=510,height=590,left=50,top=50,titlebar=yes')";
//startTag += '">';
//var endTag    = "</a><img src='http://www.word-bank.com/images/external.png' width='1px' height='1px' alt=', opens in new window'>";

var startTag  = "<img src='http://www.word-bank.com/images/external.png' width='1px' height='1px' alt='Definition of: '><a href='http://www.enabledglossary.com/words_output_test.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,resizable=yes,menubar=0,width=510,height=590,left=50,top=50,titlebar=yes')";
startTag += '">';
var endTag    = "</a>";


    var newHTML = "";
    var i = -1;
    var lcSearchTerm = searchTerm.toLowerCase();
    var lcBodyText   = bodyText.toLowerCase();
    var startScript = "<script ";
    var endScript = "/script>";
    var startLink = "<a ";
    var endLink = "/a>";
    var startWBExclude;
    if ( navigator.appName == "Microsoft Internet Explorer" ) {
        startWBExclude = 'wb_exclude';
    }
    else {
        startWBExclude = '<span class="wb_exclude"';
    }
    var endWBExclude = "</span>";

    while (bodyText.length > 0)
    {
        i = lcBodyText.indexOf(lcSearchTerm, i+1);
        if (i < 0) {
            newHTML += bodyText;
            bodyText = "";
        }
        else
        {
            if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i))
            {
                // make sure we're not in a <script> tag
                //   1) not seen a startScript tag at all
                //   2) we've seen a startScript tag but have seen an endScript tag more recently
                 if ( (lcBodyText.lastIndexOf(startScript,i) < 0) ||
                      (lcBodyText.lastIndexOf(endScript,i) > lcBodyText.lastIndexOf(startScript,i) ) ) {
                      // make sure we're not in a <a href> tag
                      //   1) not seen a startlink tag at all
                      //   2) we've seen a startlink tag but have seen an endLink tag more recently
                      if ( (lcBodyText.lastIndexOf(startLink,i) < 0) ||
                           (lcBodyText.lastIndexOf(endLink,i) > lcBodyText.lastIndexOf(startLink,i) ) ) {
                          // make sure we're not in a <span class='wbExclude'> tag
                          if ( (lcBodyText.lastIndexOf(startWBExclude,i) < 0) ||
                               (lcBodyText.lastIndexOf(endWBExclude,i) > lcBodyText.lastIndexOf(startWBExclude,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);
                              }

                              bodyText   = bodyText.substr(i + searchTerm.length);
                              lcBodyText = bodyText.toLowerCase();
                              i = -1;
                          };
                      };
                  };
            };
        };
    };

    return newHTML;
}

function wordLinker( searchText, wIndex, bodyText )
{
    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;
    //alert("Search Text = " + searchText + " index of = " + bodyText.toLowerCase().indexOf( searchText.toLowerCase() ));
    var newSearchText = searchText;
    //var endTag    = "</a>";
    //if (searchText.toLowerCase().indexOf(" ") >= 0){
	//var searchIndex = searchText.toLowerCase().indexOf(" ");
	//alert("Search Index = " + searchIndex);
    	//newSearchText = searchText.substr(0, searchIndex) + endTag + searchText.substr(searchIndex, searchText.length);
	//alert(newSearchText);
    //}
    //if ( bodyText.toLowerCase().indexOf( searchText.toLowerCase() ) >= 0 )
    if ( bodyText.toLowerCase().indexOf( newSearchText.toLowerCase() ) >= 0 )
    {
        bodyText     = findEdit( bodyText, newSearchText, wIndex );  
        document.body.innerHTML = bodyText;
    }
    return true;
}

function populateLinks()
{
	var name="onOff";
	//alert("LOCATION = " + document.location.search);
	var start=location.search.indexOf("?"+name+"=");
	if (start<0) start=location.search.indexOf("&"+name+"=");
	start += name.length+2;
	var end=location.search.indexOf("&",start)-1;
	if (end<0) end=location.search.length;
	var result='';
	for(var i=start;i<=end;i++) {
		var c=location.search.charAt(i);
		result=result+(c=='+'?' ':c);
	}
	//alert(unescape(result));
	var str=unescape(result);

	var newArr = new Array();
	var start=0;
	var rest=wordData.length;
	//alert("REST = " + rest);
	var x = 0;
	for (var i = 0; i < rest; i++){
		if ( wordData[i] != null ){
			//alert("Word Data = " + wordData[i]) ;
			newArr[x] = i + "|" + wordData[i] ;
			//alert(" X = " + x + " ... new arr = " + newArr[x]);
			x++ ;
		}
	}
	var currStr, nextStr ;
	rest = newArr.length ;
	for (var i = rest - 1; i >= start;  i--) {
		for (var j = start; j < i; j++) {
			//alert("J =" + j);
			currStr = newArr[j].substring(newArr[j].indexOf("|")+1, newArr[j].length);	
			nextStr = newArr[j+1].substring(newArr[j+1].indexOf("|")+1, newArr[j+1].length);	
			//alert("STR = " + currStr + " ... NEW STR = " + nextStr);
			//if (wordData[j+1].length < wordData[j].length) {
			if (nextStr.length < currStr.length) {
				//alert(wordData[j] + " ... " + wordData[j+1]);
				//alert("wordDate " + j + " 1 = " + wordData[j+1].length + " Word Data " + j + " = " +  wordData[j].length);
				//var tempValue = wordData[j];
				//wordData[j] = wordData[j+1];
				//wordData[j+1] = tempValue;
				var tempValue = newArr[j];
				newArr[j] = newArr[j+1];
				newArr[j+1] = tempValue;
			}
		}
	}
	
	currStr='';
	var wordIndex ;

	//if (str == "ON" || str == ""){
	if (str == ""){
		//alert("In If");
		var cookieVal = getCookie("wordbankcookie");
		// if (cookieVal == "ON" || cookieVal == null){
		//alert("Cookie Val = " + cookieVal);
		if (cookieVal == "ON"){
			alert("In NExt If");
			setCookie("wordbankcookie", "ON", 1);
			//for(i = 1; i < wordData.length; i ++)
			//for(i = wordData.length; i > 0; i --)
			for(i = newArr.length - 1; i >= 0; i --)
			{
				currStr = newArr[i].substring(newArr[i].indexOf("|")+1, newArr[i].length);
				wordIndex= newArr[i].substring(0, newArr[i].indexOf("|"));
				//alert("Word Index = " + wordIndex);
				//if ( wordData[i] != null ) 
				//{
					//alert(wordData[i] + " ... " + i);
					//wordLinker( wordData[i], i );
					wordLinker( currStr, wordIndex );
				//};
			};
		}
		else{
			setCookie("wordbankcookie", "OFF", 1);
		}
	}
	else{
		if (str == "ON"){
			//alert("In Else");
			setCookie("wordbankcookie", "ON", 1);
			//for(i = 1; i < wordData.length; i ++)
			//for(i = wordData.length; i > 0; i --)
			for(i = newArr.length - 1; i >= 0; i --)
			{
				currStr = newArr[i].substring(newArr[i].indexOf("|")+1, newArr[i].length);
				wordIndex= newArr[i].substring(0, newArr[i].indexOf("|"));
				//alert("Word Index = " + wordIndex);
				//if ( wordData[i] != null ) 
				//{
					//alert(wordData[i] + " ... " + i);
					//wordLinker( wordData[i], i );
					wordLinker( currStr, wordIndex );
				//};
			};
		}
		else{
			setCookie("wordbankcookie", "OFF", 1);
		}
	}
}

function getCookie(name){
	var start = document.cookie.indexOf(name+"="); 
	var len = start+name.length+1; 
	if ((!start) && (name != document.cookie.substring(0,name.length))) return null; 
	if (start == -1) return null; 
	var end = document.cookie.indexOf(";",len); 
	if (end == -1) end = document.cookie.length; 
	return unescape(document.cookie.substring(len,end)); 
}

function setCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
}

