// Wintertree Spelling Server
// SpellingAppletScript.js: Client-side JavaScript code used to
// check spelling of text areas using Spelling Applet.
// Copyright (c) 2002 Wintertree Software Inc.
// www.wintertree-software.com
// $Id: SpellingAppletScript.js,v 1.6 2002/09/03 15:35:50 wsi Exp wsi $

var fieldsToCheck;
var curFieldToCheck = 0;

var userDictStr = "";

// Set the check boxes with the initial option values
var CASE_SENSITIVE_OPT = 0x0200;
var IGNORE_ALL_CAPS_WORD_OPT = 0x0008;
var IGNORE_CAPPED_WORD_OPT = 0x0001;
var IGNORE_MIXED_CASE_OPT = 0x0002;
var IGNORE_MIXED_DIGITS_OPT = 0x0040;
var IGNORE_HTML_MARKUPS_OPT = 0x200000;
var REPORT_DOUBLED_WORD_OPT = 0x0100;
var SUGGEST_SPLIT_WORDS_OPT = 0x2000;
var IGNORE_DOMAIN_NAMES_OPT = 0x40000;

var options = 0xb8790; // defaults
options  |= IGNORE_HTML_MARKUPS_OPT;  

// Check the spelling of the next field in the fieldsToCheck array.
function checkNextField() {
    // Get the text from the first field, and send it to SpellingApplet.
    // the spelling object misplaces corrections if the test has new line characters - replace with a form fee
    var strText = fieldsToCheck[curFieldToCheck].value
    strText = strText.replace(/\r\n/gi, '\f')
    document.SpellingApplet.setText(strText);

    // Start checking the spelling. The rest is done in the waitForSpellCheck
    // function.
    document.SpellingApplet.check();
    waitForSpellCheck();
}

// Respond to a Check Spelling button press
// Parameter: List of text area object to check, one after the other
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) {
        // A spelling check is already in progress. Calling the check method
        // will bring the dialog box to the front.
        document.SpellingApplet.check();
        return;
    }

    setControlOptions()

    // Start checking spelling.
    fieldsToCheck = new Array();
    for (var i = 0; i < arguments.length; ++i) {
        fieldsToCheck[i] = arguments[i];
    }
    curFieldToCheck = 0;
    checkNextField();
}

// Respond to a Check Spelling button press
// Parameter: List of text area object to check, one after the other
function ChecktinyMCESpelling() {
    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) {
        // A spelling check is already in progress. Calling the check method
        // will bring the dialog box to the front.
        document.SpellingApplet.check();
        return;
    }

    setControlOptions()

     var strText = tinyMCE.getContent()
    strText = strText.replace(/\r\n/gi, '\f')
    document.SpellingApplet.setText(strText);

    // Start checking the spelling. The rest is done in the waitForSpellCheck
    // function.
    document.SpellingApplet.check();
    waitFortinyMCESpellCheck();
}

function setControlOptions(){
	// Retrieve the options and user dictionary cookie.
    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);
        }
    }
}

// Wait for the spelling check to complete, and process
// the result when done.
function waitFortinyMCESpellCheck() {
    // getSpellCheckStatus returns:
    // 0 if the spelling check is still in progress
    // 1 if the spellign check completed successfully
    // 2 if the user cancelled the spelling check
    // < 0 if an error occurred
   
    var status = document.SpellingApplet.getSpellCheckStatus();
    if (status != 0) {
        if (status == 1) {
            // Done. Get the updated text from SpellingApplet and place it
            tinyMCE.setContent(document.SpellingApplet.getText().replace(/\f/gi, '\r\n'))

            // Save the options and user dictionary in case the user dictionary changed.
            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();

        }
        // status == 2 means the user canceled; do nothing.
        else if (status < 0) {
            alert("An error occurred while checking spelling.");
            // The most likely cause of errors is a misconfiguration or
            // a communication problem with the server. Additional information
            // on the problem is recorded to the browser's Java Console.
        }
    }
    else {
        // The spelling check is still in progress. Check again in 100ms.
        setTimeout("waitFortinyMCESpellCheck()", 100);
    }
}

