var options_url = '/room-post.pl';
var readonly_time;
var deleted_time;
var sticky_time;
var b_is_watching;
var b_is_moderator;
var thread_id;

function submit_report_abuse_form( post_id )
{
    var complaint = '';
    var o = gk_get_el( 'report_abuse_note' );
    if (o)  complaint = o.value;

    var params = [];
    params.push( 'opt=fra' );
    params.push( 'note='+encodeURIComponent(complaint) );
    params.push( 'pid='+post_id );
    params.push( 'ref='+encodeURIComponent(location.href) );
    var txt = gk_request_remote_data( options_url, 0, null, params.join('&') );

    if (!txt) return;	// page reload?

    var b_success = 0;
    var msg = '';
    var data = txt.split('\n');
    if (data[0]!='forum-post.1.0')
    {
	msg = '<em class=err>ERROR:</em> unable to establish connection to the server!';
    }
    else
    {
	b_success = to_int(data[1]);
	msg = data[2];
    }
    
    var frame = gk_get_el( 'report_abuse_form' );
    if (!frame) frame = document.body;
    gui_pop_message( msg, frame, b_success ? 1000 : 3000 );
    
    if (b_success) gui_pop_form_remove();
}

function open_report_abuse_form( el, post_id, poster_uid, time_posted )
{
    var txt = 
	'<table border=0 cellspacing=6 cellpadding=0 id="report_abuse_form">'+
	'<tr><td>'+
	'Report this forum post by <em>'+poster_uid+'</em> made<br>on '+time_posted+' to the moderators'+
	'</td></tr>'+
	'<tr><td>Complaint: (please be specific!)<br>'+
	'<INPUT TYPE="TEXT" id="report_abuse_note" SIZE="50" MAXLENGTH="200"></td></tr>'+
	'<tr><td style="text-align: center; padding-top: 6px;">'+
	'<INPUT TYPE="button" VALUE="Submit" onClick="submit_report_abuse_form('+post_id+');"> '+
	'<INPUT TYPE="button" VALUE="Cancel" onClick="gui_pop_form_remove();">'+
	'</td></tr>'+
	'</table>';
	
    gui_pop_form( txt, el, { 'dir': 1, 'align': 0, 'header': 'Report abuse:' } );
}


function submit_thread_options_form()
{
    var o = gk_get_el( 'thread_option_watching' );
    if (o) b_is_watching = o.checked ? 1 : 0;

    if (b_is_moderator)
    {
	o = gk_get_el( 'thread_option_readonly' );
	if (o) readonly_time = o.checked ? 1 : 0;

	o = gk_get_el( 'thread_option_deleted' );
	if (o) deleted_time = o.checked ? 1 : 0;

	o = gk_get_el( 'thread_option_sticky' );
	if (o) sticky_time = o.checked ? 1 : 0;
    }

    var params = [];
    params.push( 'opt=fto' );
    params.push( 'tid='+thread_id );
    params.push( 'w='+b_is_watching );
    if (b_is_moderator)
    {
	params.push( 'r='+readonly_time );
	params.push( 'd='+deleted_time );
	params.push( 's='+sticky_time );
    }
    params.push( 'ref='+encodeURIComponent(location.href) );
    var txt = gk_request_remote_data( options_url, 0, null, params.join('&') );

    if (!txt) return;	// page reload?

    var b_success = 0;
    var msg = '';
    var data = txt.split('\n');
    if (data[0]!='forum-post.1.0')
    {
	msg = '<em class=err>ERROR:</em> unable to establish connection to the server!';
    }
    else
    {
	b_success = to_int(data[1]);
	msg = data[2];
    }
    
    var frame = gk_get_el( 'thread_options_form' );
    if (!frame) frame = document.body;
    gui_pop_message( msg, frame, b_success ? 1000 : 3000 );
    
    if (b_success) gui_pop_form_remove();

    update_watching();
}

function open_thread_options_form( el )
{
    var txt = 
	'<table border=0 cellspacing=6 cellpadding=0 id="thread_options_form">'+
	'<tr><td><INPUT TYPE="checkbox" id="thread_option_watching" VALUE="1" '+(b_is_watching?'checked':'')+'>'+
	'</td><td>- watching (get a notification when someone posts a reply)</td></tr>'+
    ( b_is_moderator ? 
	'<tr><td><INPUT TYPE="checkbox" id="thread_option_readonly" VALUE="1" '+(readonly_time?'checked':'')+'>'+
	'</td><td>- locked (closed for new posts)</td></tr>'+
	'<tr><td><INPUT TYPE="checkbox" id="thread_option_deleted" VALUE="1" '+(deleted_time?'checked':'')+'>'+
	'</td><td>- deleted (only the moderators can read it)</td></tr>'+
	'<tr><td><INPUT TYPE="checkbox" id="thread_option_sticky" VALUE="1" '+(sticky_time?'checked':'')+'>'+
	'</td><td>- sticky (always appears above all other message threads)</td></tr>'
    : '' )+
	'<tr><td style="text-align: center; padding-top: 6px;" colspan=2>'+
	'<INPUT TYPE="button" VALUE="Submit" onClick="submit_thread_options_form();"> '+
	'<INPUT TYPE="button" VALUE="Cancel" onClick="gui_pop_form_remove();">'+
	'</td></tr>'+
	'</table>';
	
    gui_pop_form( txt, el, { 'dir': 1, 'align': 0, 'header': 'Message thread options:' } );
}

