var scriptObjArray = new Array();

function MBWscriptRequest(fullUrl,id) {
    this.fullUrl = fullUrl; 
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    this.headLoc = document.getElementsByTagName("head").item(0);
    this.scriptId = 'JscriptId_' + id;
}

MBWscriptRequest.prototype.buildScriptTag = function () {
    this.scriptObj = document.createElement("script");
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
MBWscriptRequest.prototype.removeScriptTag = function () {
	this.headLoc.removeChild(this.scriptObj);  
}

MBWscriptRequest.prototype.addScriptTag = function () {
	this.headLoc.appendChild(this.scriptObj);
}

function MBW_unload_app(obj)
{
	document.getElementsByTagName("head").item(0).removeChild(obj);
}

function MBW_load_app(name)
{
	name=escape(name);
	var req = '/js/app/'+name+'.js';
	MBWObj = new MBWscriptRequest(req,name);
	MBWObj.buildScriptTag();
	MBWObj.addScriptTag();
	return MBWObj.scriptId;
}

function MBW_load_apps(names)
{
	var namesArray = names.split(',');
	
	/* clear all loaded scripts */
	var n = scriptObjArray.length;
	for (i=0;i<n;i++)
	{
		var oldObj = scriptObjArray.shift();
		// MBW_unload_app(document.getElementById(oldObj));
		obj = document.getElementById(oldObj);
		document.getElementsByTagName("head").item(0).removeChild(obj);
	}
	
	/* load new scripts */
	for (i=0;i<namesArray.length;i++)
	{
		id = MBW_load_app(namesArray[i]);
		/* add script id to currently loaded scripts array */
		scriptObjArray[scriptObjArray.length] = id;
	}
	
}

