var postid = 0;

function setupAjax() {
var ajaxRequest;

//Browser Support Code
try{
	// Opera 8.0+, Firefox, Safari
	ajaxRequest = new XMLHttpRequest();
} catch (e){
	// Internet Explorer Browsers
	try{
		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e){
			// Something went wrong
			alert("Unable to initialize Ajax functions. Please check that you have an up-to-date browser and your security settings are not too strict.");
			return false;
		}
	}
}
return ajaxRequest;
}

function showComments(getpostid,getstart) {
	document.getElementById("gotcha-captcha").src="http://www.digitalrebellion.com/scripts/captcha_image.php";
	document.getElementById("commentstitle").href="";

	loadComments(getpostid,getstart);
}

function loadComments(getpostid,getstart) {
	postid = getpostid;

	var ajaxRequest = setupAjax();

ajaxRequest.onreadystatechange = function(){
	if(ajaxRequest.readyState == 4){
				document.getElementById("commentsview").innerHTML = ajaxRequest.responseText;
	}
}

	document.getElementById("commentwrapper").style.display = "block";
	document.getElementById("commentsview").innerHTML = "Loading comments...";
	
	ajaxRequest.open("GET", "http://www.digitalrebellion.com/blog.php?getcomments&id=" + postid + "&start=" + getstart, true);
	ajaxRequest.send(null);
}
	
function submitComment() {
	if (document.getElementById("name").value.length == 0) {
		document.getElementById("commentstatus").innerHTML = "<font color=\"red\"><b>You must enter a name.</b></font>";
		return false;
	}
	
	if (document.getElementById("name").value.length < 3 || document.getElementById("name").value.length > 255) {
		document.getElementById("commentstatus").innerHTML = "<font color=\"red\"><b>You must enter a name between 3 and 255 characters in length.</b></font>";
		return false;
	}
	
	if (document.getElementById("email").value.length == 0) {
		document.getElementById("commentstatus").innerHTML = "<font color=\"red\"><b>You must enter an email address.</b></font>";
		return false;
	}
	
	if (document.getElementById("email").value.length < 3 || document.getElementById("email").value.length > 255) {
		document.getElementById("commentstatus").innerHTML = "<font color=\"red\"><b>You must enter an email address between 3 and 255 characters in length.</b></font>";
		return false;
	}
	
	if (document.getElementById("email").value.indexOf("@") == -1 || document.getElementById("email").value.indexOf(".") == -1) {
		document.getElementById("commentstatus").innerHTML = "<font color=\"red\"><b>You must enter an email address in the format name@example.com.</b></font>";
		return false;
	}
	
	if (document.getElementById("message").value.length < 10) {
		document.getElementById("commentstatus").innerHTML = "<font color=\"red\"><b>Your message must be at least 10 characters long.</b></font>";
		return false;
	}
	
	if (document.getElementById("code").value.length == 0) {
		document.getElementById("commentstatus").innerHTML = "<font color=\"red\"><b>You must enter a code into the box.</b></font>";
		return false;
	}
	
	var ajaxRequest = setupAjax();

ajaxRequest.onreadystatechange = function(){
	if(ajaxRequest.readyState == 4){
				// Reset captcha
				var now = new Date();
				var ticks = now.getTime();
				document.getElementById("gotcha-captcha").src = "http://www.digitalrebellion.com/scripts/captcha_image.php?" + ticks;
				
				switch(ajaxRequest.responseText) {
					case "0": // Success
						document.getElementById("commentform").className = "hide";
						document.getElementById("commentstatus").innerHTML = "<font color=\"green\"><b>Your comment is awaiting approval.</b></font>";
						loadComments(postid,0);
						break;
					case "1": // No name
						document.getElementById("commentstatus").innerHTML = "<font color=\"red\"><b>You must enter a name.</b></font>";
						break;
					case "2": // Name too short
						document.getElementById("commentstatus").innerHTML = "<font color=\"red\"><b>You must enter a name between 3 and 255 characters in length.</b></font>";
						break;
					case "3": // No email
						document.getElementById("commentstatus").innerHTML = "<font color=\"red\"><b>You must enter an email address.</b></font>";
						break;
					case "4": // Email too short
						document.getElementById("commentstatus").innerHTML = "<font color=\"red\"><b>You must enter an email address between 3 and 255 characters in length.</b></font>";
						break;
					case "5": // Email wrong format
						document.getElementById("commentstatus").innerHTML = "<font color=\"red\"><b>You must enter an email address in the format name@example.com.</b></font>";
						break;
					case "6": // Message too short
						document.getElementById("commentstatus").innerHTML = "<font color=\"red\"><b>Your message must be at least 10 characters in length.</b></font>";
						break;
					case "7": // No code
						document.getElementById("commentstatus").innerHTML = "<font color=\"red\"><b>You must enter a code into the box.</b></font>";
						break;
					case "8": // Invalid code
						document.getElementById("commentstatus").innerHTML = "<font color=\"red\"><b>Invalid code.</b></font>";
						break;
					case "9": // Spam
						document.getElementById("commentstatus").innerHTML = "<font color=\"red\"><b>This message was flagged up by our spam detector.</b></font>";
						break;
					default: // Unknown error
						document.getElementById("commentstatus").innerHTML = "<font color=\"red\"><b>Unknown error. Please report this to Digital Rebellion staff.</b></font>";
						break;
				}
	}
}
	
	ajaxRequest.open("POST", "http://www.digitalrebellion.com/blog.php?submit=" + postid, true);
	var params = "name=" + escape(document.getElementById("name").value) + "&email=" + escape(document.getElementById("email").value) + "&message=" + escape(document.getElementById("message").value) + "&code=" + escape(document.getElementById("code").value);
	
	//Send the proper header information along with the request
	ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajaxRequest.setRequestHeader("Content-length", params.length);
	ajaxRequest.setRequestHeader("Connection", "close");
	ajaxRequest.send(params);
}
