function dosubmit( type, action ) {
    if( type == "save" ) {
        document.forms["posting"].elements["bwaction"].value = action;
        document.posting.submit();
    } else if( type == "preview" ) {
        document.forms["posting"].elements["bwaction"].value = "postingpreview";
        document.posting.submit();
    }
}

function Go(x) {
    if(x == 0) {
        return false;
    } else if(x == -1) {
        location.href = "index.html";
    } else {
        document.location.href = "forum.html?bwboardid="+x+"&bwpage=1";
    }
}

function editsubmit( type ) {
    if( type == "save" ) {
        document.forms["edit"].elements["bwaction"].value = "changeposting";
        document.edit.submit();
    } else if( type == "preview" ) {
        document.forms["edit"].elements["bwaction"].value = "editposting";
        document.edit.submit();
    }
}

function triggerHidden( posting ) {
    newstyle = posting.nextSibling.style.display == "none" ? "block" : "none";
    posting.nextSibling.style.display = newstyle;
}

function insertText(aTag, eTag) {
    var input = document.getElementById("bodytext");
    input.focus();

    if( !eTag ) {
        eTag='';
    }

    if( document.selection ) {
        /* IE */
        var range = document.selection.createRange();
        var insText = range.text;
        range.text = aTag + insText + eTag;

        range = document.selection.createRange();
        if( insText.length == 0 ) {
            range.move('character', -eTag.length);
        } else {
            range.moveStart('character', aTag.length + insText.length + eTag.length);
        }
    } else if(typeof input.selectionStart != 'undefined') {
        /* Firefox */
        var start = input.selectionStart;
        var end = input.selectionEnd;
        var insText = input.value.substring(start, end);
        input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);

        var pos = start + aTag.length + eTag.length;
        if (insText.length > 0) {
            pos += insText.length;
        }
        input.selectionStart = pos;
        input.selectionEnd = pos;
    } else {
        var pos = input.value.length;
        var insText = prompt("Bitte geben Sie den zu formatierenden Text ein:");
        input.value = input.value.substr(0, pos) + aTag + insText + eTag + input.value.substr(pos);
    }
}

function inserturl() {
    url = prompt('(1/2) URL','http://');
    if (url == '' || url == null) {
        return false;
    }
    linktext = prompt('(2/2) Linktext','');
    if( linktext == null ) {
        return false;
    }
    if (linktext == '') {
        linktext = url;
    }
    var reg = new RegExp("([a-zA-Z]+://)");
    if( !reg.test(url) ) {
        url = "http://" + url;
    }
    insertText( '[@link ' + url + ']' + linktext + '[/link]' );
}

function insertimage() {
    url = prompt('URL des Bildes','http://');
    if (url == '' || url == null) {
        return false;
    }
    insertText( '[@img ' + url + ']' );
}

function encloseInTag( tag ) {
    insertText( '['+tag+']', '[/'+tag+']' );
}

function disablePosting( postingid, threadid, sessid ) {
    if( confirm("Beitrag wirklich sperren?") ) {
        document.location.href = "/forum/topic.asp?bwaction=disableposting&bwpostingid="+postingid+"&bwthreadid="+threadid+"&"+sessid;
    }
}

function disableThread( threadid, sessid ) {
    if( confirm("Thema wirklich sperren?") ) {
        document.location.href = "/forum/topic.asp?bwaction=lockthread&bwthreadid="+threadid+"&"+sessid;
    }
}

function colorize() {
    var elColor = document.getElementById("textcolor");
    if( elColor.selectedIndex > 0 ) {
        encloseInTag( elColor.options[elColor.selectedIndex].value );
    }
    elColor.selectedIndex = 0;
}

function confirmAction( msg, targeturl ) {
    if( confirm( msg ) ) {
        document.location.href = targeturl;
    }
}

function showReason( shareid ) {
    if( $('#sharereason_'+shareid).is(':hidden') ) {
        $('#sharereason_'+shareid).fadeIn('slow');
        $('#sharebutton_'+shareid).html('verstecken');
    } else {
        $('#sharereason_'+shareid).fadeOut('slow');
        $('#sharebutton_'+shareid).html('anzeigen');
    }
}

function showShareEngComments( shareid, prefix ) {
    var myrand = Math.floor(Math.random()*1000000);
    if( $('#comments_'+prefix+shareid).is(":empty") ) {
        $.get( '/ajax.php',
                { module: 'engagements', action: 'ajaxshareengcomments', engagement_id: shareid, myrand: myrand },
                function(data) { $('#comments_'+prefix+shareid).html(data) } );
    }
    $('#commentdiv_'+prefix+shareid).toggle();
    if( $('#sharereason_'+prefix+shareid).is(':hidden') ) {
        $('#sharereason_'+prefix+shareid).fadeIn('slow');
        $('#sharebutton_'+prefix+shareid).html('verstecken');
    }
}

function toggleMsgText( id ) {
    if( $('#msgtext_'+id).is(':hidden') ) {
        $('#msgtext_'+id).fadeIn('slow');
        $('#readbutton_'+id).html('schliessen');
        if( $('#msgstatus_'+id).html() == 'neu' ) {
            $.get( '/ajax.php',
                    {module: 'messaging', action: 'markpmread', msg_id: id},
                    function(data) { $('#msgstatus_'+id).html(data) } );
        }
    } else {
        $('#msgtext_'+id).fadeOut('slow');
        $('#readbutton_'+id).html('lesen');
    }
}

