var fieldsToCheck;
var curFieldToCheck = 0;
var arr = new Array();
var editor;
var textarea;

function checkIt2( value, end_event_handler ){
	var options = document.SpellingApplet.getOptions();
	var cookies = document.cookie;
	var pos = cookies.indexOf("spellcheck=");
	var s;
	if ( pos >= 0 ) {
		var start = pos + 11;
		var end = cookies.indexOf(";", start);
		if (end < 0) {
			end = cookies.length;
		}
		var scCookie = unescape(cookies.substring(start, end));
		end = scCookie.indexOf(":");
		if (end < 0) {
			end = scCookie.length;
		}
		s = scCookie.substring(0, end);
		options = s - 0;
		if (options != 0) {
			document.SpellingApplet.setOptions(options);
		}
		if (end != scCookie.length) {
			start = end + 1;
			s = scCookie.substring(start, scCookie.length);
			document.SpellingApplet.setUserDictionary(s);
		}
	 }
	document.SpellingApplet.setText( rplTagsOnSymbol(value) );
    document.SpellingApplet.check();
	waitForCheckEnd( end_event_handler );
}


function waitForCheckEnd( end_event_handler_ ) {
	var result
	var objHidden
	var status = document.SpellingApplet.getSpellCheckStatus();
	if (status != 0) {
        if (status == 1) {			 
			alert( "spelling check almost finished..." );
			objHidden = document.createElement("input");
			objHidden.setAttribute("name", "spell_check_result");
			objHidden.setAttribute("id", "spell_check_result");
			objHidden.setAttribute("type", "hidden");
			document.body.insertBefore(objHidden, document.body.firstChild);
			objHidden.value = document.SpellingApplet.getText();
			var nextYear = new Date();
            nextYear.setFullYear(nextYear.getFullYear() + 1);
			options = document.SpellingApplet.getOptions();
 			var userDict = document.SpellingApplet.getUserDictionary();
			document.cookie = "spellcheck=" +
              escape(options.toString() + ":" + userDict) +
              "; expires=" + nextYear.toGMTString();
	            alert("Spelling check complete." );
				end_event_handler_( rplSymbolOnTags( objHidden.value ) );
		}
        else if (status < 0) {
            alert("An error occurred while checking spelling.");
        }
    }
    else {
		window.setTimeout("waitForCheckEnd(" + end_event_handler_ + ")", 1000);
	}
}


function startChek_wysiwyg( _textarea, _editor ) {
	textarea = _textarea
	editor = _editor;
	textarea.value = rplTagsOnSymbol( editor.getHTML() ); 						
	onCheckSpelling_wysiwyg(textarea, editor);
}

function startChek( textarea ) {
	if (RichHTML.document.body.innerText.length > 0) {
		textarea.value = rplTagsOnSymbol( RichHTML.document.body.innerHTML ); 						
		onCheckSpelling(textarea)
	}
}

function rplTagsOnSymbol( source_str ){
	var re = new RegExp("(<([^>])*>[ \r\n]*)+|(\&[^;]*;)+","ig");
	var str = source_str;
	arr = str.match( re ); 
	str = str.replace( re, String.fromCharCode( 160 ) );
	return str;
}

function rplSymbolOnTags( source_str ){
	var str = source_str;
	var re = new RegExp( String.fromCharCode( 160 ) );
	if ( arr != null )
		for ( var i = 0; i < arr.length; i ++ ){
			str = str.replace( re, arr[i] );
		}
    arr = new Array();
	return str;
}

function checkNextField_wysiwyg(editor) {
	document.SpellingApplet.setText(fieldsToCheck[curFieldToCheck].value);
    document.SpellingApplet.check();
    waitForSpellCheck_wysiwyg();
}

function checkNextField() {
    document.SpellingApplet.setText(fieldsToCheck[curFieldToCheck].value);
    document.SpellingApplet.check();
    waitForSpellCheck();
}

function onCheckSpelling() {

	if (!navigator.javaEnabled()) {
        alert("Java must be enabled in your browser to use the spelling checker.");
        return;
    }														
	var status = document.SpellingApplet.getSpellCheckStatus();
    if (status == 0) {
        alert("The spelling checker is busy -- Please wait.");
        return;
    }
	var options = document.SpellingApplet.getOptions();
    var cookies = document.cookie;
    var pos = cookies.indexOf("spellcheck=");
    var s;
    if ( pos >= 0 ) {
        var start = pos + 11;
        var end = cookies.indexOf(";", start);
        if (end < 0) {
            end = cookies.length;
        }
        var scCookie = unescape(cookies.substring(start, end));
        end = scCookie.indexOf(":");
        if (end < 0) {
            end = scCookie.length;
        }
        s = scCookie.substring(0, end);
        options = s - 0;
        if (options != 0) {
            document.SpellingApplet.setOptions(options);
        }
        if (end != scCookie.length) {
            start = end + 1;
            s = scCookie.substring(start, scCookie.length);
            document.SpellingApplet.setUserDictionary(s);
        }
	}
    fieldsToCheck = new Array();
    for (var i = 0; i < arguments.length; ++i) {
        fieldsToCheck[i] = arguments[i];
    }
    curFieldToCheck = 0;
	checkNextField();
}



