var ListArray;
var NumList = 0;

function GetStem(szWord)
{
	var aStems = new Array();
	aStems[0] = "ED";
	aStems[1] = "ES";
	aStems[2] = "ER";
	aStems[3] = "E";
	aStems[4] = "S";
	aStems[5] = "INGLY";
	aStems[6] = "ING";
	aStems[7] = "LY";

	// Loop through all the stems
	var nNumStems = aStems.length;
	var	nStemPos = 0;
	var csStem = "";
	var csSubString = "";
	for (var iStem = 0; iStem < nNumStems; iStem++) {

		// Get the "would be" position of the stem in the word and see
		// if this word has the stem
		nStemPos = szWord.lastIndexOf(aStems[iStem]);
		if (nStemPos > 0) {
			csSubString = szWord.substring(nStemPos);
			if (csSubString == aStems[iStem]) {
				
				// Found a stem

				// Check for double consonant
				csStem = szWord;
				if (szWord.charAt(nStemPos - 2) == szWord.charAt(nStemPos - 1)) {
					csStem = csStem.substring(0, nStemPos - 1);
				} else {
					csStem = csStem.substring(0, nStemPos);
				}
				return csStem;
			}
		}
	}

	// No stem found
	csStem = szWord;
	return csStem;
}

function find_fts_node(wrd)
{
	var iFirst, iLast, i, nRet;
	var indx = -1;

//		alert(wrd);
	iFirst = 0;
	iLast = numFtsNodes-1;
	while(iFirst <= iLast)
	{
		i = parseInt((iFirst + iLast) / 2);

		s2 = ftsnodes[i].word.replace("_", "-");
//			alert(s2);
		if(s2.indexOf(wrd) == 0)
		{
			indx = i;
			break;
		}
		if(wrd > s2)
		{
			iFirst = i+1;
		}
		else if(wrd < s2)
		{
			iLast = i-1;
		}
//			else
//			{
//				indx = i;
//				break;
//			}
	}
	
	return indx;
}

function addToList(indx)
{
	if(NumList == 0)
	{
		ListArray = new Array();
	}

	found = false;
	for(ii = 0; ii < NumList; ii++)
	{
		if(ListArray[ii] == indx)
			found = true;
	}
	if(!found)
	{
		ListArray[NumList] = indx;
		NumList++;
	}
}

function num_sort(ListArray, NumList)
{
	for(iii = 0; iii < NumList-1; iii++)
	{
		for(jjj = iii+1; jjj < NumList; jjj++)
		{
			if(Number(ListArray[iii]) > Number(ListArray[jjj]))
			{
				tmp = ListArray[iii];
				ListArray[iii] = ListArray[jjj];
				ListArray[jjj] = tmp;
			}
		}
	}
}

function do_search()
{
	txt = window.top.get_search_str();
	if(txt == '')	return;
	txt = txt.toUpperCase();
//alert(txt);

	wrd = GetStem(txt);
	s1 = wrd.replace("_", "-");
	indx = find_fts_node(s1);
//		alert(indx);
	NumList = 0;
	if(indx > -1)
	{
//alert(indx);
		outdoc = parent.frames["FTSBotmFrm"].document;
//			alert("Found " + ftsnodes[indx].numLinks + " Matchs");
		outdoc.write("<HTML>\n");
		outdoc.write("<HEAD>\n");
		outdoc.write(" <title>formZ API</title>\n");
		outdoc.write(" <LINK REL=stylesheet HREF=\"formZApi.css\" TYPE=\"text/css\">\n");
		outdoc.write("</HEAD>\n");
		outdoc.write("<BODY>\n");
		indx2 = indx;
		s2 = ftsnodes[indx2].word.replace("_", "-");
//alert(s2);
//alert(s1);
//alert(s2.indexOf(s1));
		while(indx2 > -1 && s2.indexOf(s1) == 0)
		{
//alert(indx2);
//alert(s2);
//alert(s1);
//alert(s2.indexOf(s1));
			for(i = 0; i < ftsnodes[indx2].numLinks; i++)
			{
				j = ftsnodes[indx2].links[i];
//					alert(j);
//					alert(numIndxTopNodes + j);
//					alert("Match " + i + ": " + indxnodes[numIndxTopNodes + j].title);
//					alert(indxnodes[numIndxTopNodes + j].link);
				addToList(j);

//					outdoc.write("<P style=\"margin-top:2.00pt;margin-bottom:1.00pt;\"><A HREF=\"files/" + indxnodes[numIndxTopNodes + j].link + "\" TARGET=\"RightFrm\">\n");
//					outdoc.write(indxnodes[numIndxTopNodes + j].title + "</P>\n");
			}
			indx2++;
			s2 = ftsnodes[indx2].word.replace("_", "-");
		}
		indx2 = indx-1;
		s2 = ftsnodes[indx2].word.replace("_", "-");
//alert(s2);
//alert(s1);
//alert(s2.indexOf(s1));
		while(indx2 > -1 && s2.indexOf(s1) == 0)
		{
//alert(indx2);
//alert(s2);
//alert(s1);
//alert(s2.indexOf(s1));
			for(i = 0; i < ftsnodes[indx2].numLinks; i++)
			{
				j = ftsnodes[indx2].links[i];
//					alert(j);
//					alert(numIndxTopNodes + j);
//					alert("Match " + i + ": " + indxnodes[numIndxTopNodes + j].title);
//					alert(indxnodes[numIndxTopNodes + j].link);
				addToList(j);

//					outdoc.write("<P style=\"margin-top:2.00pt;margin-bottom:1.00pt;\"><A HREF=\"files/" + indxnodes[numIndxTopNodes + j].link + "\" TARGET=\"RightFrm\">\n");
//					outdoc.write(indxnodes[numIndxTopNodes + j].title + "</P>\n");
			}
			indx2 = indx2 - 1;
			s2 = ftsnodes[indx2].word.replace("_", "-");
		}

		num_sort(ListArray, NumList);

		for(i = 0; i < NumList; i++)
		{
			outdoc.write("<P style=\"margin-top:2.00pt;margin-bottom:1.00pt;\"><A  class=\"TocLeaf\" HREF=\"files/" + indxnodes[numIndxTopNodes + ListArray[i]].link + "\" TARGET=\"RightFrm\">\n");
			outdoc.write(indxnodes[numIndxTopNodes + ListArray[i]].title + "</P>\n");
		}

		outdoc.write("</BODY>\n");
		outdoc.write("</HTML>\n");
		outdoc.close();
	}
	else
	{
		alert("Nomatch found for " + window.top.get_search_str());
	}
}
