/* Expand Comments version 1.1
 *  Copyright 2006 Andrew Rader
 *
 * This file is part of Expand Comments
 *
 * Expand Comments is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * Expand Comments is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Expand Comments; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

// buildAjaxRequest is inspired by www.sitepoint.com/article/take-command-ajax
// which is taken from other various tuts. Sharing is caring.
// For documentation's sake, callback is a function that takes
// a single argument. This argument holds what is returned from
// the server, as XML of retXML is true, or as Text if not
// buildAjaxRequest will also look to see if there is a third
// argument, which should be an array of more arguments.
// This array is expanded, and when the callback is called,
// It will be given this list of arguments

function buildAjaxRequest( callback, retXML, ignoreStatus ) {
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if( http_request ) {

		if( retXML ) {
			evalString = '(http_request.responseXML';
		}
		else {
			evalString = '(http_request.responseText';
		}

		if( arguments.length == 4 ) {
			args = arguments[3];

			for( var i = 0; i < args.length; i++ ) {
				evalString += (',args[' + i + ']');
			}
		}

		evalString += ')';

		http_request.onreadystatechange = function() {
			if (http_request.readyState == 4) {
				if( ignoreStatus || http_request.status == 200 ) {
					eval(callback + evalString);
				}
			}
		}
	}

	return http_request;
}

/*
 * makeAjaxRequest will form a GET request from the server.
 * Any arguments past the first 3 will be sent to the callback
 */
function makeAjaxRequest(callback, retXML, url) {
	args = [];
	for( i = 3; i < arguments.length; i++ ) {
		args.push( arguments[i] );
	}

	http_request = buildAjaxRequest(callback, retXML, false, args);

	/* This RANDOM query is so the page isn't cached */
	url += "&RANDOM=" + (Math.random() * Date.parse(new Date()))
	http_request.open('GET', url, true);
	http_request.send(null);
}

/*
 * makeAjaxPost will form a POST request to the server.
 * params should be a properly formatted line (id=3&submit=true)
 * Any arguments past the first 4 will be sent to the callback
 */
function makeAjaxPost(callback, retXML, url, params) {

	args = [];
	for( i = 4; i < arguments.length; i++ ) {
		args.push( arguments[i] );
	}

	http_request = buildAjaxRequest(callback, retXML, true, args);

	http_request.open("POST", url, true);
	http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	http_request.send(params);

	return false;
}
