var openReply = '';

function AjaxPostComment(id)
{
	var ajr;

	try
	{
		ajr = new ActiveXObject('Msxml2.XMLHTTP');
	}
	catch (e)
	{
        	try
		{
			ajr = new ActiveXObject('Microsoft.XMLHTTP');
		}
		catch (e2)
		{
			try
			{
				ajr = new XMLHttpRequest();
			}
			catch (e3)
			{
				ajr = false;
			}
		}
	}

	ajr.onreadystatechange = function()
	{
		if (ajr.readyState == 4)
		{
			if(ajr.status == 200)
			{
			//	alert (ajr.responseText);

				var doc = ajr.responseXML;
				var elmDiscussion = doc.getElementsByTagName("discussion").item(0);   // Read the first element
				var elmError = 	doc.getElementsByTagName("error").item(0);

/*
var elmRootid = doc.getElementsByTagName("rootid").item(0);   // Read the first element
DisplayRootReply(elmRootid.firstChild.data);
document.getElementById("rpl-out-root").style.display = "block";
*/
				document.getElementById("cmnt").style.display = "block";

				var error = null;

				if (elmError != null)
					error = elmError.firstChild.data;

				if (error)
				{
					var id = doc.getElementsByTagName("reto").item(0).firstChild.data;
					var senddiv = document.getElementById("senddiv-" + id);

					switch (error)
					{
					case "incorrect-captcha-sol":
						senddiv.innerHTML = "<span style=\"color:red;\">Incorrect words. Please try again.</span>";
						DisplaySendButton(id);
						break;
					case "empty-comment":
						senddiv.innerHTML = "<span style=\"color:red;\">Your comment is empty!</span>";
						DisplaySendButton(id);
						break;
					default:
						alert ("Chyba: " + elmError.innerHTML);
						break;
					}
					Recaptcha.reload();
				}
				else
				{
//					alert("OK.");
					document.getElementById("dscs").innerHTML = elmDiscussion.firstChild.data;
					document.getElementById("cmnt").style.display = "block";

					var rootid = doc.getElementsByTagName("rootid").item(0).firstChild.data;
					HideRootReply(rootid);

					openReply = "";
				}
			}
			else
			{
				alert("Error code " + ajr.status);
			}
		}
	}

	var frm = document.getElementById("frm-c-" + id);

	var data = "rootid=" + frm.rootid.value +
		"&reto=" + frm.reto.value +
		"&lngid=" + frm.lngid.value +
		"&nick=" + frm.nick.value +
		"&subject=" + frm.subject.value +
		"&message=" + frm.message.value +
		"&recaptcha_challenge_field=" + frm.recaptcha_challenge_field.value +
		"&recaptcha_response_field=" +	frm.recaptcha_response_field.value;

	ajr.open("POST", "http://www.earthinpictures.com/inc/forum.php",  true);
	ajr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajr.send(data);

	var senddiv = document.getElementById("senddiv-" + id);

	senddiv.innerHTML = "Sending in progress. Prease wait..." + "<br>\nhttp://www.earthinpictures.com/inc/forum.php?" + data;
	document.getElementById("cmnt").style.display = "none";
}

