function getXMLHttpRequest(){
	try{
		req = new XMLHttpRequest();
	}
	catch(err1){
		try{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(err2){
			try{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(err3){
				req = false;
			}
		}
	}
	return req;
}

var http = getXMLHttpRequest();

function doReply(){
	if(http){
		//Variables
		var a = document.frmReply.a.value;
		var v = document.frmReply.v.value;
		var reply = document.frmReply.reply.value;
		var code = document.frmReply.code.value;
		var email = document.frmReply.email.value;
		//Validation
		//Email
		var tst2;
		tst2 = new RegExp("^[a-zA-Z0-9_\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$");
		if(!tst2.test(email) && email != "")
		{
			alert("Your Email Address seems to be invlaid. Please check it and try again.");
			document.frmReply.email.focus();
			return(false);
		}
		else if(document.frmReply.email.value=="")
		{
			alert("A valid E-mail Address is required.");
			document.frmReply.email.focus();
			return(false);
		}
		//Reply
		if(document.frmReply.reply.value.length < 1 || document.frmReply.reply.value.length > 1000)
		{
			alert("Replies can neither be blank nor can they exceed 1000 characters.\nYour Reply is "+document.frmReply.reply.value.length+" character(s).\nPlease edit your Reply and try again.");
			document.frmReply.reply.focus();
			return(false);
		}
		//Captcha
		if(document.frmReply.code.value=="")
		{
			alert("You must enter the code shown to proceed. If you cannot see it, make sure your browser is loading images.");
			document.frmReply.code.focus();
			return(false);
		}
		document.frmReply.rply.value = "Sending... please wait";
		document.frmReply.rply.disabled = true;

		var myurl = 'scripts/reply.php';
		var parms = 'a=' + encodeURI(a) + '&v=' + encodeURI(v) + '&reply=' + encodeURI(reply) + '&code=' + encodeURI(code) + '&email=' + encodeURI(email);
		myRand = parseInt(Math.random()*999999999999999); //avoid caching
		http.open('POST', myurl + '?rand=' + myRand, true);
		http.onreadystatechange = useHttpResponse;
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		//http.setRequestHeader("Content-length", parms.length);
		//http.setRequestHeader("Connection", "close");
		http.send(parms);
	}
}

function useHttpResponse(){
	if(http.readyState == 4){
		if(http.status == 200){
			alert(http.responseText);
			if(http.responseText.indexOf('Successfully') > 0){
				document.frmReply.reply.value = '';
				document.frmReply.code.value = '';
				document.frmReply.email.value = '';
				window.location.reload(true);
			}
			else{
				document.frmReply.code.focus();
			}
			document.frmReply.rply.value = "Send Reply";
			document.frmReply.rply.removeAttribute('disabled');
			http.abort();
		}
		else{
			http.abort();
			alert('An error occurred. Please try again.');
			document.frmReply.rply.value = "Send Reply";
			document.frmReply.rply.removeAttribute('disabled');
		}
	}
}
