﻿/*
[Discuz!] (C)2001-2007 Comsenz Inc.
This is NOT a freeware, use is subject to license terms

$RCSfile: post.js,v $
$Revision: 1.24 $
$Date: 2007/07/20 12:20:51 $
*/
var postform = 'postform';
var postSubmited = false;
var smdiv = new Array();

function AddText(txt) {
    obj = $(postform).message;
    selection = document.selection;
    checkFocus();
    if (!isUndefined(obj.selectionStart)) {
        var opn = obj.selectionStart + 0;
        obj.value = obj.value.substr(0, obj.selectionStart) + txt + obj.value.substr(obj.selectionEnd);
    } else if (selection && selection.createRange) {
        var sel = selection.createRange();
        sel.text = txt;
        sel.moveStart('character', -strlen(txt));
    } else {
        obj.value += txt;
    }
}

function checkFocus() {
    var obj = typeof wysiwyg == 'undefined' || !wysiwyg ? $(postform).message : editwin;
    if (!obj.hasfocus) {
        obj.focus();
    }
}

function ctlent(event, control, postform) {
    if (postSubmited == false && (event.ctrlKey && event.keyCode == 13) || (event.altKey && event.keyCode == 83)) {
        try {
            control.disabled = true;
            if (postform) {
                postform.submit();
            }
            else {
                control.submit();
            }

            postSubmited = true;
        }
        catch (e) {
            return;
        }
    }
}

function ctSubmit(control, postform) {
    if (postSubmited == false) {
        try {
            control.disabled = true;
            if (postform) {
                postform.submit();
            }
            else {
                control.submit();
            }

            postSubmited = true;
        }
        catch (e) {
            return;
        }
    }
}

function ctsubmitParent(event) {

    alert(1);
    if (postSubmited == false && (event.ctrlKey && event.keyCode == 13) || (event.altKey && event.keyCode == 83)) {
        alert(2);
        var pForm = parent.window.document.getElementById(postform);
        postSubmited = true;
        pForm.submit();
        return true;
    }
    else {
        return false;
    }
}

function deleteData() {
    if (is_ie) {
        saveData('', 'delete');
    } else if (window.sessionStorage) {
        try {
            sessionStorage.removeItem('save_postdata');
        } catch (e) { }
    }
}
/*
function insertSmiley(smilieid) {
checkFocus();
var src = $('smilie_' + smilieid).src;
var code = $('smilie_' + smilieid).pop;
if(typeof wysiwyg != 'undefined' && wysiwyg && allowsmilies && (!$('smileyoff') || $('smileyoff').checked == false)) {
if(is_moz) {
applyFormat('InsertImage', false, src);
var smilies = editdoc.body.getElementsByTagName('img');
for(var i = 0; i < smilies.length; i++) {
if(smilies[i].src == src && smilies[i].getAttribute('smilieid') < 1) {
smilies[i].setAttribute('smilieid', smilieid);
smilies[i].setAttribute('border', "0");
}
}
} else {
insertText('<img src="' + src + '" border="0" smilieid="' + smilieid + '" alt="" /> ', false);
}
} else {
code += ' ';
AddText(code);
}
}
*/
function smileyMenu(ctrl) {
    ctrl.style.cursor = 'pointer';
    if (ctrl.alt) {
        ctrl.pop = ctrl.alt;
        ctrl.alt = '';
    }
    if (ctrl.title) {
        ctrl.lw = ctrl.title;
        ctrl.title = '';
    }
    //if(!smdiv[ctrl.id]) {
    smdiv[ctrl.id] = document.createElement('div');
    smdiv[ctrl.id].id = ctrl.id + '_menu';
    smdiv[ctrl.id].style.display = 'none';
    smdiv[ctrl.id].style.width = '60px';
    smdiv[ctrl.id].style.height = '60px';
    smdiv[ctrl.id].className = 'popupmenu_popup';
    ctrl.parentNode.appendChild(smdiv[ctrl.id]);
    //}
    smdiv[ctrl.id].innerHTML = '<table width="100%" height="100%"><tr><td align="center" valign="middle"><img src="' + ctrl.src + '" border="0" /></td></tr></table>';
    showMenu(ctrl.id, 0, 0, 1, 0);
}


