// Wintertree Spell Check Applet
// JavaScript support functions
// Copyright (c) 2001 Wintertree Software Inc.
// www.wintertree-software.com
//
// $Id: SpellCheckApplet.js,v 1.5.8 2004/03/25 19:17:07 wsi Exp wsi $

// Name of the cookie containing spelling options and the user dictionary
var cookieName = "SpellCheckApplet";

// List of fields (text areas) to check, and the index of the field currently
// being checked.
var fieldsToCheck;
var curFieldToCheck = 0;

// Bit mask containing spelling options
var options = 0;

// User dictionary string. Entries in the string are comma-separated.
var userDictStr = "";

// Location of Spell Check Applet. This can be overridden to access the
// applet in another frame, etc.
var appletLoc = document;

// Retrieve the options and user dictionary from a cookie.
var cookies = document.cookie;
var pos = cookies.indexOf(cookieName + "=");
var s;
var start;
var end;
if (pos >= 0) {
    start = pos + cookieName.length + 1;    // +1 to skip =
    end = cookies.indexOf(";", start);
    if (end < 0) {
        end = cookies.length;
    }
    var scCookie = unescape(cookies.substring(start, end));
    // Skip over the options
    end = scCookie.indexOf(":");
    if (end < 0) {
        end = scCookie.length;
    }
    options = scCookie.substring(0, end);
    options = options - 0;
    if (end != scCookie.length) {
        start = end + 1;
        userDictStr = scCookie.substring(start, scCookie.length);
    }
}

// Check the spelling of the next field in the fieldsToCheck array.
function checkNextField() {
    // Get the text from the first field, and send it to SpellCheckApplet.
    appletLoc.SpellCheckApplet.setText(fieldsToCheck[curFieldToCheck].value);

    // Start checking the spelling. The rest is done in the waitForSpellCheck
    // function.
    appletLoc.SpellCheckApplet.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;
    }

    if (appletLoc.SpellCheckApplet.getStatus() == 0) {
        // A spelling check is already in progress. Calling the check method
        // will bring the dialog box to the front.
        appletLoc.SpellCheckApplet.check();
        return;
    }

    appletLoc.SpellCheckApplet.setUserDictionary(userDictStr);
    appletLoc.SpellCheckApplet.setOptions(options);

    // Start checking spelling.
    fieldsToCheck = new Array();
    for (var i = 0; i < arguments.length; ++i) {
        fieldsToCheck[i] = arguments[i];
    }
    curFieldToCheck = 0;
    checkNextField();
}

// 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 = appletLoc.SpellCheckApplet.getStatus();
    if (status != 0) {
        if (status == 1) {
            // Done. Get the updated text from SpellCheckApplet and place it
            // in the current text area.
            fieldsToCheck[curFieldToCheck].value = appletLoc.SpellCheckApplet.getText();

            // Save the options and user dictionary in case the user dictionary changed.
            options = appletLoc.SpellCheckApplet.getOptions();
            userDictStr = appletLoc.SpellCheckApplet.getUserDictionary();
            saveCookie();

            // Advance to the next text area.
            if (++curFieldToCheck < fieldsToCheck.length) {
                checkNextField();
            }
            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 occured 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);
    }
}

// Save the options and user dictionary in a cookie.
function saveCookie() {
    // The cookie expires in one year.
    var nextYear = new Date();
    nextYear.setFullYear(nextYear.getFullYear() + 1);
    document.cookie = cookieName + "=" + escape(options.toString() + ":" + userDictStr) +
      "; expires=" + nextYear.toGMTString() + "; path=/";
}

