/**********************************************************************************************
 *	Copyright © 2001-08 Cordys R&D B.V. All rights reserved. 
 *
 *	The  computer program(s)  is the  proprietary  information of  Cordys R&D B.V., 
 *	and provided  under the relevant Agreement between yourself  and Cordys R&D B.V. 
 *	containing restrictions on use and disclosure, and are  also protected by copyright, 
 *	patent, and other intellectual and industrial property laws. No part of this program 
 *	may be used/copied without the prior written consent of Cordys R&D B.V. 
 *
 *	Project		: Cordys custom login form example
 *	Description : This script is contains some function to do the cordys SSO login and start 
 *				  the Cordys desktop.
 */

/**********************************************************************************************
 * Function		: loadCordysApplicationReady() 
 * Description	: Do initialization and load some libraries then do sso login.
 */
function initializeBeforeLoadingCordys()
{
	CordysRoot.system.addListener("onbeforelogout", cordysBeforeLogoutHandler);
	CordysRoot.system.addListener("onafterlogout",  cordysAfterLogoutHandler);

	application.addLibrary("/cordys/wcp/library/data/busdataisland.htm", rightFrame);
	application.addLibrary("/cordys/wcp/library/system/sso.htm", rightFrame);

	
	parent.cordys = window;
	
	cordysSSOLogin();
}

/**********************************************************************************************
 * Function		: cordysSSOLogin() 
 * Description	: Do the actual sso login.
 */
function cordysSSOLogin()
{
	/*var loginEventData = parent.window.cordysLoginInfo.eventData;
	if (loginEventData != null && loginEventData.id == "doLogin")
	{
		//var userName 				= loginEventData.userName;
		//var userPassword 			= loginEventData.userPassword;
		loginEventData.id 			= "";
		loginEventData.userName 	= "";
		loginEventData.userPassword	= "";*/
		
		if (!CordysRoot.sso.loggedOn()) 
		{
			CordysRoot.system.addListener("onafterlogin", cordysAfterLoginHandler);
			CordysRoot.sso.login('temp', 'welcome');
    
		}
	//}
}

/**********************************************************************************************
 * Function		: cordysAfterLoginHandler() 
 * Description	: Is called after succesfull sso login. here the cordys desktop is loaded.
 */
function cordysAfterLoginHandler(ssoEventObject)
{
	CordysRoot.system.removeListener("onafterlogin", cordysAfterLoginHandler);
	if (ssoEventObject.loginSuccess)
	{
		var appXML = xmlCordysDesktopApp.documentElement.cloneNode(true);
		CordysRoot.sso.activate(null, appXML, null);

		// Show the helper frame in which Cordys will be loaded
		document.getElementById("rightFrame").style.display = "";
	}
	else
	{
		parent.window.cordysLoginInfo.eventData.error = ssoEventObject.faultString;
		// Hide the cordys helper frame
		document.getElementById("rightFrame").style.display = "none";
	}
}

/**********************************************************************************************
 * Function		: cordysBeforeLogoutHandler() 
 * Description	: Before we logout first do some cleaning up.
 * Note			: Code taken from unloadSystem() in Cordys framework.htm
 */
function cordysBeforeLogoutHandler()
{
	var reverseOrderedContainers = new Array();
	for (var id in CordysRoot.system.containers)
	{
		reverseOrderedContainers[reverseOrderedContainers.length] = id;
	}
	for (var i=reverseOrderedContainers.length-1; i>=0; i--)
	{
		var id = reverseOrderedContainers[i];
		if (id == "startup") continue;
		var container = CordysRoot.system.containers[id] ? CordysRoot.system.containers[id] : 0;
		if (container) container.close(true); 	// TRIGGERS A save dialog
	}
}

/**********************************************************************************************
 * Function		: cordysAfterLogoutHandler() 
 * Description	: Reload the application. This will display the initial login page again.
 */
function cordysAfterLogoutHandler()
{
	top.location.replace(top.location.href);
}

/******************************************** EOF *********************************************/