var g_Timer = setInterval("analyze()", 1000);
// define a few words that should be ignored
var g_Ignore = [
	"and",
	"but",
	"for",
	"from",
	"head",
	"her",
	"here",
	"his",
	"how",
	"html",
	"meta",
	"nor",
	"not",
	"table",
	"than",
	"that",
	"the",
	"their",
	"them",
	"then",
	"these",
	"they",
	"this",
	"those",
	"title",
	"was",
	"were",
	"what",
	"when",
	"with"
];

if (document.getElementById("keyword1") != null)
	document.getElementById("keyword1").focus();

// prepareString - prepare string for processing
function prepareString(sData)
{
	// make it lower case
	sData = sData.toLowerCase();

	// remove non-alpha numeric characters
	for (i = 0; i < sData.length; i++)
	{
		var ch = sData.charCodeAt(i);

		if (!((ch >= 97 && ch <= 122) || (ch >= 48 && ch <= 57)))
			sData = sData.substring(0, i) + " " + sData.substring(i + 1);
	}

	return (sData);
}


function analyze()
{
	var elText = document.getElementById("bodytext");
	if (elText != null)
	{
		var sBody = elText.value;

		if (sBody.length == 0)
			return (false);
	}

	// get the keywords
	var elKw1 = document.getElementById("keyword1");
	var elKw2 = document.getElementById("keyword2");
	var elKw3 = document.getElementById("keyword3");

	var sKw1 = sKw2 = sKw3 = null;
	if (elKw1 != null && elKw1.value != "")
		sKw1 = elKw1.value;
	if (elKw2 != null && elKw2.value != "")
		sKw2 = elKw2.value;
	if (elKw3 != null && elKw3.value != "")
		sKw3 = elKw3.value;

	// at least one keyword my be present in order to continue
	if (sKw1 == null && sKw2 == null && sKw3 == null)
		return;


	// remove anything in angle brackets
	var nBegin = 0;
	while ((nBegin = sBody.indexOf("<")) > 0)
	{
		var nEnd = sBody.indexOf(">", nBegin);
		if (nEnd != -1)
			sBody = sBody.substring(0, nBegin) + sBody.substring(nEnd + 1);
		else
			sBody = sBody.substring(0, nBegin);
	}

	// we have text and keywords - do the analysis
	sBody = prepareString(sBody);

	var arWords = sBody.split(" ");
	elCount = document.getElementById("wordcount");
	if (elCount != null)
		elCount.innerHTML = arWords.length + "";

	if (sKw1 != null)
		checkKeyword(sKw1, arWords, '1');
	else
	{
		setRatio("1", 0);
		setCount("1", 0);
	}

	if (sKw2 != null)
		checkKeyword(sKw2, arWords, '2');
	else
	{
		setRatio("2", 0);
		setCount("2", 0);
	}

	if (sKw3 != null)
		checkKeyword(sKw3, arWords, '3');
	else
	{
		setRatio("3", 0);
		setCount("3", 0);
	}

//clearInterval(g_Timer);
}


// setRatio - set the keyword ratio data
function setRatio(sElem, nRatio)
{
	var num = new Number(nRatio);
	var sRatio = num.toFixed(2) + "";

	var el = document.getElementById("ratio" + sElem);
	if (el != null)
		el.innerHTML = sRatio;
}

// setCount - set the keyword count data
function setCount(sElem, nCount)
{
	var el = document.getElementById("count" + sElem);
	if (el != null)
		el.innerHTML = nCount;
}


// checkKeyword - check a keyword phrase against the words in the body text
function checkKeyword(sKeyword, arWords, sElem)
{
	sKeyword = prepareString(sKeyword);

	var nCount = 0;
	var arKwList = sKeyword.split(" ");
	for (var nIdx = 0; nIdx < arWords.length; nIdx++)
	{
		var sWord = arWords[nIdx];

		if (sWord.length > 2 && !contains(g_Ignore, sWord))
		{
			for (var i = 0; i < arKwList.length; i++)
			{
				if (arKwList[i] == sWord || arKwList[i] + "s" == sWord)
				{
					nCount++;
				}
			}
		}
	}

	// calculate the ratio
	var nRatio = (nCount * 100) / arWords.length;

	setCount(sElem, nCount);
	setRatio(sElem, nRatio);
}


// contains - return true if array contains a given word
function contains(arList, sWord)
{
	for (var i = 0; i < arList.length; i++)
	{
		if (arList[i] == sWord)
			return (true);
	}
	return (false);
}