<%@  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("FwResources"); 
   c.stringTable("DevResources"); %>
<HTML>
<HEAD>
<TITLE><%= c.toHTML(c.string("RBSManagement.AssignUsersTitle")) %></TITLE>
<LINK href="<%= c.getModulesUrl() + "/dev/css/hf_style.css" %>" rel="styleSheet" type="text/css">
<jsp:include page='<%= c.getPath("dev/eMFrameScripts_inc.jsp") %>' flush="true" />
<jsp:include page='<%= c.getPath("dev/OSScripts_inc.jsp") %>' flush="true" />
<SCRIPT language="JavaScript">
	
   // Global variables
   var deleteItemsArray 	= new Array();
   var rowCheckBoxChecked	= new Boolean(false);
   var memberArray			= new Array();
   
   function checkNullInput()
   {
      if (document.AssignUsersForm.elements["Role.Member.Count"].value == 0)
      {
         var message = "<%= c.toScript(c.string("AssignUsers.NoMembersAssigned1")) %>";
         message += "\n";
         message += "<%= c.toScript(c.string("AssignUsers.NoMembersAssigned2")) %>";
         
         if (!confirm(message))
         {
         	setFocusToFormElement();
         	return false;
         }
         else
         {
            // Go to summary, dont show scope page since there is no assigned members
            document.AssignUsersForm.elements["merge"].value="fw.CrtRoleSummary";
            document.AssignUsersForm.elements["nextState"].value="getSummaryInfo";
            return true;
         }
      }
      
      return true;
   }


	function setFocusToFormElement()
   {
      document.AssignUsersForm.NewMemberName.focus();
   }
   
	
   function addButtonSelected()
   {
      var memberList 		= document.AssignUsersForm.elements["NewMemberList"].value;
      memberArray 			= memberList.split(";");
      var newMembers 		= document.AssignUsersForm.elements["NewMemberName"].value;
      var newMembersArray 	= newMembers.split("+");
      var duplicateName 	= false;
      var finalArray			= new Array();
      var count 				= 0;
      
      // Make sure the text box isn't empty
      if (newMembers.length > 0)
      {
         for (var i = 0; i < newMembersArray.length; i++)
         {
            // Compare to existing member list
            for (var j = 0; j < memberArray.length; j++)
            {
               if (newMembersArray[i].toUpperCase() == memberArray[j].toUpperCase())
		         {
                  alert("<%= c.toScript(c.string("AssignUsers.MemberAlreadyInTable")) %> " + newMembersArray[j]);
                  duplicateName = true;
                  setFocusToFormElement();
                  break;
		         }
            }
            
            if (duplicateName == false)
            {
               finalArray[count] = osEscape(newMembersArray[i]);
               count++;
            }
            else
            {
            	duplicateName = false; // reset the value
            }	
         }
	      
         if (finalArray.length > 0)
         {
            document.AssignUsersForm.elements["NewRoleMembers"].value = finalArray.join("$");
	         document.AssignUsersForm.elements["merge"].value="fw.AssignUsers";
			   document.AssignUsersForm.elements["nextState"].value="addMembersToTable";
			   document.AssignUsersForm.submit();
         }
      }
      else
      {
      	setFocusToFormElement();
      }
   }
   
  
   function removeButtonSelected()
   {
      var count = 0;
      													 
      // Get all of the checked items
      if (rowCheckBoxStatus() == true)
      {
         var elementCount = document.forms[0].elements.length;
         
         for (var i = 0; i < elementCount; i++)
	      {
            if (document.forms[0].elements[i].type == "checkbox")
           	{
	            if (document.forms[0].elements[i].name.search("RowCheckBox") != -1)
		         {
		            if (document.forms[0].elements[i].checked)
		            {
	                  var memberName = document.forms[0].elements[i].value;
                     deleteItemsArray[count] = osEscape(memberName) + "$"; 
                     count++;
	               }
		         }
            }
	      }
         
         if (deleteItemsArray != null && deleteItemsArray.length > 0)
         {
	         document.AssignUsersForm.elements["DeleteMemberList"].value = deleteItemsArray.join("$");
	         document.AssignUsersForm.elements["DeleteMemberListCount"].value = count;
	         
	         // make the changes to DS and return
		      document.AssignUsersForm.elements["merge"].value="fw.AssignUsers";
		      document.AssignUsersForm.elements["nextState"].value="deleteMembersFromWIzardTable";
		      document.AssignUsersForm.submit();
         }
      }
      else
      {
      	alert("<%= c.toScript(c.string("AssignUsers.NoObjectsSelected")) %>");
      }
   }

   
   function previousButtonSelected()
   {
      document.AssignUsersForm.elements["merge"].value="fw.ChooseTasks";
      document.AssignUsersForm.elements["nextState"].value="getTasksForRole";
      document.AssignUsersForm.submit();
   }
   
   
   // Returns true if any row checkboxes are checked
   function rowCheckBoxStatus()
   {
   	var elementCount 	= document.forms[0].elements.length;
      
      for (var i = 0; i < elementCount; i++)
      {
        	if (document.forms[0].elements[i].type == "checkbox")
         {
         	if (document.forms[0].elements[i].name.search("RowCheckBox") != -1)
	         {
               if (document.forms[0].elements[i].checked)
	            {
	               return true;
	            }										
	         }
         }
      }
      
      return false;
   }


	function setRowCheckBoxValues(checkBoxValue)
   {
   	var elementCount 	= document.forms[0].elements.length;
      
      // Set all rowCheckBoxes to true
      for (var i = 0; i < elementCount; i++)
      {
      	if (document.forms[0].elements[i].type == "checkbox")
         {
	         if (document.forms[0].elements[i].name.search("RowCheckBox") != -1)
	         {
	            document.forms[0].elements[i].checked = checkBoxValue;
	         }
         }
      }
   }
 
   
   function osEscape(s)
   {
   	var r = escape(s,1);
      
      return r.replace(/\+/g,"%2B");
   }
   

