/*

OBSOLETE - Don't know if this is still being called by any script, but new file is in /ajax/init.js

YES - still being used by the news template, at least.

---- attempt should be made to roll this all into ajax/comments.js and ajax/init.js

reqType = "POST" or "GET"
*/

var request = null;
var queryString;

// this function is called from the onclick/onsubmit of the original regular HTML page
// it needs the url, which will be a PHP script that handles everything
//   and 'thisForm' which is the form that contained the data
//   and 'responseHandle' is the javascript function that will handle what to do with the data retrieved from the server (i.e. PHP file called by the url)

function processComment(thisForm)
{
	setQueryString(thisForm);
	var url = "/php/ajaxprocess/newsComments.php";
	doXHR("POST",url);
}


function setQueryString(thisForm)
{
	queryString = "";
	numElements = thisForm.elements.length;
	for (var i=0; i < numElements; i++)
	{
		queryString += thisForm.elements[i].name+"="+encodeURIComponent(thisForm.elements[i].value);
		if (i < numElements-1)
			queryString += "&";
	}
}

function doXHR(reqType,url)
{
	// Mozilla
	if (window.XMLHttpRequest)
	{
		request = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		request = new ActiveXObject("Msxml2.XMLHTTP");
		if (!request)
		{
			request = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	// the request could still be null
	// check
	if (request)
	{
		initReq(reqType,url);
		/*
		// if the reqType parameter is POST then the 5th argument to the function is the POSTed data
		if (reqType.toLowerCase()!="post")
		{
			initReq(reqType,url);
		}
		else
		{
			var args = arguments[4]; // the fifth argument
			if (args != null && args.length>0)
			{
				initReq(reqType,url,args);
			}
		}
		*/
	}
	else
	{
		// alert or something
	}
}

function initReq(reqType,url)
{
	/* specify the function that will handle the HTTP response - in this case 'handleResponse' */
	request.onreadystatechange = handleResponse;
	request.open(reqType,url);
	if (reqType.toLowerCase() == "post")
	{
		request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		// request.send(arguments[4]);
		// with a "post" you have to send the queryString ... with a "get" you do not, since it's in the URL in "request.open"
		request.send(queryString);
	}
}

function handleResponse()
{
	var temp = document.getElementById('newsCommentDisplay').innerHTML;
	if (request.readyState == 1)
	{
		//alert("YES?");
		document.getElementById('newsCommentDisplay').style.background = "url(/images/spinner.gif) center center no-repeat";
		document.getElementById('newsCommentDisplay').style.textAlign  = "center";
		document.getElementById('newsCommentDisplay').style.paddingTop = "60px";
		document.getElementById('newsCommentDisplay').innerHTML = ". . . W A I T I N G . . . ";
		//alert (document.getElementById('newsCommentDisplay').style.background);
	}
	if (request.readyState == 4)
	{
		document.getElementById('newsCommentDisplay').style.background = "white";
		if (request.status == 200)
		{
			// send the request.responseText back to the page
			// alert(request.responseText);
			// this should be separated out into a different file and/or function
			//    because they are unique to the Article Comments script
			//    it's the only unique thing here, except for the hard coding of the url above
			if (request.responseText.indexOf("ERROR:")==0)
			{
				alert(request.responseText);
				document.getElementById('newsCommentDisplay').innerHTML = temp;
			}
			else
			{
				document.getElementById('newsCommentDisplay').innerHTML = request.responseText;
				document.getElementById('newsCommentDisplay').style.textAlign  = "left";
				document.getElementById('newsCommentDisplay').style.paddingTop = "0px";
				document.commentForm.comment.value = "";
			}
		}
		else
		{
			// error
		}
	}
}