//  ****************             Rights Management              **********************
//	****************	AW Merged Version of Ian & Geale's Code	**********************
//  
//  The array rmRights is rendered by the Openwork.SharePoint.WebControls.AccessRightsStyleControl
//

function Custom_AddListMenuItems(m, ctx)
{
    var rmList = ctx.listName;  //GUID
    
    if (ctx.listBaseType == BASETYPE_DISCUSSION)
    {
		//can you edit/post reply
		if (typeof(rmRights) == "undefined"  || typeof(rmRights['Edit'][rmList]) == "undefined"  || rmRights['Edit'][rmList] == true)
		{
			strDisplayText = L_Reply_Text;
			if(itemTable.Ordering.length>=504) 
			{
				var L_ReplyLimitMsg_Text="Cannot reply to this thread. The reply limit has been reached.";
				strAction="alert('" + L_ReplyLimitMsg_Text + "')";
			}
			else
			{
				strAction = "STSNavigate('" + ctx.newFormUrl 
				+ "?Threading=" + escapeProperly(itemTable.Ordering)
				+ "&Guid=" + escapeProperly(itemTable.ThreadID)
				+ "&Subject=" + escapeProperly(itemTable.Subject)
				+ "&Source=" + GetSource() + "')";
			}
			strImagePath = ctx.imagesPath + "reply.gif";
			CAMOpt(m, strDisplayText, strAction, strImagePath);
		}
    }
    strDisplayText = L_ViewItem_Text;
    strAction = "STSNavigate('" + ctx.displayFormUrl+"?ID="+ currentItemID + "&Source=" +
                GetSource() + "')";
    strImagePath = "";
    CAMOpt(m, strDisplayText, strAction, strImagePath);
    
    if ((ctx.listTemplate != 1150 && ctx.listTemplate !=1151) && (typeof(rmRights) == "undefined"  || typeof(rmRights['Edit'][rmList]) == "undefined"  || rmRights['Edit'][rmList] == true))
    {
		strDisplayText = L_EditItem_Text;
		strAction = "STSNavigate('" + ctx.editFormUrl+"?ID="+ currentItemID + "&Source=" +
					GetSource() + "')";
		strImagePath = ctx.imagesPath + "edititem.gif";
		CAMOpt(m, strDisplayText, strAction, strImagePath);
    }
    if ((ctx.listTemplate == 1150) && (typeof(rmRights) == "undefined" || typeof(rmRights['Edit'][rmList]) == "undefined"  || rmRights['Edit'][rmList] == true))
    {
    	strDisplayText = "Reply";
		strAction = "STSNavigate('" + ctx.HttpRoot + "/Messages%20Sent/NewForm.aspx?rcvID=" + currentItemID + "&Source=" +
					GetSource() + "')";
		strImagePath = ctx.imagesPath + "edititem.gif";
		CAMOpt(m, strDisplayText, strAction, strImagePath);
    }
    if ((ctx.listTemplate == LISTTEMPLATE_EVENTS ||  ctx.listTemplate == 10600) &&
        currentItemID.indexOf(".0.") > 0)
    {
		//check if edit is allowed
		if (typeof(rmRights) == "undefined" || typeof(rmRights['Edit'][rmList]) == "undefined" || rmRights['Edit'][rmList] == true)
		{	
			var SeriesIdEnd = currentItemID.indexOf(".0.");
			var itemSeriesID = currentItemID.substr(0, SeriesIdEnd);
			strDisplayText = L_EditSeriesItem_Text;
			strAction = "STSNavigate('" + ctx.editFormUrl+"?ID="+ itemSeriesID + "&Source=" +
						GetSource() + "')";
			strImagePath = ctx.imagesPath + "recur.gif";
			CAMOpt(m, strDisplayText, strAction, strImagePath);
		}
    }
    if (currentItemID.indexOf(".0.") < 0)
    {
		//check if delete is allowed
		if (typeof(rmRights) == "undefined" || typeof(rmRights['Delete'][rmList]) == "undefined" || rmRights['Delete'][rmList] == true)
		{	
			strDisplayText = L_DeleteItem_Text;
			strAction = "DeleteListItem()";
			strImagePath = ctx.imagesPath + "delitem.gif";
			CAMOpt(m, strDisplayText, strAction, strImagePath);
		}
    }
    
    //CR478 - Add Delete Attachments link for Messages received - START 
    if ((ctx.listTemplate == 1150) && (typeof(rmRights) == "undefined" || typeof(rmRights['Edit'][rmList]) == "undefined"  || rmRights['Edit'][rmList] == true))
    {
			//Get the current url, encode it, and pass it to the query string as the Referrer parameter
			var currentHref = escape( window.location.href );
    
    	strDisplayText = "Delete Attachments";
			strAction = "STSNavigate('" + ctx.HttpRoot + "/_layouts/Openwork.SharePoint.Web/DeleteAttachments.aspx?ID=" + currentItemID + "&MessagesType=MessagesReceived&Referrer=" + currentHref + "')";
			strImagePath = ctx.imagesPath + "attachtb.gif";
			CAMOpt(m, strDisplayText, strAction, strImagePath);
    }
    //CR478 - Add Delete Attachments link for Messages received - END     
    
    //CR478 - Add Delete Attachments link for Messages sent - START 
    if ((ctx.listTemplate == 1151) && (typeof(rmRights) == "undefined" || typeof(rmRights['Edit'][rmList]) == "undefined"  || rmRights['Edit'][rmList] == true))
    {
			//Get the current url, encode it, and pass it to the query string as the Referrer parameter
			var currentHref = escape( window.location.href );

    	strDisplayText = "Delete Attachments";
			strAction = "STSNavigate('" + ctx.HttpRoot + "/_layouts/Openwork.SharePoint.Web/DeleteAttachments.aspx?ID=" + currentItemID + "&MessagesType=MessagesSent&Referrer=" + currentHref + "')";
			strImagePath = ctx.imagesPath + "attachtb.gif";
			CAMOpt(m, strDisplayText, strAction, strImagePath);
    }
    //CR478 - Add Delete Attachments link for Messages sent - END 
    
    
    
    
    if (ctx.listTemplate == LISTTEMPLATE_CONTACTS || ctx.listTemplate == 10200)
    {
        strDisplayText = L_ExportContact_Text;
        strAction = "STSNavigate('" + ctx.HttpPath + "&Cmd=Display&CacheControl=1&List=" + ctx.listName + "&ID=" +  currentItemID + "&Using=" + escapeProperly(ctx.listUrlDir) + "/vcard.vcf" + "')";
        strImagePath = ctx.imagesPath + "exptitem.gif";
        CAMOpt(m, strDisplayText, strAction, strImagePath);
    }
    if (currentItemID.indexOf(".0.") < 0 && ctx.listTemplate != 1150 && ctx.listTemplate !=1151)
    {
		strDisplayText = L_Subscribe_Text;
		strAction = "NavigateToSubNewAspx('" + ctx.HttpRoot + "', 'List=" + ctx.listName + "&ID=" + currentItemID +"')";
		strImagePath = "";
		CAMOpt(m, strDisplayText, strAction, strImagePath);
    }
    if (ctx.isModerated == true &&
        ctx.listBaseType != BASETYPE_SURVEY)
    {
		//check if modify rights
		if (typeof(rmRights) == "undefined" || typeof(rmRights['Modify'][rmList]) == "undefined" || rmRights['Modify'][rmList] == true)
		{	
			strDisplayText = L_ModerateItem_Text;
			strAction = "STSNavigate('" + ctx.editFormUrl+"?ID="+ currentItemID + "&ChangeApproval=TRUE&Source=" +
						GetSource() + "')";
			strImagePath = "";
			CAMOpt(m, strDisplayText, strAction, strImagePath);
        }
    }
    
    return true;
}

