function show_article_report(article_id)
{
	article_id = Number(article_id);
	
	if(!article_id) return;
	
	entity_active_id = article_id;
	
	popup_create();
	popup_title("Why are you reporting this article?");
	popup_area("report-reason", "Reason", "");
	popup_button("report-submit", "Submit");
	
	document.getElementById('report-submit').onclick = function()
	{
		send_article_report();
	}
}


function show_subscription_load(article_id)
{	
	loading_subscription = true;
	
	popup_create();
	popup_title("Manage subscription");


	var popup = document.getElementById('popup');
	
	if(!popup) return;
	
	popup.className = 'loading';

	
	show_processing("Loading data...");
	
	request_do('/scripts/article_subscription_load.php', 'POST', '&article_id=' + escape(entity_active_id));
}


function show_subscription(response_data)
{
	loading_subscription = false;
	
	var popup = document.getElementById('popup');
	
	if(!popup) return;
	
	popup.className = '';
	
	popup.removeChild(popup.childNodes[2]);

	popup_checkbox("subscription_category", "1", response_data.getElementsByTagName('sub_cat')[0].firstChild.nodeValue, "Subscribe to this category");
	popup_checkbox("subscription_category_active", "1", response_data.getElementsByTagName('sub_cat_active')[0].firstChild.nodeValue, "Activate category subscription", "inside");
	popup_checkbox("subscription_user", "1", response_data.getElementsByTagName('sub_user')[0].firstChild.nodeValue, "Subscribe to this journalist");
	popup_checkbox("subscription_user_active", "1", response_data.getElementsByTagName('sub_user_active')[0].firstChild.nodeValue, "Activate journalist subscription", "inside");
	popup_button("subscription-submit", "Save Changes");
	
	document.getElementById('subscription-submit').onclick = function()
	{
		send_subscription();
	}
}


function send_subscription()
{
	var sub_cat			= document.getElementById('subscription_category').checked ? 1 : 0;
	var sub_cat_active	= document.getElementById('subscription_category_active').checked ? 1 : 0;
	var sub_user		= document.getElementById('subscription_user').checked ? 1 : 0;
	var sub_user_active	= document.getElementById('subscription_user_active').checked ? 1 : 0;
	
	show_processing();

	request_do('/scripts/article_subscription_manage.php', 'POST', '&article_id=' + escape(entity_active_id) + '&sub_cat=' + escape(sub_cat) + '&sub_cat_active=' + escape(sub_cat_active) + '&sub_user=' + escape(sub_user) + '&sub_user_active=' + escape(sub_user_active));
}


function send_article_report()
{
	var report_reason = document.getElementById('report-reason');
	
	if(!report_reason || report_reason.value.length < 5)
	{
		alert("Please enter a valid reason.");
		
		return;
	}
	
	show_processing();
	
	request_do('/scripts/article_report.php', 'POST', '&article_id=' + escape(entity_active_id) + '&report_message=' + escape(report_reason.value));
}


function article_search()
{
	var seach_term;
	
	try
	{
		search_term = document.getElementById('search_term').value;
	}
	catch(error)
	{
		search_term = '';
	}
	
	if(search_term.length < 1)
	{
		return;
	}

	popup_create();
	popup_title("Article search");


	var popup = document.getElementById('popup');
	
	if(!popup) return;
	
	popup.className = 'loading';

	
	show_processing("Searching...");

	
	searching = true;
	
	request_do('/scripts/article_search.php', 'POST', '&search_term=' + escape(search_term));
}


function article_search_process(response_data)
{
	searching = false;
	
	try
	{
		var search_id = escape(response_data.getElementsByTagName('id')[0].firstChild.nodeValue);
		
		window.location = "/Search/" + search_id;
	}
	catch(error)
	{
		return;
	}
}


function getElementsByClassName(node, classname)
{
    var found = [];
    var expression = new RegExp('\\b' + classname + '\\b');
    
    var all_elements = node.getElementsByTagName("*");
    
    for(var i=0, j=all_elements.length; i<j; i++)
    {
		if(expression.test(all_elements[i].className))
		{
			found.push(all_elements[i]);
		}
	}
    
    return found;
}


function sort_numbers(a, b)
{
	return a - b;
}


var screen_height = 0;

var ielement = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body

var response_data;

var comment_active;
var comment_active_id;

var comment_page = 1;

var entity_active_id;

var load_comments = false;
var loading_comment = false;
var edit_comment = false;
var add_comment = false;
var loading_subscription = false;
var searching = false;

var num_comments = 0;
var num_limit = 20;
var num_pages = Math.ceil(num_comments/num_limit);

var nav_top = true;

var comment_add = getElementsByClassName(document, 'comment-add');

if(comment_add.length)
{
	var page_location = window.location + '/';
	
	for(var i=0; i<comment_add.length; i++)
	{
		if(page_location.indexOf('/' + comment_add[i].className.substring(20) + '/') >= 0)
		{
			comment_add[i].onclick = function()
			{
				show_comment_add(this.className.substring(20));
			}
		}
	}
}


var article_report = getElementsByClassName(document, 'report-article');

if(article_report.length)
{
	for(var i=0; i<article_report.length; i++)
	{
		article_report[i].onclick = function()
		{
			show_article_report(this.className.substring(23));
		}
	}
}


var article_subscription = getElementsByClassName(document, 'subscription');

if(article_subscription.length)
{
	for(var i=0; i<article_subscription.length; i++)
	{
		article_subscription[i].onclick = function()
		{
			entity_active_id = this.className.substring(21);
			
			show_subscription_load(entity_active_id);
		}
	}
}


var comments = document.getElementById('comments');

if(comments)
{
	entity_active_id = comments.className.substring(8);

	if(comments.childNodes.length > -1)
	{
		display_comments(comment_page);
	}
	else
	{
		display_no_comments();
	}
}