﻿/**
<!
(c) 2003 Chhahari.com
Original Author: Rajesh B. Shrestha, 2004
Version: 1.3
14/03/05 Richard Wordingham replaced char by chars to avoid clash wioth reserved word.

Feel free to copy, modify and distribute as long as this header is kept intact.
-->
*/

var wordDB = new Array(); 
var charDB = new Array(); //@@@ could be implemented as hash instead
var index = 0;
var MAX_DBSIZE = 100;

/**
 * Adds the given word and character to the pnemonic database. Invoked at the end of the word.
 **/
function addWordToDB(word, wordchar) {

	word_begin = 1;
	if (word == '') return;

	// Reset the lastword
	lastword = '';
	if (isWordInDB (word)) return;

    	wordDB[index] = word;
	charDB[index] = wordchar;
	if (++index > MAX_DBSIZE) index = 0;
}

/**
 * Displays the pnemonic bar for the given character with words from the database.
 **/
function showPnemonicBar (chars) {

    	var content =   "<div>"
	                    + "<table width=100% height=100% border=1 cellspacing=0 cellpadding=2>";

	for (var i = 0; i < MAX_DBSIZE; i++) {

		//alert ("index: " + i + " lastword: " + charDB[i] + " " + wordDB[i]);

		if (charDB[i] == undefined) break;

		if (chars == charDB[i])	
    			content += "<a href='javascript:parent.addChar(\"" + wordDB[i] + "\")' style=\"text-decoration:none;color:black;\"><td align='center' style='border:1px solid gray' onmouseover='this.style.border=\"1px solid red\";window.status=\"\";return true;' onmouseout='this.style.border=\"1px solid gray\"' style='cursor:hand'>" + charDB[i] + wordDB[i] + "</td></a>";
	}
	
	content 	+= "</table>"
                    	+ "</div>"
                    	;

    	show_helpbar(EDITOR_PNEMONICS, content);
}

/**
 * Returns true if we are at the beginning of the word.
 **/
function isWordBegin (word) {

	if (word_begin == 1)
		return true;

	return false;
}

/**
 * Clears the pnemonic bar displayed on-screen.
 **/
function clearPnemonicBar() {
	show_helpbar(EDITOR_PNEMONICS, "");
}

/**
 * Sets the pnemonic for the given code.
 **/
function setPnemonic (code) {

	if (isWordBegin ()) {
		showPnemonicBar (code);
		lastwordchar = code;
		word_begin= 0;

	} else {
		clearPnemonicBar ();
		lastword += code; 
	}
}

/**
 * Checks to see if the given word already exists in our database. 
 * Returns true if the word is present, false otherwise.
 **/
function isWordInDB (word) {

	for (var i = 0; i < MAX_DBSIZE; i++) {

		//alert ("index: " + i + " lastword: " + charDB[i] + " " + wordDB[i]);

		if (wordDB[i] == undefined) break;

		if (word == wordDB[i])
			return true;	
	}

	return false;

}

/**
 * Handle the backspacing of the pnemonic word.
 **/
function backspacePnemonic() {

	//truncate the last character of lastword to effect the backspace
	lastword = lastword.replace (/\D$/, '');

	clearPnemonicBar();
}