//<SCRIPT>
if( ! window.navigator.appVersion.match(/MSIE ([0-9]+.)[A-Za-z0-9.]+;/) || new Number(parseFloat (RegExp.$1)) < 5.5 )
{
	window.location.replace("/cordys/wcp/iedownload.htm");
}

window.isCordysApplication = true;
findCordysRoot(window);
window.document.createStyleSheet("/cordys/wcp/style/default.css");
function findCordysRoot(ancestorWindow)
{		
	if (typeof(ancestorWindow.document)=="unknown") 
	{
		if (ancestorWindow.parent!=ancestorWindow) findCordysRoot(ancestorWindow.parent);
		else if (ancestorWindow.opener) findCordysRoot(ancestorWindow.opener);
		return;
	}	
	if (ancestorWindow.isCordysApplication) CordysRoot = ancestorWindow;
	if (ancestorWindow.opener) findCordysRoot(ancestorWindow.opener);
	if (ancestorWindow.dialogArguments && ancestorWindow.dialogArguments.system) CordysRoot = ancestorWindow.dialogArguments.system.document.parentWindow;
	if (ancestorWindow.parent==ancestorWindow) return;
	findCordysRoot(ancestorWindow.parent);
}

if (CordysRoot==window)
{
	// Create a global system object with some initializers
	system = new Object();
	// Create a storage for all visited urls with getURL()
	visitedURLs = new Array();

	/** Plugin related properties are now set(from C3 release) from the clickchoice component. **/
	//system.pluginVersion =  PLUGIN_VERSION;
	//system.CordysBrowserPlugin = CordysRoot.CordysBrowserPlugin;

	// Create a storage for all containers (must be here because of bootstrap)
	system.containers = new Array(); // To contain all iframe containers loaded through the cordys framework

	//TODO: remove libraries collection!!!
	system.libraries = system.containers;

	system.isIE6 = new Number(parseFloat (RegExp.$1)) < 7.0;

	system.getURL = function (url, always) // Get the url in a synchronous manner
	{
		/** TODO: we could add a global variable that allows
		 * disabling of this dynamic preloading, so that the IE setting
		 * 'Every visit to page' will perform better. Currently, this setting
		 * will cause a double number of roundtrips, whereas the setting 'Automatically'
		 * will reduce the number of roundtrips to 1 per url. To overcome the issue
		 * with the setting 'Every visit to page', we could introduce a url cache inside
		 * jscript, but that would then be another cache on top of the IE cache, which could make
		 * memory grow, and performance go down if there are a huge number of urls loaded.
		 */
		if (url.indexOf("://")>0)
		{
			return;
		}
	
		var object = visitedURLs[url];
		if (! object)
		{
			object = new Object();
			CordysRoot.visitedURLs[url] = object;
		}
		else if (! always)
		{
			return;
		}
	
		var xmlhttpConnection = new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttpConnection.open("GET", url, false);
		/** TODO: Check whether we can do something with window.setTimeout() in case the send() method takes too long... */
		xmlhttpConnection.send();
		return xmlhttpConnection;
	}

	system.getScript = function (url, bShowError) // function get's a script either from the library package or straight from the server
	{
		// First try to take it from the library package. If that is not present, or the url is not present
		//  in that package, then we will try to get it from the webserver itself.
		if (system.libraryPackage)
		{
			var lib = system.libraryIndex.selectSingleNode("index/library[@url='"+url+"']");
			if (lib) return system.libraryPackage.substring(lib.getAttribute("start"), lib.getAttribute("end"));
		}
		var syncHTTPConnection = system.getURL(url, true);
		if (syncHTTPConnection.status!=200 && CordysRoot && CordysRoot.application)
		{
			if (bShowError) CordysRoot.application.showError("Could not load script "+url+" it gave response status "+syncHTTPConnection.status);
			return;
		}
		return syncHTTPConnection.responseText;
	}
	system.libraryPackage = "";
	system.libraryIndex = new ActiveXObject("Microsoft.XMLDOM");
	system.libraryIndex.loadXML("<index/>");
	system.libraryPackages = {};
	system.addLibraryPackage = function (url) // returns false if it fails.
	{
		if (system.libraryPackages[url]) return;
		system.libraryPackages[url] = true;
		// Load the source of all libraries
		var httpCall = system.getURL(url, true);
		if (httpCall.status==200)
		{
			var packageSource = httpCall.responseText;
			var offset = packageSource.indexOf("</index>") + 8;
			var packageIndex = new ActiveXObject("Microsoft.XMLDOM");
			packageIndex.loadXML(packageSource.substring(0, offset));
			var currentPackageLength = system.libraryPackage.length;
			var librariesInIndex = packageIndex.selectNodes("index/library[@url]");
			for (var lib = librariesInIndex.nextNode(); lib; lib = librariesInIndex.nextNode())
			{
				lib.setAttribute("start", new Number(lib.getAttribute("start")) + currentPackageLength);
				lib.setAttribute("end", new Number(lib.getAttribute("end")) + currentPackageLength);
				system.libraryIndex.documentElement.appendChild(lib);
			}
			system.libraryPackage += packageSource.substring(offset);
		}
		return httpCall;
	}

	// Load the source of all libraries
	system.addLibraryPackage("/cordys/wcp/libraries.htm");
	
	frameworkLoaders = new Array();
	frameworkLoaders[frameworkLoaders.length] = window;
	
	window.attachEvent("onload", loadFramework);
}
else
{
	if (CordysRoot.system.framework)
	{
		window.attachEvent("onload", loadApplication);
	}
	else
	{
		CordysRoot.frameworkLoaders[CordysRoot.frameworkLoaders.length] = window;
	}
}

function loadFramework()
{
	var iframeObject = CordysRoot.document.createElement("<iframe style='display:none' src='javascript:\"void(0)\"' >");
	CordysRoot.document.documentElement.appendChild(iframeObject);
	system.framework = CordysRoot.frames[iframeObject.uniqueID];
	system.framework.document.writeln("<!-- Cordys Framework Library -->");
	// Give the script a pointer to the cordys root
	system.framework.CordysRoot = CordysRoot;
	system.framework.document.write(system.getScript("/cordys/wcp/library/system/framework.htm"));
	system.framework.document.close();
	// initialize system further
	system.framework.attachSystemLibrary(system);
	for (var wdw in frameworkLoaders)
	{
		CordysRoot.system.framework.attachApplicationLibrary(frameworkLoaders[wdw].document.documentElement);
	}
}

function loadApplication()
{
	// Initialize the current window to attach it to the framework.
	CordysRoot.system.framework.attachApplicationLibrary(window.document.documentElement);
}
