 /******************************************************************

Copyright © Global Knowledge Software LLC 2007.  All Rights Reserved.

This program is protected by U.S. and International Copyright and
Patent Laws.  Unauthorized duplication and/or distribution of this
program is strictly prohibited. Distribution and duplication of this
program are limited by license. If you do not currently have a valid
license from Global Knowledge for this program, any copying or
distribution of the program is unauthorized.  If you do have a current
license from Global Knowledge to utilize this program, your use is
strictly limited by the terms of that license.

Patent Pending.

******************************************************************/

function CheckTitle(app_folder)
{
	for (var i=0; i<systemApps.length; i++)
	{
		if (systemApps[i].folder.toUpperCase()==app_folder.toUpperCase()) // && systemApps[i].toc)
			return true;
	}
	
	return false;
}

stateCurrentApp=systemFirstTitle;
var initApplication="";
var initCtxTracking=false;

var params=unescape(window.location);

var s=params.indexOf("?");
if (s==-1)
	s=params.indexOf("#");
if (s!=-1)
{
	params=params.substr(s+1);

	var par=params.split("&");
	for (var p=0; p<par.length; p++)
	{
		var paritem=par[p].split("=");
		if (paritem[0].toUpperCase()=="APPLICATION")
			initApplication=paritem[1].toUpperCase();
		else if (paritem[0].toUpperCase()=="SEARCH")
			stateSearchTerm=paritem[1];
		else if (paritem[0].toUpperCase()=="VIEW")
			stateView=paritem[1];
		else if (paritem[0].toUpperCase()=="FATPLAYER")
		{
			if (paritem[1].toUpperCase()=="FS" || paritem[1].toUpperCase()=="WEB")
				systemFatPlayer=paritem[1].toUpperCase();
//			if (systemFatPlayer=="FS")
//				systemSearchEnabled=true;
		}
		else if (paritem[0].toUpperCase()=="CTX")
			stateCurrentCtxOd=paritem[1];
		else if (paritem[0].toUpperCase()=="CTXEX")
		{
			if (!IsEmptyArgument(paritem[1]))
			{
				stateCurrentCtxEx=paritem[1];
				systemCtxTracking=true;
			}
		}
		else if (paritem[0].toUpperCase()=="TEST" && paritem[1].toUpperCase()=="ON")
	   {
			systemTest=true;
			systemCtxTracking=true;
		}
		else if (paritem[0].toUpperCase()=="MAINCOLS")
			systemMainCols=paritem[1];
		else if (paritem[0].toUpperCase()=="INNERROWS")
			systemInnerRows=paritem[1];
		else if (paritem[0].toUpperCase()=="TRACKING" && paritem[1].toUpperCase()=="ON")
			initCtxTracking=true;
	}
}

function IsEmptyArgument(s)
{
	if (s.length==0)
		return true;
	if (s=='""' || s=="''")
		return true;
	return false;
}

if (systemFatPlayer=="")
{
	systemCookie.Load();
	if (systemCookie.MainCols && systemCookie.MainCols!="")
	{
		systemMainCols=systemCookie.MainCols;
		systemInnerRows=systemCookie.InnerRows;
	}

	if (stateView=="" && systemCookie.LastView)
		stateView=systemCookie.LastView;
}
else
{
	systemCtxTracking=initCtxTracking;
}

if ( (stateCurrentCtxOd!="" || stateCurrentCtxEx!="") && stateView=="" && systemCtxTracking)
	stateView="applicable";
else
{
	if (initApplication=="" && systemFatPlayer=="" && systemCookie.LastApplication)
		initApplication=systemCookie.LastApplication;

	if (stateView=="" && systemCookie.LastView)
		stateView=systemCookie.LastView;
}

if ( !systemCtxTracking && stateView=="applicable")
	stateView="all";

if (stateView=="search" && systemSearchEnabled!=true)
	stateView="all";

if (stateView=="applicable" && stateCurrentCtxOd!="")
{
	var ctxArray=stateCurrentCtxOd.split("+");
	firstApplication="";
	for (var c=0; c<ctxArray.length; c++)
	{
		var ctxElem=ctxArray[c].split("$");
		if (c==0)
			firstApplication=ctxElem[0];
		if (ctxElem[0]==initApplication)
			break;
	}
	if (c==ctxArray.length && firstApplication!="")
		initApplication=firstApplication;
}

if (initApplication!="" && CheckTitle(initApplication))
	stateCurrentApp=initApplication;

if (stateView=="")
	stateView="all";
stateLastValidApp=stateCurrentApp;


if (stateView=="all")
{
	for (var i=0; i<systemApps.length; i++)
	   stateAvailableApps[stateAvailableApps.length]=new objApplication(systemApps[i].folder,systemApps[i].title,systemApps[i].mappingmode,systemApps[i].first_topic_index,systemApps[i].last_topic_index);
}


var user_role_count=UserRoles.Roles.length;
for (var ur=0; ur<UserRoles.Roles.length; ur++)
{
	for (var sr=0; sr<systemRoles.length && UserRoles.Roles[ur]!=systemRoles[sr]; sr++);
	if (sr==systemRoles.length)
	{
		for (var ur1=ur; ur1<UserRoles.Roles.length-1; ur1++)
			UserRoles.Roles[ur1]=UserRoles.Roles[ur1+1];
		UserRoles.Roles.length--;
		ur--;
	}
}

if (user_role_count>UserRoles.Roles.length)
{
	if (systemRoles.length==0)
	{
		UserRoles.Filtering=false;
		alert(R_roles_all_removed);
	}
	else if (!UserRoles.Filtering || UserRoles.Roles.length>0)
	{
		alert(R_roles_removed);
	}
	else
	{
		UserRoles.Filtering=false;
		alert(R_roles_removed_all_selected);
	}

	UserRoles.Persist();
}

QueryTopicsToRoles(UserRoles.Roles);


try
{
	if(window.opener)
	{
		if (!window.opener.closed)
			window.opener.blur();
	}
}
catch(e)
{
}