// Wait for the spelling check to complete, and process
// the result when done.
function waitForSpellCheck() {
    // getSpellCheckStatus returns:
    // 0 if the spelling check is still in progress
    // 1 if the spellign check completed successfully
    // 2 if the user cancelled the spelling check
    // < 0 if an error occurred
   
    var status = document.SpellingApplet.getSpellCheckStatus();
    if (status != 0) {
        if (status == 1) {
            // Done. Get the updated text from SpellingApplet and place it
            // in the current text area.
            fieldsToCheck[curFieldToCheck].value = document.SpellingApplet.getText().replace(/\f/gi, '\r\n');
            // this resets the dhtml in the rich text editcontrol
            if(typeof(resetRichTextDHTML) != 'undefined'){resetRichTextDHTML()}
            

            // Save the options and user dictionary in case the user dictionary changed.
            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();

            // Advance to the next text area.
            if (++curFieldToCheck < fieldsToCheck.length) {
                checkNextField(fieldsToCheck);
            }
            else {
                // All text areas have been checked.
                alert("Spelling check complete.");
            }
        }
        // status == 2 means the user canceled; do nothing.
        else if (status < 0) {
            alert("An error occurred while checking spelling.");
            // The most likely cause of errors is a misconfiguration or
            // a communication problem with the server. Additional information
            // on the problem is recorded to the browser's Java Console.
        }
    }
    else {
        // The spelling check is still in progress. Check again in 100ms.
        setTimeout("waitForSpellCheck()", 100);
    }
}

// Retrieve the options and user dictionary cookie.
function getOptions(theField){
	
	getSpellingCookies()
	theField.caseSensitiveBtn.checked = (options & CASE_SENSITIVE_OPT);
	theField.ignoreAllCapsWordsBtn.checked = (options & IGNORE_ALL_CAPS_WORD_OPT);
	theField.ignoreCappedWordsBtn.checked = (options & IGNORE_CAPPED_WORD_OPT);
	theField.ignoreMixedCaseBtn.checked = (options & IGNORE_MIXED_CASE_OPT);
	theField.ignoreMixedDigitsBtn.checked = (options & IGNORE_MIXED_DIGITS_OPT);
	theField.reportDoubledWordsBtn.checked = (options & REPORT_DOUBLED_WORD_OPT);
	theField.suggestSplitWordsBtn.checked = (options & SUGGEST_SPLIT_WORDS_OPT);
	theField.ignoreDomainNamesBtn.checked = (options & IGNORE_DOMAIN_NAMES_OPT);
}

function getSpellingCookies(){
	var cookies = document.cookie;
	var pos = cookies.indexOf("spellcheck=");
	var s;
	var start;
	var end;
	if (pos >= 0) {
	    start = pos + 11;
	    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;
	    }
	    options = scCookie.substring(0, end);
	    if (end != scCookie.length) {
	        start = end + 1;
	        userDictStr = scCookie.substring(start, scCookie.length);
	    }
	}
}

// Respond to an OK button press by saving the options
// and returning to the previous page
function saveOptions() {
    // Build an option mask
    if (document.setOptions.caseSensitiveBtn.checked) {
        options |= CASE_SENSITIVE_OPT;
    } else {
		options &= ~CASE_SENSITIVE_OPT;
    }
    if (document.setOptions.ignoreAllCapsWordsBtn.checked) {
        options |= IGNORE_ALL_CAPS_WORD_OPT;
    }
    else {
        options &= ~IGNORE_ALL_CAPS_WORD_OPT;
    }
    if (document.setOptions.ignoreCappedWordsBtn.checked) {
        options |= IGNORE_CAPPED_WORD_OPT;
    }
    else {
        options &= ~IGNORE_CAPPED_WORD_OPT;
    }
    if (document.setOptions.ignoreMixedCaseBtn.checked) {
        options |= IGNORE_MIXED_CASE_OPT;
    }
    else {
        options &= ~IGNORE_MIXED_CASE_OPT;
    }
    if (document.setOptions.ignoreMixedDigitsBtn.checked) {
        options |= IGNORE_MIXED_DIGITS_OPT;
    }
    else {
        options &= ~IGNORE_MIXED_DIGITS_OPT;
    }
    if (document.setOptions.reportDoubledWordsBtn.checked) {
        options |= REPORT_DOUBLED_WORD_OPT;
    }
    else {
        options &= ~REPORT_DOUBLED_WORD_OPT;
    }
    if (document.setOptions.suggestSplitWordsBtn.checked) {
        options |= SUGGEST_SPLIT_WORDS_OPT;
    }
    else {
        options &= ~SUGGEST_SPLIT_WORDS_OPT;
    }
    if (document.setOptions.ignoreDomainNamesBtn.checked) {
        options |= IGNORE_DOMAIN_NAMES_OPT;
    }
    else {
        options &= ~IGNORE_DOMAIN_NAMES_OPT;
    }
    
    var nextYear = new Date();
    nextYear.setFullYear(nextYear.getFullYear() + 1);
    document.cookie = "spellcheck=" + escape(options.toString() + ":" + userDictStr) +
      "; expires=" + nextYear.toGMTString() + "; path=/"
	self.close();
}