function get_ajax() {
	try { return new XMLHttpRequest(); } catch (e) {}
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
	return null;
}
var ajax_request = get_ajax();

function form_to_string(f) {
	var a = '';
	for(var i = 0; i < f.elements.length; i++)
	{
		e = f.elements[i];
		if (typeof(e.name) != 'undefined' && !e.disabled && e.type != "submit")
		{
			v = e.tagName == 'SELECT' ? e.options[e.selectedIndex].value : (((e.type == "checkbox" || e.type == "radio") && !e.checked) ? '' : e.value);
			a += (e.type == 'radio' && !e.checked) ? '' : ((a == '' ? '' : '&') + encodeURIComponent(e.name) + '=' + encodeURIComponent(v));
		}
	}
	return a;
}

function comment_expand(a, more, less)
{
	var b = $(a).parents('.comment_body').eq(0).find('.more_comment_text').eq(0);
	b.toggle();
	a.innerHTML = (b.get(0).style.display == 'none') ? more : less;
	return true;
}

function comment_page(a) {
	ajax_request.onreadystatechange = function() { if (ajax_request.readyState == 4) { comment_page_area(a); } };
	ajax_request.open("GET", a.href + '&ajax=true', true);
	ajax_request.send(null);
	return true;
}

function comment_page_area(a) {
	var r = $.trim(ajax_request.responseText);
	if (r)
	{
		$(a).parents('.prime_comments_section').eq(0).html(r);
	}
}

function comment_post(f, t) {
	var a = $(f).prev('.prime_comments_section').eq(0);
	var b = a.find('.display_number').eq(0).html();
	var c = 'ajax=true&final_num=' + (b ? b.replace(/[^0-9]/, '') : '0') + '&' + form_to_string(f);

	a.nextAll('.comment_result').eq(0).html('<div><p>' + t + '</p></div>').slideDown('fast');
	ajax_request.onreadystatechange = function() { if (ajax_request.readyState == 4) { comment_result(f); } };
	ajax_request.open("POST", f.action, true);
	ajax_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	ajax_request.send(c);
	return true;
}

function comment_result(f) {
	var r = $.trim(ajax_request.responseText);
//	alert(r);
	if (r)
	{
		var s = r.split('|', 3);
		var a = s[0] || '', b = s[1] || '', c = s[2] || '';
		$(f).nextAll('.comment_result').html(b).slideDown('normal');
		if (a == 'post')
		{
			var d = $(f).prevAll('.prime_comments_section').eq(0);
			d.find('.comment_list').eq(0).prepend(c);
			d.find('.comment_area').eq(0).slideDown('slow');
			d.find('.comment_area_empty').eq(0).slideUp('slow');
			$(f).slideUp('slow');
		}
	}
}

function toggle_report_text(a) {
	var b = (a.options[a.selectedIndex].value == 'custom') ? true : false;
	var c = $(a).parents('form').find(':text').toggle(b);
}

function hide_report_form(a) {
	$(a).parents('form').slideUp('fast');
	return true;
}

function show_report_form(a) {
	var b = $(a).parents('.comment_area').find('.report_comment_form').eq(0);
	if (b.get(0))
	{
		b.slideToggle('fast'); // If the form is already there then toggle it
	}
	else
	{
		b = $(a).parents('.prime_comments_section').find('.report_comment_form').eq(0);
		$(a).parents('.comment_info').after(b);
		b.slideDown('fast');
	}

	//take post_id from link and insert it into the form
	var cid = a.href.replace(/^.*id=([0-9]+)$/, '$1');
	b.find(':hidden[name="post_id"]').eq(0).attr('value', cid);
	return true;
}

function report_comment(f) {
	var a = 'ajax=true&' + form_to_string(f), b = $(f).find('.comment_result').eq(0);
	$(f).nextAll('.comment_result').slideUp('normal'); // hide result from a previous submission
	b = b.clone().insertAfter(f).eq(0);
	b.slideDown('fast');
	ajax_request.onreadystatechange = function() { if (ajax_request.readyState == 4) { comment_reported(f, b); } };
	ajax_request.open("POST", f.action, true);
	ajax_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	ajax_request.send(a);

	return true;
}

function comment_reported(f, c) {
	var r = ajax_request.responseText;
	if (r)
	{
		var s = r.split('|', 2);
		var a = s[0] || '', b = s[1] || '';
		c.addClass(a).html(b).slideDown('normal');
		$(f).slideUp('slow');
	}
}

function toggle_comment_review(obj) {
	var c = $(obj).find('input:checkbox').eq(0).attr('checked');
	var s = $(obj).siblings('.article_correction_confirm').eq(0);
	if (c)
	{
		s.slideDown();
	}
	else
	{
		s.slideUp();
	}
}
