/* Flagging System Global Variables */
var POP_DIV   = null;
var POST_ID   = '';
var FLAG_LVL  = '';
var CURRENT_FLAG = '';

/*----------------------------------------------------------------------
 * function interface_check()
 *
 * Make the interface pieces if they do not exist.
 *----------------------------------------------------------------------
*/
function interface_check()
{
        if ( POP_DIV == null ) {
                POP_DIV = new DialogBox('abuse123');
        }
}

/*----------------------------------------------------------------------
 * function show_thanks()
 *
 * Show a thank you message.
 *----------------------------------------------------------------------
*/
function show_thanks()
{
    var msg_row = document.getElementById('FLAG_CONTROL_'+CURRENT_FLAG);
    var name    = getCookie(COOKIE_UNAME) || 'Guest';
    msg_row.style.fontWeight = 'bold';
    msg_row.style.color = '#006666';
    msg_row.align = 'center';
    msg_row.innerHTML = 'Thank You ' + name + ', your flag has been recorded';
}

/*----------------------------------------------------------------------
 * function confirm_guest(XML)
 *
 * Handles the response from the server when flagging as a guest.
 *----------------------------------------------------------------------
*/
function confirm_guest(XML)
{
        try {
                show_thanks();
                POP_DIV.hide();
        } catch(e) { }
}

/*----------------------------------------------------------------------
 * function confirm_user(XML)
 *
 * Handle the response from the server when flagging as a user.
 *----------------------------------------------------------------------
*/
function confirm_user(XML)
{
        try {
                var info    = XML.getNodeGroup('info');
                var message = XML.getNodeValue('message',info[0]);
                if ( message == 'ERROR' ) {
                        POP_DIV.setErrorMessage(ERROR_GENERAL);
                } else if ( message == 'NO_INFO' ) {
                        POP_DIV.setErrorMessage(ERROR_NO_INFO);
                } else if ( message == 'BAD_LOGIN' ) {
                        POP_DIV.setErrorMessage(ERROR_BAD_LOGIN);
                } else if ( message == 'OK' ) {
                        var id    = XML.getNodeValue('userid',info[0]);
                        var stamp = XML.getNodeValue('stamp',info[0]);
                        var uname = XML.getNodeValue('uname',info[0]);
                        deleteCookie(COOKIE_USERID);
                        deleteCookie(COOKIE_STAMP);
                        setCookie(COOKIE_USERID,id,COOKIE_EXPIRE);
                        setCookie(COOKIE_STAMP,stamp,COOKIE_EXPIRE);
                        setCookie(COOKIE_UNAME,uname,COOKIE_EXPIRE);

                        show_thanks();
                        POP_DIV.hide();
                } else { POP_DIV.hide(); }
        } catch(e) { }
}

/*----------------------------------------------------------------------
 * function flag_guest()
 *
 * Send a request to flag as a guest.
 *----------------------------------------------------------------------
*/
function flag_guest()
{
    var http = new SimpleAJAX();
    http.setAction(ABUSE_SCRIPT);
    http.setMethod('POST');
    http.addParameter('post_id',POST_ID);
    http.addParameter('flag_lvl',FLAG_LVL);
    http.addParameter('user_id',-1);
    http.setHandler(confirm_guest);
    POP_DIV.setErrorMessage(MSG_SENDING);
    http.send();
}

/*----------------------------------------------------------------------
 * function login_vote(XML)
 *
 * Logged in user tried to vote.
 *----------------------------------------------------------------------
*/
function login_vote(XML)
{
        try {
                var info    = XML.getNodeGroup('info');
                var message = XML.getNodeValue('message',info[0]);
                if ( message == 'OK' ) {
                        show_thanks();
                } else {
                        interface_check();
                        POP_DIV.setSize(HTML_LoginWidth,HTML_LoginHeight);
                        POP_DIV.setContents(HTML_LoginContent);
                        POP_DIV.moveTo(parseInt(screen.width/2),parseInt(screen.height/2));
                        POP_DIV.setErrorMessage(ERROR_GENERAL);
                        POP_DIV.show();
                }
        } catch(e) { }
}

