﻿function Permissions( userPermissionsList, userRolesList, permFuncs, defaultAccessRights, mainObj )
{
	this.AccessRightsView = 0x01;
	this.AccessRightsAdd = 0x02;
	this.AccessRightsEdit = 0x04;
	this.AccessRightsDelete = 0x08;
	this.AccessRightsAdvancedView = 0x10;
	this.AccessRightsAdvancedEdit = 0x20;
	this.AccessRightsAdvancedDelete = 0x40;
	this.AccessRightsSystemEdit = 0x80;
	this.AccessRightsSeasonOverride = 0x100;
	this.AccessRightsImportExport = 0x200;
	this.AccessRightsUnused1 = 0x400;
	this.AccessRightsHasJuniors = 0x800;
	this.SARights = 0xFFF;
	this.EditAccessRights = this.AccessRightsAdd | this.AccessRightsEdit | this.AccessRightsDelete | this.AccessRightsAdvancedEdit | this.AccessRightsAdvancedDelete | this.AccessRightsSystemEdit | this.AccessRightsImportExport;
	this.Int64Conversion = "0123456789ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz{|";
	
	this.IntToInt64 = function( num )
	{
		var code = [];
		var num1 = Math.floor(num / 64);
		var num2 = num % 64;
		code.push( this.Int64Conversion.charAt([num1]) );
		code.push( this.Int64Conversion.charAt([num2]) );
		return code.join("");
	}

	this.ParseBase64 = function( code )
	{
		var num = code - 48;
		if( num > 9 )
			num -= 7;
		if( num > 35 )
			num -= 6;
		return num;
	}

	this.ParseInt64 = function( code )
	{
		var num1 = this.ParseBase64( code.charCodeAt(0) );
		var num2 = this.ParseBase64( code.charCodeAt(1) );
		return num2 + (64*num1);
	}

	this.DecodeOldAccessString = function( accessRights )
	{
		var accessCodes = {};
		if( accessRights != null )
		{
			var codeList, permissionID, permObj;
			var list = accessRights.split(",");
			if( list != null )
			{
				var inx = 0;
				for( permissionID in this.userPermissionsList )
				{
					permObj = this.userPermissionsList[permissionID];
					code = list[inx++];
					codeList = [];
					if( code.length >= 2 )
						codeList.push( this.ParseInt64( code.substr(0,2) ) );
					if( code.length >= 4 )
						codeList.push( this.ParseInt64( code.substr(2,2) ) );
					if( code.length >= 6 )
						codeList.push( this.ParseInt64( code.substr(4,2) ) );
					accessCodes[permissionID] = codeList;
				}
			}
			else
				{description:throw "Invalid User Permissions, Please inform the System Administrator"};
		}
		return accessCodes;
	}
	
	this.DecodeAccessString = function( accessRights )
	{
		if( typeof(accessRights) == "string" && accessRights.indexOf(",") != -1 )
			return this.DecodeOldAccessString( accessRights );
		var accessCodes = {};
		if( accessRights != null )
		{
			var inx = 0;
			var permObj, permissionID, codeList;
			while( inx < accessRights.length )
			{
				codeList = [];
				permissionID = this.ParseInt64( accessRights.substr(inx,2) );
				if( permissionID == 0 )
				{
					inx -= 2;
					permissionID = this.ParseInt64( accessRights.substr(inx,2) );
				}
				inx += 2;
				if( permissionID < this.AccessRightsHasJuniors )
				{
					if( inx+2 > accessRights.length )
						throw "Error parsing Access Rights string";
					codeList.push( this.ParseInt64( accessRights.substr(inx,2) ) );
					inx += 2;
				}
				else
				{
					if( inx+6 > accessRights.length )
						throw "Error parsing Access Rights string";
					codeList.push( this.ParseInt64( accessRights.substr(inx,2) ) );
					codeList.push( this.ParseInt64( accessRights.substr(inx+2,2) ) );
					codeList.push( this.ParseInt64( accessRights.substr(inx+4,2) ) );
					inx += 6;
					permissionID = permissionID - this.AccessRightsHasJuniors;
				}
				accessCodes[permissionID] = codeList;
			}
		}
		return accessCodes;
	}

	this.ConvertAccessStringToJson = function( accessRights )
	{
		var accessCodes;
		if( typeof(accessRights) == "string" )
		{
			accessCodes = this.DecodeAccessString( accessRights );
		}
		else
		{
			accessCodes = accessRights;
		}
		var codeList, codes = [];
		for( var permissionID in accessCodes )
		{
			codeList = accessCodes[permissionID];
			codes.push( permissionID+":["+codeList.join(",")+"]" );
		}
		return "{"+codes.join(",")+"}";
	}
	
	this.BuildAccessString = function( accessCodes )
	{
		var i, code, permCode, permObj;
		var accessRights = [];
		for( var permissionID in accessCodes )
		{
			permObj = accessCodes[permissionID];
			permissionID = parseInt(permissionID,10);
			if( permObj.length > 1 )
				permissionID += this.AccessRightsHasJuniors;
			accessRights.push( this.IntToInt64( permissionID ) );
			for( i=0; i<permObj.length; i++ )
				accessRights.push( this.IntToInt64( permObj[i] ) );
		}
		return accessRights.join("");
	}

	this.BuildPermFunction = function( js, permObj, prefix, permissionID, rights )
	{
		var jsp = [prefix,permObj.name];
		if( permObj.enabled )
		{
			jsp.push( ' = function( params ) { return this.CheckPermissions( ' );
			jsp.push( permissionID );
			jsp.push( ', ' );
			jsp.push( rights );
			jsp.push( ', params ); };' );
		}
		else
			jsp.push( ' = function() { return false; };' );
		js.push( jsp.join("") );
		this.fnCount++;
	}

	this.BuildSysAdminAccessRights = function()
	{
		var accessCodes = {};
		for( permissionID in this.userPermissionsList )
		{
			permObj = this.userPermissionsList[permissionID];
			if( permObj.enabled )
			{
				if( permObj.adultsFlag || permObj.juniorsFlag || permObj.schoolsFlag )
				{
					accessCodes[permissionID] = [this.SARights,this.SARights,this.SARights];
				}
				else
				{
					accessCodes[permissionID] = [this.SARights];
				}
			}
		}
		return accessCodes;
	}
	
	this.LogicalOrAccessRights = function( rights1, rights2 )
	{

		if( rights1 == null )
			return rights2;
		if( rights2 == null )
			return rights1;
		var a1, a2, c1, c2, i, codes, count;
		var accessCodes = [];
		for( permID in this.userPermissionsList )
		{
			permObj = this.userPermissionsList[permID];
			a1 = rights1[permID];
			a2 = rights2[permID];
			if( a1 == null )
			{
				if( a2 != null )
					accessCodes[permID] = a2;
			}
			else
			{
				if( a2 != null )
				{
					codes = [];
					count = 1;
					if( permObj.adultsFlag || permObj.juniorsFlag || permObj.schoolsFlag )
						count = 3;
					for( i=0; i<count; i++ )
					{
						if( i < a1.length )
						{
							if( i < a2.length )
								codes.push( a1[i] | a2[i] );
							else
								codes.push( a1[i] );
						}
						else
						{
							if( i < a2.length )
								codes.push( a2[i] );
							else
								codes.push( 0 );
						}
					}
					accessCodes[permID] = codes;
				}
				else
					accessCodes[permID] = a1;
			}
		}
		return accessCodes;
	}
	
	// Methods

	this.SetLogonState = function( data )
	{
		var acs = null;
		if( data.isLoggedOn )
		{
			this.accessRights = null;
			if( !data.SysAdmin && data.AccessRights != null )
			{
				this.accessRights = this.DecodeAccessString(data.AccessRights);
				if( this.accessRights != null )
					acs = this.ConvertAccessStringToJson(this.accessRights);
			}
			this.adultTeamList = data.AdultTeamList;
			this.juniorTeamList = data.JuniorTeamList;
			this.schoolTeamList = data.SchoolTeamList;
			this.clubList = data.ClubList;
			this.userPlayerID = data.PlayerID;
			this.canLockLogins = data.CanLockLogins;
			this.isSysAdmin = data.SysAdmin;
			this.userRoleID = data.UserRoleID == null ? 0 : data.UserRoleID;
			this.userID = data.UserID;
			this.username = data.Username;
		}
		else
		{
			this.accessRights = null;
			this.adultTeamList = "";
			this.juniorTeamList = "";
			this.schoolTeamList = "";
			this.clubList = "";
			this.userPlayerID = 0;
			this.canLockLogins = false;
			this.isSysAdmin = false;
			this.userRoleID = 0;
			this.userID = 0;
			this.username = "";
		}
		if( this.serverInstance )
		{
			Session("ACS") = acs;
			Session("ATL") = this.adultTeamList;
			Session("JTL") = this.juniorTeamList;
			Session("STL") = this.schoolTeamList;
			Session("CL") = this.clubList;
			Session("PID") = this.userPlayerID;
			Session("LL") = this.canLockLogins;
			Session("SA") = this.isSysAdmin;
			Session("UR") = this.userRoleID;
			Session("UID") = this.userID;
			Session("UN") = this.username;
		}
	}
	
	this.GetDefaultAccessRights = function()
	{
		return this.defaultAccessRights;
	}

	this.GetRawAccessRights = function()
	{
		if( this.serverInstance )
		{
			var accessJson = Session("ACS");
			if( accessJson == null || accessJson == "" )
				this.accessRights = null;
			else
				eval( 'this.accessRights = "'+accessJson+'";' );
		}
		return this.accessRights;
	}
	

	this.ReadTeamAccessList = function( dbCmdObj, juniors, schools )
	{
		if( this.serverInstance )
		{
			if( this.teamAccessList == null )
			{
				if( dbCmdObj == null )
				{
					throw {description:"ReadTeamAccessList: No database provider supplied"};
				}
				else
				{
					var relClublist = this.GetRelatedClubList();
					if( relClublist == null )
						return null;
					this.teamAccessList = {};
					if( objectLength( relClublist ) > 0 )
					{
						this.teamAccessList = this.GetRelatedTeamList( juniors, schools );
						if( this.teamAccessList == null )
						{
							this.teamAccessList = {};
							var rowList = dbCmdObj.ReadDbRowList( "SELECT TeamID FROM tblTeam WHERE ClubID In ("+objectJoin( relClublist, ",", true )+")", false );
							for( var i=0; i<rowList.length; i++ )
							{
								this.teamAccessList[rowList[i].TeamID] = true;
							}
						}
					}
				}
			}
			return this.teamAccessList;
		}
		throw {description:"ReadTeamAccessList method not available for client code"};
	}


	this.BuildTeamAccessListJson = function()
	{
		if( dbCmdObj != null && this.serverInstance )
		{
			var htm = [];
			if( this.teamAccessList == null )
				htm.push( 'var teamAccessList = null;\n' );
			else
			{
				htm.push( 'var teamAccessList = {' );
				var jsList = [];
				for( var teamID in this.teamAccessList )
				{
					jsList.push( teamID+':true' );
				}
				htm.push( jsList.join(",") );
				htm.push( '};' );
			}
			return htm.join("");
		}
		throw {description:"BuildTeamAccessListJson method not available for client code"};
	}

	this.GetUserRoleID = function()
	{
		if( this.serverInstance )
		{
			this.userRoleID = Session("UR");
			if( this.userRoleID == null )
				this.userRoleID = 0;
		}	
		return this.userRoleID;
	}

	this.GetUserID = function()
	{
		if( this.serverInstance )
		{
			this.userID = 0;
			var ID = Session("UID");
			if( ID != null && ID != "" )
			{
				var num = parseInt(ID,10);
				if( !isNaN(num) )
					this.userID = num;
			}
		}
		else
		{
			if( this.userID == null )
				this.userID = 0;
		}
		return this.userID;
	}

	this.GetUserPlayerID = function()
	{
		if( this.serverInstance )
		{
			var playerID = 0;
			var ID = Session("PID");
			if( ID != null && ID != "" )
			{
				var num = parseInt(ID,10);
				if( !isNaN(num) )
					this.userPlayerID = num;
			}
		}
		return this.userPlayerID;
	}
	
	this.IsLoggedIn = function()
	{
		return this.GetUserID() != 0;
	}
	
	this.IsSystemAdministrator = function()
	{
		if( this.serverInstance )
			this.isSysAdmin = Session("SA");
		return this.isSysAdmin;
	}
	
	this.CanLockLogins = function()
	{
		if( this.serverInstance )
			this.canLockLogins = Session("LL");
		return this.canLockLogins;
	}
	
	this.GetAccessRightsForUser = function( accessRights, userRoleID, sysAdmin, userRoles )
	{
		if( sysAdmin )
			return this.BuildSysAdminAccessRights();
		var rights = accessRights;
		if( userRoleID != 0 && userRoleID != null )
		{
			var userRoleObj;
			if( userRoles != null )
				userRoleObj = userRoles[userRoleID];
			else
				userRoleObj = this.userRolesList[userRoleID];
			if( userRoleObj != null )
			{
				rights = userRoleObj.accessRights;
			}
		}
		else
		{
			if( rights == null )
				rights = this.GetDefaultAccessRights();
			else
				rights = perms.DecodeAccessString( accessRights );
		}
		if( rights == null )
			rights = [];
		else
			// ensure that the user also has the default access rights
			rights = this.LogicalOrAccessRights( rights, this.GetDefaultAccessRights() );
		return rights;
	}

	this.GetAccessRights = function()
	{
		return this.GetAccessRightsForUser( this.GetRawAccessRights(), this.GetUserRoleID(), this.IsSystemAdministrator() );
	}
	
	this.GetRawRelatedClubList = function()
	{
		if( this.serverInstance )
			this.clubList = Session("CL");
		return this.clubList;
	}

	this.GetRawTeamList = function( juniors, schools )
	{
		if( this.serverInstance )
		{
			this.adultTeamList = Session("ATL");
			this.juniorTeamList = Session("JTL");
			this.schoolTeamList = Session("STL");
		}
		if( juniors )
			return this.juniorTeamList;
		else
		{
			if( schools )
				return this.schoolTeamList;
		}
		return this.adultTeamList;
	}

	this.GetRawRelatedTeamList = function( juniors, schools )
	{
		return this.GetRawTeamList( juniors, schools );
	}

	this.GetRelatedClubList = function()
	{
		var arr = null;
		var clubList = this.GetRawRelatedClubList();
		if( clubList != null )
		{
			arr = {};
			if( clubList != "" )
			{
				var strList = new String(clubList);
				var list = strList.split(",");
				for( var i=0; i<list.length; i++ )
					arr[list[i]] = true;
			}
		}
		return arr;
	}

	this.GetRelatedTeamList = function( juniors, schools )
	{
		var arr = null;
		var teamList = this.GetRawRelatedTeamList( juniors, schools );
		if( teamList != null )
		{
			arr = {};
			if( teamList != "" )
			{
				var strList = new String(teamList);
				var list = strList.split(",");
				for( var i=0; i<list.length; i++ )
					arr[list[i]] = true;
			}
		}
		return arr;
	}

	this.HasClubAccess = function( clubIDList )
	{
		var clubList = this.GetRelatedClubList();
		var hasAccess = true;
		if( clubList != null && clubIDList != null )
		{
			if( typeof(clubIDList) == "number" )
				clubIDList = [clubIDList];
			if( clubIDList.length > 0 )
			{
				hasAccess = false;
				for( var i=0; i<clubIDList.length; i++ )
				{
					if( clubList[clubIDList[i]] == true )
					{
						hasAccess = true;
						break;
					}
				}
			}
		}
		return hasAccess;
	}

	this.HasTeamAccess = function( teamIDList, juniors, schools )
	{
		var teamList = this.GetRelatedTeamList( juniors, schools );
		var hasAccess = true;
		if( teamList != null && teamIDList != null )
		{
			if( typeof(teamIDList) == "number" )
				teamIDList = [teamIDList];
			if( teamIDList.length > 0 )
			{
				hasAccess = false;
				for( var i=0; i<teamIDList.length; i++ )
				{
					if( teamList[teamIDList[i]] == true )
					{
						hasAccess = true;
						break;
					}
				}
			}
		}
		return hasAccess;
	}
	
	this.IsPreviousSeason = function()
	{
		if( this.serverInstance )
			return season < NextSeason
		return mainObj.getCurrentSeason() < mainObj.getNextSeason();
	}
	
	this.CheckAccess = function( access, permissionID, accessRight, params )
	{
		var obj = access[permissionID];
		if( obj == null )
			return false;
		var hasAccess = false;
		if( params.juniors )
			hasAccess = obj.length > 1 && (obj[1]&accessRight) == accessRight;
		if( !hasAccess && params.schools )
			hasAccess = obj.length > 2 && (obj[2]&accessRight) == accessRight;
		if( !hasAccess && !params.juniors && !params.schools )
			hasAccess = obj.length > 0 && (obj[0]&accessRight) == accessRight;
		return hasAccess;
	}
	
	this.CheckPermissions = function( permissionID, rights, params )
	{
		if( params == null )
			params = {};
		var access = params.overrideAccessRights == null ? this.GetAccessRights() : params.overrideAccessRights;
		var permObj = this.userPermissionsList[permissionID];
		var overrideSeason = this.CheckAccess( access, permissionID, this.AccessRightsSeasonOverride, params );

		var isPrevSeason = this.IsPreviousSeason();
		var isEditPerm = (rights & this.EditAccessRights) != 0;
		if( permObj.hasSeasons )
		{
			if( isPrevSeason && isEditPerm && !overrideSeason )
				return false;
		}

		hasAccess = this.CheckAccess( access, permissionID, rights, params );
		if( !hasAccess )
			return false;
		var clubList = this.GetRelatedClubList();
		var teamList = this.GetRelatedTeamList( params.juniors, params.schools );
		if( teamList != null && params.teamIDList != null )
		{
			return this.HasTeamAccess( params.teamIDList, params.juniors, params.schools );
		}
		return this.HasClubAccess( params.clubIDList );
	}

	this.CheckAnyAccess = function( compareTo, accessRequired, permObj, permissionID )
	{
		var reqdObj = accessRequired[permissionID];
		if( reqdObj == null )
			return false;
		var rights, isEditPerm, isPrevSeason = this.IsPreviousSeason();
		var compareObj = compareTo[permissionID];
		if( compareObj == null )
			compareObj = [0,0,0];
		var overrideSeason = this.CheckAccess( compareTo, permissionID, this.AccessRightsSeasonOverride, {} );
		for( var i=0; i<reqdObj.length; i++ )
		{
			if( i < compareObj.length )
			{
				if( (compareObj[i]&reqdObj[i]) != 0 )
				{
					rights = reqdObj[i];
					if( permObj.hasSeasons )
					{
						isEditPerm = (rights & this.EditAccessRights) == rights;
						if( !isPrevSeason || !isEditPerm || overrideSeason )
							return true;
					}
					else
						return true;
				}
			}
		}
		return false;
	}

	this.CheckAnyPermissions = function( accessRequired, compareTo )
	{
		var permReqd, userPerm;
		if( accessRequired == null )
			return true;
		if( compareTo == null )
		{
			compareTo = this.GetAccessRights();
		}
		if( compareTo == null || compareTo == "" )
			return false;
		for( var permissionID in this.userPermissionsList )
		{
			permObj = this.userPermissionsList[permissionID];
			if( permObj.enabled )
			{
				if( this.CheckAnyAccess( compareTo, accessRequired, permObj, permissionID ) )
					return true;
			}
		}
		return false;
	}

	this.CheckAllAccess = function( compareTo, accessRequired, permissionID )
	{
		var reqdObj = accessRequired[permissionID];
		if( reqdObj == null )
			return true;
		var overrideSeason = this.CheckAccess( compareTo, permissionID, this.AccessRightsSeasonOverride, {} );
		var rights, isEditPerm, sPrevSeason = this.IsPreviousSeason();
		var compareObj = compareTo[permissionID];
		if( compareObj == null )
			compareObj = [0,0,0];
		var isPrevSeason = this.IsPreviousSeason();
		for( var i=0; i<reqdObj.length; i++ )
		{
			rights = reqdObj[i];
			if( i < compareObj.length )
			{
				if( (compareObj[i]&rights) != rights )
					return false;
				//if( permObj.hasSeasons )
				//{
				//	isEditPerm = (rights & this.EditAccessRights) != 0;
				//	if( isPrevSeason && isEditPerm && !overrideSeason )
				//		return false;
				//}
			}
			else
			{
				if( rights != 0 )
					return false;
			}
		}
		return true;
	}

	this.CheckAllPermissions = function( accessRequired, compareTo )
	{
		var permReqd, userPerm;
		if( accessRequired == null )
			return true;
		if( compareTo == null )
			compareTo = this.GetAccessRights();
		if( compareTo == null || compareTo == "" )
			return false;
		for( var permissionID in this.userPermissionsList )
		{
			permObj = this.userPermissionsList[permissionID];
			if( permObj.enabled )
			{
				if( !this.CheckAllAccess( compareTo, accessRequired, permissionID ) )
					return false;
			}
		}
		return true;
	}

	this.GetBestViewPermission = function( permissionID, params )
	{
		if( params == null )
			params = {};
		var accessRights = params.overrideAccessRights == null ? this.GetAccessRights() : params.overrideAccessRights;
		if( this.CheckAccess( accessRights, permissionID, this.AccessRightsAdvancedView, params ) )
			return this.AccessRightsAdvancedView;
		return this.CheckAccess( accessRights, permissionID, this.AccessRightsView, params ) ? this.AccessRightsView : 0;
	}

	this.GetBestEditPermission = function( permissionID, params )
	{
		if( params == null )
			params = {};
		var accessRights = params.overrideAccessRights == null ? this.GetAccessRights() : params.overrideAccessRights;
		if( this.CheckAccess( accessRights, permissionID, this.AccessRightsSystemEdit, params ) )
			return this.AccessRightsSystemEdit;
		if( this.CheckAccess( accessRights, permissionID, this.AccessRightsAdvancedEdit, params ) )
			return this.AccessRightsAdvancedEdit;
		return this.CheckAccess( accessRights, permissionID, this.AccessRightsEdit, params ) ? this.AccessRightsEdit : 0;
	}

	this.GetBestAddPermission = function( permissionID, params )
	{
		if( params == null )
			params = {};
		var accessRights = params.overrideAccessRights == null ? this.GetAccessRights() : params.overrideAccessRights;

		if( this.CheckAccess( accessRights, permissionID, this.AccessRightsImportExport, params ) )
			return this.AccessRightsImportExport;
		return this.CheckAccess( accessRights, permissionID, this.AccessRightsAdd, params ) ? this.AccessRightsAdd : 0;
	}

	this.GetBestDeletePermission = function( permissionID, params )
	{
		if( params == null )
			params = {};
		var accessRights = params.overrideAccessRights == null ? this.GetAccessRights() : params.overrideAccessRights;

		if( this.CheckAccess( accessRights, permissionID, this.AccessRightsAdvancedDelete, params ) )
			return this.AccessRightsAdvancedDelete;
		return this.CheckAccess( accessRights, permissionID, this.AccessRightsDelete, params ) ? this.AccessRightsDelete : 0;
	}
	
	this.RenderAppVars = function()
	{
		if( this.serverInstance )
		{
			var json = getApplicationVar("UserRoles");
			if( json == null )
				this.userRolesList = {};
			else
				eval( "this.userRolesList = "+json+";" );

			json = getSystemProperty("DefaultAccessRights");
			if( json == null || json == "" )
				this.defaultAccessRights = {};
			else
				eval( 'this.defaultAccessRights = '+json+';' );
		}
	}

	if( permFuncs == null )
	{
		this.serverInstance = true;
		eval( 'this.userPermissionsList = ' + getApplicationVar("UserPerms") + ';' );
		var js, permObj;
		var js = [];
		this.fnCount;
		var index = 0;
		this.userRoleID = 0;
		this.accessRights = null;
		this.isSysAdmin = false;
		this.canLockLogins = false;
		this.adultTeamList = "";
		this.juniorTeamList = "";
		this.schoolTeamList = "";
		this.clubList = "";
		this.userPlayerID = 0;
		this.userID = 0;
		this.username = "";

		for( var permissionID in this.userPermissionsList )
		{
			permObj = this.userPermissionsList[permissionID];
			this.fnCount = 0;
			if( permObj.viewPermission )
				this.BuildPermFunction( js, permObj, "this.CanView", permissionID, this.AccessRightsView );
			if( permObj.addPermission )
				this.BuildPermFunction( js, permObj, "this.CanAdd", permissionID, this.AccessRightsAdd );
			if( permObj.editPermission )
				this.BuildPermFunction( js, permObj, "this.CanEdit", permissionID, this.AccessRightsEdit );
			if( permObj.deletePermission )
				this.BuildPermFunction( js, permObj, "this.CanDelete", permissionID, this.AccessRightsDelete );
			if( permObj.advancedViewPermission )
				this.BuildPermFunction( js, permObj, "this.CanAdvancedView", permissionID, this.AccessRightsAdvancedView );
			if( permObj.advancedEditPermission )
				this.BuildPermFunction( js, permObj, "this.CanAdvancedEdit", permissionID, this.AccessRightsAdvancedEdit );
			if( permObj.advancedDeletePermission )
				this.BuildPermFunction( js, permObj, "this.CanAdvancedDelete", permissionID, this.AccessRightsAdvancedDelete );
			if( permObj.systemEditPermission )
				this.BuildPermFunction( js, permObj, "this.CanSystemEdit", permissionID, this.AccessRightsSystemEdit );
			if( permObj.usePermission )
				this.BuildPermFunction( js, permObj, "this.CanUse", permissionID, this.AccessRightsView );
			if( permObj.importPermission )
				this.BuildPermFunction( js, permObj, "this.CanImport", permissionID, this.AccessRightsImportExport );
			// if no explicit functions, use generic one
			if( this.fnCount == 0 )
			{
				this.BuildPermFunction( js, permObj, "this.Can", permissionID, this.AccessRightsView );
			}
		
			if( permObj.adultsFlag || permObj.juniorsFlag || permObj.schoolsFlag )
			{
				jsp = ['this.Adult',permObj.name,'Enabled = ',(permObj.enabled&&permObj.adultsFlag?"true;":"false;")];
				jsp.push( 'this.Junior',permObj.name,'Enabled = ',(permObj.enabled&&permObj.juniorsFlag?"true;":"false;") );
				jsp.push( 'this.School',permObj.name,'Enabled = ',(permObj.enabled&&permObj.schoolsFlag?"true;":"false;") );
			}
			else
				jsp = ['this.',permObj.name,'Enabled = ',(permObj.enabled?"true;":"false;")];
			js.push( jsp.join("") );
			index++;
		}
		permFuncs = js.join("");
		this.permFuncs = permFuncs;
	}
	else
	{
		this.mainObj = mainObj;
		this.serverInstance = false;
		eval( 'this.userPermissionsList = ' + userPermissionsList + ';' );
		eval( 'this.userRolesList = '+userRolesList+';' );
		eval( 'this.defaultAccessRights = '+defaultAccessRights+';' );
	}
	eval( permFuncs );

	this.GetUserRoles = function() { return this.userRolesList; };
	this.GetUserPermissionsList = function() { return this.userPermissionsList; };
}
