// JavaScript Document
function ajax(options){
	options={
		type: options.type || "GET",
		url: options.url || "",
		timeout: options.timeout || 5000,
		onComplete: options.onComplete || function(){},
		onError: options.onError ||function(){},
		onSuccess: options.onSuccess || function(){},
		data: options.data || ""
	};
	
	//create request object
	if (window.XMLHttpRequest) {
	  xml = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
	  xml = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
	  alert("Your browser lacks the needed ability to use Ajax");
	  return false;
	}
	//var xml = new XMLHttpRequest();
	xml.open(options.type, options.url, true); //open the socket
	var timeoutLength = options.timeout; // set the timeout
	var requestDone=false; // initialize requestDone
	setTimeout(function(){
							  		requestDone=true;
							  }, timeoutLength);
	
	xml.onreadystatechange=function(){ //watch for doc state
		if(xml.readyState == 4 && !requestDone){ // make sure it is fully loaded and has not timed out
			if(httpSuccess(xml)){										// was it successful
				options.onSuccess(httpData(xml, options.type)); // run the callback with the data returned from the server
			}
			else{
				options.onError();
			}
			options.onComplete();
			xml=null;
		}
	};
	
	//establish server connection
	xml.send(null);
	
	// determine the success of the HTTP response
	function httpSuccess(r){
			try {
				return !r.status && location.protocol == "file:" ||
					(r.status >= 200 && r.status < 300) ||
					r.status == 304 ||
					navigator.userAgent.indexOf("Safari") >= 0 &&
					typeof r.status == "undefined";
					//alert('updated');
			}
			catch(e){}
			return false;
			
	}
	
	//extract the correct data from the HTTP response
	function httpData(r,type){
		var ct=r.getResponseHeader("content-type");
		var data = !type && ct && ct.indexOf("xml") >= 0;
		data = type == "xml" || data ? r.responseXML : r.responseText;
		if(type=="script")
			eval.call(window, data);
		return data;
	}

}
