/*--------------------------------------------------
	GLOBALS

	New Cookie Format for Rights
	{userid:1}right:flag,right:flag...{groupid:2}right:flag,right:flag

--------------------------------------------------*/
var commaRights = new String( );


/*--------------------------------------------------
	A simple support function that redirects the 
	popup rights window back to the rights list
	screen.
--------------------------------------------------*/
function returntolist()
{
	if( account_list.length )
		location.href=account_list;
}


/*--------------------------------------------------
	This function is used to determine whether or
	not to update the cmoma delimited string of
	selected rights. This function is passed both 
	the reference to the radio button rights and the
	reference to the no propgate checkbox.
--------------------------------------------------*/
function addright( right_id, right_fld, propogate_fld )
{	
	var flags = 0;

	flags = flags | right_fld.value;
	if( propogate_fld )
		flags = flags | propogate_fld.value;

	if( flags == 0 )
		return;

	if( commaRights.length > 0 )
		commaRights += ",";
	
	commaRights = commaRights + right_id + ":" + flags;
}


/*--------------------------------------------------
	This function ensures that the entire list of 
	rigths are built up in the variable commaRights
	by making successive calls for each right
	fiel to addright().
--------------------------------------------------*/
function buildrightslist( )
{
	commaRights="";

	// this function needs to access and build a comma
	// delimited list of rights, the following are also friendly
	// to the fact that not all rights are always disaplyed

	if( document.f.inherit_playslideshow )
		addright( 2011, document.f.right_playslideshow, document.f.rightp_playslideshow );
	if( document.f.inherit_orderprints )
		addright( 2010, document.f.right_orderprints, document.f.rightp_orderprints );
	if( document.f.inherit_enhancemedia )
		addright( 2007, document.f.right_enhancemedia, document.f.rightp_enhancemedia );
	if( document.f.inherit_sendmedia )
		addright( 2006, document.f.right_sendmedia, document.f.rightp_sendmedia );
	if( document.f.inherit_printmedia )
		addright( 2005, document.f.right_printmedia, document.f.rightp_printmedia );
	if( document.f.inherit_viewfullsize )
		addright( 2004, document.f.right_viewfullsize, document.f.rightp_viewfullsize );
	if( document.f.inherit_viewmedia )
		addright( 2003, document.f.right_viewmedia, document.f.rightp_viewmedia );

	if( document.f.inherit_addmedia )
		addright( 2002, document.f.right_addmedia, document.f.rightp_addmedia );
	if( document.f.inherit_addfolder )
		addright( 2001, document.f.right_addfolder, document.f.rightp_addfolder );
	if( document.f.inherit_addsection )
		addright( 2000, document.f.right_addsection, document.f.rightp_addsection );

	if( document.f.inherit_audit )
		addright( 1011, document.f.right_audit, document.f.rightp_audit );
	if( document.f.inherit_altname )
		addright( 1010, document.f.right_altname, document.f.rightp_altname );
	if( document.f.inherit_restrictions )
		addright( 1009, document.f.right_restrictions, document.f.rightp_restrictions );
	if( document.f.inherit_acceptref )
		addright( 1008, document.f.right_acceptref, document.f.rightp_acceptref );
	if( document.f.inherit_createref )
		addright( 1007, document.f.right_createref, document.f.rightp_createref );
	if( document.f.inherit_reorder )
		addright( 1006, document.f.right_reorder, document.f.rightp_reorder );
	if( document.f.inherit_email )
		addright( 1005, document.f.right_email, document.f.rightp_email );
	if( document.f.inherit_setrights )
		addright( 1004, document.f.right_setrights, document.f.rightp_setrights );
	if( document.f.inherit_move )
		addright( 1003, document.f.right_move, document.f.rightp_move );
	if( document.f.inherit_delete )
		addright( 1002, document.f.right_delete, document.f.rightp_delete );
	if( document.f.inherit_modify )
		addright( 1001, document.f.right_modify, document.f.rightp_modify );
	if( document.f.inherit_view )
		addright( 1000, document.f.right_view, document.f.rightp_view );
}


/*--------------------------------------------------
	This function simply removes a user definintion
	from the cookie, if the user indicated in account_id
	is already part of the cookie.
--------------------------------------------------*/
function removeuserfromcookie( type )
{
	var theRights = new String( GetCookie( "rights" ) );	
	if( theRights == "null" )
		return;

	var userDelim = "{" + account_id + ":" + type + "}";

	// If don't find user, simply return as it is not in the cookie
	begin = theRights.indexOf( userDelim, 0 );
	if( begin < 0 )
		return;

	end = theRights.indexOf( "{", begin+1 );
	if( end < 0 )
		end = theRights.length;

	theRights = theRights.substr( 0, begin ) + theRights.substr( end, theRights.length - end );

//	alert( "new right "  + theRights );
	SetCookie( "rights", theRights );
}


