var report_main;
var mypop_loaded   = 0;
var loaded_non_xml = 0;
var message = '';
var adding_comment;
var updating_prefences;
var mytitle;

var f_my_assis = document.getElementById( 'get-myassistant' );
f_my_assis.style.width = "490px";

function checkvalue()
{
	var comment  = document.getElementById('ed-0_textarea').value;

	if ( ! escape(comment) )
	{
		alert( message );
		return false;	
	}
	else
	{
		return ValidateForm();
	}
}

function xml_validate()
{
	if ( loaded_non_xml )
	{
		return true;	
	}
	else
	{
		return false;	
	}
}

function dimmer(dim)
{
	var shallwe = document.getElementById('mypop-dimmer');
	
	if( dim == true )
	{
		shallwe.style.display='';
	}
	else
	{
		shallwe.style.display='none';
	}
}

// Bug fix (No ID)
// Fix's pixel gap on options

function dimmer2(dim)
{
	var shallwe = document.getElementById('mypop-dimmer2');
	
	if( dim == true )
	{
		shallwe.style.display='';
	}
	else
	{
		shallwe.style.display='none';
	}
}

function do_update()
{
	var checkedids = Array('pm','email','boardalert');
	var additions = '';
	
	if ( loaded_non_xml )
	{
		return;
	}
	
	for( var i in checkedids )
	{
		var available = document.getElementById( checkedids[i] );	
		if( available.checked == true )
		{
			additions = additions+'&'+checkedids[i]+'='+1;
		}
		else
		{
			additions = additions+'&'+checkedids[i]+'='+0;	
		}
	}
	
	var url2      = ipb_var_base_url+'autocom=reportmanager&CODE=myass-alert-on' + additions;
	
	do_request_function2 = function()
	{
		if ( ! xmlobj2.readystate_ready_and_ok() )
		{
			dimmer2(true);
			xmlobj2.show_loading( updating_preferences );
			return;
		}
		
		xmlobj2.hide_loading();
		dimmer2(false)
		
		var output4 = xmlobj2.xmlhandler.responseText;		
						
	}
	
	xmlobj2 = new ajax_request();
	xmlobj2.onreadystatechange( do_request_function2 );
	
	xmlobj2.process( url2 );
}

function add_comment()
{
	var comment  = document.getElementById('comment-rmgr').value;
	var id   	 = document.getElementById('id-rmgr').value;
	var url      = ipb_var_base_url+'autocom=reportmanager&CODE=view-single&code2=do-comment&id='+escape(id)+'&reportid='+escape(id)+"&comment="+escape(comment);
	
	if ( loaded_non_xml )
	{
		return;
	}
	
	if ( ! escape(comment) )
	{
		alert( message );		
	}
	else
	{
		do_request_function = function()
		{
			if ( ! xmlobj.readystate_ready_and_ok() )
			{
				dimmer(true);
				xmlobj.show_loading( adding_comment );
				return;
			}
		
			xmlobj.hide_loading();
			dimmer(false)
		
			var output2 = xmlobj.xmlhandler.responseText;
		
			if( output2 )
			{
				var output3 = output2.split(';');
				report_popup( ipb_var_base_url + 'autocom=reportmanager&CODE=view-single&id=' + escape(id) + '&code2=comments', mytitle );
			}
			else
			{
				alert();
			}			
		}
	
		xmlobj = new ajax_request();
		xmlobj.onreadystatechange( do_request_function );
		
		xmlobj.process( url );
		
	}

}

function xml_popup( url, reporttitle )
{
	var not_loaded_yet = 0;
	
//	menu_action_close();
	
	if ( use_enhanced_js )
	{
		try
		{
			report_popup( url, reporttitle );
			not_loaded_yet = 1;
		}
		catch( e )
		{
			not_loaded_yet = 0;
		}
	}
	
	if ( ! not_loaded_yet )
	{
		loaded_non_xml = 1;
		ipb_var_base_url = ipb_var_base_url.replace( '&amp;', '&' );
		window.open( ipb_var_base_url + url  + '&noxml=1','ReportManager','width=600,height=500,resizable=yes,scrollbars=no');
	}
}

