/*
 * Copyright (C) 2005  Lars K. Schunk.
 *
 * This script is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This script is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Read the license at: http://www.gnu.org/licenses/gpl.html
 */


self.onerror = function(msg, url, line) {
	alert("Error: " + msg + "\n\nDocument: " + url + "\n\nLine: " + line);
}

// ******************************************************
// 		The Lix Class
// ******************************************************

function Lix(text) {
	this.text = text;
	this.modifiedText = this.getModifiedText();
	this.words = this.getWordArray();
	this.n_words = this.getNoOfWords();
	this.n_longWords = this.getNoOfLongWords();
	this.n_periods = this.getNoOfPeriods();
	this.lix = this.getLix();
}

Lix.prototype.getModifiedText = function() {
	var tmpText = this.text;

	// Handle abbreviations, such as dvs. and bl.a.
	tmpText = tmpText.replace(/\.([a-zA-Z])/g, "$1");
	tmpText = tmpText.replace(/\. ([a-z])/g, " $1");

	// Remove superfluous whitespace
	tmpText = tmpText.replace(/^\s+/, "");
	tmpText = tmpText.replace(/\s+$/, "");
	tmpText = tmpText.replace(/(\s)\s+/g, "$1");

	// Remove superfluous characters
	tmpText = tmpText.replace(/[\(\)\[\]\{\}\$"'`’;,]/g, "");

	// Remove superfluous periods
	tmpText = tmpText.replace(/\.\.+/g, ".");

	// Handle initials, such as those in H.C. Andersen
	tmpText = tmpText.replace(/([A-Z])\./g, "$1");

	// Make sure the text ends with a period
	if ((tmpText.charAt(tmpText.length-1)).match(/[^.:!?]/))
		tmpText += ".";

	return tmpText;
};

Lix.prototype.getWordArray = function() {
	return this.modifiedText.split(/\s/);
};

Lix.prototype.getNoOfWords = function() {
	return this.words.length;
};

Lix.prototype.getNoOfLongWords = function() {
	var count = 0;

	for (var i=0; i<this.words.length; i++) {
		if (this.words[i].length >= 7)
			count++;
	}

	return count;
};

// Count the number of periods. :, !, and ? are treated as periods as well.
Lix.prototype.getNoOfPeriods = function() {
	var count = 0;
	var pos;
	var tmpText = this.modifiedText;
	
	do {
		pos = tmpText.search(/[.:!?]/);
		if (pos != -1) {
			count++;
			tmpText = tmpText.substring(pos+1);
		}
	} while (pos != -1);

	return count;
};

Lix.prototype.getLix = function() {
	return Math.round(this.n_words/this.n_periods + (this.n_longWords/this.n_words * 100));
};

// ******************************************************
//		End of Lix Class
// ******************************************************

function getLix() {
	var lix = new Lix(document.lixForm.text.value);

	document.lixForm.newtext.value = lix.modifiedText;
	document.lixForm.output.value = lix.lix;
}
