﻿// 定义全局变量
var contentBox;
var parentWindow;

// description: 辅助函数，通过ID获取Document中的某个元素
// id: 元素ID号
// return: Document元素对象
function GetElement(id)
{
    return document.getElementById(id);
}

// description: 获取Input输入框的值
// id: 输入框的ID号
// return: 如果输入框存在则返回输入框的文本，否则返回null
function GetInputValue(id)
{
    var input = GetElement(id);
    return (input != null) ? input.value : null;
}

// description: 为Input输入框设置新值
// id: 输入框的ID号
// newValue: 输入框的新值
function SetInputValue(id, newValue)
{
    GetElement(id).value = newValue;
}

// description: 获取需要进行拼写检查的内容
// return: 需要进行拼写检查的内容
function GetText()
{
    return contentBox.value;
}

// description: 设置拼写检查框的内容
// text: 新的内容
function SetText(text)
{
    contentBox.value = text;
}

// description: 拼写检查按钮的单击事件
function OnSpellChecker(textBox)
{
    if (textBox.value.length > 0)
    {
        contentBox = textBox;
        var webRoot = (window.location.hostname == "localhost") ? "/Web" : "";
        window.showModalDialog("http://" + window.location.host + webRoot + "/Control/SpellingCheckContainer.aspx", window, 
            "dialogHeight:330px; dialogWidth:400px; edge:Raised; center:Yes; help:No; resizable:No; status:No; scroll:No");
    }
    else
    {
        alert("No content in the special textbox to check.");
    }
}

// 居中对齐窗口，因为Firefox不支持showModalDialog方法中的center选项，所以只能程序设置居中对齐，
// 但是这种做法在Firefox下会发现窗口跳动现象
function CenterWindow()   
{
    top.moveTo(screen.width/2 - 200, screen.height/2 - 150);
}

// description: 初始化拼写检查对话框内容
function Initialize()
{
    // 居中对齐窗口
    //CenterWindow();
    
    // 获取拼写检查对话框的父窗口，以便对其父窗口的内容进行设置
    if (parent.window.dialogArguments)
        parentWindow = parent.window.dialogArguments;
    else if (top.opener)
        parentWindow = top.opener;

    // 获取当前的拼写检查Mode并做相应处理
    var spellMode = GetInputValue("spellMode");
    switch (spellMode)
    {
        case "start" :
            break;

        case "suggest" :
            UpdateText();
            break;

        case "end" :
            UpdateText();
            EndCheck();
            break;

        default :
            if(LoadText())
            {
                document.SpellingForm.submit();
            }
            break;
    }
}

// description: 从父窗口中获取需要拼写检查的文本
// return: 如果父窗口中的文本不为空，则返回true，否则返回false
function LoadText()
{
    var text = parentWindow.GetText();
    if (text.length > 0)
    {
		UpdateSettings(text, 0, "start");
		GetElement("statusText").innerText = "Spell Checking Text ...";
		return true;
    }
    return false;
}

// description: 更新相关拼写检查参数的设置
// currentText: 需要被拼写检查的文本
// wordIndex: 当前被检查的单词在currentText中的序号
// mode: 拼写检查的Mode
function UpdateSettings(currentText, wordIndex, mode)
{
    SetInputValue("currentText", currentText);
    SetInputValue("wordIndex", wordIndex);
    SetInputValue("spellMode", mode);
}

// description: 更新父窗口中内容框的文本
function UpdateText()
{
	var newValue = GetInputValue("currentText");
    parentWindow.SetText(newValue);
}

// descrption: 拼写检查完成时的响应函数
function EndCheck()
{
    alert("Spell Check Complete.");
    CloseWindow();
}

// description: 关闭拼写检查窗口
function CloseWindow()
{
    if (top.opener || parent.window.dialogArguments)
	    top.close();
}

// description: 设置替代单词
// suggestionsListbox: 建议单词列表框对象
function ChangeWord(suggestionsListbox)
{
    var index = suggestionsListbox.selectedIndex;
    suggestionsListbox.form.replacementWord.value = suggestionsListbox.options[index].value;
}
