var changed     = false;
var isPage      = false;
var isWysiwyg   = true;

function init()
{
    document.info1.location.value = location;
}

function previewLink()
{
    document.all.previewTr.style.display = "";
    preview.location = document.info1.reference.value;
}

function selePage()
{
    var f = document.info1;
    var feature = "width=600px, height=400px, location=0, menubar=0, toolbar=0, scrollbars=1, status=1, resizable=1";
    selePageWin = window.open("/sys/SM_selePage?user=" + f.user.value + "&passwd=" + f.passwd.value + "&page=" + f.page.value, "selePageWin", feature);
    if(selePageWin && selePageWin.opener == null)
        selePageWin.opener = self;
    selePageWin.focus();
}

function initWysiwyg(target)
{
    var s = ''+'<html>'
//        +'<head><style>body{border:1px black solid; margin:4px;}</style></head>'
        +'<body></body>'
        +'</html>';
    wysiwyg.document.designMode = 'on';
    wysiwyg.document.open('text/html', 'replace');
    wysiwyg.document.write(s);
    wysiwyg.document.close();
    //wysiwyg.document.body.innerHTML = window.opener.document.info1.theme_content.innerText;
    wysiwyg.document.body.innerHTML = eval('window.opener.document.'+target+'.innerText');
}

function hasChange()
{
    changed = true;
}

function notNull(obj)
{
    if (obj.value == '')
    {
        alert("此欄不可空白");
        obj.focus();
        return false;
    }
    return true;
}

function toTextEdit()
{
    isWysiwyg = false;
    document.info1.content.innerText = wysiwyg.document.body.innerHTML;
    document.all.wysiwygEdit.style.display = "none";
    document.all.textEdit.style.display = "";
}

function toWysiwyg()
{
    isWysiwyg = true;
    wysiwyg.document.body.innerHTML = document.info1.content.innerText;
    document.all.wysiwygEdit.style.display = "";
    document.all.textEdit.style.display = "none";
}

function toFilesMgt()
{
    var f = document.info1;
    var feature = "width=600px, height=400px, location=0, menubar=0, toolbar=0, scrollbars=1, status=1, resizable=1";
    filesMgtWin = window.open("/sys/SM_filesMgt?user=" + f.user.value + "&passwd=" + f.passwd.value + "&page=" + f.page.value, "filesMgtWin", feature);
    if(filesMgtWin && filesMgtWin.opener == null)
        filesMgtWin.opener = self;
    filesMgtWin.focus();
}

function DoExec(s)
{
    wysiwyg.document.execCommand(s);
}

function ChooseColor()
{
    // display color dialog box
    var sColor = document.all.colorSelector.ChooseColorDlg();
    // change decimal to hex
    sColor = sColor.toString(16);
    // add extra zeroes if hex number is less than 6 digits
    if (sColor.length < 6)
    {
        var sTempString = "000000".substring(0, 6 - sColor.length);
        sColor = sTempString.concat(sColor);
    }
    sColor = "#" + sColor;
    // change color
    return sColor;
}

function SetFontColor()
{
    wysiwyg.document.execCommand('ForeColor', false, ChooseColor());
}

function SetBackColor()
{
    wysiwyg.document.execCommand('BackColor', false, ChooseColor());
}

function SetFontFace(p)
{
    wysiwyg.document.execCommand('FontName', false, p.options[p.selectedIndex].value);
    p.selectedIndex = 0;
}

function SetFontSize(p)
{
    wysiwyg.document.execCommand('FontSize', false, p.options[p.selectedIndex].value);
    p.selectedIndex = 0;
}

function SetBlockFormat(p)
{
    wysiwyg.document.execCommand('FormatBlock', false, p.options[p.selectedIndex].value);
    p.selectedIndex = 0;
}

function AddLink()
{
    //var s = document.info1.linkurl.value;
    var s = document.getElementById('linkurl').value;
    if(s != '' && s != 'http://')
        wysiwyg.document.execCommand('CreateLink', false, s);
    else
        wysiwyg.document.execCommand('CreateLink', true);
}

function ChooseEmoticon()
{
    //var f = document.info1;
    //var feature = "dialogWidth:600px; dialogHeight:400px; help:0; status:1; resizable:1;";
    //value = window.showModalDialog("/sys/SM_seleImage?user=" + f.user.value + "&passwd=" + f.passwd.value + "&page=" + f.page.value, null, feature);
    //if (value)
    //{
    //    wysiwyg.focus();
    //    wysiwyg.document.selection.createRange().pasteHTML(value);
    //}
    var f = document.wysiwyg_Edit;
    var feature = "width=600px, height=400px, location=0, menubar=0, toolbar=0, scrollbars=1, status=1, resizable=1";
    filesMgtWin = window.open("/sys/MM_fileadm?acc=" + f.acc.value + "&pwd=" + f.pwd.value + "&org_file=" + f.org_file.value + "&auth=" + f.auth.value + "&toselect=" + f.toselect.value, "MM_fileadmwin", feature);
    if(filesMgtWin && filesMgtWin.opener == null)
        filesMgtWin.opener = self;
    filesMgtWin.focus();
}

function ChooseEmoticon2()
{
    var f = document.wysiwyg_Edit;
    var feature = "width=600px, height=400px, location=0, menubar=0, toolbar=0, scrollbars=1, status=1, resizable=1";
    filesMgtWin = window.open("/sys/MM_fileadm?acc=" + f.acc.value + "&pwd=" + f.pwd.value + "&ts=" + f.ts.value + "&authority=" + f.authority.value , "MM_fileadmwin", feature);
    if(filesMgtWin && filesMgtWin.opener == null)
        filesMgtWin.opener = self;
    filesMgtWin.focus();
}

function doSubmit()
{
    var f = document.info1;
    if (notNull(f.title))
    {
        if (isPage)
            if (isWysiwyg)
                f.content.innerText = wysiwyg.document.body.innerHTML;
        
        if (changed)
        {
            f.submit();
            //alert("changed");
        } else {
            doCancel();
        }
    }
}

function doCancel()
{
    var f = document.info1;
    location = "/sys/SM_siteMaker?user=" + f.user.value + "&passwd=" + f.passwd.value + "&page=" + f.page.value;
}

function sendFile(obj, descr)
{
    var html = "<img src=\"" + obj.src + "\" alt=\"" + descr + "\" border=\"0\">";
    //window.returnValue = html;
    window.opener.document.wysiwyg.focus();
    window.opener.document.wysiwyg.document.selection.createRange().pasteHTML(html);
    window.close();
}

function sendUrl()
{
    if (notNull(document.info2.url))
    {
        var html = "<img src=\"" + document.info2.url.value + "\" border=\"0\">";
        //window.returnValue = html;
        window.opener.document.wysiwyg.focus();
        window.opener.document.wysiwyg.document.selection.createRange().pasteHTML(html);
        window.close();
    }
}