function submit_thread_mod_form()
{
    var new_home = -1;
    var n = 0;
    while(1)
    {
	var o = gk_get_el('new-home-'+n);
	if (!o) break;
	if (o.checked)
	{
	    new_home = n;
	    break;
	}
	n++;
    }
    
    var frame = gk_get_el( 'thread_mod_form' );
    if (!frame) frame = document.body;
    
    if (new_home<0)
    {
	gui_pop_message( 'No selection was made!', frame, 1000 );
	gui_pop_form_remove();
	return;
    }

    var params = [];
    params.push( 'opt=stv' );
    params.push( 'tid='+thread_id );
    params.push( 'nh='+new_home );
    params.push( 'ref='+encodeURIComponent(location.href) );
    var txt = gk_request_remote_data( options_url, 0, null, params.join('&') );
    
    if (!txt) return;	// page reload?

    var b_success = 0;
    var msg = '';
    var data = txt.split('\n');
    if (data[0]!='forum-post.1.0')
    {
	msg = '<em class=err>ERROR:</em> unable to establish connection to the server!';
    }
    else
    {
	b_success = to_int(data[1]);
	msg = data[2];
    }
    
    gui_pop_message( msg, frame, b_success ? 1000 : 3000 );
    
    if (b_success) gui_pop_form_remove();
}

function open_thread_mod_form( el )
{
    var params = [];
    params.push( 'opt=gtv' );
    params.push( 'tid='+thread_id );
    params.push( 'ref='+encodeURIComponent(location.href) );
    var txt = gk_request_remote_data( options_url, 0, null, params.join('&') );
    
    if (!txt) return;	// page reload?

    var b_success = 0;
    var msg = '';
    var data = txt.split('\n');
    if (data[0]!='forum-post.1.0')
    {
	msg = '<em class=err>ERROR:</em> unable to establish connection to the server!';
    }
    else
    {
	b_success = to_int(data[1]);
	msg = data[2];
    }
    
    if (!b_success)
    {
	gui_pop_message( msg, el, 3000 );
	return;
    }
    
    var data;
    try
    {
	data = eval( '('+msg+')' );
    }
    catch(e)
    {
	gui_pop_message( '<em class=err>ERROR:</em> unable to communicate with the server!\n'+msg, el, 3000 );
	gk_log_error( '(forum.js) Erroneous JSON received from '+options_url+':\n'+msg );
	return;
    }
    
    if (data.tid!=thread_id)
    {
	gui_pop_message( '<em class=err>ERROR:</em> unable to communicate with the server!', el, 3000 );
	gk_log_error( '(forum.js) Thread id mismatch, got: '+data.tid+', expecting: '+thread_id );
	return;
    }
    
    var all_forums = [ 'GameKnot related', 1, 'Chess related', 2, 'Tournaments and League', 3, 'Teams related', 4, 'Just delete it!', 0 ];
    
    var my_new_home = data.my;
    
    var all_forums_options = [];
    for( var i=0; i<all_forums.length; i+=2 )
    {
	var name = all_forums[i];
	var new_home = all_forums[i+1];
	var count = to_int( data.all[new_home] );
	all_forums_options.push( '<tr><td><INPUT TYPE="radio" NAME="new-home-radio" id="new-home-'+new_home+'" VALUE=1'+(my_new_home==new_home?' CHECKED':'')+'></td><td> - '+name+'</td><td>('+count+')</td></tr>' );
    }
    
    var txt = 
	'<table border=0 cellspacing=4 cellpadding=0 id="thread_mod_form" style="margin: 4px;">'+
	'<tr><td style="padding-bottom: 8px;" colspan=3>This message thread belongs in the following<br>forum:</td></tr>'+
	all_forums_options.join('')+
	'<tr><td style="text-align: center; padding-top: 8px;" colspan=3>'+
	'<INPUT TYPE="button" VALUE="Submit" onClick="submit_thread_mod_form();"> '+
	'<INPUT TYPE="button" VALUE="Cancel" onClick="gui_pop_form_remove();">'+
	'</td></tr>'+
	'</table>';
	
    gui_pop_form( txt, el, { 'dir': 1, 'align': 0, 'header': 'Moderate the message thread:' } );
}

function set_watching(el)
{
    var old_b_watching = b_is_watching;
    b_is_watching = el && el.checked ? 1 : 0;

    update_watching(1);
    
    var params = [];
    params.push( 'opt=ftw' );	// forum thread watch
    params.push( 'watch='+b_is_watching );
    params.push( 'tid='+thread_id );
    params.push( 'ref='+encodeURIComponent(location.href) );
    var txt = gk_request_remote_data( options_url, 0, null, params.join('&') );

    if (!txt) return;	// page reload?

    var b_success = 0;
    var msg = '';
    var data = txt.split('\n');
    if (data[0]!='forum-post.1.0')
    {
	msg = '<em class=err>ERROR:</em> unable to establish connection to the server!';
    }
    else
    {
	b_success = to_int(data[1]);
	msg = data[2];
    }
    
    if (msg) gui_pop_message( msg, gk_get_el('box-notify') || document.body, b_success ? 1000 : 3000 );
    if (!b_success) b_is_watching = old_b_watching;
    
    setTimeout( function() { update_watching(); }, 500 );
}

function update_watching( b_wait )
{
    var o = gk_get_el('box-notify');
    if (!o) return;
    
    var action = b_wait ? 'DISABLED' : 'onClick="set_watching(this);"';
    var set_width = b_wait ? o.offsetWidth : 0;
    
    o.innerHTML = '<INPUT TYPE=CHECKBOX VALUE=1 style="vertical-align: middle;" '+action+' '+(b_is_watching?'CHECKED':'')+'> '+
		( b_wait ? '<span class=span-wait>Saving...</span>' : '<img src="/img/i/magnifier-medium.png" class=img-i style="margin: 0 -2px;"> Notify me about new posts' );
		
    o.style.width = set_width ? set_width + 'px' : '';
}


