function SjTextLoader() {
	this._id = SjTextLoader._cnt ++;
	this.req = null;
	this.tid=null;
	this.text=null;
	this.IFrameDoc=null;
	this.first = false;
	this.counter = 0;
	SjTextLoader.all[this._id] = this;
	if( document.childNodes && document.createElement ) {
		sjCreateDiv(null,"datadiv"+this._id);
		var dname="datadiv"+this._id;
		var lname="loader"+this._id;
		var str=' <div id='+dname+' style="position:absolute;visibility:hidden"><iframe src="about:blank" id='+lname+' name='+lname+' onload=""></iframe> </div> ';
		sjSetLayerHTML("datadiv"+this._id,str);
	}
}

SjTextLoader.prototype.load = function(inURL,firstLoading) {
	if (!firstLoading){
		if (window.XMLHttpRequest) {//native XMLHttpRequest object
			this.req = new XMLHttpRequest();
			eval('this.req.onreadystatechange = function() { SjTextLoader.all[' + this._id + ']._onLoadText(); }');
			this.req.open("GET", inURL, true);
			this.req.send(null);
		}/* else if ((window.ActiveXObject) && 
					(!(navigator.userAgent.indexOf('MSIE 5.0') && 
						navigator.userAgent.indexOf('Mac') != -1))){//IE/Windows ActiveX version
			this.req = new ActiveXObject("Microsoft.XMLHTTP");
			if (this.req) {
				eval('this.req.onreadystatechange = function() { SjTextLoader.all[' + this._id + ']._onLoadText(); }');
				this.req.open("GET", inURL, true);
				this.req.send();
			}
		}*/ else {
			var elm=sjGetElement("loader" + this._id);
			if (elm){
				if (typeof elm.src != 'undefined')
					elm.src = inURL;
				else if (typeof elm.location != 'undefined')
					elm.location = inURL;
				this.tid=setInterval(this+'.checkLoad()', 100);
			}
		} 
	}else{
		image = new Object;
		metadata = new Object;//for future
		protocol = new Object;//for future
		context = new Object;//for future
		this.first = true;
		document.write("<script language='javascript' src='" + inURL+  ",javascript'></" + "script>");
		this.tid=setInterval(this+'.checkLoad()', 100);
	}
};

SjTextLoader.prototype._onLoadText = function() {
	if (this.req.readyState == 4) {//"loaded";0 = uninitialized 1 = loading 2 = loaded 3 = interactive 4 = complete
		if (this.req.status == 200) {//"OK" Numeric code returned by server, such as 404 for "Not Found" or 200 for "OK"
			this.text = this.req.responseText;
			if (this.onLoadText){
				this.onLoadText();
			}
        } else {
            //alert("There was a problem retrieving data:\n" + this.req.statusText);
            alert(sj_resource.getResource("%THERE_WAS_A_PROBLEM_RETRIEVING_DATA%:\n") + this.req.statusText);
        }
    }
};

function dumpProps(obj,obj_name) {
	var str = "", i ="";
	for (i in obj)
	str += obj_name +"."+ i +" = "+ obj[i] +"\n";
	return str;
}

SjTextLoader.prototype.checkLoad = function() {
	if (this.first == true){
		if (image.rect) {
			this.text = dumpProps(image,"image");
			if (metadata){
				this.text += dumpProps(metadata,"metadata");
			}
			if (protocol){
				this.text += dumpProps(protocol,"protocol");
			}
			if (context){
				this.text += dumpProps(context,"context");
			}
			if (this.onLoadText){
				this.onLoadText();
			}
			this.counter = 0;
			clearInterval(this.tid);
		}else{
			if (this.counter < 100){
				this.counter++;
				window.status = 'Loading context...'+this.counter+' attempt.';
			}else{
				window.status = 'Error loading context!';
				//alert('Error loading context!');
				alert(sj_resource.getResource('%ERROR_LOADING_CONTEXT%!'));
				this.counter = 0;
				clearInterval(this.tid);
			}
		}
	}else{
		var elm=sjGetElement("loader" + this._id);
		if (elm != null)
		if(document.frames){
		 if(document.frames[elm.name]) { this.IFrameDoc=document.frames[elm.name].document } // For IE5
		}else if (elm.contentDocument) { this.IFrameDoc=elm.contentDocument } //For NS6
		 else if (elm.contentWindow) { this.IFrameDoc=elm.contentWindow.document } //For IE5.5
		else{
			//alert('Problems.....');
			alert(sj_resource.getResource('%PROBLEMS%.....'));
			clearInterval(this.tid);
			return true;
		}
		var inf = sjGetTextContent(this.IFrameDoc);
		if (inf){
			this.text = inf;
			if (this.onLoadText){
				this.onLoadText();
			}
			clearInterval(this.tid);
		}
	}
}

SjTextLoader.prototype.clearText = function(txt) {
	var testText = txt;
	if ((testText != null) && (testText != '')) {
	    var result = testText;
	    var teg=testText.substring(testText.indexOf("<"),testText.indexOf(">")+1);
		var idx = testText.indexOf(teg);
		while (idx != -1) {
			result = result.substring(0, idx) + '' + result.substring(idx + teg.length);
			testText=result;
		    idx=-1;
			teg=testText.substring(testText.indexOf("<"),testText.indexOf(">")+1);
			if (teg!=''){
		     idx = testText.indexOf(teg);
			 result=testText;
			}
		}
 	    
		testText=testText.replace("&lt;","<"); 
		testText=testText.replace("&gt;",">"); 
		this.text = testText;
	}
	return this.text;
}

SjTextLoader.prototype.toString = function() {
	return 'SjTextLoader.all[' + this._id + ']';
};

SjTextLoader.all = [];
SjTextLoader._cnt = 0;

