var jsLoader = {
	jsResult: [],
	jsCount: 0,
	load: function(jspath, cbFunc) {
		var js = document.createElement("script");
		var requestId = this.jsCount++;
		js.setAttribute("language","javascript");
		js.setAttribute("src", jspath + "&_rid="+requestId);
		var header = document.getElementsByTagName("HEAD");

		if (cbFunc!=null && typeof(cbFunc)=="function") {
			js.cbFunc = cbFunc;
			js.onreadystatechange =function(){ 
				if (this.cbFunc===undefined) return; 
				if (this.cbFunc==null) return;
				if (this.readyState=="complete" || this.readyState=="loaded")
				{
					var result = jsLoader.getResult(requestId);
					this.cbFunc(result);
					this.cbFunc = null;
					this.parentNode.removeChild(this);
				 }
			};
			js.onload = function() {
				if (cbFunc) {
					var result = jsLoader.getResult(requestId);
					cbFunc(result);
				}
				js.parentNode.removeChild(js);
			}
		}
		
		if (header != null && header.length>0) {
			header[0].appendChild(js);
		} else {
			alert("Can't find the document header, please add it " +
				  "before adding any js module.");
		}
	},
	setResult: function(requestId,result) {
		this.jsResult[requestId] = result;
	},
	getResult: function(requestId) {
		if (this.jsResult[requestId]!==undefined) {
			var result = this.jsResult[requestId];
			delete this.jsResult[requestId];
			return result;
		}
		return null;
	}
};

window.jsLoader = jsLoader;