function Custom_AddDocLibMenuItems(m, ctx)
{
    var RootFolder = GetRootFolder(ctx);
    
	setupMenuContext(ctx);
	if (currentItemFileUrl == null)
		currentItemFileUrl = itemTable.ServerUrl;
	if (currentItemFSObjType == null)
		currentItemFSObjType = itemTable.FSObjType;
	var currentItemEscapedFileUrl = escapeProperly(
		unescapeProperly(currentItemFileUrl));
	if (currentItemFSObjType != 1) 
	{
		strDisplayText = L_ViewProperties_Text;    
		strAction = "STSNavigate('" + ctx.displayFormUrl+"?ID="+ currentItemID + 
					"&Source=" + GetSource() + RootFolder + "')";
		strImagePath = "";
		CAMOpt(m, strDisplayText, strAction, strImagePath);
	}

    var rmList = ctx.listName;
	if (typeof(rmRights) == "undefined" || typeof(rmRights['Edit'][rmList]) == "undefined"  || rmRights['Edit'][rmList] == true)
	{    			
		strDisplayText = L_EditProperties_Text;    
		strAction = "STSNavigate('" + ctx.editFormUrl+"?ID="+ currentItemID + 
				"&Source=" + GetSource() + RootFolder + "')";
		strImagePath = ctx.imagesPath + "edititem.gif";
		CAMOpt(m, strDisplayText, strAction, strImagePath);
		EditItemAlreadyinMenu = true;
	}	
	if (currentItemFSObjType != 1) 
	{
		if (ctx.isWebEditorPreview == 0)
		{
			if (ctx.listTemplate == LISTTEMPLATE_IMAGE_LIBRARY && itemTable.IsImage == "1")
			{
				//Check edit permissions
				if (typeof(rmRights) == "undefined" || typeof(rmRights['Edit'][rmList]) == "undefined"  || rmRights['Edit'][rmList] == true)
				{    
					strDisplayText = L_EditInOIS_Text;
					strAction = "EditSingleImage('" + currentItemID  + "')";
					strImagePath = ctx.imagesPath + "oisweb.gif";
					CAMOpt(m, strDisplayText, strAction, strImagePath);
				}
			}
			else
			{
				setDocType();
				if (currentItemAppName != "" && currentItemOpenControl != "")
				{
					//Check edit permissions
					if (typeof(rmRights) == "undefined" || typeof(rmRights['Edit'][rmList]) == "undefined"  || rmRights['Edit'][rmList] == true)
					{    
						strDisplayText = StBuildParam(L_EditIn_Text, currentItemAppName);
						strAction = "editDocumentWithProgID2('" + currentItemFileUrl + "', '" + currentItemProgId + "', '" + currentItemOpenControl + "')";
						strImagePath = ctx.imagesPath + currentItemIcon;
						CAMOpt(m, strDisplayText, strAction, strImagePath);
					}
				}
			}
		}
	}
	//check if delete is allowed
	if (typeof(rmRights) == "undefined" || typeof(rmRights['Delete'][rmList]) == "undefined"  || rmRights['Delete'][rmList] == true)
	{    
		strDisplayText = L_DeleteDocItem_Text;
		strAction = "DeleteDocLibItem('" + 
				ctx.HttpPath + "&Cmd=Delete&List=" + ctx.listName + 
				"&ID=" + currentItemID + "&owsfileref=" +
				currentItemEscapedFileUrl + "&NextUsing=" + GetSource() + "')";
		strImagePath = ctx.imagesPath + "delitem.gif";
		CAMOpt(m, strDisplayText, strAction, strImagePath);
	}
	
	if ((ctx.isModerated == true) && (typeof(rmRights) == "undefined" || typeof(rmRights['Modify'][rmList]) == "undefined"  || rmRights['Modify'][rmList] == true))
	{
		strDisplayText = L_ModerateItem_Text;
		strAction = "STSNavigate('" + ctx.editFormUrl+"?ID="+ currentItemID + "&ChangeApproval=TRUE&Source=" +
					GetSource() + RootFolder + "')";
		strImagePath = "";
		CAMOpt(m, strDisplayText, strAction, strImagePath);
	}
	if (currentItemFSObjType != 1 && ctx.listTemplate == LISTTEMPLATE_IMAGE_LIBRARY)
	{
		strAction = "DownloadOriginalImage(" + currentItemID + ")";
		strImagePath = ctx.imagesPath + "download.gif";
		strDisplayText = L_DownloadOriginal_Text;
		CAMOpt(m, strDisplayText, strAction, strImagePath);
	}
	if (currentItemFSObjType != 1) 
	{
		//Check edit permissions
		if (typeof(rmRights) == "undefined" || typeof(rmRights['Edit'][rmList]) == "undefined"  || rmRights['Edit'][rmList] == true)
		{
			CAMSep(m);
			AddCheckinCheckoutMenuItem(m, ctx, currentItemEscapedFileUrl);	
			AddVersionsMenuItem(m, ctx, currentItemEscapedFileUrl);
			CAMSep(m);
		}
		if (ctx.PortalUrl != null)
		{
			strDisplayText = L_AddToMyLinks_Text;
			strAction = "Portal_Tasks('PinToMyPage')"; ;
			strImagePath = "";
			CAMOpt(m, strDisplayText, strAction, strImagePath);

			//if you can add/edit/delete OR modify then allow publish to portal
			if (typeof(rmRights) == "undefined" 
				|| (typeof(rmRights['Add'][rmList]) == "undefined" || rmRights['Add'][rmList] == true)
				|| (typeof(rmRights['Edit'][rmList]) == "undefined" || rmRights['Edit'][rmList] == true)
				|| (typeof(rmRights['Delete'][rmList]) == "undefined" || rmRights['Delete'][rmList] == true)
				|| (typeof(rmRights['Modify'][rmList]) == "undefined" || rmRights['Modify'][rmList] == true))		
			{	

				strDisplayText = L_AddToCategory_Text;
				strAction = "Portal_Tasks('Categorize')"; ;
				strImagePath = "";
				CAMOpt(m, strDisplayText, strAction, strImagePath);
				CAMSep(m);
			}
		}
	}
	    
	strDisplayText = L_Subscribe_Text;
	strAction = "NavigateToSubNewAspx('" + ctx.HttpRoot + "', 'List=" + ctx.listName + "&ID=" + currentItemID + "')";
	strImagePath = "";
	CAMOpt(m, strDisplayText, strAction, strImagePath);
	
	if (currentItemFSObjType != 1) 
	{
		//Check edit permissions
		if (typeof(rmRights) == "undefined" || typeof(rmRights['Edit'][rmList]) == "undefined"  || rmRights['Edit'][rmList] == true)
		{
		
			strDisplayText = L_Discuss_Text;
			strAction = "STSNavigate('" + ctx.HttpPath + "&Cmd=COMMFRMS&URL=";
			if (ctx.isWebEditorPreview == 0)
				strAction += currentItemEscapedFileUrl;
			strAction += "')";
			strImagePath = ctx.imagesPath + "icdisc.gif";
			CAMOpt(m, strDisplayText, strAction, strImagePath);
		}
		
		if (ctx.listTemplate != LISTTEMPLATE_IMAGE_LIBRARY)
			//Check add/create permissions
			if (typeof(rmRights) == "undefined" || typeof(rmRights['Add'][rmList]) == "undefined"  || rmRights['Add'][rmList] == true)
			{
			// As per the requirements, no users are allowed to add a workspace, therefore it is commented out
        		//AddWorkspaceMenuItem(m, ctx);
        	}
	}
    
    // false means that the standard menu items should also be rendered
    return true; 
}

//	If the user does not have rights to perform tasks hide the controls
//  rmList = List.ID (Guid)
//  Rights = Add, Edit, Delete &  Modify
function rmHideControls(rmList, rights)
{
	if (!rmHasRights(rmList, rights))
	{
		var items = document.getElementsByName("RM_" + rights + "_" + rmList);
		if (items != null)
		{
			for(var i=0; i<items.length; i++)
			{
				items[i].style.display = 'none';
			}
		}
	}
}

function rmHasRights(rmList, rights)
{	
	return (typeof(rmRights) == "undefined" || typeof(rmRights[rights][rmList]) == "undefined"  || rmRights[rights][rmList] == true);
}

