/**
 *	Examples
 */
 
/*
 		var parameters = {
			
			// string path to server file
			url: '/path/to/server_ajax_file.php',
			
			// string [optional] method of request
			method: 'POST',
			
			// string [optional] element attribute
			resultIn: 'this.innerHTML',
			
			// string [optional] element attribute
			resultInConcat: 'this.innerHTML',
			
			// string [optional] element.attribute
			resultConcatIn: 'this.innerHTML',
			
			// function [optional] executes after completeing the request
			onComplete: function() {
				alert('string');
			},
			
			// function [optional] executes on SUCCESS after completeing the request
			onSuccess: function(e) {
				alert(e);
			},
			
			// function [optional] executes on FAILURE after completeing the request
			onFailure: function(e) {
				alert(e);
			},
			
			// integer [optional] miliseconds before executing the request
			delay: 1000,
			
			// object [optional] data to be send to the server
			data: {
				key1: 'value1',
				key2: 'value2'
			},
			
			// boolean [optional] if request is started with delay, request can be canceled
			cancel: true
		};
		
		myAJAX(parameters);
	
*/

if (typeof XMLHttpRequest  === "undefined") {
  XMLHttpRequest = function() {
    try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
      catch(e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
      catch(e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP"); }
      catch(e) {}
    try { return new ActiveXObject("Microsoft.XMLHTTP"); }
      catch(e) {}
    throw new Error("This browser does not support XMLHttpRequest.");
  };
}


var timo;
function myAJAX(request) {
	runIt = function() {
		var parameters = request.method == 'GET' ? '?':'';
		var myHTTP;
		try {
		  myHTTP = new XMLHttpRequest();
		} catch (e) {
			throw new Error("This browser does not support XMLHttpRequest.");
		}
	
		myHTTP.onreadystatechange = function() {
			if(myHTTP.readyState == 4) {
				if (myHTTP.getResponseHeader('Status') == 'success' && typeof(request.onSuccess) == 'function') {
					request.onSuccess(myHTTP.responseText);
				}
				else if (myHTTP.getResponseHeader('Status') == 'eval' && myHTTP.responseText.length > 0) {
					eval(myHTTP.responseText);
				}
				else if (myHTTP.getResponseHeader('Status') == 'failure' && typeof(request.onFailure) == 'function') {
					request.onFailure(myHTTP.responseText);
				}
				
				if (typeof(request.onComplete) == 'function') {
					request.onComplete();
				}
				
				if (myHTTP.getResponseHeader('Status') == 'success' && request.resultIn && myHTTP.responseText.length > 0) {
					eval(request.resultIn + ' = myHTTP.responseText;');
				}
				else if (myHTTP.getResponseHeader('Status') == 'success' && request.resultConcatIn && myHTTP.responseText.length > 0) {
					eval(request.resultConcatIn + ' =  myHTTP.responseText + ' + request.resultConcatIn + ';');
				}
				else if (myHTTP.getResponseHeader('Status') == 'success' && request.resultInConcat && myHTTP.responseText.length > 0) {
					eval(request.resultInConcat + ' = ' + request.resultInConcat + ' + myHTTP.responseText;');
				}
			}
		}
		
		request.url += request.url.indexOf('?') > -1  ? '&'+Math.random() : '?'+Math.random();
		
		if (request.data) {
			for (param in request.data) {
				if(typeof(request.data[param]) == 'array') {
					for(var i=0; i<request.data[param].length; i++) {
						parameters += param+'['+i+']=' + encodeURIComponent(request.data[param][i]) + '&';
					}
				} else if(typeof(request.data[param]) == 'object') {
					
					for(var i in request.data[param]) {
						parameters += param+'['+i+']=' + encodeURIComponent(request.data[param][i]) + '&';
					}
				}else {
					parameters += param+'=' + encodeURIComponent(request.data[param]) + '&';
				}
				
			}
			parameters = parameters.substr(0, parameters.length - 1);
		}
		
		if (request.method == 'GET') {
			myHTTP.open('GET', request.url + parameters, true);
			parameters = null;
		}
		else if (request.method == 'POST' || typeof(request.method) == 'undefined') {
			myHTTP.open('POST', request.url, true);
			myHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			myHTTP.setRequestHeader("Content-length", parameters.length);
			myHTTP.setRequestHeader("Connection", "close");
		}
		
		myHTTP.setRequestHeader("Cache-Control", "no-cache");
		
		myHTTP.send(parameters);
	}
	
	if (request.delay) {
		if (timo) {
			clearTimeout(timo);
		}
		timo = setTimeout('eval("runIt();")', request.delay);
	}
	else if (request.cancel && timo) {
		clearTimeout(timo);
	}
	else {
		runIt();
	}
}