var xOp7Up,xOp6Dn,xIE4Up,xIE4,xIE5,xIE6,xIE7,xIE7,xIE9,xIE10,xNN4,xMac;
var xUA=navigator.userAgent.toLowerCase();
// Detect browser variants
	if(window.opera) {
	  	var i=xUA.indexOf('opera');
	 	 if(i!=-1){
	    	var v=parseInt(xUA.charAt(i+6));
	   	 	xOp7Up=v>=7;
	    	xOp6Dn=v<7;
	  	}
	}else if(navigator.vendor!='KDE' && document.all && xUA.indexOf('msie')!=-1) {
	  xIE4Up=parseFloat(navigator.appVersion)>=4;
	  xIE4=xUA.indexOf('msie 4')!=-1;
	  xIE5=xUA.indexOf('msie 5')!=-1;
	  xIE6=xUA.indexOf('msie 6')!=-1;
	  xIE7=xUA.indexOf('msie 7')!=-1;
	  xIE8=xUA.indexOf('msie 8')!=-1;
	 xIE9=xUA.indexOf('msie 9')!=-1;
	 xIE10=xUA.indexOf('msie 10')!=-1;
	}else if(document.layers){
		xNN4=true;
	}
	xMac=xUA.indexOf('mac')!=-1;


//Local variables
var webEngine="http://www.mobilerota.com/server.php";
if(typeof(DOMParser) == 'undefined') {
	DOMParser = function() {};
	DOMParser.prototype.parseFromString = function(str, contentType) {
		if(typeof(ActiveXObject) != 'undefined') {
			var xmldata = new ActiveXObject('MSXML.DomDocument');
			xmldata.async = false;
			xmldata.loadXML(str);
			return xmldata;
		}else if(typeof(XMLHttpRequest) != 'undefined') {
			var xmldata = new XMLHttpRequest;
			if(!contentType) {
				contentType = 'application/xml';
			}
			xmldata.open('GET', 'data:' + contentType + ';charset=utf-8,' + encodeURIComponent(str), false);
			if(xmldata.overrideMimeType) {
				xmldata.overrideMimeType(contentType);
			}
			return xmldata.responseXML;
		}
		return null;
	}
}


var rotaError=null;
var edgeUser=null;
var debug=true;
var objectArray=new Array();

var rotaConfig="hello";
var initTimer=null;
var xmlTreeMenu=null;

var lang="en";
var xUA=navigator.userAgent.toLowerCase()

var CMSTitle="GTS";


// used for page titles
//var locationStr="Welcome to "+cmsTitle+"admin";

// add default scripts
var coreScriptArray=new Array();
var utilScriptArray=new Array();
var langScriptArray=new Array();
var widgetScriptArray=new Array();

coreScriptArray.push('classHelpers/ClassHandler.js');
//coreScriptArray.push('forms/FormObject.js');
//coreScriptArray.push('forms/FormBuilder2.js');
coreScriptArray.push('engines/WebEngine.js');
//coreScriptArray.push('engines/XMLEngine.js');
coreScriptArray.push('engines/FileEngine.js');

//load interface scripts
//coreScriptArray.push('interface/InterfaceLayout.js');
//coreScriptArray.push('interface/Module.js');
//coreScriptArray.push('objects/ObjectInterface.js');
//coreScriptArray.push('objects/UserHandler.js');
coreScriptArray.push('objects/ErrorHandler.js');
coreScriptArray.push('Rota.js');
coreScriptArray.push('RotaConfig.js');

langScriptArray.push('date/date_'+lang+'.js');
//langScriptArray.push('forms/registration_'+lang+'.js');

utilScriptArray.push('sarissa_new/sarissa.js');
utilScriptArray.push('helpers/Browser.js');
//utilScriptArray.push('helpers/arrays.js');
utilScriptArray.push('helpers/Strings.js');
//utilScriptArray.push('helpers/events.js');
utilScriptArray.push('helpers/XML.js');
//utilScriptArray.push('helpers/flash.js');
utilScriptArray.push('helpers/date.js');
//utilScriptArray.push('helpers/date.js');
utilScriptArray.push('helpers/color.js');
//utilScriptArray.push('storage/json.js');
//utilScriptArray.push('storage/CookieHandler.js');
//utilScriptArray.push('forms/regExp.js');
utilScriptArray.push('forms/Calendar1.js');
//utilScriptArray.push('forms/DatePicker1.js');
//utilScriptArray.push('forms/autoComplete.js');
utilScriptArray.push('sarissa_new/sarissa_ieemu_xpath.js');

//widgetScriptArray.push('tooltip.js');

//load stylesheets

document.write('<link type="text/css" rel="stylesheet" href="css/form.css" />');