function ShowRecaptcha(id)
{
	recapdiv = document.getElementById("recap-" + id);

	p = document.createElement("div");
	p.className = "recaptcha_only_if_image";
	p.innerHTML = "<br>Type the two words:";
	p.style.width = "200px";
	recapdiv.appendChild(p);

	p = document.createElement("p");
	p.className = "recaptcha_only_if_audio";
	p.innerHTML = "<br>Type the eight numbers:";
	p.style.width = "200px";
	recapdiv.appendChild(p);

	div = document.createElement("div");
	div.id = "recaptcha_image";
	div.style.width = "300px";
	div.style.height = "57px";

	recapdiv.appendChild(div);

	text = document.createElement("input");
	text.id = "recaptcha_response_field";
	text.name = "recaptcha_response_field";
	text.type = "text";
	text.title = "Enter the two words above.";
	text.className = "rpl-it";
	recapdiv.appendChild(text);


//Audio mode:

	img = document.createElement("img");
	img.src = "http://www.earthinpictures.com/design/recap-reload.png";
	img.style.width = "17px";
	img.style.height = "17px";
	img.alt = "Get another audio.";
	img.title = "Get another audio.";
	img.onclick = function () {Recaptcha.reload();}
	img.className = "recaptcha_only_if_audio";
	img.style.padding = "0 10px 0 2px";
	recapdiv.appendChild(img);

	img = document.createElement("img");
	img.src = "http://www.earthinpictures.com/design/recap-img.png";
	img.style.width = "17px";
	img.style.height = "17px";
	img.alt = "Get an image CAPTCHA..";
	img.title = "Get an image CAPTCHA.";
	img.onclick = function ()
	{
		document.getElementById("recaptcha_response_field").title = "Enter the two words above.";
		Recaptcha.switch_type('image');
	}
	img.className = "recaptcha_only_if_audio";
	recapdiv.appendChild(img);

	img = document.createElement("img");
	img.src = "http://www.earthinpictures.com/design/recap-logo.png";
	img.style.width = "63px";
	img.style.height = "17px";
	img.alt = "reCapcha";
	img.title = "reCapcha";
	img.onclick = function () {window.open("http://recaptcha.net");}
	img.className = "recaptcha_only_if_audio";
	img.style.padding = "0 20px 0 2px";
	recapdiv.appendChild(img);

//Image mode mode:

	img = document.createElement("img");
	img.src = "http://www.earthinpictures.com/design/recap-reload.png";
	img.style.width = "17px";
	img.style.height = "17px";
	img.alt = "Get another image.";
	img.title = "Get another image.";
	img.onclick = function () {Recaptcha.reload();}
	img.className = "recaptcha_only_if_image";
	img.style.padding = "0 10px 0 2px";
	recapdiv.appendChild(img);

	img = document.createElement("img");
	img.src = "http://www.earthinpictures.com/design/recap-audio.png";
	img.style.width = "17px";
	img.style.height = "17px";
	img.alt = "Get an audio CAPTCHA";
	img.title = "Get an audio CAPTCHA";
	img.onclick = function ()
	{
		document.getElementById("recaptcha_response_field").title = "Enter the english numbers you hear.";
		Recaptcha.switch_type('audio');
	}
	img.className = "recaptcha_only_if_image";
	recapdiv.appendChild(img);

	img = document.createElement("img");
	img.src = "http://www.earthinpictures.com/design/recap-logo.png";
	img.style.width = "63px";
	img.style.height = "17px";
	img.alt = "reCapcha";
	img.title = "reCapcha";
	img.onclick = function () {window.open("http://recaptcha.net");}
	img.className = "recaptcha_only_if_image";
	img.style.padding = "0 20px 0 2px";
	recapdiv.appendChild(img);

	Recaptcha.create("6LevXQEAAAAAAM3Tbh0ScaR6DDTu7da3SX1bEl3j", "recap-" + id,
			{
			theme: "custom",
			tabindex: 4,
			callback: function()
					{
						form = document.getElementById("frm-c-" + id);
						form.nick.focus();
//        		Recaptcha.focus_response_field();
					}
  			}
	);
}

function CloseReply(id)
{
	var answer = confirm("Do you want to cancel your reply?");
	if (answer)
	{
//		Recaptcha.destroy();
		var rootid = document.getElementById("frm-c-" + id).rootid.value;
		if (id == rootid)
			HideRootReply(rootid);
		else
		{
			document.getElementById("rpl-out-" + id).style.display = "none";
			outer = document.getElementById("rpl-c-" + id);
			outer.innerHTML = "";

			button = document.getElementById("btn-" + id);
			button.innerHTML = "Reply";
			button.onclick = function() {ReplyTo(id);};
		}
		openReply = "";

		return true;
	}
	else
		return false;
}

function ReplyTo(id)
{
	if (openReply != "")
		if (!CloseReply(openReply))
			return;

	button = document.getElementById("btn-" + id);
	button.innerHTML = "Close";
	button.onclick = function() {CloseReply(id);};

	document.getElementById("rpl-out-" + id).style.display = "block";

	DisplayReplyForm(id);

	ShowRecaptcha(id);

	form = document.getElementById("frm-c-" + id);
	form.nick.focus();

	openReply = id;
}