function smileyMenuQt(ctrl) {
    ctrl.style.cursor = 'pointer';
    if (ctrl.alt) {
        ctrl.pop = ctrl.alt;
        ctrl.alt = '';
    }
    if (ctrl.title) {
        ctrl.lw = ctrl.title;
        ctrl.title = '';
    }
    //if(!smdiv[ctrl.id]) {
    smdiv[ctrl.id] = document.createElement('div');
    smdiv[ctrl.id].id = ctrl.id + '_menu';
    smdiv[ctrl.id].style.display = 'none';
    smdiv[ctrl.id].style.width = '120px';
    smdiv[ctrl.id].style.height = '120px';
    smdiv[ctrl.id].className = 'popupmenu_popup';
    ctrl.parentNode.appendChild(smdiv[ctrl.id]);
    //}
    smdiv[ctrl.id].innerHTML = '<table width="100%" height="100%"><tr><td align="center" valign="middle"><img src="' + ctrl.src + '" border="0" /></td></tr></table>';
    showMenu(ctrl.id, 0, 0, 1, 0);
}
function insertSmiley(code) {
    checkFocus();
    var src = $('smilie_' + code).src;
    //var code = $('smilie_' + code).pop;
    if (typeof wysiwyg != 'undefined' && wysiwyg && allowsmilies && (!$('smileyoff') || $('smileyoff').checked == false)) {
        if (is_moz) {
            applyFormat('InsertImage', false, src);
            var smilies = findtags(editdoc.body, 'img');
            for (var i = 0; i < smilies.length; i++) {
                if (smilies[i].src == src && !smilies[i].getAttribute('smiliecode')) {
                    smilies[i].setAttribute('smiliecode', code);
                    smilies[i].setAttribute('border', "0");
                }
            }
        } else {
            insertText('<img src="' + src + '" border="0" smiliecode="' + code + '" alt="" /> ', false);
        }
    } else {
        code += ' ';
        AddText(code);
    }

    var popup_menu = document.getElementById("posteditor_popup_smile_menu");
    if(popup_menu)
        popup_menu.style.display = "none";

    var smail_group = document.getElementById("smile_groups");
    if (smail_group)
        smail_group.style.display = "none";    
    hideMenu();
}

function showsmiles(index, typename, pageindex) {
    $("s_" + index).className = "lian";
    var cIndex = 1;
    for (i in smilies_HASH) {
        if (cIndex != index) {
            $("s_" + cIndex).className = "";
        }
        cIndex++;
    }

    var pagesize = (typeof smiliePageSize) == 'undefined' ? 12 : smiliePageSize;
    var s = smilies_HASH[typename];
    var pagecount = Math.ceil(s.length / pagesize);

    if (pagecount < 1) {
        pagecount = 1;
    }

    if (isUndefined(pageindex)) {
        pageindex = 1;
    }
    if (pageindex > pagecount) {
        pageindex = pagecount;
    }

    var maxIndex = pageindex * pagesize;
    if (maxIndex > s.length) {
        maxIndex = s.length;
    }
    maxIndex = maxIndex - 1;

    var minIndex = (pageindex - 1) * pagesize;

    var html = '<table align="center" border="0" cellpadding="3" cellspacing="0" width="90%"><tr>';

    var ci = 1;
    if (typename == '魔法表情') {
        for (var id = minIndex; id <= maxIndex; id++) {
            html += '<td valign="middle"><img style="cursor: pointer;" src="editor/images/smilies/' + s[id]['url'] + '" id=smilie_' + s[id]['code'] + ' alt="" onclick="insertSmiley(\'' + addslashes(s[id]['code']) + '\')" onmouseover="DispMagicEmot(this,' + s[id]['url'].substr(10, s[id]['url'].length - 14) + ')" title="" border="0" height="20" width="20" /></td>';
            if (ci % 4 == 0) {
                html += '</tr><tr>'
            }
            ci++;
        }
    }
    else if (typename == '默认表情') {
        for (var id = minIndex; id <= maxIndex; id++) {
            html += '<td valign="middle"><img style="cursor: pointer;" src="editor/images/smilies/' + s[id]['url'] + '" id=smilie_' + s[id]['code'] + ' alt="" onclick="insertSmiley(\'' + addslashes(s[id]['code']) + '\')" onmouseover="smileyMenu(this)" title="" border="0" height="20" width="20" /></td>';
            if (ci % 4 == 0) {
                html += '</tr><tr>'
            }
            ci++;
        }
    }
    else {
        for (var id = minIndex; id <= maxIndex; id++) {
            html += '<td valign="middle"><img style="cursor: pointer;" src="editor/images/smilies/' + s[id]['url'] + '" id=smilie_' + s[id]['code'] + ' alt="" onclick="insertSmiley(\'' + addslashes(s[id]['code']) + '\')" onmouseover="smileyMenuQt(this)" title="" border="0" height="20" width="20" /></td>';
            if (ci % 4 == 0) {
                html += '</tr><tr>'
            }
            ci++;
        }
    }
    html += '<td colspan="' + (4 - ((ci - 1) % 4)) + '"></td>';
    html += '</tr>';
    html += '</table>';
    $("showsmilie").innerHTML = html;

    if (pagecount > 1) {
        html = '<div class="p_bar">';
        for (var i = 1; i <= pagecount; i++) {
            if (i == pageindex) {
                html += "<a class=\"p_curpage\">" + i + "</a>";
            }
            else {
                html += "<a class=\"p_num\" href='#smiliyanchor' onclick=\"showsmiles(" + index + ", '" + typename + "', " + i + ")\">" + i + "</a>"
            }
        }
        html += '</div>';
        $("showsmilie_pagenum").innerHTML = html;
    }
    else {
        $("showsmilie_pagenum").innerHTML = "";
    }
}