function onCheckSpelling_wysiwyg(editor ) {
	if (!navigator.javaEnabled()) {
        alert("Java must be enabled in your browser to use the spelling checker.");
        return;
    }														
	var status = document.SpellingApplet.getSpellCheckStatus();
    if (status == 0) {
        alert("The spelling checker is busy -- Please wait.");
        return;
    }
	var options = document.SpellingApplet.getOptions();
    var cookies = document.cookie;
    var pos = cookies.indexOf("spellcheck=");
    var s;
    if ( pos >= 0 ) {
        var start = pos + 11;
        var end = cookies.indexOf(";", start);
        if (end < 0) {
            end = cookies.length;
        }
        var scCookie = unescape(cookies.substring(start, end));
        end = scCookie.indexOf(":");
        if (end < 0) {
            end = scCookie.length;
        }
        s = scCookie.substring(0, end);
        options = s - 0;
        if (options != 0) {
            document.SpellingApplet.setOptions(options);
        }
        if (end != scCookie.length) {
            start = end + 1;
            s = scCookie.substring(start, scCookie.length);
            document.SpellingApplet.setUserDictionary(s);
        }
	}
    fieldsToCheck = new Array();
    for (var i = 0; i < arguments.length; ++i) {
        fieldsToCheck[i] = arguments[i];
    }
    curFieldToCheck = 0;
	checkNextField_wysiwyg(editor);
}



function waitForSpellCheck() {
	var status = document.SpellingApplet.getSpellCheckStatus();
    if (status != 0) {
        if (status == 1) {
            fieldsToCheck[curFieldToCheck].value = document.SpellingApplet.getText();
            var nextYear = new Date();
            nextYear.setFullYear(nextYear.getFullYear() + 1);
            options = document.SpellingApplet.getOptions();
            var userDict = document.SpellingApplet.getUserDictionary();
            document.cookie = "spellcheck=" +
              escape(options.toString() + ":" + userDict) +
              "; expires=" + nextYear.toGMTString();
            if (++curFieldToCheck < fieldsToCheck.length) {
                checkNextField(fieldsToCheck);
            }
            else {
	            alert("Spelling check complete." );
				RichHTML.document.body.innerHTML = rplSymbolOnTags( fieldsToCheck[0].value );
			}
        }
        else if (status < 0) {
            alert("An error occurred while checking spelling.");
        }
    }
    else {
        setTimeout("waitForSpellCheck()", 1000);
    }
}



function waitForSpellCheck_wysiwyg() {
	var status = document.SpellingApplet.getSpellCheckStatus();
	
	if (status != 0) {
        if (status == 1) {
			var agt = navigator.userAgent.toLowerCase();
			var is_ie	   = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
			var is_opera  = (agt.indexOf("opera") != -1);
			var is_mac	   = (agt.indexOf("mac") != -1);
			var is_mac_ie = (is_ie && is_mac);
			var is_win_ie = (is_ie && !is_mac);
			var is_gecko  = (navigator.product == "Gecko");

			fieldsToCheck[curFieldToCheck].value = document.SpellingApplet.getText();
            var nextYear = new Date();
            nextYear.setFullYear(nextYear.getFullYear() + 1);
            options = document.SpellingApplet.getOptions();
            var userDict = document.SpellingApplet.getUserDictionary();
            document.cookie = "spellcheck=" +
              escape(options.toString() + ":" + userDict) +
              "; expires=" + nextYear.toGMTString();
            if (++curFieldToCheck < fieldsToCheck.length) {
                checkNextField_wysiwyg(editor);
            }
            else {
	            alert("Spelling check complete." );
				var doc = editor._iframe.contentWindow.document;
	
				if (is_gecko) {
					// enable editable mode for Mozilla
					doc.designMode = "on";
				}

				var html = "<html>\n";
				html += "<head>\n";
				html += "<style> html,body { border: 0px; } " + "</style>\n";
				html += "</head>\n";
				html += "<body>\n";
				html += rplSymbolOnTags( fieldsToCheck[0].value );
				html += "</body>\n";
				html += "</html>";
		
				doc.open();
				doc.write( html );
				doc.close();

				if (is_ie) {
					// enable editable mode for IE.	 For some reason this
					// doesn't work if done in the same place as for Gecko
					// (above).
					doc.body.contentEditable = true;
				}

				editor._iframe.style.borderStyle = "solid";
				editor._iframe.style.borderWidth = "0px 1px 1px 1px";
				editor._iframe.style.borderColor = "white ButtonShadow ButtonShadow ButtonShadow"

				editor.focusEditor();
			}
        }
        else if (status < 0) {
            alert("An error occurred while checking spelling.");
        }
    }
    else {
        setTimeout("waitForSpellCheck_wysiwyg()", 1000);
    }
}