function loadLangScripts()
{
	// Add Util Scripts
	var lang=new Array();
	for(var i=0; i < langScriptArray.length;i++) {
		var str="lang/"+langScriptArray[i]
		lang.push(addScript(str));

		if(lang[i].src.indexOf(langScriptArray[langScriptArray.length-1]) > 0) {
			lang[i].onreadystatechange=delegate(this,checkLoaded,lang[i],loadUtilScripts);
		}
		lang[i].onload=function() {
			if(this.src.indexOf(langScriptArray[langScriptArray.length-1]) > 0) {
				loadUtilScripts()
			}
		}
		document.getElementsByTagName("head")[0].appendChild(lang[i]);
	}
}

function loadUtilScripts() {
	// Add Util Scripts
	var utils=new Array();
	for(var i=0; i < utilScriptArray.length;i++) {
		var str="javascript/utils/"+utilScriptArray[i]
		utils.push(addScript(str));

		if(utils[i].src.indexOf(utilScriptArray[utilScriptArray.length-1]) > 0) {
			utils[i].onreadystatechange=delegate(this,checkLoaded,utils[i],loadCoreScripts);
		}
		utils[i].onload=function() {
			if(this.src.indexOf(utilScriptArray[utilScriptArray.length-1]) > 0) {
				loadCoreScripts()
			}
		}

		document.getElementsByTagName("head")[0].appendChild(utils[i]);
	}
	utils=null;
}

function loadCoreScripts()
{

	// Add Core Scripts
	var core=new Array();
	for(var i=0; i < coreScriptArray.length;i++) {
		var str="javascript/core/"+coreScriptArray[i];
		core.push(addScript(str));

		if(core[i].src.indexOf(coreScriptArray[coreScriptArray.length-1]) > 0) {
			core[i].onreadystatechange=delegate(this,this.checkLoaded,core[i],loadWidgetsScripts);
		}
 if(!xIE9) {
		core[i].onload=function() {
			if(this.src.indexOf(coreScriptArray[coreScriptArray.length-1]) > 0) {
				loadWidgetsScripts()
			}
		}
}
		document.getElementsByTagName("head")[0].appendChild(core[i]);
	}
	core=null;
}

function loadWidgetsScripts()
{
	if(widgetScriptArray.length > 0) {
	// Add Widget Scripts
	var widgets=new Array();
	for(var i=0; i < widgetScriptArray.length;i++) {
		var str="javascript/widgets/"+widgetScriptArray[i];
		widgets.push(addScript(str));
		if(widgets[i].src.indexOf(widgetScriptArray[widgetScriptArray.length-1]) > 0) {
			widgets[i].onreadystatechange=delegate(this,this.checkLoaded,widgets[i],initialiseApp);
		}

		widgets[i].onload=function() {
			if(this.src.indexOf(widgetScriptArray[widgetScriptArray.length-1]) > 0) {
				initialiseApp();
			}
		}

		document.getElementsByTagName("head")[0].appendChild(widgets[i]);
	}

	widgets=null;
	}else{
		initialiseApp();
	}
}

function addScript(path)
{

	var e=document.createElement("script");
	e.type="text/javascript";
	e.src=path;



	return e;
}

function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}else{
		window.onload = function() {
			if (oldonload) {
				oldonload();
				}
			func();
		}
	}
}


function checkLoaded(obj,func)
{

	if (obj.readyState=="loaded" || obj.readyState=="complete") {
		//showProcess("loading MobileRota",false);
		func();
	}
}

// created delegate funtionality for use in classes
function delegate( that, thatMethod) {
	if(window.opera) {
		return function() { return thatMethod.call(that); }
	}else{
		if(arguments.length > 2) {
	    	var _params = [];
	    	for(var n = 2; n < arguments.length; ++n) _params.push(arguments[n]);

	    	return function() {
	    		return thatMethod.apply(that,_params);
	    		}
	    }else if (xIE4Up) {
		    if(xIE6|| xIE7 || xIE8 || xIE9) {

		    }else{
				thatMethod.call(that);
			}
	 	}
		return function() { return thatMethod.call(that); }
	}
}

	//loading script

	var appInit=false;
	function initialiseApp() {
	if(appInit==false) {
		try{
			rotaConfig=new RotaConfig();

			//clear init Timer
			if(initTimer!=null) {
					clearInterval(initTimer);
				}
					pageLoadedFunction();
			appInit=true;
			return;
		}catch(e) {
		//	alert(e);
			//init timer used by ie for file load synchronisation errors
			initTimer=setInterval("initialiseApp()",200);
		}
	}
	}

	addLoadEvent(function()
	{
		
  		var e=addScript("javascript/core/interface/MaskFunctions.js");
		e.onreadystatechange=delegate(this,this.checkLoaded,e,loadUtilScripts);
		 if(!xIE9) {
			e.onload=function() {
				// detect first script loaded
			
				if(this.src.indexOf("Mask") > 0) {
					//showProcess("loading Mobile Rota",false);
					loadLangScripts();
				}

			}
		}
		
		
document.getElementsByTagName("head")[0].appendChild(e);
	}

);