//--------------------------------------------------
function dorights( type )
{
	// First remove existing user cookie, if exists
	removeuserfromcookie( type );

	// Now we need to add a new user to the cookie, so first
	// get the cookie
	var theRights = new String( GetCookie( "rights" ) );
		theRigths = "";

	// Now we need to append the new user information 
	// and rights to the cookie
	buildrightslist();

	// But, only add if there were any rights selected. This
	// is indicated if the resulting rights string from the 
	// previous call has a length
	if( commaRights.length )
	{
		if( theRights == "null" )
			theRights = "{" + account_id + ":" + type + "}" + commaRights;
		else
			theRights = theRights + "{" + account_id + ":" + type + "}" + commaRights;
	}
	else
	{
		if( theRights == "null" )
			theRights = "{" + account_id + ":" + type + "}";
		else
			theRights = theRights + "{" + account_id + ":" + type + "}";
	}

//	alert( theRights );

	SetCookie( "rights", theRights );
	returntolist();
}

//--------------------------------------------------
function doaccountrights( )
{
	dorights( 1 ); 
}

//--------------------------------------------------
function dogrouprights( )
{
	dorights( 2 ); 
}


/*--------------------------------------------------
	This function will retrieve the comma delimited
	list of rights out of the cookie, if exists,
	and put the resulting delimited list into the
	global variable 'commaRights'. By passing the 
	type field, yuo can set whether yuo are looking
	for a group's or a user's rights.
--------------------------------------------------*/
function getusercookierights( type, id )
{
	commaRights = "";

	// If no rights, then do nothing
	var theRights = new String( GetCookie( "rights" ) );	
	if( theRights == "null" )
		return false;

//	alert( theRights );
	var userDelim = "{" + id + ":" + type + "}";

	// If don't find user, simply return as it is not in the cookie
	begin = theRights.indexOf( userDelim, 0 );
	if( begin < 0 )
		return false;

	begin += userDelim.length;

	end = theRights.indexOf( "{", begin );
	if( end < 0 )
		end = theRights.length;

	commaRights = theRights.substr( begin, end-begin );
//	alert( commaRights );

	return true;
}


/*--------------------------------------------------
	This function looks through the passed comma
	delimited string of right:flag pairs and returns
	the flag for the passed right id if it is found.
-------------------------------------------------*/
function getrightflag( str, right )
{
	if( str == "null" || !str.length )
		return false;

	var begin = 0;

	while (true )
	{
		end = str.indexOf( ",", begin );
		if( end < 0 )
			end = str.length;

		semi = str.indexOf( ":", begin );
		if( semi < 0 )
			return 0;

		if( str.substr( begin, semi-begin ) == right )
			return str.substr( semi+1, end-semi-1 )

		begin = end+1;

		if( begin >= str.length )
			return 0;
	}

	return 0;
}


/*--------------------------------------------------
	This function serves as a common function that
	takes as input a right id, a comma-deliminted
	string and a reference to the rights radio
	button and a reference to the no propogate
	radio button. Using these values, this function
	is able to setup the initial states of the 
	buttons to mimic the cookie or database values
	as appropriate.
-------------------------------------------------*/
function setupright( right, str, right_fld, propogate_fld )
{
	var rightsFlag = 0;
	rightsFlag = getrightflag( str, right );
	
//	alert( rightsFlag );

	if( rightsFlag & 1 )
		right_fld.value=1;
	if( rightsFlag & 2 )
		right_fld.value=2;

	if( propogate_fld )
	{
		if( rightsFlag & 4 )
			propogate_fld.value=4;
		if( rightsFlag & 8 )
			propogate_fld.value=8;
	}
}