/*----------------------------------------------------------------------
 * function flag_member()
 *
 * Send a request to flag as a member.
 *----------------------------------------------------------------------
*/
function flag_member()
{
        var username = document.getElementById('flag_username').value;
        var password = document.getElementById('flag_password').value;
        var http     = new SimpleAJAX();
        http.setAction(ABUSE_SCRIPT);
    http.setMethod('POST');
        http.addParameter('post_id',POST_ID);
    http.addParameter('flag_lvl',FLAG_LVL);
    http.addParameter('username',username);
    http.addParameter('password',password);
    http.setHandler(confirm_user);
    POP_DIV.setErrorMessage(MSG_SENDING);
    http.send();
}

/*----------------------------------------------------------------------
 * function flag_mild(post,level)
 *
 * Report mild abuse.
 *----------------------------------------------------------------------
*/
function flag_mild(post,level)
{
        return send_flag(post,level);
}

/*----------------------------------------------------------------------
 * function flag_abuse(post,level)
 *
 * Report abuse.
 *----------------------------------------------------------------------
*/
function flag_abuse(post,level)
{
        return send_flag(post,level);
}

/*----------------------------------------------------------------------
 * function flag_flagrant(post,level)
 *
 * Report flagrant abuse.
 *----------------------------------------------------------------------
*/
function flag_flagrant(post,level)
{
        return send_flag(post,level);
}

/*----------------------------------------------------------------------
 * function send_flag(post,level)
 *
 * Sends the flag to the server.
 *----------------------------------------------------------------------
*/
function send_flag(post,level) {
        POST_ID  = post;
        FLAG_LVL = level;
        CURRENT_FLAG = post;
        if ( getCookie(COOKIE_USERID) ) {
                //if ( confirm('Are you sure you wish to flag this post?') ) {
                        var http     = new SimpleAJAX();
                        http.setAction(ABUSE_SCRIPT);
                        http.setMethod('POST');
                        http.addParameter('post_id',POST_ID);
                        http.addParameter('flag_lvl',FLAG_LVL);
                        http.addParameter('user_id',getCookie(COOKIE_USERID));
                        http.addParameter('poster_stamp',getCookie(COOKIE_STAMP));
                        http.setHandler(login_vote);
                        http.send();
                //}
        } else {
                interface_check();
                POP_DIV.setSize(HTML_LoginWidth,HTML_LoginHeight);
                POP_DIV.setContents(HTML_LoginContent);
                POP_DIV.moveTo(parseInt(screen.width/2),parseInt(screen.height/2));
                POP_DIV.show();
                document.getElementById('flag_username').focus();
        }
        return false;
}

function refreshLoginLine()
{
        var html  = '<b>Welcome ' + getCookie(COOKIE_UNAME) + '</b>';
                html += '(<a href="#" onclick="setCookie(\'FLAG_uname\',0,0); setCookie(\'FLAG_userid\',0,0); setCookie(\'FLAG_stamp\',0,0); top.location = top.location; return false;">Log-out</a>)';
                html += '<a href="./discussion.shtml?up='+getCookie(COOKIE_USERID)+'&v=1">View Your Page</a>';
        document.getElementById('login_line').innerHTML = html;
}

function general_login(XML)
{
        try {
                var info    = XML.getNodeGroup('info');
                var message = XML.getNodeValue('message',info[0]);
                if ( message == 'OK' ) {
                        var id    = XML.getNodeValue('userid',info[0]);
                        var stamp = XML.getNodeValue('stamp',info[0]);
                        var uname = XML.getNodeValue('uname',info[0]);
                        deleteCookie(COOKIE_USERID);
                        deleteCookie(COOKIE_STAMP);
                        setCookie(COOKIE_USERID,id,COOKIE_EXPIRE);
                        setCookie(COOKIE_STAMP,stamp,COOKIE_EXPIRE);
                        setCookie(COOKIE_UNAME,uname,COOKIE_EXPIRE);
                        LOGIN.hide();
                        refreshLoginLine();
                } else {
                        interface_check();
                        LOGIN.setSize(HTML_LoginWidth,HTML_LoginHeight);
                        LOGIN.setContents(HTML_GeneralLoginContent);
                        LOGIN.moveTo(parseInt(screen.width/2),parseInt(screen.height/2));
                        LOGIN.setErrorMessage(ERROR_GENERAL);
                        LOGIN.show();
                        document.getElementById('login_username').focus();
                }
        } catch(e) { }
}