function report_popup( url, reporttitle, is_myassis )
{
	var in_mypop_loaded = 0;
	
	if( ! is_myassis )
	{
		report_main    = document.getElementById( 'get-mypopup' );
		report_drag    = document.getElementById( 'mypop-drag' );
		report_content = document.getElementById( 'mypop-content' );
		report_title   = document.getElementById( 'mypop-title' );
		report_title.innerHTML = reporttitle;
		in_mypop_loaded = mypop_loaded;
	}
	else
	{
		report_main    = document.getElementById( 'get-myassistant' );
		report_drag    = document.getElementById( 'myass-drag' );
		report_content = document.getElementById( 'myass-content' );
		in_mypop_loaded = myass_loaded;
	}

	do_request_function = function()
	{
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			return;
		}
		
		var html = xmlobj.xmlhandler.responseText;
		
		if ( is_ie )
		{
			 html = "<iframe id='mypop-shim' src='javascript;' class='iframshim' scrolling='no' frameborder='0' style='position:absolute; top:0px; left:0px; right:0px; display: none;'></iframe>" + html;
		}
		
		report_content.innerHTML = html;
		
		if ( is_ie )
		{
			report_shim               = document.getElementById('mypop-shim');
			report_shim.style.width   = report_content.offsetWidth;
			report_shim.style.height  = report_content.offsetHeight;
			report_shim.style.zIndex  = report_content.style.zIndex - 1;
			report_shim.style.top     = report_content.style.top;
			report_shim.style.left    = report_content.style.left;
			report_shim.style.display = "block";
		}
	
	}
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	
	xmlobj.process( ipb_var_base_url + url );
  	
  	report_main.style.position = 'absolute';
	report_main.style.display  = 'block';
	report_main.style.zIndex   = 99;
	
	if ( ! in_mypop_loaded )
	{
		var report_width  = 0;
		var report_height = 0;
		
		if ( typeof( window.innerWidth ) == 'number' )
		{
			report_width  = window.innerWidth;
			report_height = window.innerHeight;
		}
		else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
		{
			report_width  = document.documentElement.clientWidth;
			report_height = document.documentElement.clientHeight;
		}
		else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
		{
			report_width  = document.body.clientWidth;
			report_height = document.body.clientHeight;
		}
		
		var divheight = parseInt( report_main.style.height );
		var divwidth  = parseInt( report_main.style.width );
		
		divheight = divheight ? divheight : 500;
		divwidth  = divwidth  ? divwidth  : 600;
		
		if( ! is_myassis )
		{
			var extratop = get_scrolling();
		}
		else
		{
			Drag.cookiename = 'ipb-myass-div';
			var divxy = my_getcookie( 'ipb-myass-div' );
			var extratop = 0;
		}
		
		var co_ords;
		
		if ( divxy && divxy != null )
		{
			co_ords = divxy.split( ',' );
			
			if ( co_ords.length )
			{
				var final_width  = co_ords[0];
				var final_height = co_ords[1];
				
				if ( co_ords[0] > report_width )
				{
					final_width = report_width - divwidth;
				}
				
				if ( co_ords[1] > report_height )
				{
					final_height = report_height - divheight;
				}
				
				report_main.style.left = final_width  + 'px';
				report_main.style.top  = final_height + 'px';
			}
		}
		else
		{
			report_main.style.left = report_width  / 2  - (divwidth / 2)  + 'px';
			report_main.style.top  = ((report_height / 2 - (divheight / 2)) + extratop) + 'px';
		}
		
		if( is_myassis )
		{
			Drag.cookiename = 'ipb-myass-div';
		}
		
		Drag.init( report_drag, report_main );
		
		in_mypop_loaded = 1;
	}
}

function get_scrolling()
{
	var yaxis = 0;
	
	if ( document.documentElement.scrollTop && document.documentElement )
	{
		yaxis = document.documentElement.scrollTop;
	}
	else if ( document.body.scrollTop && document.body )
	{
		yaxis = document.body.scrollTop;
	}
	else if ( window.pageYOffset )
	{
		yaxis = window.pageYOffset;
	}
	else if ( window.yaxis )
	{
		yaxis = window.yaxis;
	}
	
	return yaxis;
}

function xml_myassistant_change( linka )
{
	report_popup( linka, '', 1 );
}


function pms_redirect()
{
	myass_content = document.getElementById( 'myass-content' );
	var limit1 = 0;
	
	pm_cycle_count++;
	
	if ( pm_cycle_count <= ipb_new_msgs )
	{
		limit1 = pm_cycle_count - 1;
	}
	else
	{
		pm_cycle_count = 1;
	}
	
	xml_myassistant_change( 'act=buddy&xml=1&tab=newpms&limit=' + limit1 );

	myass_content.style.overflow = 'auto';	
}