</SCRIPT>
</HEAD>
<BODY TEXT="#000000" LINK="#000000" VLINK="#000000" ALINK="#000000" bgcolor="#FFFFFF" onload="returnFromOS();setFocusToFormElement();">
<FORM name="AssignUsersForm" method=post action="webacc" onsubmit="return(checkNullInput())">
   <input type=hidden name="taskId" value="<%= c.var("taskId") %>">
   <input type=hidden name="merge" value="fw.AssignScopes">
   <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="getScopeInfo">
   <input type=hidden name="Role.Member.Count" value="<%= c.var("Role.Member.Count") %>">
   <input type=hidden name="DeleteMemberList" value="">
   <input type=hidden name="DeleteMemberListCount" value="">
   <input type=hidden name="RowCheckBoxStatus" value="false">
   <input type=hidden name="CreateRolePreviousPage" value="AssignUsers">
   <input type=hidden name="NewRoleMembers" value="">
   <input type=hidden name="NewMemberList" value="<%= c.toHTML(c.var("NewMemberList")) %>">
   <!-- End of hidden tags -->

<% if (session.getAttribute("DeviceType").equals("browser")) { %>
	<DIV id="Layer2" style="position:relative; left:5px; top:4px;">   
<% } %>   
	<TABLE width="100%" border="0" cellspacing="0" cellpadding="3">
      <TR>
	     	<TH bgcolor="#458ab9" colspan="7" height="19">
	      	<div align="left" class="formhead1">
		      	<font color="#FFFFFF"><B><%= c.toHTML(c.string("CreateRole.WizardTitle")) %></B></font>
	         </DIV>
		   </TH>
      </TR>
      <TR bgcolor="#EFEEE9"> 
      	<TD height="16" colspan="5" align="center" valign="middle">
         	<p align="left">&nbsp;<IMG src="<%= c.getModulesUrl() + "/fw/images/wizard_on.gif" %>" width="24" height="23" align="middle"></P>
         </TD>
         <TD height="16" width="33%" align="left" valign="middle" class="tablesubhead1"><B><%= c.toHTML(c.string("AssignUsers.Associate")) %></B></TD>
      	<TD height="16" width="62%" align="left" valign="middle" class="helptext">&nbsp;</TD>
   	</TR>
	</TABLE>
   <BR>
   <TABLE width="100%" border="0" cellspacing="0" cellpadding="3">
   	<TR bgcolor="#FFFFFF"> 
	      <TD colspan="4" nowrap valign="top"><b><%= c.toHTML(c.string("AssignUsers.RoleName")) %></b>&nbsp;<%= c.toHTML(c.var("RoleDisplayName")) %></TD>
	   </TR>
	   <TR bgcolor="#FFFFFF"> 
	      <TD colspan="4" nowrap valign="top">&nbsp;</TD>
	   </TR>
	   <TR bgcolor="#FFFFFF"> 
	      <TD colspan="4" nowrap valign="top" class="instructions"><%= c.toHTML(c.string("AssignUsers.RoleMustHave")) %><BR><%= c.toHTML(c.string("AssignUsers.EnterOrBrowse")) %></TD>
	   </TR>
      <TR bgcolor="#FFFFFF">
      	<TD colspan="4" nowrap valign="top" height="9"><BR><%= c.toHTML(c.string("AssignUsers.ObjectName")) %></TD>
      </TR>
      <TR bgcolor="#FFFFFF">
      	<DIV align="left">
         	<TD colspan="4" nowrap valign="top" height="9">
			      <input type=text name="NewMemberName" value="" size=<% if (c.var("Browser.name").equals("NSN") && c.var("Browser.mjrVer").equals("4")) { %>18<% } else { %>35<% } %> maxlength=64>
			   	<% c.set("OS.TypeFilter", "domain,Organization,Organizational Unit,User,Group"); 
			   	   c.set("OS.Control", "NewMemberName"); 
			         if (session.getAttribute("DeviceType").equals("browser")) {
               	   c.set("OS.MultiSelect", "true"); 
                  } %>
			   	<jsp:include page='<%= c.getPath("dev/OS_inc.jsp") %>' flush="true" />
			      <a href="#" onClick="javascript:addButtonSelected();return false"><IMG src="<%= c.getModulesUrl() + "/dev/images/" + c.string("Button.Add.image") %>" alt="<%= c.toHTML(c.string("Button.Add.alt")) %>" border=0></a>
            </TD>
   		</DIV>
      </TR>
  	</TABLE>
   <P>
   <TABLE width="100%" border="0" cellspacing="0" cellpadding="3">
   	<TR>
      	<TD colspan="4">
         	<DIV align="left"> &nbsp;
            	<a href="#" onClick="javascript:removeButtonSelected();return false"><IMG src="<%= c.getModulesUrl() + "/dev/images/" + c.string("Button.RemoveSmall.image") %>" alt="<%= c.toHTML(c.string("Button.RemoveMember.alt")) %>" border=0></a>
            </DIV>
         </TD>
      </TR>
      <TR  bgcolor="#DFDDD5" > 
	      <TD width="10%" height="24"> 
	        <DIV align="center"> 
	          <input type="checkbox" name="NameCheckBox" value="checkbox" onclick="setRowCheckBoxValues(this.checked)">
	        </div>
	      </TD>
	      <TD width="50%" height="24" class="tablecolumnhead1"> 
	        <DIV align="left"><font color="#000000"><b><%= c.toHTML(c.string("AssignUsers.Name")) %></b></font></DIV>
	      </TD>
	      <TD width="20%" height="24">&nbsp;</TD>
	      <TD width="20%" height="24">&nbsp;</TD>
    	</TR>
   
      <!-- Load table info -->
      <% c.set("i", "0"); 					  
   	   c.set("color", "#FFFFFF"); 
         c.set("PreviousName", ""); 
         if (c.var("Role.Member.Count").equals("0")) { %>
      	<TD colspan="4">
	      	<DIV align="center"> 
	        		<%= c.toHTML(c.string("AssignUsers.NoTableEntry")) %>
	         </DIV>
			</TD>
      <% } 
         while (c.lt(c.var("i"), c.var("Role.Member.Count"))) { %>
      <TR bgcolor=<%= c.var("color") %>>
	      <% if (c.var("Role.Member."+c.var("i")+ ".RowId").equals("") == false) { 
         	   if (c.var("Role.Member."+c.var("i")+ ".FullName").equals(c.var("PreviousName")) == false) { 
		            c.set("PreviousName", c.var("Role.Member."+c.var("i")+ ".FullName")); %>
               <TD width="10%" height="17">
               	<DIV align="center"> 
			        		<input type="checkbox" name="RowCheckBox<%= c.var("i") %>" width="16" height="16" value="<%= c.toHTML(c.var("Role.Member."+c.var("i")+ ".FullName")) %>">
                  </DIV>
			      </TD>
	            <TD width="50%" height="17">
               	<DIV align="left"><%= c.toHTML(c.var("Role.Member."+c.var("i")+ ".FullName")) %></DIV>
	            </TD>
	            <TD width="20%" height="17">&nbsp;</TD>
	      		<TD width="20%" height="17">&nbsp;</TD>
            <% } 
            } %>
	   </TR>
   	<% c.inc("i"); 
         if (c.var("color").equals("#FFFFFF")) { 
      	   c.set("color", "#EFEEE9"); 
         } else { 
      	   c.set("color", "#FFFFFF"); 
         } 
   	   } %>
	</TABLE>
   <BR>
