<%@  page pageEncoding="utf-8" contentType="text/html;charset=utf-8"
   import="
         java.util.List,
         java.util.Iterator,
         com.novell.emframe.fw.ContentData,
         com.novell.emframe.dev.dataTable.TableCell,
         com.novell.emframe.dev.dataTable.IconTableCell,
         com.novell.emframe.dev.dataTable.TextTableCell,
         com.novell.webaccess.common.JSPConduit"
 %>

<%
   JSPConduit c = JSPConduit.getJSPConduit(request);
   c.stringTable("DevResources");
   c.stringTable("FwResources");
%>

<%@ taglib uri="/WEB-INF/iman.tld" prefix="iman" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>

<iman:stringtable bundle="FwResources"/>
<iman:stringtable bundle="DevResources"/>

<HTML>
   <HEAD>
      <iman:eMFrameScripts/>
      <iman:dataTableScripts/>
      <iman:stylesheet/>
      <SCRIPT>

         /*******************
          * FUNCTION HEADER *
          *******************/
         function contentEventHandler(menuItemId, selectedIndices, tableData)
         {
            if(menuItemId=="AddContent")
            {
               addContent();
            }
            else if(menuItemId=="RemoveContent")
            {
               removeContent(selectedIndices, tableData);
            }

            return;
         }

         /*******************
          * FUNCTION HEADER *
          *******************/
         function addContent()
         {
            window.location = "frameservice?NPService=fw.LaunchService" +
               "&NPAction=Delegate" +
               "&delegate=fw.Modify Role Task" +
               "&launcher=<c:out value="${taskId}"/>" +
               "&returnID=<c:out value="${taskId}"/>" +
               "&lifecycle=Recreate" +
               "&SelectedRoleFullName=" + urlEncodePack("<c:out value="${SelectedRoleFullName}"/>") +
               "&SelectedRoleDisplayName=" + urlEncodePack("<c:out value="${SelectedRoleDisplayName}"/>") +
               "&RBSAdmin_RoleType=<c:out value="${RBSAdmin_RoleType}"/>" +
               "&RBSAdmin_TaskDelegate=taskDelegate" +
               "&merge=fw.ModifyRoleTasks" +
               "&nextState=setupModifyTasksPage";
         }

	      function urlEncodePack(stringToPack)
	      {
	      	var encodedStr = urlEncode(stringToPack);
	         var packedArray = new Array(1);   
	         packedArray[0] =  encodedStr;
	         return pack(packedArray);
	     }

         /*******************
          * FUNCTION HEADER *
          *******************/
         function removeContent(selectedIndices, tableData)
         {
            if(selectedIndices.length > 0)
            {
               var tasksToKeep = new Array(tableData.length-selectedIndices.length);
               var count = 0;

               for(var x=0; x < tableData.length; x++)
               {
                  var found = false;
                  for(var i=0; i < selectedIndices.length; i++)
                  {
                     var y = selectedIndices[i];
                     if(x == y)
                     {
                        found = true;
                        break;
                     }
                  }

                  if(found == false)
                  {
                     tasksToKeep[count++] = tableData[x][1];
                  }
               }

               if(confirm("<%= c.string("RBS.RemoveContent") %>"))
               {
                  var form = document.ContentMain;
                  form.nextState.value = "removeTasksFromRole";
                  form.merge.value = "dev.GenMsg";
                  form.error.value = "dev.GenErr";
                  form.TasksToKeepPacked.value = pack(tasksToKeep);
                  form.elements["GeneralMsg.Url"].value = "webacc?taskId=<c:out value="${taskId}"/>"
                     + "&merge=fw.ContentMain"
                     + "&nextState=roleSelected"
                     + "&SelectedRoleDisplayName=" + urlEncode("<c:out value="${SelectedRoleDisplayName}"/>")
                     + "&SelectedRoleFullName=" + urlEncode("<c:out value="${SelectedRoleFullName}"/>");

                  form.submit();
               }
            }
            else
            {
            alert("<%= c.string("RBS.CheckOneOrMoreObjectsToPerformOperation") %>");
            }
         }

         /*******************
          * FUNCTION HEADER *
          *******************/
         function backToCollectionList()
         {
            document.ContentMain.nextState.value = "backToCollectionList";
            document.ContentMain.merge.value = "fw.CollectionMain";
            document.ContentMain.submit();
         }

         /*******************
          * FUNCTION HEADER *
          *******************/
         function backToRoleList()
         {
            document.ContentMain.nextState.value = "backToRoleList";
            document.ContentMain.merge.value = "fw.RoleMain";
            document.ContentMain.submit();
         }

      /*******************
       * FUNCTION HEADER *
       *******************/
      function objectSortHandler(tableName, column, direction, tableData)
      {
         var form = document.ContentMain;
         form.nextState.value = "contentSort";
         form.sortContentColumn.value = column;
         form.sortContentDirection.value = direction;
         form.merge.value = "fw.ContentMain";
         form.submit();
      }

      </SCRIPT>
   </HEAD>
   <BODY>
      <FORM name="ContentMain">
         <INPUT type="hidden" name="nextState">
         <INPUT type="hidden" name="SelectedRoleDisplayName" value="<c:out value="${SelectedRoleDisplayName}"/>">
         <INPUT type="hidden" name="SelectedRoleFullName" value="<c:out value="${SelectedRoleFullName}"/>">
         <INPUT type="hidden" name="RBSAdmin_RoleType">
         <INPUT type="hidden" name="taskId" value="<c:out value="${taskId}"/>">
         <INPUT type="hidden" name="merge">
         <INPUT type="hidden" name="error" value="dev.GenErr">
         <INPUT type="hidden" name="TasksToKeepPacked">
         <input type="hidden" name="GeneralMsg.Url" value="">
   <INPUT type="hidden" name="sortContentColumn" value="<c:out value="${sortContentColumn}"/>">
   <INPUT type="hidden" name="sortContentDirection" value="<c:out value="${sortContentDirection}"/>">

         <div class="smalltext" style="margin-bottom: 1em">
            <a href="#" onClick="backToCollectionList()"><iman:string key="RBS.Configuration"/></a><img src="/nps/portal/modules/dev/images/breadspace.gif" width="17" height="9" border="0" align="absmiddle" alt=""><a href="#" onClick="backToRoleList()"><iman:string key="RBS.Collection"/></a>
            <img src="/nps/portal/modules/dev/images/breadspace.gif" width="17" height="9" border="0" align="absmiddle" alt=""><iman:string key="RBS.Role"/>
         </div>
         <%
            c.set("taskHeaderTitle", c.string("RBSManagement.RoleWithColon") + " " + c.var("SelectedRoleDisplayName"));
         %>
         <iman:taskHeader title="${taskHeaderTitle}" iconUrl="dev/dir/rbsRole2.gif" helpFile="fw/contentmain.html" /><BR>
         <iman:include page="dev/Messagebar_inc.jsp"/>
         <div class="formdescriptextml0" style="width: 80%"><iman:string key="RBS.RoleInstructions"/></div>

         <iman:dataTable id="myTable" titleKey="RBS.Content" tableData="${tableData}" menuActionHandler="contentEventHandler" sortHandler="objectSortHandler" sortedColumn="${sortContentColumn}" sortedDirection="${sortContentDirection}">
            <iman:menuItem textKey="RBS.Add" id="AddContent"/>
            <iman:menuItem textKey="RBS.Remove" id="RemoveContent"/>

            <iman:columnHeader textKey="RBS.Type"/>
            <iman:columnHeader textKey="RBS.Name"/>
            <iman:columnHeader textKey="RBS.InitialVersion"/>
            <iman:columnHeader textKey="RBS.ExpiredVersion"/>
         </iman:dataTable>

         <iman:spacer/>
         <iman:bar/>
         <iman:button key="Close" onClick="backToRoleList(); return false;"/>
      </FORM>
   </BODY>
</HTML>
