// This is a javascript library that makes fetching stuff from url nice and easy

// it's quite handy to return something that is actually a javascript object i.e. {data}



function HtmlFetcher() {
	
	
	// Atributes
	this.xmlDocument = null;
	this.data = null;

	// Methods
	this.fetchData = fetchData;
	this.fetchXml = fetchXml;
	this._fetchUrl = _fetchUrl;
	
	this.stateChange = _stateChange;
	
	this.xmlHttp = _getXMLHTTP();
	
}

new HtmlFetcher();



HtmlFetcher.fetch = function(url) {
	// fetch a url and wait for the result
	// fetch a url and don't wait for the result before returning
	//alert("fin " +finishedFunction)
	

	var xmlHttp = new XMLHttp();
	
	if(xmlHttp && url) {	// don't try this if we don't have an xmlhttp object to use or a url to fetch
	
		// true means do this asyncronsly. i.e. carry on rather than waiting for a response.
		xmlHttp.xmlHttp.open("GET",url,false);
		
		// set up an event handler, to listen out for event changes and do something useful when they happen
		
		//xmlHttp.xmlHttp.onreadystatechange = function() {xmlHttp.xmlHttp_EventHandler()};
		
		// actually send the request. the event handler should deal with what ever happens next
    	var thing = xmlHttp.xmlHttp.send(null);

		return xmlHttp.xmlHttp.responseText;

	}
	
}


HtmlFetcher.fetchNoWait = function(url, finishedFunction, args) {
	// fetch a url and don't wait for the result before returning
	//alert("fin " +finishedFunction)
	

	var xmlHttp = new XMLHttp(finishedFunction, args);
	
	if(xmlHttp && url) {	// don't try this if we don't have an xmlhttp object to use or a url to fetch
	
		// true means do this asyncronsly. i.e. carry on rather than waiting for a response.
		xmlHttp.xmlHttp.open("GET",url,true);
		
		// set up an event handler, to listen out for event changes and do something useful when they happen
		
		xmlHttp.xmlHttp.onreadystatechange = function() {xmlHttp.xmlHttp_EventHandler()};
		
		// actually send the request. the event handler should deal with what ever happens next
    	var thing = xmlHttp.xmlHttp.send(null);

	}
	
}



function XMLHttp(finishedFunction, args) {
	
	this.finishedFunction = finishedFunction || function() {};	// call this function when the url has been fetched, with the result as the first arg
	this.finishedArgs = args;
	
	
	var A=null;
	try {
		A=new ActiveXObject("Msxml2.XMLHTTP")
	} catch(e) {
		try {
			A=new ActiveXObject("Microsoft.XMLHTTP")
		} catch(oc){
			A=null
		}
	}
	if(!A && typeof XMLHttpRequest != "undefined") {
		A=new XMLHttpRequest()
	}
	
	
	this.xmlHttp = A;
}
new XMLHttp();


XMLHttp.prototype.xmlHttp_EventHandler = function xmlHttp_EventHandler() {

	// this is a custom XMLHttp object which contains a xmlHttp property which is a propper HTTPRequest object
	
	//alert(this.xmlHttp.readyState);
	switch (this.xmlHttp.readyState) {
	case 1:
		//self.onLoading();
		break;
	case 2:
	//	self.onLoaded();
		break;
	case 3:
	//	self.onInteractive();
		break;
	case 4:
		//alert("finished!");
		var response = this.xmlHttp.responseText;
		this.finishedFunction(response, this.finishedArgs);
		//alert(response);
	//	self.responseXML = this.xmlHttp.responseXML;
	//	self.responseStatus[0] = this.xmlHttp.status;
	//	self.responseStatus[1] = this.xmlHttp.statusText;
	//	self.onCompletion();
	//	if(self.execute){ self.runResponse(); }
	//	if (self.elementObj) {
	//		var elemNodeName = self.elementObj.nodeName;
	//		elemNodeName.toLowerCase();
	//		if (elemNodeName == "input" || elemNodeName == "select" || elemNodeName == "option" || elemNodeName == "textarea"){
	//			self.elementObj.value = self.response;
	//		} else {
	//			self.elementObj.innerHTML = self.response;
	//		}
	//	}
	//	self.afterCompletion();
	//	self.URLString = "";
		break;
	}
};



function _stateChange(e) {
		if (!e) var e = window.event
		alert(e);
		alert("state : " + this );
		if (this.xmlHttp.readyState == 4) {	// status 4 means that it's completed, and ready to be looked at.
			this.xmlDocument = this.xmlHttp.responseXML.documentElement;
			this.data = this.xmlHttp.responseText;
		}
}




// Create an xml Http object in a supposedly browser independant way.
function _getXMLHTTP(){

}

function fetchData (urlToFetch) {
	this.xmlDocument= null;
	this._fetchUrl(urlToFetch, false);
	return this.data
}

function fetchXml (urlToFetch) {
	this.xmlDocument= null;
	this._fetchUrl(urlToFetch, true);
	return this.xmlDocument;
	
}

function _fetchUrl(urlToFetch, xml) {
	// If we are already in the middle of fetching something, stop it. This is a bit like pressing the stop button in a browser
	if(this.xmlHttp && this.xmlHttp.readyState!=0){
		this.xmlHttp.abort()
	}
	
	// set the results to null.
	this.xmlDocument= null;
	this.data= null;

  
	if(this.xmlHttp && urlToFetch) {	// don't try this if we don't have an xmlhttp object to use
		// true means do this asyncronsly. i.e. carry on rather than waiting for a response.
		this.xmlHttp.open("GET",urlToFetch,false);
		
		// set up an event handler, to listen out for event changes and do something useful when they happen
		
		this.xmlhttp.onreadystatechange = xmlHttp_EventHandler;
		
		// actually send the request. the event handler should deal with what ever happens next
    	var thing = this.xmlHttp.send(null);
		
		if (xml == true) {
			this.xmlDocument = this.xmlHttp.responseXML.documentElement;
		}
		this.data = this.xmlHttp.responseText;

	}
}



	
//	this.onLoading = function() { };
//	this.onLoaded = function() { };
//	this.onInteractive = function() { };
//	this.onCompletion = function() { };
//	this.afterCompletion = function() { };
	