function DisplaySendButton(id)
{
	input = document.createElement("input");
	input.className = "rpl-is lnk";
	input.type = "button";
	input.value = "Send";
	input.tabindex = 5;
	input.onclick = function () {AjaxPostComment(id);};

	var senddiv = document.getElementById("senddiv-" + id);
	senddiv.appendChild(input);
}

function DisplayReplyForm(id)
{
	rpldiv = document.getElementById("rpl-c-" + id);

	h2 = document.createElement("h2");
	h2.innerHTML = "Write Comment";

	rpldiv.appendChild(h2);

	//
	lndiv = document.createElement("div");
	lndiv.className = "rpl-ln";

	rpldiv.appendChild(lndiv);

	lddiv = document.createElement("div");
	lddiv.className = "rpl-ld";
	lddiv.innerHTML = "Nick name:";

	lndiv.appendChild(lddiv);

	rddiv = document.createElement("div");
	rddiv.className = "rpl-rd";


	lndiv.appendChild(rddiv);

	input = document.createElement("input");
	input.className = "rpl-it";
	input.type = "text";
	input.tabindex = 1;

	input.id = "nick";


	rddiv.appendChild(input);

	//
	lndiv = document.createElement("div");
	lndiv.className = "rpl-ln";



	rpldiv.appendChild(lndiv);

	lddiv = document.createElement("div");
	lddiv.className = "rpl-ld";
	lddiv.innerHTML = "Subject:";

	lndiv.appendChild(lddiv);

	rddiv = document.createElement("div");
	rddiv.className = "rpl-rd";

	lndiv.appendChild(rddiv);

	input = document.createElement("input");
	input.className = "rpl-it";
	input.type = "text";
	input.id = "subject";
	input.size = 60;
	input.tabindex = 2;
	var subjDiv = document.getElementById("msg-hdr-" + id);
	if (subjDiv != null)
	{
		var subj = subjDiv.innerHTML;
		if (subj.substr(0,4) != "Re: ")
			subj = "Re: " + subj;
		input.value = subj;
	}
	rddiv.appendChild(input);

	//
	lndiv = document.createElement("div");
	lndiv.className = "rpl-ln";

	rpldiv.appendChild(lndiv);

	lddiv = document.createElement("div");
	lddiv.className = "rpl-ld";
	lddiv.innerHTML = "Comment:";

	lndiv.appendChild(lddiv);

	rddiv = document.createElement("div");
	rddiv.className = "rpl-rd";

	lndiv.appendChild(rddiv);

	textarea = document.createElement("textarea");
	textarea.className= "rpl-ta";
	textarea.id = "message";
	textarea.cols = 50;
	textarea.rows = 6;
	textarea.tabindex = 3;

	rddiv.appendChild(textarea);

	//
	lndiv = document.createElement("div");
	lndiv.className = "rpl-ln";

	rpldiv.appendChild(lndiv);


	lddiv = document.createElement("div");
	lddiv.className = "rpl-ld";
	lddiv.innerHTML = "<h2>Send:</h2>";

	lndiv.appendChild(lddiv);

	rddiv = document.createElement("div");
	rddiv.className = "rpl-rd";
	rddiv.id = "recap-" + id;

	lndiv.appendChild(rddiv);

	//

	lndiv = document.createElement("div");
	lndiv.className = "rpl-ln";

	rpldiv.appendChild(lndiv);

	lddiv = document.createElement("div");
	lddiv.className = "rpl-ld";
	lddiv.innerHTML = "&nbsp;";

	lndiv.appendChild(lddiv);

	rddiv = document.createElement("div");
	rddiv.className = "rpl-rd";
	rddiv.id = "senddiv-" + id;

	lndiv.appendChild(rddiv);

	DisplaySendButton(id);
}

function DisplayRootReply(rootid)
{
	if (openReply != "")
		CloseReply(openReply);

	document.getElementById("rpl-out-root").style.display = "block";
	DisplayReplyForm(rootid);
	ShowRecaptcha(rootid);
	openReply = rootid;
}

function HideRootReply(rootid)
{
	document.getElementById("rpl-c-" + rootid).innerHTML = "";
	document.getElementById("rpl-out-root").style.display = "none";
	openReply = "";
}