/**
 * AJAX.JS
 * v10.08
 * (kumasanmk) is not amused
 */


//Create XMLHTTPRequest (XHR) object
function getXhr()
{
	var result = null;

	if (window.XMLHttpRequest)
	{
		result = new XMLHttpRequest();
	}
	else
	{
		if (window.ActiveXObject)
		{
			result = new ActiveXObject("Microsoft.XMLHTTP"); //Internet Explorer :(
		}
		else
		{
			alert("The XMLHTTPRequest object is not supported by your navigator! Try Firefox ;)");
			result = false;
		}
	}

	return result;
}

//Global XHR object
var xhr = null;

//Loading pics (HTML)
var inline_pic = "<img src=\"" + jroot + "/pics/ajax-loader.gif\" alt=\"Loading...\" />";
var block_pic = "<p class=\"center padding\">" + inline_pic + "</p>";


function load_more_on_footer(type)
{
	var more = document.getElementById("id_load_more_on_footer");

	if (type.length > 0)
	{
		if (xhr && xhr.readyState !== 0) return;
		xhr = getXhr();

		if (xhr)
		{
			xhr.onreadystatechange = function () {
				if (xhr.readyState == 4 && xhr.status == 200)
				{
					more.style.display = "block";
					more.innerHTML = xhr.responseText;
					xhr = null;
				}
			};
		}

		more.style.display = "block";
		more.innerHTML = inline_pic;

		xhr.open("POST", jroot + "/ajax.php", true);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xhr.send("commit=load_more_on_footer&tipe=" + type);
	}
	else
	{
		more.style.display = "none";
	}
}

function loadTextBox(position, rid, rtp, nb, qid)
{
	if (xhr && xhr.readyState !== 0) return;
	xhr = getXhr();

	if (xhr)
	{
		xhr.onreadystatechange = function () {
			if (xhr.readyState == 4 && xhr.status == 200)
			{
				document.getElementById("id_textbox-" + position).innerHTML = xhr.responseText;
				document.getElementById("id_textarea-com").focus();
				xhr = null;
			}
		};
	}

	document.getElementById("id_textbox-" + position).innerHTML = block_pic;

	xhr.open("POST", jroot + "/ajax.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send("commit=load_textbox&rid=" + rid + "&rtp=" + rtp + "&nb=" + nb + "&qid=" + qid);
}

function postComment(rid, rtp, num)
{
	if (xhr && xhr.readyState !== 0) return;
	xhr = getXhr();

	var content = encodeURIComponent(document.getElementById("id_textarea-com").value);

	if (xhr && content.length > 0)
	{
		xhr.onreadystatechange = function () {
			if (xhr.readyState == 4 && xhr.status == 200)
			{
				document.getElementById("id_ajax-target").innerHTML = xhr.responseText;
				xhr = null;
			}
		};

		document.getElementById("id-ajax-com-posting").innerHTML = inline_pic;
		up_and_fade(20);

		xhr.open("POST", jroot + "/ajax.php", true);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xhr.send("commit=post_comment&rid=" + rid + "&rtp=" + rtp + "&content=" + content + "&num=" + num);
	}
}

function previewComment(id)
{
	var preview = document.getElementById("id-ajax-preview-" + id);
	var content = document.getElementById(id);
	var data = content.value;

	if (preview && content && data.length > 0)
	{
		if (xhr && xhr.readyState !== 0) return;
		xhr = getXhr();

		if (xhr)
		{
			xhr.onreadystatechange = function () {
				if (xhr.readyState == 4 && xhr.status == 200)
				{
					preview.style.display = "block";
					preview.innerHTML = "";
					preview.innerHTML = xhr.responseText;
					xhr = null;
				}
			};

			xhr.open("POST", jroot + "/ajax.php", true);
			xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xhr.send("commit=preview_comment&content=" + encodeURIComponent(data));
		}
	}
}

function deleteComment(cid)
{
	if (xhr && xhr.readyState !== 0) return;
	xhr = getXhr();

	if (xhr)
	{
		xhr.onreadystatechange = function () {
			if (xhr.readyState == 4 && xhr.status == 200)
			{
				document.getElementById("id_ajax_target_delete_comment_" + cid).innerHTML = xhr.responseText;
				xhr = null;
			}
		};

		xhr.open("POST", jroot + "/ajax.php", true);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xhr.send("commit=delete_comment&cid=" + cid);
	}
}

function aSearchFigure(id, from, in_collection)
{
	var f = document.getElementById("id_neosearch");
	var k = f.value;
	var t = document.getElementById("id_ajax-target");
	if (in_collection == "0") in_collection = (document.getElementById("id_limit_to_collection").checked ? 1 : 0);

	if (k.length > 0)
	{
		if (xhr && xhr.readyState !== 0) return;
		xhr = getXhr();

		if (xhr)
		{
			xhr.onreadystatechange = function () {
				if (xhr.readyState == 4 && xhr.status == 200)
				{
					t.style.display = "block";
					t.innerHTML = xhr.responseText;
					xhr = null;
				}
			};

			t.innerHTML = block_pic;

			xhr.open("POST", jroot + "/ajax.php", true);
			xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xhr.send("commit=search_figure&iid=" + id + "&from=" + from + "&collection=" + in_collection + "&keywords=" + encodeURIComponent(k));
		}
	}
}

function maxmin_announce()
{
	if (xhr && xhr.readyState !== 0) return;
	xhr = getXhr();

	if (xhr)
	{
		xhr.onreadystatechange = function () {
			if (xhr.readyState == 4 && xhr.status == 200)
			{
				xhr = null;
			}
		};

		togglevisibility("id_announcement_max");
		togglevisibility("id_announcement_min");

		xhr.open("POST", jroot + "/ajax.php", true);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xhr.send("commit=maxmin_announce");
	}
}

function favorite_sdb(id, type)
{
	if (xhr && xhr.readyState !== 0) return;
	xhr = getXhr();

	if (xhr)
	{
		xhr.onreadystatechange = function () {
			if (xhr.readyState == 4 && xhr.status == 200)
			{
				document.getElementById("id_ajax_target_favorite").innerHTML = xhr.responseText;
				xhr = null;
			}
		};
	}

	xhr.open("POST", jroot + "/ajax.php?id=" + id + "&type=" + type, true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send("commit=favorite_sdb");

	return false;
}

function subscribe_sdb(id, type)
{
	if (xhr && xhr.readyState !== 0) return;
	xhr = getXhr();

	if (xhr)
	{
		xhr.onreadystatechange = function () {
			if (xhr.readyState == 4 && xhr.status == 200)
			{
				document.getElementById("id_ajax_target_subscribe").innerHTML = xhr.responseText;
				xhr = null;
			}
		};
	}

	xhr.open("POST", jroot + "/ajax.php?id=" + id + "&type=" + type, true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send("commit=subscribe_sdb");

	return false;
}

