
function reportBroken()
{
	document.getElementById("broken").innerHTML = '<img src="/images/processing.gif" alt="Processing" />';

	var message = prompt("Please add a short note describing what is wrong", "");

	if (message)
	{
		ajaxRequest("report", "/report.php", "?url=" + doc_url + "&message=" + encodeURIComponent(message));
	}
	else
	{
		document.getElementById("broken").innerHTML = '<a href="#" onclick="reportBroken();">Broken?</a>';
	}
}

function validateCommentForm(theForm)
{
	var reason = "";

	reason += validateNotEmpty(theForm.field_name);
	reason += validateNotEmpty(theForm.field_comment);
      
	if (reason != "")
	{
		alert("Error(s):\n\n" + reason);
		return false;
	}

	return true;
}

function validateNotEmpty(fld)
{
	var error = "";
  
	if (fld.value.length == 0)
	{
		error = "The field " + fld.name + " must be filled in.\n";
		fld.style.border = "2px dotted #B77049";
		fld.select();
	}
	else
	{
		fld.style.border = "2px #FFFFFF inset";
	}

	return error;
}

function postComment(obj)
{
	document.getElementById("processingcomment").innerHTML = '<h2>Processing comment...</h2>';

	var poststr = "?url=" + doc_url + "&name=" + encodeURIComponent(document.getElementById("field_name").value) + "&comment=" + encodeURIComponent(document.getElementById("field_comment").value) + "&test1=" + document.getElementById("field_test1").checked + "&test2=" + document.getElementById("field_test2").checked + "&test3=" + document.getElementById("field_test3").checked + "&test4=" + document.getElementById("field_test4").checked;
	ajaxRequest("comment", "/post.php", poststr);
}

function ajaxRequest(feature, url, content)
{
	var AJAX;

	try
	{
		AJAX = new XMLHttpRequest();
	}
	catch(e)
	{
		try
		{
			AJAX = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				AJAX = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				alert("Your browser does not support this feature.");

				if (feature == "report")
				{
					document.getElementById("broken").innerHTML = '<a href="#" onclick="reportBroken();">Broken?</a>';
				}
				else if (feature == "comment")
				{
					document.getElementById("processingcomment").innerHTML = "";
				}

				return false;
			}
		}
	}

	AJAX.onreadystatechange = function()
	{
		if (AJAX.readyState == 4)
		{
			if (AJAX.status == 200)
			{
				eval(AJAX.responseText);
			}
			else
			{
				alert("Error: "+ AJAX.statusText +" "+ AJAX.status);

				if (feature == "report")
				{
					document.getElementById("broken").innerHTML = '<a href="#" onclick="reportBroken();">Broken?</a>';
				}
				else if (feature == "comment")
				{
					document.getElementById("processingcomment").innerHTML = "";
				}
			}
		}   
	}

	var randomnumber = Math.floor(Math.random()*9999)
	AJAX.open("GET", url + content + "&rand=" + randomnumber, true);
	AJAX.send(null);
}

function limitText(limitField, limitCount, limitNum)
{
	if (limitField.value.length > limitNum)
	{
		limitField.value = limitField.value.substring(0, limitNum);
	}
	else
	{
		document.getElementById("limitcounter").innerHTML = limitNum - limitField.value.length;
	}
}
