<%@  page pageEncoding="utf-8" contentType="text/html;charset=utf-8" import="com.novell.webaccess.common.JSPConduit, com.novell.emframe.dev.eMFrameUtils" %>
<%   JSPConduit c = JSPConduit.getJSPConduit(request);
   c.stringTable("DevResources");
   c.stringTable("FwResources"); %>
<HTML>
<HEAD>
<TITLE><%= c.toHTML(c.string("")) %></TITLE>
<LINK   href="<%= c.getModulesUrl() + "/dev/css/hf_style.css" %>"
        rel="styleSheet"
        type="text/css">

<jsp:include page='<%= c.getPath("dev/MoveListItems_inc.jsp") %>' flush="true" />
<jsp:include page='<%= c.getPath("dev/eMFrameScripts_inc.jsp") %>' flush="true" />

<SCRIPT language="JavaScript">


    function detectBrowser()
    {
        var appVer = navigator.appVersion;
        var iePos = appVer.indexOf('MSIE');
        if (iePos != -1)
        {
            document.RoleCategoryForm.AvailableCategories.attachEvent('ondblclick',addSelectedItems);
            document.RoleCategoryForm.AssignedCategories.attachEvent('ondblclick',removeSelectedItems);
        }

        sortAvailList(document.RoleCategoryForm.AvailableCategories.options);
        sortAssignedList(document.RoleCategoryForm.AssignedCategories.options);
    }

   /*******************
    * FUNCTION HEADER *
    *******************/
    function onPageLoad()
    {
        if (document.RoleCategoryForm.AvailableCategories.length != 0)
        {
            document.RoleCategoryForm.AvailableCategories.focus();
            document.RoleCategoryForm.AvailableCategories[0].selected = true;
            document.RoleCategoryForm.AvailableCategories.selectedIndex = 0; // For Mozilla
            document.RoleCategoryForm.Description.value = parseDescription(document.RoleCategoryForm.AvailableCategories.options[0].value);
            document.RoleCategoryForm.Path.value = parseName(document.RoleCategoryForm.AvailableCategories.options[0].value);
        }
    }


   /*******************
    * FUNCTION HEADER *
    *******************/
   // Set the focus to only the selected list and avoid the problem of both lists showing selected items
    function setListFocus(list)
    {
        if (list.name == "AssignedCategories")
        {
            document.RoleCategoryForm.AvailableCategories.selectedIndex=-1;
        }
        else
        {
            document.RoleCategoryForm.AssignedCategories.selectedIndex=-1;
        }
    }

   /*******************
    * FUNCTION HEADER *
    *******************/
   function addSelectedItems(event)
   {
	   var availOpt      = document.RoleCategoryForm.AvailableCategories.options;
	   var optOpt        = document.RoleCategoryForm.AssignedCategories.options;
      var selectedNum   = 0;

      if (document.RoleCategoryForm.AvailableCategories.selectedIndex == -1)
      {
         // Nothing is selected in the All Tasks List when the user clicked on the add button. Just return.
         return;
      }

	   for (var i=(availOpt.length-1); i>=0; i--)
	   {
		   if(availOpt[i].selected)
		   {
            optOpt[optOpt.length] = new Option(availOpt[i].text, availOpt[i].value, false, availOpt[i].selected);
			   availOpt[i] = null;
            selectedNum = i;
		   }
	   }

      document.RoleCategoryForm.elements["RoleCategoryFormChanged"].value = "true";
      sortAssignedList(optOpt);
      setAvailableListFocus(availOpt,selectedNum);
   }

    /*******************
    * FUNCTION HEADER *
    *******************/
    function removeSelectedItems(event)
    {
       var availAttrOpt = document.RoleCategoryForm.AvailableCategories.options;
       var optAttrOpt   = document.RoleCategoryForm.AssignedCategories.options;
       var selectedNum  = 0;

       if (document.RoleCategoryForm.AssignedCategories.selectedIndex == -1)
       {
         // Nothing is selected in the Assigned Categories List when the user clicked on the remove button. Just return.
         return;
       }

       for (var i=(optAttrOpt.length-1); i>=0; i--)
       {
          if(optAttrOpt[i].selected)
          {
             availAttrOpt[availAttrOpt.length] = new Option(optAttrOpt[i].text, optAttrOpt[i].value, false, false);
             optAttrOpt[i] = null;
             selectedNum = i;
          }
       }

      document.RoleCategoryForm.elements["RoleCategoryFormChanged"].value = "true";
      sortAvailList(availAttrOpt);
      setAssignedListFocus(optAttrOpt,selectedNum);
    }

   /*******************
    * FUNCTION HEADER *
    *******************/
   function setAssignedListFocus(list,num)
   {
      if(list.length > 0)
      {
         if(list[num] == null)
         {
           document.RoleCategoryForm.AssignedCategories.selectedIndex = num - 1;
         }
         else
         {
           document.RoleCategoryForm.AssignedCategories.selectedIndex = num;
         }
         getDescription(document.RoleCategoryForm.AssignedCategories);
         getPath(document.RoleCategoryForm.AssignedCategories);
      }
      else
      {
         document.RoleCategoryForm.AvailableCategories.selectedIndex = 0;
         getDescription(document.RoleCategoryForm.AvailableCategories);
         getPath(document.RoleCategoryForm.AvailableCategories);
      }
   }

   /*******************
    * FUNCTION HEADER *
    *******************/
   function setAvailableListFocus(list,num)
   {
       if(list.length > 0)
       {
          if(list[num] == null)
          {
            document.RoleCategoryForm.AvailableCategories.selectedIndex = num - 1;
          }
          else
          {
            document.RoleCategoryForm.AvailableCategories.selectedIndex = num;
          }

          getDescription(document.RoleCategoryForm.AvailableCategories);
          getPath(document.RoleCategoryForm.AvailableCategories);
       }
       else
       {
         document.RoleCategoryForm.AssignedCategories.selectedIndex = 0;
         getDescription(document.RoleCategoryForm.AssignedCategories);
         getPath(document.RoleCategoryForm.AssignedCategories);
       }
    }

    function sortAssignedList(x)
    {
        var iePos = navigator.appVersion.indexOf('MSIE');
        sortedArray = new Array();

        for (var i=0; i<x.length; i++) {
            sortedArray[i] = new Array(x[i].text, x[i].value, false, false);
        }

        sortedArray.sort(sortBYtext);

        for (var i=0; i<x.length; i++) {
            x[i] = new Option(sortedArray[i][0], sortedArray[i][1], sortedArray[i][2], sortedArray[i][3]);
            if (iePos == -1)
                x[i].addEventListener("dblclick",removeSelectedItems, false);
            x[i].selected = sortedArray[i][3];
        }

        return;
    }


    function sortAvailList(x)
    {
        var iePos = navigator.appVersion.indexOf('MSIE');
        sortedArray = new Array();

        for (var i=0; i<x.length; i++) {
            sortedArray[i] = new Array(x[i].text, x[i].value, false, x[i].selected);
        }

        sortedArray.sort(sortBYtext);

      for (var i=0; i<x.length; i++)
      {
         x[i] = new Option(sortedArray[i][0], sortedArray[i][1], sortedArray[i][2], sortedArray[i][3]);
         if (iePos == -1)
         {
            x[i].addEventListener("dblclick",addSelectedItems, false);
            if (x[i].selected == true)
            {
               x.selectedIndex = i;
            }
         }
         x[i].selected = sortedArray[i][3];
      }

      return;
    }


    function sortBYtext(a,b)
    {
        if(a[0].toLowerCase() < b[0].toLowerCase())
        {
            return -1;
        }

        if(a[0].toLowerCase() > b[0].toLowerCase())
        {
            return 1;
        }

        return 0;
    }


   /*******************
    * FUNCTION HEADER *
    *******************/
    function getAssignedCategories()
    {
        var boxLength = document.RoleCategoryForm.AssignedCategories.length;

        // Get everything in the AssignedCategories list
        ShortName = new Array(boxLength);
        FullName = new Array(boxLength);
        Description = new Array(boxLength);

        for (i = 0; i < boxLength; i++)
        {
            if (document.RoleCategoryForm.AssignedCategories.options[i].text != emptyListBracket)
            {
                ShortName[i] = urlEncode(document.RoleCategoryForm.AssignedCategories.options[i].text);
                FullName[i] = urlEncode(parseName(document.RoleCategoryForm.AssignedCategories.options[i].value));
                Description[i] = urlEncode(parseDescription(document.RoleCategoryForm.AssignedCategories.options[i].value));
            }
        }

        // Convert array into delimited string to be passed into the java file
        document.RoleCategoryForm.elements["AssignedCategoryFullName"].value = FullName.join(";");
        document.RoleCategoryForm.elements["AssignedCategoryShortName"].value = ShortName.join(";");
        document.RoleCategoryForm.elements["AssignedCategoryDescription"].value = Description.join(";");

        return true;
    }


   /*******************
    * FUNCTION HEADER *
    *******************/
    function onPageSubmit()
    {
        if (document.RoleCategoryForm.AssignedCategories.length == 1)
        {
            if (document.RoleCategoryForm.AssignedCategories.options[0].text == emptyListBracket)
            {
                var message = "<%= c.toScript(c.string("RoleCategory.NoCategoryAssignment")) %>";
                message += "\n";
                message += "<%= c.toScript(c.string("BookSummary.Continue")) %>";

                if (!confirm(message))
                {
                    onPageLoad();
                    return false;
                }
            }
        }

        getAssignedCategories();

        document.RoleCategoryForm.submit();
    }


   /*******************
    * FUNCTION HEADER *
    *******************/
    function getDescription(list)
    {
        if ((list != null) && (list.selectedIndex != -1))
        {
            var desc = parseDescription(list.options[list.selectedIndex].value);
            document.RoleCategoryForm.Description.value = desc;
        }
    }


   /*******************
    * FUNCTION HEADER *
    *******************/
    function getPath(list)
    {
        if ((list != null) && (list.selectedIndex != -1))
        {
            var path = parseName(list.options[list.selectedIndex].value);
            document.RoleCategoryForm.Path.value = path;
        }
    }


   /*******************
    * FUNCTION HEADER *
    *******************/
    function parseDescription(nameAndDescription)
    {
        var nameDescArray = nameAndDescription.split("$");
        var desc = nameDescArray[1];
        if(desc == null)
        {
            return "";
        }

        return urlDecode(desc);
    }


   /*******************
    * FUNCTION HEADER *
    *******************/
    function parseName(nameAndDescription)
    {
        var nameDescArray = nameAndDescription.split("$");
        var path = nameDescArray[0];
        if(path == null)
        {
            return "";
        }

        return urlDecode(path);
    }


