var mystored_selection = '';
var post_cache    = new Array();
var ajax_loaded   = 1;
var ignore_cache  = new Array();

/*--------------------------------------------*/
// Ajax: Save for edit
/*--------------------------------------------*/
function ajax_send_mail( send_type )
{
	//----------------------------------
	// INIT
	//----------------------------------
	var url    = 'index.php?act=mailout&do='+send_type;
	var fields = new Array();
	//----------------------------------
	// Populate fields
	//----------------------------------
	fields['act']      = 'mailout';
	fields['do']       = send_type;
	fields['image_file']      = document.getElementById('image_file').value;
	/*--------------------------------------------*/
	// Main function to do on request
	// Must be defined first!!
	/*--------------------------------------------*/
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			xmlobj.show_loading();
			return;
		}
		xmlobj.hide_loading();
		//----------------------------------
		// INIT
		//----------------------------------
		var html = xmlobj.xmlhandler.responseText;
		if ( html != 'error' )
		{
			document.getElementById('error_message').innerHTML = html;
		}
		else
		{
			document.getElementById('error_message').innerHTML = html;
		}
	}
	//----------------------------------
	// LOAD XML
	//----------------------------------
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	var xmlreturn = xmlobj.process( url, 'POST', xmlobj.format_for_post(fields) );
	return false;
}