/*--------------------------------------------------
	This function serves as a common function that 
	takes as a paramters a string which will contain
	a list of comma delimited rights that have either
	been retrieved from the cookie or from the database.
	This routine then parses the list of rights to 
	determine which rights boxes to check.

	The initial 'if' statement is to detect to see
	if the set of rights buttons are even present
	as it is possible for different screens to 
	have different rights, e.g. the Folder Rights
	screen doesn;t require rights for Add Section
	and Add Folder.
--------------------------------------------------*/
function setinitialrights( str )
{
	if( document.f.inherit_playslideshow )
		setupright( 2011, str, document.f.right_playslideshow, document.f.rightp_playslideshow );
	if( document.f.inherit_orderprints )
		setupright( 2010, str, document.f.right_orderprints, document.f.rightp_orderprints );
	if( document.f.inherit_enhancemedia )
		setupright( 2007, str, document.f.right_enhancemedia, document.f.rightp_enhancemedia );
	if( document.f.inherit_sendmedia )
		setupright( 2006, str, document.f.right_sendmedia, document.f.rightp_sendmedia );
	if( document.f.inherit_printmedia )
		setupright( 2005, str, document.f.right_printmedia, document.f.rightp_printmedia );
	if( document.f.inherit_viewfullsize )
		setupright( 2004, str, document.f.right_viewfullsize, document.f.rightp_viewfullsize );
	if( document.f.inherit_viewmedia )
		setupright( 2003, str, document.f.right_viewmedia, document.f.rightp_viewmedia );

	if( document.f.inherit_addmedia )
		setupright( 2002, str, document.f.right_addmedia, document.f.rightp_addmedia );
	if( document.f.inherit_addfolder )
		setupright( 2001, str, document.f.right_addfolder, document.f.rightp_addfolder );
	if( document.f.inherit_addsection )
		setupright( 2000, str, document.f.right_addsection, document.f.rightp_addsection );

	if( document.f.inherit_audit )
		setupright( 1011, str, document.f.right_audit, document.f.rightp_audit );
	if( document.f.inherit_altname )
		setupright( 1010, str, document.f.right_altname, document.f.rightp_altname );
	if( document.f.inherit_restrictions )
		setupright( 1009, str, document.f.right_restrictions, document.f.rightp_restrictions );
	if( document.f.inherit_acceptref )
		setupright( 1008, str, document.f.right_acceptref, document.f.rightp_acceptref );
	if( document.f.inherit_createref )
		setupright( 1007, str, document.f.right_createref, document.f.rightp_createref );
	if( document.f.inherit_reorder )
		setupright( 1006, str, document.f.right_reorder, document.f.rightp_reorder );
	if( document.f.inherit_email )
		setupright( 1005, str, document.f.right_email, document.f.rightp_email );
	if( document.f.inherit_setrights )
		setupright( 1004, str, document.f.right_setrights, document.f.rightp_setrights );
	if( document.f.inherit_move )
		setupright( 1003, str, document.f.right_move, document.f.rightp_move );
	if( document.f.inherit_delete )
		setupright( 1002, str, document.f.right_delete, document.f.rightp_delete );
	if( document.f.inherit_modify )
		setupright( 1001, str, document.f.right_modify, document.f.rightp_modify );
	if( document.f.inherit_view )
		setupright( 1000, str, document.f.right_view, document.f.rightp_view );
}


/*--------------------------------------------------
	This function is called when entering into the 
	account rights screen when adding a control. When
	adding it does NOT need to consider database
	rights to be present.
--------------------------------------------------*/
function setinitialaddaccountrights( )
{
	// attempt to get existing cookie values for this user
	// NOTE: passing type=1 indicates to look for a user
	if( !getusercookierights( 1, account_id ) )
		return;

	// if rights have been found for this user in the cookie, 
	// then set them as the default settings
	setinitialrights( commaRights );
}


/*--------------------------------------------------
	This function is called when entering into the 
	group rights screen when adding a control. When
	adding it does NOT need to consider database
	rights to be present.
--------------------------------------------------*/
function setinitialaddgrouprights( )
{
	// attempt to get existing cookie values for this group
	// NOTE: passing type=2 indicates to look for a group
	if( !getusercookierights( 2, account_id ) )
		return;

	// if rights have been found for this user in the cookie, 
	// then set them as the default settings
	setinitialrights( commaRights );
}


/*--------------------------------------------------
	This function is called when entering into the 
	account rights screen when modifying a control
	record as it considers the potential of database
	rights to be present.
--------------------------------------------------*/
function setinitialmodifyaccountrights( )
{
	var rights_str = new String;

	// If cookie values have been set for this user, then 
	// use them to set the initial values
	if( getusercookierights( 1, account_id ) )
		rights_str = commaRights;
	
	// otherwise, use the current database values
	else
		rights_str = account_rights;

//	alert( rights_str );

	// Set the rights fields based on either the database or cookie values
	setinitialrights( rights_str );
}


/*--------------------------------------------------
	This function is called when entering into the 
	group rights screen when modifying a control
	record as it considers the potential of database
	rights to be present.
--------------------------------------------------*/
function setinitialmodifygrouprights( )
{
	var rights_str = new String;

	// If cookie values have been set for this group, then 
	// use them to set the initial values
	if( getusercookierights( 2, account_id ) )
		rights_str = commaRights;

	// otherwise, use the current database rights for this group
	else
		rights_str = account_rights;

	// Set the rights fields based on either the database or cookie values
	setinitialrights( rights_str );
}