function scrollSmilieTypeBar(bar, scrollwidth) {
    //bar.scrollLeft += scrollwidth;
    var i = 0;
    if (scrollwidth > 0) {
        var scl = window.setInterval(function() {
            if (i < scrollwidth) {
                bar.scrollLeft += 1;
                i++
            }
            else
                window.clearInterval(scl);
        }, 1);
    }
    else {
        var scl = window.setInterval(function() {
            if (i > scrollwidth) {
                bar.scrollLeft -= 1;
                i--
            }
            else
                window.clearInterval(scl);
        }, 1);
    }
}
/*Discuz!NT end*/
function parseurl(str, mode) {
    str = str.replace(/([^>=\]"'\/]|^)((((https?|ftp):\/\/)|www\.)([\w\-]+\.)*[\w\-\u4e00-\u9fa5]+\.([\.a-zA-Z0-9]+|\u4E2D\u56FD|\u7F51\u7EDC|\u516C\u53F8)((\?|\/|:)+[\w\.\/=\?%\-&~`@':+!]*)+\.(jpg|gif|png|bmp))/ig, mode == 'html' ? '$1<img src="$2" border="0">' : '$1[img]$2[/img]');
    str = str.replace(/([^>=\]"'\/@]|^)((((https?|ftp|gopher|news|telnet|rtsp|mms|callto|bctp|ed2k|thunder|synacast):\/\/))([\w\-]+\.)*[:\.@\-\w\u4e00-\u9fa5]+\.([\.a-zA-Z0-9]+|\u4E2D\u56FD|\u7F51\u7EDC|\u516C\u53F8)((\?|\/|:)+[\w\.\/=\?%\-&~`@':+!#]*)*)/ig, mode == 'html' ? '$1<a href="$2" target="_blank">$2</a>' : '$1[url]$2[/url]');
    str = str.replace(/([^\w>=\]"'\/@]|^)((www\.)([\w\-]+\.)*[:\.@\-\w\u4e00-\u9fa5]+\.([\.a-zA-Z0-9]+|\u4E2D\u56FD|\u7F51\u7EDC|\u516C\u53F8)((\?|\/|:)+[\w\.\/=\?%\-&~`@':+!#]*)*)/ig, mode == 'html' ? '$1<a href="$2" target="_blank">$2</a>' : '$1[url]$2[/url]');
    str = str.replace(/([^\w->=\]:"'\.\/]|^)(([\-\.\w]+@[\.\-\w]+(\.\w+)+))/ig, mode == 'html' ? '$1<a href="mailto:$2">$2</a>' : '$1[email]$2[/email]');
    return str;
}

function loadData() {
    var message = '';
    if (is_ie) {
        try {
            textobj.load('save_postdata');
            var oXMLDoc = textobj.XMLDocument;
            var nodes = oXMLDoc.documentElement.childNodes;
            message = nodes.item(nodes.length - 1).getAttribute('message');
        } catch (e) { }
    } else if (window.sessionStorage) {
        try {
            message = sessionStorage.getItem('save_postdata');
        } catch (e) { }
    }
    message = message.toString();

    if (in_array((message = trim(message)), ['', 'null', 'false', null, false])) {
        alert(lang['post_autosave_none']);
        return;
    }
    if (!confirm(lang['post_autosave_confirm'])) {
        return;
    }

    var formdata = message.split(/\x09\x09/);
    for (var i = 0; i < $(postform).elements.length; i++) {
        var el = $(postform).elements[i];
        if (el.name != '' && (el.tagName == 'TEXTAREA' || el.tagName == 'INPUT' && el.type == 'text')) {
            for (var j = 0; j < formdata.length; j++) {
                var ele = formdata[j].split(/\x09/);
                if (ele[0] == el.name) {
                    elvalue = !isUndefined(ele[3]) ? ele[3] : '';
                    if (ele[1] == 'INPUT') {
                        if (ele[2] == 'text') {
                            el.value = elvalue;
                        } else if (ele[2] == 'checkbox' || ele[2] == 'radio') {
                            el.checked = true;
                        }
                    } else if (ele[1] == 'TEXTAREA') {
                        if (ele[0] == 'message') {
                            if (typeof wysiwyg == 'undefined' || !wysiwyg) {
                                textobj.value = elvalue;
                            } else {
                                editdoc.body.innerHTML = bbcode2html(elvalue);
                            }
                        } else {
                            el.value = elvalue;
                        }
                    }
                    break
                }
            }
        }
    }
}

function getcookieex(name) {
    var cookie_start = document.cookie.indexOf(name + '=');
    var cookie_end = document.cookie.indexOf(";", cookie_start);
    if(cookie_start == -1){
        return '';
    }
    else {
        var msg = document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length));
        while (msg.indexOf('+') != -1) {
            msg = msg.replace('+', ' ');
        }
        
        return unescape(decodeURI(msg));
    }
}

function deleteCookie(name, path, domain) {
    if (getcookieex(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }

}

function saveData(data, del) {
    if (!data && isUndefined(del)) {
        return;
    }
    if (typeof wysiwyg != 'undefined' && typeof editorid != 'undefined' && typeof bbinsert != 'undefined' && bbinsert && $(editorid + '_mode') && $(editorid + '_mode').value == 1) {
        data = html2bbcode(data);
    }
    var formdata = '';
    for (var i = 0; i < $(postform).elements.length; i++) {
        var el = $(postform).elements[i];
        if (el.name != '' && (el.tagName == 'TEXTAREA' || el.tagName == 'INPUT' && el.type == 'text') && el.name.substr(0, 6) != 'attach') {
            var elvalue = el.name == 'message' ? data : el.value;
            formdata += el.name + String.fromCharCode(9) + el.tagName + String.fromCharCode(9) + el.type + String.fromCharCode(9) + elvalue + String.fromCharCode(9, 9);
        }
    }
    if (is_ie) {
        try {
            var oXMLDoc = textobj.XMLDocument;
            var root = oXMLDoc.firstChild;
            if (root.childNodes.length > 0) {
                root.removeChild(root.firstChild);
            }
            var node = oXMLDoc.createNode(1, 'POST', '');
            var oTimeNow = new Date();
            oTimeNow.setHours(oTimeNow.getHours() + 24);
            textobj.expires = oTimeNow.toUTCString();
            node.setAttribute('message', formdata);
            oXMLDoc.documentElement.appendChild(node);
            textobj.save('save_postdata');
        } catch (e) { }
    } else if (window.sessionStorage) {
        try {
            sessionStorage.setItem('save_postdata', formdata);
        } catch (e) { }
    }
}

function setCaretAtEnd() {
    var obj = typeof wysiwyg == 'undefined' || !wysiwyg ? $(postform).message : editwin;
    if (typeof wysiwyg != 'undefined' && wysiwyg) {
        if (is_moz || is_opera) {

        } else {
            var sel = editdoc.selection.createRange();
            sel.moveStart('character', strlen(getEditorContents()));
            sel.select();
        }
    } else {
        if (obj.createTextRange) {
            var sel = obj.createTextRange();
            sel.moveStart('character', strlen(obj.value));
            sel.collapse();
            sel.select();
        }
    }
}

function storeCaret(textEl) {
    if (textEl.createTextRange) {
        textEl.caretPos = document.selection.createRange().duplicate();
    }
}

if (is_ie >= 5 || is_moz >= 2) {
    window.onbeforeunload = function() {
        try {
            saveData(wysiwyg && bbinsert ? editdoc.body.innerHTML : textobj.value);
        } catch (e) { }
    };
}

function setmediacode(editorid) {
    insertText('[media=' + $(editorid + '_mediatype').value +
		',' + $(editorid + '_mediawidth').value +
		',' + $(editorid + '_mediaheight').value +
		',' + $(editorid + '_mediaautostart').value + ']' +
		$(editorid + '_mediaurl').value + '[/media]');
    hideMenu();
}

function setmediatype(editorid) {
    var ext = $(editorid + '_mediaurl').value.lastIndexOf('.') == -1 ? '' : $(editorid + '_mediaurl').value.substr($(editorid + '_mediaurl').value.lastIndexOf('.') + 1, $(editorid + '_mediaurl').value.length).toLowerCase();
    if (ext == 'rmvb') {
        ext = 'rm';
    }
    if ($(editorid + '_mediatyperadio_' + ext)) {
        $(editorid + '_mediatyperadio_' + ext).checked = true;
        $(editorid + '_mediatype').value = ext;
    }
}

var divdragstart = new Array();
function divdrag(e, op, obj) {
    if (op == 1) {
        divdragstart = is_ie ? [event.clientX, event.clientY] : [e.clientX, e.clientY];
        divdragstart[2] = parseInt(obj.style.left);
        divdragstart[3] = parseInt(obj.style.top);
        doane(e);
    } else if (op == 2 && divdragstart[0]) {
        var divdragnow = is_ie ? [event.clientX, event.clientY] : [e.clientX, e.clientY];
        obj.style.left = (divdragstart[2] + divdragnow[0] - divdragstart[0]) + 'px';
        obj.style.top = (divdragstart[3] + divdragnow[1] - divdragstart[1]) + 'px';
        doane(e);
    } else if (op == 3) {
        divdragstart = [];
        doane(e);
    }
}

function DispMagicEmot(EmotID, i) {
    MagicEmotUrl = "editor/images/smilies/MagicEmot/swf/" + i + ".swf";
    EmotID.style.cursor = 'pointer';
    if (EmotID.alt) {
        EmotID.pop = EmotID.alt;
        EmotID.alt = '';
    }
    if (EmotID.title) {
        EmotID.lw = EmotID.title;
        EmotID.title = '';
    }
    smdiv[EmotID.id] = document.createElement('div');
    smdiv[EmotID.id].id = EmotID.id + '_menu';
    smdiv[EmotID.id].style.display = 'none';
    smdiv[EmotID.id].style.width = '192px';
    smdiv[EmotID.id].style.height = '192px';
    smdiv[EmotID.id].className = 'popupmenu_popup';
    EmotID.parentNode.appendChild(smdiv[EmotID.id]);
    smdiv[EmotID.id].innerHTML = '<Div style="padding:0;position:relative;"><Div style="padding:0;position:absolute;z-index:1;"><OBJECT codeBase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="192" height="192"><PARAM NAME=movie VALUE="' + MagicEmotUrl + '"><PARAM NAME=quality VALUE=high><PARAM NAME=play VALUE=true><param name="wmode" value="transparent"><embed src="' + MagicEmotUrl + '" wmode="transparent" quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="192" height="192"></embed></OBJECT></Div><img src="editor/images/blank.gif" style="padding:0;position:absolute;width:400;height:300;left:0;top:0;z-index:2;cursor:hand;" Onclick="document.getElementById(\'Xnc_Emot\').innerHTML=\'\';" Alt="点击隐藏魔法表情"  oncontextmenu="false"></Div>';
    showMenu(EmotID.id, 0, 0, 1, 0);
}

function DispMagicEm(obj, EmotID) {
    MagicEmotUrl = "editor/images/smilies/MagicEmot/swf/" + EmotID + ".swf";
    var obj = document.getElementById("Xnc_Emot");
    obj.innerHTML = '<Div style="padding:0;position:relative;"><Div style="padding:0;position:absolute;z-index:1;"><OBJECT codeBase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="400" height="300"><PARAM NAME=movie VALUE="' + MagicEmotUrl + '"><PARAM NAME=quality VALUE=high><PARAM NAME=play VALUE=true><param name="wmode" value="transparent"><embed src="' + MagicEmotUrl + '" wmode="transparent" quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="400" height="300"></embed></OBJECT></Div><img src="editor/images/blank.gif" style="padding:0;position:absolute;width:400;height:300;left:0;top:0;z-index:2;cursor:hand;" Onclick="document.getElementById(\'Xnc_Emot\').innerHTML=\'\';" Alt="点击隐藏魔法表情"  oncontextmenu="false"></Div>';
    obj.style.top = (document.documentElement.scrollTop + ((document.documentElement.clientHeight - 300) / 2)) + "px";
    obj.style.left = (document.body.scrollLeft + ((document.body.clientWidth - 400) / 2)) + "px";
    obj.style.display = 'inline';
    EmotID += Math.random();
    window.setTimeout('HideMagicEm(' + EmotID + ')', 5000);
    NowMeID = EmotID;
}

function HideMagicEm(EmotID) {
    if (NowMeID == EmotID) {
        document.getElementById("Xnc_Emot").style.display = 'none';
        NowMeID = '';
    }
}
function HideMagicEmot(EmotID) {
    if (NowMeID == EmotID) {
        document.getElementById("" + EmotID + "").style.display = 'none';
        NowMeID = '';
    }
}