/* 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="/MarketPlace/dashboard/">View Your Dashboard</a>';
             do_refresh();
        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();
}