//*************************************************************************
// Class Module
// Author : R Tostevin 2005
//*************************************************************************


//****************************************************************
//  FileLoader Class
//****************************************************************

/* Note : Client CallBackFunc Signature
function ready(filecontents,objFileLoader){
	
}
*/

var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');	
var ie = (typeof window.ActiveXObject != 'undefined');

//Constructor
function class_FileLoader(requestType,URL,CallBackFunc){	
	var httpRequest;
		
	//Create a HttpRequest Object
	if (moz){
		//Mozilla
		httpRequest = new XMLHttpRequest();
	} else if (ie){
		//IE
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
		  	try {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		  	} catch (E) {
				httpRequest = false;
				return;
		  	}
		}
    }
	
	//Define Properties *********	
	this.CallBackFunc=CallBackFunc; //Func to Call when Load is complete
	this.URL=URL;					//URL to File to load
	this.requestType=requestType;	//Should be GET/POST/HEAD
	this.httpRequest=httpRequest;
	//this.httpRequest.myowner=this;	//Create extra property on httpRequest object to point to this instance (for a call back)	
	
	
	//Define Methods ***********
	this.LoadFile=LoadFile;	

}


//Methods
function LoadFile (){
	var callerinstance;		//Reference to instance of class_FileLoader object being used (effectively 'this' object)
	
	
	//Record a reference inside this method to the caller so it can be used inside the 
	//call back function() specified for the onreadystatechange event
	//Note : this.httpRequest.myowner cant be used if we are using an ActiveX object in IE
	callerinstance=this;
	
	
	//Start the File Loading
	//Note : open Params (GET/POST/HEAD , URL , aSync false=synchronous)

	//Synchronous Loading
	//this.httpRequest.open(this.requestType,this.URL, false);
	//this.httpRequest.send(null);
	//textToBeWritten = this.httpRequest.responseText;
			
	//A-Synchronous
	this.httpRequest.open(this.requestType,this.URL, true);
	//this.httpRequest.onreadystatechange=file_onreadystatechange; //Define a  call back for events from the httpRequest
	this.httpRequest.onreadystatechange = function() {
	  if (callerinstance.httpRequest.readyState==4 ) {
		var fileContents; 
	    fileContents = callerinstance.httpRequest.responseText;		
		callerinstance.CallBackFunc(fileContents,callerinstance);
	  }
	 }
	this.httpRequest.send(null);
	
	/*Alternate Code :
	xmlhttp.open("GET", "test.txt",true);
	 xmlhttp.onreadystatechange=function() {
	  if (xmlhttp.readyState==4) {
	   alert(xmlhttp.responseText)
	  }
	 }
	 xmlhttp.send(null)
	  */
}


//Events
/*function file_onreadystatechange(){
	var fileContents="";
	if (this.readyState==4) {		
	if (this.readyState==4) {		
		//Get File Contents and Call back to Client
		fileContents = this.responseText;
		this.myowner.CallBackFunc(fileContents,this.myowner);		
	}
}
*/
//###################################