/*--------------------------------------------*/
// Ajax: Vote for Contestant
/*--------------------------------------------*/
function ajax_contestant_vote( cid, vote )
{
	//----------------------------------
	// INIT
	//----------------------------------
	var url    = 'index.php?act=eventsxmlout&do=vote-contestant&cid='+cid;
	var fields = new Array();
	//----------------------------------
	// Populate fields
	//----------------------------------
	fields['act']      = 'eventsxmlout';
	fields['do']       = 'vote-contestant';
	fields['cid']       = cid;
	fields['md5check'] = sdk_md5_check;
	/*--------------------------------------------*/
	// Main function to do on request
	// Must be defined first!!
	/*--------------------------------------------*/
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			xmlobj.show_loading();
			return;
		}
		xmlobj.hide_loading();
		//----------------------------------
		// INIT
		//----------------------------------
		var html = xmlobj.xmlhandler.responseText;
		if ( html != 'error' )
		{
			var result_html = html.split("|");
			if ( result_html[0] != 'error')
			{
				if ( document.getElementById('vote_'+cid) ) document.getElementById('vote_'+cid).innerHTML = vote+1;
				if ( window.opener && window.opener.document.getElementById('vote_'+cid) ) window.opener.document.getElementById('vote_'+cid).innerHTML = document.getElementById('vote_'+cid).innerHTML;
				document.getElementById('ajax-post-msg').innerHTML = html;
			}
			else
			{
				document.getElementById( 'ajax-post-msg' ).innerHTML = result_html[1];
			}
		}
		else
		{
			document.getElementById('ajax-post-msg').innerHTML = html;
		}
	}
	//----------------------------------
	// LOAD XML
	//----------------------------------
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	var xmlreturn = xmlobj.process( url, 'POST', xmlobj.format_for_post(fields) );
	return false;
}
/*--------------------------------------------*/
// Ajax: Go Contestant Vote Page
/*--------------------------------------------*/
function ajax_contestant_go_vote( eid )
{
	//----------------------------------
	// INIT
	//----------------------------------
	var url    = 'index.php?act=eventsxmlout&do=vote-page&eid='+eid;
	var fields = new Array();
	//----------------------------------
	// Populate fields
	//----------------------------------
	fields['act']      = 'eventsxmlout';
	fields['do']       = 'vote-page';
	fields['eid']      = eid;
	fields['md5check'] = sdk_md5_check;
	/*--------------------------------------------*/
	// Main function to do on request
	// Must be defined first!!
	/*--------------------------------------------*/
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			xmlobj.show_loading();
			return;
		}
		xmlobj.hide_loading();
		//----------------------------------
		// INIT
		//----------------------------------
		var html = xmlobj.xmlhandler.responseText;
		if ( html != 'error' )
		{
			var result_html = html.split("|");
			if ( result_html[0] != 'error')
			{
				document.getElementById('rightcorner').innerHTML = html;
			}
			else
			{
				document.getElementById( 'rightcorner' ).innerHTML = result_html[1];
			}
		}
		else
		{
			document.getElementById('rightcorner').innerHTML = html;
		}
	}
	//----------------------------------
	// LOAD XML
	//----------------------------------
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	var xmlreturn = xmlobj.process( url, 'POST', xmlobj.format_for_post(fields) );
	return false;
}
/*--------------------------------------------*/
// Ajax: Voting for Contestant
/*--------------------------------------------*/
function ajax_contestant_voting( eid )
{
	var radioObj=document.forms['votingnow'].elements['con_radio'];
	var radioLength = radioObj.length;
	var cid=0;
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			cid = radioObj[i].value;
		}
	}
	if (cid <=0)
	{
		alert('Please choice a Contestant!!');
		return false;
	}
	//----------------------------------
	// INIT
	//----------------------------------
	var url    = 'index.php?act=eventsxmlout&do=voting-contestant&cid='+cid+'&eid='+eid;
	var fields = new Array();
	//----------------------------------
	// Populate fields
	//----------------------------------
	fields['act']      = 'eventsxmlout';
	fields['do']       = 'voting-contestant';
	fields['cid']      = cid;
	fields['eid']      = eid;
	fields['md5check'] = sdk_md5_check;
	/*--------------------------------------------*/
	// Main function to do on request
	// Must be defined first!!
	/*--------------------------------------------*/
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			xmlobj.show_loading();
			return;
		}
		xmlobj.hide_loading();
		//----------------------------------
		// INIT
		//----------------------------------
		var html = xmlobj.xmlhandler.responseText;
		if ( html != 'error' )
		{
			var result_html = html.split("|");
			if ( result_html[0] != 'error')
			{
				document.getElementById('rightcorner').innerHTML = html;
			}
			else
			{
				document.getElementById( 'rightcorner' ).innerHTML = result_html[1];
			}
		}
		else
		{
			document.getElementById('rightcorner').innerHTML = html;
		}
	}
	//----------------------------------
	// LOAD XML
	//----------------------------------
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	var xmlreturn = xmlobj.process( url, 'POST', xmlobj.format_for_post(fields) );
	return false;
}
/*--------------------------------------------*/
// Ajax: Voting Shoutout
/*--------------------------------------------*/
function ajax_contestant_shoutout( eid, cid )
{
	//----------------------------------
	// INIT
	//----------------------------------
	var url    = 'index.php?act=eventsxmlout&do=vote-shoutout&cid='+cid+'&eid='+eid;
	var fields = new Array();
	//----------------------------------
	// Populate fields
	//----------------------------------
	fields['act']      = 'eventsxmlout';
	fields['do']       = 'vote-shoutout';
	fields['md5check'] = sdk_md5_check;
	fields['eid']      = eid;
	fields['cid']      = cid;
	/*--------------------------------------------*/
	// Main function to do on request
	// Must be defined first!!
	/*--------------------------------------------*/
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			xmlobj.show_loading();
			return;
		}
		xmlobj.hide_loading();
		//----------------------------------
		// INIT
		//----------------------------------
		var html = xmlobj.xmlhandler.responseText;
		if ( html != 'error' )
		{
			var result_html = html.split("|");
			if ( result_html[0] != 'error')
			{
				document.getElementById('rightcorner').innerHTML = html;
			}
			else
			{
				document.getElementById( 'rightcorner' ).innerHTML = result_html[1];
			}
		}
		else
		{
			document.getElementById('rightcorner').innerHTML = html;
		}
	}
	//----------------------------------
	// LOAD XML
	//----------------------------------
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	var xmlreturn = xmlobj.process( url, 'POST', xmlobj.format_for_post(fields) );
	return false;
}
/*--------------------------------------------*/
// Ajax: Voting Result
/*--------------------------------------------*/
function ajax_contestant_result( eid, cid )
{
	//----------------------------------
	// INIT
	//----------------------------------
	var url    = 'index.php?act=eventsxmlout&do=vote-result&cid='+cid+'&eid='+eid;
	var fields = new Array();
	//----------------------------------
	// Populate fields
	//----------------------------------
	fields['act']      = 'eventsxmlout';
	fields['do']       = 'vote-result';
	fields['md5check'] = sdk_md5_check;
	fields['eid']      = eid;
	fields['cid']      = cid;
	/*--------------------------------------------*/
	// Main function to do on request
	// Must be defined first!!
	/*--------------------------------------------*/
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			xmlobj.show_loading();
			return;
		}
		xmlobj.hide_loading();
		//----------------------------------
		// INIT
		//----------------------------------
		var html = xmlobj.xmlhandler.responseText;
		if ( html != 'error' )
		{
			var result_html = html.split("|");
			if ( result_html[0] != 'error')
			{
				document.getElementById('rightcorner').innerHTML = html;
			}
			else
			{
				document.getElementById( 'rightcorner' ).innerHTML = result_html[1];
			}
		}
		else
		{
			document.getElementById('rightcorner').innerHTML = html;
		}
	}
	//----------------------------------
	// LOAD XML
	//----------------------------------
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	var xmlreturn = xmlobj.process( url, 'POST', xmlobj.format_for_post(fields) );
	return false;
}
/*--------------------------------------------*/
// Ajax: ajax for action
/*--------------------------------------------*/
function ajax_for_action( action, ct, topic_id, div_refresh, ajax_msg )
{
	//----------------------------------
	// INIT
	//----------------------------------
	var url    = 'index.php?act=eventsxmlout&do='+action+'&ct='+ct+'&tid='+topic_id;
	var fields = new Array();
	//----------------------------------
	// Populate fields
	//----------------------------------
	fields['md5check'] = sdk_md5_check;
	fields['act']      = 'eventsxmlout';
	fields['tid']      = topic_id;
	fields['ct']       = ct;
	fields['do']       = action;
	fields['comment']  = document.getElementById( 'txt_comment' ).value;
	document.getElementById( 'ajax-post-msg' ).innerHTML = '';
	/*--------------------------------------------*/
	// Main function to do on request
	// Must be defined first!!
	/*--------------------------------------------*/
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			xmlobj.show_loading(ajax_msg);
			return;
		}
		xmlobj.hide_loading();
		//----------------------------------
		// INIT
		//----------------------------------
		var html = xmlobj.xmlhandler.responseText;
		if ( html != 'error' )
		{
			var result_html = html.split("|");
			if ( result_html[0] != 'error')
			{
				if ( (div_refresh) && document.getElementById( div_refresh ) )
				{
					document.getElementById( 'txt_comment' ).value = '';
					document.getElementById( div_refresh ).innerHTML = html;
				}
			}
			else
			{
				document.getElementById( 'ajax-post-msg' ).innerHTML = result_html[1];
			}
		}
		else
		{
			document.getElementById( 'ajax-post-msg' ).innerHTML = html;
		}
	}
	//----------------------------------
	// LOAD XML
	//----------------------------------
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	var xmlreturn = xmlobj.process( url, 'POST', xmlobj.format_for_post(fields) );
	return false;
}
/*--------------------------------------------*/
// Ajax: Cancel for edit
/*--------------------------------------------*/
function ajax_cancel_for_edit( comment_id )
{
	if ( post_cache[ comment_id ] != "" )
	{
		document.getElementById( 'comment-'+comment_id ).innerHTML = post_cache[ comment_id ];
	}
	
	return false;
}
/*--------------------------------------------*/
// Ajax: Save for edit
/*--------------------------------------------*/
function ajax_save_for_edit( comment_id, ct )
{
	//----------------------------------
	// INIT
	//----------------------------------
	var url    = 'index.php?act=eventsxmlout&do=comment-edit-save&c='+comment_id+'&ct='+ct;
	var fields = new Array();
	//----------------------------------
	// Populate fields
	//----------------------------------
	
	fields['md5check'] = sdk_md5_check;
	fields['ct']        = ct;
	fields['c']        = comment_id;
	fields['act']      = 'eventsxmlout';
	fields['do']       = 'comment-edit-save';
	fields['comment']     = document.getElementById( 'comment-edit-'+comment_id ).value;
	fields['std_used'] = 1;  // Make sure STD BBCode parser is used
	
	/*--------------------------------------------*/
	// Main function to do on request
	// Must be defined first!!
	/*--------------------------------------------*/
	
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			xmlobj.show_loading();
			return;
		}
		
		xmlobj.hide_loading();
		
		//----------------------------------
		// INIT
		//----------------------------------
		
		var html = xmlobj.xmlhandler.responseText;

		if ( html != 'error' )
		{
			document.getElementById( 'comment-'+comment_id ).innerHTML = html;
		}
		else
		{
			document.getElementById( 'comment-'+comment_id ).innerHTML = html;
		}
	}
	
	//----------------------------------
	// LOAD XML
	//----------------------------------
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	var xmlreturn = xmlobj.process( url, 'POST', xmlobj.format_for_post(fields) );
	
	post_cache[ comment_id ] = '';
	
	return false;
}
/*--------------------------------------------*/
// Ajax: Prep for edit
/*--------------------------------------------*/
function ajax_prep_for_edit( topic_id, comment_id, ct, event )
{
	//----------------------------------
	// Cancel bubble (Prevent IE scroll...)
	//----------------------------------
	global_cancel_bubble( event, true );
	
	var post_main_obj = document.getElementById( 'comment-main-' + comment_id );
	var post_box_top  = _get_obj_toppos( post_main_obj );
	
	//----------------------------------
	// INIT
	//----------------------------------
	var url = 'index.php?act=eventsxmlout&do=comment-edit-show&c='+comment_id+'&t='+topic_id+'&ct='+ct;
	
	if ( ! post_cache[ comment_id ] || post_cache[ comment_id ] == '' )
	{
		post_cache[ comment_id ] = document.getElementById( 'comment-'+comment_id ).innerHTML;
	}

	//----------------------------------
	// Attempt to close open menus
	//----------------------------------
	
	try
	{
		menu_action_close();
	}
	catch(e)
	{
		//alert( e );
	}
	
	/*--------------------------------------------*/
	// Main function to do on request
	// Must be defined first!!
	/*--------------------------------------------*/
	
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			xmlobj.show_loading();
			return;
		}
		
		xmlobj.hide_loading();
		
		//----------------------------------
		// INIT
		//----------------------------------
		
		var html = xmlobj.xmlhandler.responseText;
		
		if ( html == 'nopermission' )
		{
			alert( js_error_no_permission );
		}
		else if ( html != 'error' )
		{
			if ( post_box_top )
			{
				scroll( 0, post_box_top - 30 );
			}
			
			document.getElementById( 'comment-'+comment_id ).innerHTML = html;
		}
		else {
			document.getElementById( 'comment-'+comment_id ).innerHTML = 'ERROR';
		}
		
	}
	
	//----------------------------------
	// LOAD XML
	//----------------------------------
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );

	xmlobj.process( url );
	
	return false;
}
/*--------------------------------------------*/
// Ajax: ajax for postvote
/*--------------------------------------------*/
function ajax_for_postvote( action, ct, topic_id, vote, div_refresh, ajax_msg )
{
	//----------------------------------
	// INIT
	//----------------------------------
	var url    = 'index.php?act=eventsxmlout&do='+action+'&ct='+ct+'&tid='+topic_id;
	var fields = new Array();
	//----------------------------------
	// Populate fields
	//----------------------------------
	fields['md5check'] = sdk_md5_check;
	fields['act']      = 'eventsxmlout';
	fields['tid']      = topic_id;
	fields['ct']       = ct;
	fields['do']       = action;
	fields['comment']  = document.getElementById( 'txt_comment' ).value;
	document.getElementById( 'ajax-post-msg' ).innerHTML = '';
	/*--------------------------------------------*/
	// Main function to do on request
	// Must be defined first!!
	/*--------------------------------------------*/
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			xmlobj.show_loading(ajax_msg);
			return;
		}
		xmlobj.hide_loading();
		//----------------------------------
		// INIT
		//----------------------------------
		var html = xmlobj.xmlhandler.responseText;
		if ( html != 'error' )
		{
			var result_html = html.split("|");
			if ( result_html[0] != 'error')
			{
				if ( (div_refresh) && document.getElementById( div_refresh ) )
				{
					document.getElementById( 'ajax-post-msg' ).innerHTML = result_html[0];
					document.getElementById( 'txt_comment' ).value = '';
					if ( result_html[1] != 'error')
					{
						if ( document.getElementById('vote_'+topic_id) ) document.getElementById('vote_'+topic_id).innerHTML = vote+1;
						if ( window.opener && window.opener.document.getElementById('vote_'+topic_id) ) window.opener.document.getElementById('vote_'+topic_id).innerHTML = document.getElementById('vote_'+topic_id).innerHTML;
						document.getElementById( div_refresh ).innerHTML = result_html[1];
					}
					else document.getElementById( div_refresh ).innerHTML = result_html[2];
				}
			}
			else
			{
				document.getElementById( 'ajax-post-msg' ).innerHTML = result_html[1];
			}
		}
		else
		{
			document.getElementById( 'ajax-post-msg' ).innerHTML = html;
		}
	}
	//----------------------------------
	// LOAD XML
	//----------------------------------
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	var xmlreturn = xmlobj.process( url, 'POST', xmlobj.format_for_post(fields) );
	return false;
}