// Copyright 2009 Chase Moskal


function toggleVis(id) {
	var element = document.getElementById(id);
	
	if (!element.style.display) {
		element.style.display = "none";
	}
	
	if (element.style.display == "none") {
		element.style.display = "block";
	} else {
		element.style.display = "none";
	}
}


function sentenceIsValid(sentence)
{
	var valid = false;
	for (var charIndex=0; charIndex < sentence.length; charIndex++)
	{
		var char = sentence.charAt(charIndex);
		if (char !== " ") { valid = true; }
	}
	// alert(sentence+";;;"+valid);
	return valid;
}

function totalReplace(text, a, b)
{
	while (text.indexOf(a) != -1)
	{
		text = text.replace(a, b);
	}
	return text;
}

function interpret(text)
{
	text = totalReplace(text, '\r\n', '\n');
	text = totalReplace(text, '\r', ' ');
	text = totalReplace(text, '\n', ' ');
	text = totalReplace(text, '\t', ' ');
	text = totalReplace(text, '-', ' '); // Hyphens are regarded as word separators.
	return text;
}

function splitSentences(text) {
	var allParts = [];
	var basicParts = text.split(".");
	for (var i=0; i < basicParts.length; i++) {
		var part = basicParts[i];
		var subparts = part.split("?");
		for (var x=0; x < subparts.length; x++) {
			var subpart = subparts[x];
			var subsubparts = subpart.split("!");
			for (var y=0; y < subsubparts.length; y++) {
				var subsubpart = subsubparts[y];
				allParts.push(subsubpart);
			}
		}
	}
	return allParts;
}

function outputResults(T, S, M, L, Total, Words) {
	var results = document.getElementById("results");
	results.innerHTML = "";
	results.innerHTML += "T: " + T + "<br />";
	results.innerHTML += "S: " + S + "<br />";
	results.innerHTML += "M: " + M + "<br />";
	results.innerHTML += "L: " + L + "<br />";
	results.innerHTML += "Total Sentences: " + Total + "<br />";
	results.innerHTML += "Words: " + Words + "<br />";
}

function countWords(text) {
	var words = text.split(" ");
	var goodWords = [];
	for (var i=0; i < words.length; i++) {
		var word = words[i];
		if (word && word !== " ") { goodWords.push(word); }
	}
	return goodWords.length;
}

function tsmlCount()
{	
	var textarea = document.getElementById("tsmlArea");
	var pretext = textarea.value;
	var text = interpret(pretext);
	
	var T = 0;
	var S = 0;
	var M = 0;
	var L = 0;
	var TotalSentences = 0;
	var TotalWords = 0;
	
	var sentences = splitSentences(text);
	
	for (var sentenceIndex=0; sentenceIndex < sentences.length; sentenceIndex++)
	{
		var sentence = sentences[sentenceIndex];
		
		if (sentenceIsValid(sentence))
		{
			// Getting the number of words in the sentence.
			var wordNum = countWords(sentence);
			
			// Only taking action if this sentence actually contains words.
			if (wordNum)
			{
				// If there are words in the sentence, then this
				// this is a real sentence, so we add one to the total.
				TotalSentences ++;
				
				// Word Count!
				TotalWords += wordNum;
				
				if (wordNum > 0 && wordNum < 5) { T++; }
				if (wordNum >= 5 && wordNum < 11) { S++; }
				if (wordNum >= 11 && wordNum < 30) { M++; }
				if (wordNum >= 30) { L++; }
			}
		}
	}
	outputResults(T, S, M, L, TotalSentences, TotalWords);
}