/*

script modified from http://www.sitepoint.com/article/take-command-ajax

class properties

this.url (string):
	the URL of the request.

this.params (string):
	used for this.method == 'POST'
	as the FORM-POST name=value pairs
	e.g., 'var1=aaa&var2=bbb&var3=ccc';

this.callback (STRING, name of a function):
	The function to call when the response is received
	this function should have one or more arguments,
		with the FIRST ARGUMENTS being the value of:
		- httpRequest.responseXML
		OR
		- httpRequest.responseText
		(the choice is made by boolean this.returnXml)

	more arguments stored in this.callbackMoreArguments

this.callbackMoreArguments (string);
	string that compose the arguments (after the first argument described above) of the callback function.  e.g.,
	'123, \'abc\', 456'
	IMPORTANT!!  escape the quotes in the argument list, like the \' bracketing the 'abc' above

this.method (string):
	the HTTP request method
	'GET' or 'POST'

this.returnXml (boolean):
	Flag if the result should be passed as XMLDocument or as plain text to the callback function 


METHODS:

this.send():
	send the http request with the properties defined above.

this.serialize(form):
	convert all input field of the form into a serialized
	name-value pairs: like
	'var1=aaa&var2=bbb&var3=ccc'
	return this as string.

*/

function AJAX() {

var me = this;

this.url = 'about:blank';
this.params = '';
this.callback = '';
this.callbackMoreArguments = '';
this.method = 'GET';
this.returnXml = false;

this.send = function () { 
	var httpRequest = false;
	
	if (window.XMLHttpRequest) { // Mozilla, Safari,... 
		httpRequest = new XMLHttpRequest(); 
		if (httpRequest.overrideMimeType && me.returnXml) { 
			httpRequest.overrideMimeType('text/xml'); 
		}
	} else if (window.ActiveXObject) { // IE 
		try { 
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
		} catch (e) { 
			try { 
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
				} catch (e) {} 
		} 
	} 
	
	if (!httpRequest) { 
		alert('Sorry, Your browser doesn\'t support this AJAX feature.');
		return false;
	}
	
	httpRequest.onreadystatechange = function() {
		if (httpRequest.readyState == 4) {
			if (httpRequest.status == 200) {
				if (me.returnXml) {
					if (me.callbackMoreArguments == '') {
						eval(me.callback + '(httpRequest.responseXML);');
					} else {
						eval(me.callback + '(httpRequest.responseXML, '+ me.callbackMoreArguments +');');
					}
				} else {
					if (me.callbackMoreArguments == '') {
						eval(me.callback + '(httpRequest.responseText);');
					} else {
						eval(me.callback + '(httpRequest.responseText, '+ me.callbackMoreArguments +');');
					}
				}
			} else if (httpRequest.status != 0) {
				alert('AJAX ERROR: There was a problem with the request.(Code: ' + httpRequest.status + ')');
			}
		}
	}

	switch (this.method.toUpperCase()) {
		case 'GET':
			httpRequest.open('GET', this.url, true);
			httpRequest.send(null);
		break;
		case 'POST':
			httpRequest.open('POST', this.url, true);
			//Send the proper header information along with the request
			httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			httpRequest.setRequestHeader("Content-length", this.params.length);
			httpRequest.setRequestHeader("Connection", "close");
			httpRequest.send(this.params);
		break;
		default:
			alert('invalid FORM method: ' + this.method);
			return;
		break;
	}

}


this.serialize = function(form) {
	var str = '';
	var aInput = form.getElementsByTagName('input');
	var aSelect = form.getElementsByTagName('select');
	var aTextarea = form.getElementsByTagName('textarea');
	
	// serialize <input>s
	for (var ii=0; ii<aInput.length; ii++) {
		var onePair = aInput[ii].name + '=' + escape(aInput[ii].value);
		if(str=='') {
			str = onePair;
		} else {
			str += '&' + onePair;
		}
	}

	// serialize <select>s
	for (var ii=0; ii<aSelect.length; ii++) {
		var idx = aSelect[ii].selectedIndex;
		var val = aSelect[ii][idx].value;
		var onePair = aSelect[ii].name + '=' + escape(val);
		if(str=='') {
			str = onePair;
		} else {
			str += '&' + onePair;
		}
	}

	// serialize <textarea>s
	for (var ii=0; ii<aTextarea.length; ii++) {
		var onePair = aTextarea[ii].name + '=' + escape(aTextarea[ii].value);
		if(str=='') {
			str = onePair;
		} else {
			str += '&' + onePair;
		}
	}

	return str;

}


}