<% if (session.getAttribute("DeviceType").equals("browser")) { %>
	</DIV>
   <DIV id="Layer1" style="position:relative; left:6px; top:480px;"> 
<% } %> 
   <TABLE width="100%" border="0" cellspacing="0" cellpadding="0" align="left">
  		<TR> 
	      <TD colspan="2"> 
	        <HR noshade size="2">
	      </TD>
    	</TR>
      <TR>
      	<TD>
         	<a href="webacc?taskId=<%= c.var("taskId") %>&merge=fw.HomePage&User.context=<%= c.var("User.context") %>"><IMG name="Cancel" alt="<%= c.toHTML(c.string("Button.Cancel.alt")) %>" src="<%= c.getModulesUrl() + "/dev/images/" + c.string("Button.Cancel.image") %>" border=0></a>
            <a href="#" onClick="javascript:previousButtonSelected();return false"><IMG src="<%= c.getModulesUrl() + "/dev/images/" + c.string("Button.Back.image") %>" alt="<%= c.toHTML(c.string("Button.Back.alt")) %>" border=0></a>
   			<INPUT type=image name="Next" alt="<%= c.toHTML(c.string("Button.Next.alt")) %>" src="<%= c.getModulesUrl() + "/dev/images/" + c.string("Button.Next.image") %>" border=0>
         </TD>
      </TR>
   </TABLE>
<% if (session.getAttribute("DeviceType").equals("browser")) { %>
	</DIV>
<% } %>
</FORM>
<jsp:include page='<%= c.getPath("dev/OSFooter_inc.jsp") %>' flush="true" />
</BODY></HTML>
