1////////////////////////////////////////////////////
2// spellChecker.js
3//
4// spellChecker object
5//
6// This file is sourced on web pages that have a textarea object to evaluate
7// for spelling. It includes the implementation for the spellCheckObject.
8//
9////////////////////////////////////////////////////
10
11
12// constructor
13function spellChecker( textObject ) {
14
15	// public properties - configurable
16//	this.popUpUrl = '/speller/spellchecker.html';							// by FredCK
17	this.popUpUrl = 'fck_spellerpages/spellerpages/spellchecker.html';		// by FredCK
18	this.popUpName = 'spellchecker';
19//	this.popUpProps = "menu=no,width=440,height=350,top=70,left=120,resizable=yes,status=yes";	// by FredCK
20	this.popUpProps = null ;																	// by FredCK
21//	this.spellCheckScript = '/speller/server-scripts/spellchecker.php';		// by FredCK
22	//this.spellCheckScript = '/cgi-bin/spellchecker.pl';
23
24	// values used to keep track of what happened to a word
25	this.replWordFlag = "R";	// single replace
26	this.ignrWordFlag = "I";	// single ignore
27	this.replAllFlag = "RA";	// replace all occurances
28	this.ignrAllFlag = "IA";	// ignore all occurances
29	this.fromReplAll = "~RA";	// an occurance of a "replace all" word
30	this.fromIgnrAll = "~IA";	// an occurance of a "ignore all" word
31	// properties set at run time
32	this.wordFlags = new Array();
33	this.currentTextIndex = 0;
34	this.currentWordIndex = 0;
35	this.spellCheckerWin = null;
36	this.controlWin = null;
37	this.wordWin = null;
38	this.textArea = textObject;	// deprecated
39	this.textInputs = arguments;
40
41	// private methods
42	this._spellcheck = _spellcheck;
43	this._getSuggestions = _getSuggestions;
44	this._setAsIgnored = _setAsIgnored;
45	this._getTotalReplaced = _getTotalReplaced;
46	this._setWordText = _setWordText;
47	this._getFormInputs = _getFormInputs;
48
49	// public methods
50	this.openChecker = openChecker;
51	this.startCheck = startCheck;
52	this.checkTextBoxes = checkTextBoxes;
53	this.checkTextAreas = checkTextAreas;
54	this.spellCheckAll = spellCheckAll;
55	this.ignoreWord = ignoreWord;
56	this.ignoreAll = ignoreAll;
57	this.replaceWord = replaceWord;
58	this.replaceAll = replaceAll;
59	this.terminateSpell = terminateSpell;
60	this.undo = undo;
61
62	// set the current window's "speller" property to the instance of this class.
63	// this object can now be referenced by child windows/frames.
64	window.speller = this;
65}
66
67// call this method to check all text boxes (and only text boxes) in the HTML document
68function checkTextBoxes() {
69	this.textInputs = this._getFormInputs( "^text$" );
70	this.openChecker();
71}
72
73// call this method to check all textareas (and only textareas ) in the HTML document
74function checkTextAreas() {
75	this.textInputs = this._getFormInputs( "^textarea$" );
76	this.openChecker();
77}
78
79// call this method to check all text boxes and textareas in the HTML document
80function spellCheckAll() {
81	this.textInputs = this._getFormInputs( "^text(area)?$" );
82	this.openChecker();
83}
84
85// call this method to check text boxe(s) and/or textarea(s) that were passed in to the
86// object's constructor or to the textInputs property
87function openChecker() {
88	this.spellCheckerWin = window.open( this.popUpUrl, this.popUpName, this.popUpProps );
89	if( !this.spellCheckerWin.opener ) {
90		this.spellCheckerWin.opener = window;
91	}
92}
93
94function startCheck( wordWindowObj, controlWindowObj ) {
95
96	// set properties from args
97	this.wordWin = wordWindowObj;
98	this.controlWin = controlWindowObj;
99
100	// reset properties
101	this.wordWin.resetForm();
102	this.controlWin.resetForm();
103	this.currentTextIndex = 0;
104	this.currentWordIndex = 0;
105	// initialize the flags to an array - one element for each text input
106	this.wordFlags = new Array( this.wordWin.textInputs.length );
107	// each element will be an array that keeps track of each word in the text
108	for( var i=0; i<this.wordFlags.length; i++ ) {
109		this.wordFlags[i] = [];
110	}
111
112	// start
113	this._spellcheck();
114
115	return true;
116}
117
118function ignoreWord() {
119	var wi = this.currentWordIndex;
120	var ti = this.currentTextIndex;
121	if( !this.wordWin ) {
122		alert( 'Error: Word frame not available.' );
123		return false;
124	}
125	if( !this.wordWin.getTextVal( ti, wi )) {
126       this.terminateSpell();
127       return true;
128		alert( 'Error: "Not in dictionary" text is missing.' );
129		return false;
130	}
131	// set as ignored
132	if( this._setAsIgnored( ti, wi, this.ignrWordFlag )) {
133		this.currentWordIndex++;
134		this._spellcheck();
135	}
136	return true;
137}
138
139function ignoreAll() {
140	var wi = this.currentWordIndex;
141	var ti = this.currentTextIndex;
142	if( !this.wordWin ) {
143		alert( 'Error: Word frame not available.' );
144		return false;
145	}
146	// get the word that is currently being evaluated.
147	var s_word_to_repl = this.wordWin.getTextVal( ti, wi );
148	if( !s_word_to_repl ) {
149      this.terminateSpell();
150       return true;
151
152		alert( 'Error: "Not in dictionary" text is missing' );
153		return false;
154	}
155
156	// set this word as an "ignore all" word.
157	this._setAsIgnored( ti, wi, this.ignrAllFlag );
158
159	// loop through all the words after this word
160	for( var i = ti; i < this.wordWin.textInputs.length; i++ ) {
161		for( var j = 0; j < this.wordWin.totalWords( i ); j++ ) {
162			if(( i == ti && j > wi ) || i > ti ) {
163				// future word: set as "from ignore all" if
164				// 1) do not already have a flag and
165				// 2) have the same value as current word
166				if(( this.wordWin.getTextVal( i, j ) == s_word_to_repl )
167				&& ( !this.wordFlags[i][j] )) {
168					this._setAsIgnored( i, j, this.fromIgnrAll );
169				}
170			}
171		}
172	}
173
174	// finally, move on
175	this.currentWordIndex++;
176	this._spellcheck();
177	return true;
178}
179
180function replaceWord() {
181	var wi = this.currentWordIndex;
182	var ti = this.currentTextIndex;
183	if( !this.wordWin ) {
184		alert( 'Error: Word frame not available.' );
185		return false;
186	}
187	if( !this.wordWin.getTextVal( ti, wi )) {
188      this.terminateSpell();
189       return true;
190
191		alert( 'Error: "Not in dictionary" text is missing' );
192		return false;
193	}
194	if( !this.controlWin.replacementText ) {
195		return false ;
196	}
197	var txt = this.controlWin.replacementText;
198	if( txt.value ) {
199		var newspell = new String( txt.value );
200		if( this._setWordText( ti, wi, newspell, this.replWordFlag )) {
201			this.currentWordIndex++;
202			this._spellcheck();
203		}
204	}
205	return true;
206}
207
208function replaceAll() {
209	var ti = this.currentTextIndex;
210	var wi = this.currentWordIndex;
211	if( !this.wordWin ) {
212		alert( 'Error: Word frame not available.' );
213		return false;
214	}
215	var s_word_to_repl = this.wordWin.getTextVal( ti, wi );
216	if( !s_word_to_repl ) {
217      this.terminateSpell();
218       return true;
219
220		alert( 'Error: "Not in dictionary" text is missing' );
221		return false;
222	}
223	var txt = this.controlWin.replacementText;
224	if( !txt.value ) return false;
225	var newspell = new String( txt.value );
226
227	// set this word as a "replace all" word.
228	this._setWordText( ti, wi, newspell, this.replAllFlag );
229
230	// loop through all the words after this word
231	for( var i = ti; i < this.wordWin.textInputs.length; i++ ) {
232		for( var j = 0; j < this.wordWin.totalWords( i ); j++ ) {
233			if(( i == ti && j > wi ) || i > ti ) {
234				// future word: set word text to s_word_to_repl if
235				// 1) do not already have a flag and
236				// 2) have the same value as s_word_to_repl
237				if(( this.wordWin.getTextVal( i, j ) == s_word_to_repl )
238				&& ( !this.wordFlags[i][j] )) {
239					this._setWordText( i, j, newspell, this.fromReplAll );
240				}
241			}
242		}
243	}
244
245	// finally, move on
246	this.currentWordIndex++;
247	this._spellcheck();
248	return true;
249}
250
251function terminateSpell() {
252	// called when we have reached the end of the spell checking.
253	var msg = "";		// by FredCK
254	var numrepl = this._getTotalReplaced();
255
256	if( numrepl == 0 ) {
257		// see if there were no misspellings to begin with
258		if( !this.wordWin ) {
259			msg = "";
260		} else {
261			if( this.wordWin.totalMisspellings() ) {
262//				msg += "No words changed.";			// by FredCK
263				msg += FCKLang.DlgSpellNoChanges ;	// by FredCK
264			} else {
265//				msg += "No misspellings found.";	// by FredCK
266				msg += FCKLang.DlgSpellNoMispell ;	// by FredCK
267			}
268		}
269	} else if( numrepl == 1 ) {
270//		msg += "One word changed.";			// by FredCK
271		msg += FCKLang.DlgSpellOneChange ;	// by FredCK
272	} else {
273//		msg += numrepl + " words changed.";	// by FredCK
274		msg += FCKLang.DlgSpellManyChanges.replace( /%1/g, numrepl ) ;
275	}
276	if( msg ) {
277//		msg += "\n";	// by FredCK
278		alert( msg );
279	}
280
281	if( numrepl > 0 ) {
282		// update the text field(s) on the opener window
283		for( var i = 0; i < this.textInputs.length; i++ ) {
284			// this.textArea.value = this.wordWin.text;
285			if( this.wordWin ) {
286				if( this.wordWin.textInputs[i] ) {
287					this.textInputs[i].value = this.wordWin.textInputs[i];
288				}
289			}
290		}
291	}
292
293	// return back to the calling window
294//	this.spellCheckerWin.close();					// by FredCK
295	if ( typeof( this.OnFinished ) == 'function' )	// by FredCK
296		this.OnFinished(numrepl) ;					// by FredCK
297
298	return true;
299}
300
301function undo() {
302	// skip if this is the first word!
303	var ti = this.currentTextIndex;
304	var wi = this.currentWordIndex;
305
306	if( this.wordWin.totalPreviousWords( ti, wi ) > 0 ) {
307		this.wordWin.removeFocus( ti, wi );
308
309		// go back to the last word index that was acted upon
310		do {
311			// if the current word index is zero then reset the seed
312			if( this.currentWordIndex == 0 && this.currentTextIndex > 0 ) {
313				this.currentTextIndex--;
314				this.currentWordIndex = this.wordWin.totalWords( this.currentTextIndex )-1;
315				if( this.currentWordIndex < 0 ) this.currentWordIndex = 0;
316			} else {
317				if( this.currentWordIndex > 0 ) {
318					this.currentWordIndex--;
319				}
320			}
321		} while (
322			this.wordWin.totalWords( this.currentTextIndex ) == 0
323			|| this.wordFlags[this.currentTextIndex][this.currentWordIndex] == this.fromIgnrAll
324			|| this.wordFlags[this.currentTextIndex][this.currentWordIndex] == this.fromReplAll
325		);
326
327		var text_idx = this.currentTextIndex;
328		var idx = this.currentWordIndex;
329		var preReplSpell = this.wordWin.originalSpellings[text_idx][idx];
330
331		// if we got back to the first word then set the Undo button back to disabled
332		if( this.wordWin.totalPreviousWords( text_idx, idx ) == 0 ) {
333			this.controlWin.disableUndo();
334		}
335
336		var i, j, origSpell ;
337		// examine what happened to this current word.
338		switch( this.wordFlags[text_idx][idx] ) {
339			// replace all: go through this and all the future occurances of the word
340			// and revert them all to the original spelling and clear their flags
341			case this.replAllFlag :
342				for( i = text_idx; i < this.wordWin.textInputs.length; i++ ) {
343					for( j = 0; j < this.wordWin.totalWords( i ); j++ ) {
344						if(( i == text_idx && j >= idx ) || i > text_idx ) {
345							origSpell = this.wordWin.originalSpellings[i][j];
346							if( origSpell == preReplSpell ) {
347								this._setWordText ( i, j, origSpell, undefined );
348							}
349						}
350					}
351				}
352				break;
353
354			// ignore all: go through all the future occurances of the word
355			// and clear their flags
356			case this.ignrAllFlag :
357				for( i = text_idx; i < this.wordWin.textInputs.length; i++ ) {
358					for( j = 0; j < this.wordWin.totalWords( i ); j++ ) {
359						if(( i == text_idx && j >= idx ) || i > text_idx ) {
360							origSpell = this.wordWin.originalSpellings[i][j];
361							if( origSpell == preReplSpell ) {
362								this.wordFlags[i][j] = undefined;
363							}
364						}
365					}
366				}
367				break;
368
369			// replace: revert the word to its original spelling
370			case this.replWordFlag :
371				this._setWordText ( text_idx, idx, preReplSpell, undefined );
372				break;
373		}
374
375		// For all four cases, clear the wordFlag of this word. re-start the process
376		this.wordFlags[text_idx][idx] = undefined;
377		this._spellcheck();
378	}
379}
380
381function _spellcheck() {
382	var ww = this.wordWin;
383
384	// check if this is the last word in the current text element
385	if( this.currentWordIndex == ww.totalWords( this.currentTextIndex) ) {
386		this.currentTextIndex++;
387		this.currentWordIndex = 0;
388		// keep going if we're not yet past the last text element
389		if( this.currentTextIndex < this.wordWin.textInputs.length ) {
390			this._spellcheck();
391			return;
392		} else {
393			this.terminateSpell();
394			return;
395		}
396	}
397
398	// if this is after the first one make sure the Undo button is enabled
399	if( this.currentWordIndex > 0 ) {
400		this.controlWin.enableUndo();
401	}
402
403	// skip the current word if it has already been worked on
404	if( this.wordFlags[this.currentTextIndex][this.currentWordIndex] ) {
405		// increment the global current word index and move on.
406		this.currentWordIndex++;
407		this._spellcheck();
408	} else {
409		var evalText = ww.getTextVal( this.currentTextIndex, this.currentWordIndex );
410		if( evalText ) {
411			this.controlWin.evaluatedText.value = evalText;
412			ww.setFocus( this.currentTextIndex, this.currentWordIndex );
413			this._getSuggestions( this.currentTextIndex, this.currentWordIndex );
414		}
415	}
416}
417
418function _getSuggestions( text_num, word_num ) {
419	this.controlWin.clearSuggestions();
420	// add suggestion in list for each suggested word.
421	// get the array of suggested words out of the
422	// three-dimensional array containing all suggestions.
423	var a_suggests = this.wordWin.suggestions[text_num][word_num];
424	if( a_suggests ) {
425		// got an array of suggestions.
426		for( var ii = 0; ii < a_suggests.length; ii++ ) {
427			this.controlWin.addSuggestion( a_suggests[ii] );
428		}
429	}
430	this.controlWin.selectDefaultSuggestion();
431}
432
433function _setAsIgnored( text_num, word_num, flag ) {
434	// set the UI
435	this.wordWin.removeFocus( text_num, word_num );
436	// do the bookkeeping
437	this.wordFlags[text_num][word_num] = flag;
438	return true;
439}
440
441function _getTotalReplaced() {
442	var i_replaced = 0;
443	for( var i = 0; i < this.wordFlags.length; i++ ) {
444		for( var j = 0; j < this.wordFlags[i].length; j++ ) {
445			if(( this.wordFlags[i][j] == this.replWordFlag )
446			|| ( this.wordFlags[i][j] == this.replAllFlag )
447			|| ( this.wordFlags[i][j] == this.fromReplAll )) {
448				i_replaced++;
449			}
450		}
451	}
452	return i_replaced;
453}
454
455function _setWordText( text_num, word_num, newText, flag ) {
456	// set the UI and form inputs
457	this.wordWin.setText( text_num, word_num, newText );
458	// keep track of what happened to this word:
459	this.wordFlags[text_num][word_num] = flag;
460	return true;
461}
462
463function _getFormInputs( inputPattern ) {
464	var inputs = new Array();
465	for( var i = 0; i < document.forms.length; i++ ) {
466		for( var j = 0; j < document.forms[i].elements.length; j++ ) {
467			if( document.forms[i].elements[j].type.match( inputPattern )) {
468				inputs[inputs.length] = document.forms[i].elements[j];
469			}
470		}
471	}
472	return inputs;
473}
474