</SCRIPT>
</HEAD>
<BODY   TEXT="#000000" LINK="#000000" VLINK="#000000" ALINK="#000000" bgcolor="#FFFFFF"
        onload="onPageLoad(),detectBrowser();">

<FORM   name="RoleCategoryForm"
        method=post
        action="webacc">

    <input  type=hidden
            name="taskId"
            value="<%= c.var("taskId") %>">

    <input  type=hidden
            name="merge"
            value="dev.GenConf">

    <input  type=hidden
            name="error"
            value="dev.GenErr">

    <input  type=hidden
            name="User.context"
            value="<%= c.var("User.context") %>">

    <input  type=hidden
            name="nextState"
            value="saveRoleCategoryAssignment">

    <input  type=hidden
            name="AssignedCategoryFullName"
            value="">

   <input   type=hidden
            name="AssignedCategoryShortName"
            value="">

   <input   type=hidden
            name="AssignedCategoryDescription"
            value="">

    <input  type=hidden
            name="RoleCategoryFormChanged"
            value="false">

    <!-- End of hidden tags -->

    <% c.set("TaskHeader.title", c.toHTML(c.string("ModifyRoleCategoryAssignment")));
       c.set("TaskHeader.iconUrl", "dir/rbsRole2.gif");
       c.set("TaskHeader.helpFile", ""); %>
    <jsp:include page='<%= c.getPath("dev/TaskHeader_inc.jsp") %>' flush="true" /><BR>

    <TABLE name="Taskstable" >
        <tr>
            <td colspan="4" class="mediumtext"><B><%= c.toHTML(c.string("ModifyRoleCategory.RoleName")) %></B>&nbsp;<%= c.toHTML(c.var("RoleDisplayName")) %></td>
        </tr>
        <tr>
            <td colspan="4">&nbsp;</td>
        </tr>
        <TR>
            <td align="left" class="mediumtext"><%= c.toHTML(c.string("CreateRole.AvailableCategoriess")) %></td>
            <td align="middle">&nbsp</td>
            <td align="left" class="mediumtext"><%= c.toHTML(c.string("CreateRole.AssignedCategories")) %></td>
        </TR>
        <TR>
            <TD>
                <SELECT name="AvailableCategories" size=10 WIDTH="250" style="WIDTH: 250px"
                        multiple
                        onchange="getDescription(this);getPath(this);setListFocus(this)">
                    <% c.set("j", "0");
                       while (c.lt(c.var("j"), c.var("AvailableCategory.Count"))) { %>
                        <OPTION value="<%= c.urlEncode(c.var("AvailableCategories."+c.var("j")+ ".FullName")) %>$<%= c.urlEncode(c.var("AvailableCategories."+c.var("j")+ ".Description")) %>"><%= c.toHTML(c.var("AvailableCategories."+c.var("j")+ ".ShortName")) %>
                        <% c.inc("j");
                       } %>
                </SELECT>
            </TD>
            <TD>
                <a href="#" onClick="javascript:addSelectedItems();return false"><IMG src="<%= c.getModulesUrl() + "/dev/images/" + c.string("Button.ArrowRight.image") %>" alt="<%= c.toHTML(c.string("Button.ArrowRight.alt")) %>" title="<%= c.toHTML(c.string("Button.ArrowRight.alt")) %>" border=0></a>
                <BR>
                <a href="#" onClick="javascript:removeSelectedItems();return false"><IMG src="<%= c.getModulesUrl() + "/dev/images/" + c.string("Button.ArrowLeft.image") %>" alt="<%= c.toHTML(c.string("Button.ArrowLeft.alt")) %>" title="<%= c.toHTML(c.string("Button.ArrowLeft.alt")) %>" border=0></a>
                <BR>
            </TD>
            <TD>
                <SELECT name="AssignedCategories" size=10 WIDTH="250" style="WIDTH: 250px"
                        multiple
                        onchange="getDescription(this);getPath(this);setListFocus(this)">
                    <% c.set("i", "0");
                       while (c.lt(c.var("i"), c.var("AssignedCategory.Count"))) { %>
                        <OPTION value="<%= c.urlEncode(c.var("AssignedCategories."+c.var("i")+ ".FullName")) %>$<%= c.urlEncode(c.var("AssignedCategories."+c.var("i")+ ".Description")) %>"><%= c.toHTML(c.var("AssignedCategories."+c.var("i")+ ".ShortName")) %>
                        <% c.inc("i");
                       } %>
                </SELECT>
            </TD>
            <td width="100%"></td>
        </TR>
        <TR>
            <TD colspan="4" class="mediumtext">
                <BR><%= c.toHTML(c.string("BookRole.Path")) %><BR>
                <INPUT type="text" name="Path" value="" size=<% if (c.var("Browser.mjrVer").equals("4") && c.var("Browser.name").equals("NSN")) { %>37<% } else { %>74<% } %> maxlength=128 readonly disabled="true">
            </TD>
        </TR>
        <TR>
            <TD colspan="4" class="mediumtext">
                <BR><%= c.toHTML(c.string("BookRole.Description")) %><BR>
                <TEXTAREA name="Description" rows="3" cols="32" readonly></TEXTAREA>
            </TD>
        </TR>
        <tr>
            <td colspan="4">&nbsp;</td>
        </tr>
    </TABLE>
    <jsp:include page='<%= c.getPath("dev/Bar_inc.jsp") %>' flush="true" />
    <a href="#" onClick="javascript:onPageSubmit();return false">
    <IMG src="<%= c.getModulesUrl() + "/dev/images/" + c.string("Button.OK.image") %>" alt="<%= c.string("Button.OK.alt") %>" title="<%= c.string("Button.OK.alt") %>" border=0></a>
    <jsp:include page='<%= c.getPath("dev/Cancel_inc.jsp") %>' flush="true" />
</FORM>
</BODY></HTML>