function login_member()
{
        var username = document.getElementById('login_username').value;
        var password = document.getElementById('login_password').value;
        var http     = new SimpleAJAX();
        http.setAction(ABUSE_SCRIPT);
        http.setMethod('POST');
        http.addParameter('username',username);
        http.addParameter('password',password);
        http.setHandler(general_login);
        LOGIN.setErrorMessage(MSG_SENDING);
        http.send();
}

function create_member() {
        var uname = document.getElementById('newmember_uname').value;
        var passwd = document.getElementById('newmember_passwd').value;
        var passwd2 = document.getElementById('newmember_passwd2').value;
        var email = document.getElementById('newmember_email').value;
        var http     = new SimpleAJAX();
        http.setAction(CREATE_SCRIPT);
        http.setMethod('POST');
        http.addParameter('uname',uname);
        http.addParameter('passwd',passwd);
        http.addParameter('passwd2',passwd2);
        http.addParameter('email',email);
        http.setHandler(new_member);
        NEWMEMBER.setErrorMessage(MSG_SENDING, 'flag_error_message_new_member');
        http.send();
}

function new_member(XML) {
        try {
                var info    = XML.getNodeGroup('info');
                var message = XML.getNodeValue('message',info[0]);

                if (message == 'OK') {
                        var id    = XML.getNodeValue('userid',info[0]);
                        var stamp = XML.getNodeValue('stamp',info[0]);
                        var uname = XML.getNodeValue('uname',info[0]);

                        deleteCookie(COOKIE_USERID);
                        deleteCookie(COOKIE_STAMP);
                        setCookie(COOKIE_USERID,id,COOKIE_EXPIRE);
                        setCookie(COOKIE_STAMP,stamp,COOKIE_EXPIRE);
                        setCookie(COOKIE_UNAME,uname,COOKIE_EXPIRE);
                        NEWMEMBER.hide();
                        refreshLoginLine();
                } else {
                        var uname = XML.getNodeValue('uname',info[0]);
                        var email = XML.getNodeValue('email',info[0]);

                        NEWMEMBER.setSize(HTML_NewMemberWidth,HTML_NewMemberHeight);
                        NEWMEMBER.setContents(HTML_NewMemberContent);
                        NEWMEMBER.moveTo(parseInt(screen.width/2),parseInt(screen.height/2));
                        NEWMEMBER.setErrorMessage(message, 'flag_error_message_new_member');
                        NEWMEMBER.show();
                        document.getElementById('newmember_uname').focus();
                        document.getElementById('newmember_uname').value = uname;
                        document.getElementById('newmember_email').value = email;
                }
        } catch(e) { }
}


/*----------------------------------------------------------------------
 * function check_flag(post)
 *
 * We are checking to see if the user flagged the post before they
 * reported it as abuse.
 *----------------------------------------------------------------------
*/
function check_flag(chkvlu) {

/*
* here we check if they have popups blocked
* if yes we will return an alert box
* if no we will return a new window
*/
var mine = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
 if(mine) {
    var popUpsBlocked = false;
    }
 else {
    var popUpsBlocked = true;
    }
 mine.close()

var thisname = Get_Cookie('abs_dis');

if (thisname != chkvlu) {

if (popUpsBlocked) {
alert('Before you can report abuse on a post, you must flag the post.');
}
else {
var fg = null;

        if ( fg == null ) {
            var C = '';
            C += '<div style="padding: 3px; font: 10px \'Trebuchet MS\', sans-serif; font-size: 13px;">';
            C += '<div align="right"><a style="color: red;" href="#" onclick="fg.hide(); return false;"><img src="/forums/close-button.gif" border="0" /></a></div>';

            C += 'Before you can report abuse, you must use the flagging system.<br><br>';
            C += '</div>';


            fg = new DialogBox('fg');
            fg.setSize(425,125);
            fg.setContents(C);
            fg.moveTo(parseInt(screen.width/2),parseInt(screen.height/2));
            fg.show();
         } else {
            fg.show();
         }
}
return false;
}
else {
Delete_Cookie('abs_dis', '/', '.loraincounty.com');
return true;
}


}

