<%@  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("AssignScopes.AssignScopesTitle")) %></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 modifyScopeArray 			= new Array();
   var inheritanceArray 			= new Array();
   var inheritanceKeyArray 		= new Array();
   var inheritanceValueArray 		= new Array();
   var combinedInheritanceArray 	= new Array();
   var memberArray					= new Array();
   var myWindow;
   
   function checkNullInput()
   {
      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)
	         {
	            var memberScope	= parseScope(document.forms[0].elements[i].value);
	            
	            if (memberScope == null || memberScope == "" )
	            {
	               alert("<%= c.toScript(c.string("AssignScopes.MissingScope")) %>");
	               return false;
	            }
	         }
         }
      }
      
      return true;
   }


	function setFocusToFormElement()
   {
      document.AssignScopesForm.NewMemberScope.focus();
   }
   
    
   function removeScopeButtonSelected()
   {
      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		= parseName(document.forms[0].elements[i].value);
	                  var memberScope	= parseScope(document.forms[0].elements[i].value);
	                  
	                  if (memberScope != null && memberScope != "")
	                  {
	                     deleteItemsArray[count] = osEscape(memberName) + ";" + osEscape(memberScope) + "$"; 
		                  count++;
	                  }
	                  else
	                  {
	                  	deleteItemsArray[count] = osEscape(memberName) + ";" + "[no scope]" + "$"; 
		                  count++;
	                  }
	               }
               }
	         }
            
         }
         
         if (deleteItemsArray != null && deleteItemsArray.length > 0)
         {
	         document.AssignScopesForm.elements["DeleteMemberList"].value = deleteItemsArray.join("$");
	         document.AssignScopesForm.elements["DeleteMemberListCount"].value = count;
	         
	         // make the changes to DS and return
		      document.AssignScopesForm.elements["merge"].value="fw.AssignScopes";
		      document.AssignScopesForm.elements["nextState"].value="removeScopesFromTable";
		      document.AssignScopesForm.submit();
         }
      }
      else
      {
      	alert("<%= c.toScript(c.string("AssignScopes.SelectCheckbox")) %>");
      }
   }
   
   
   function previousButtonSelected()
   {
      document.AssignScopesForm.elements["merge"].value="fw.AssignUsers";
      document.AssignScopesForm.elements["nextState"].value="getUsersAndScopes";
      document.AssignScopesForm.submit();
   }
   
   
   function addButtonSelected()
   {
      // Check to see if the object already exists in the table
      var updateTable 	= false;
      var elementCount 	= document.forms[0].elements.length;
      var newScope		= document.AssignScopesForm.elements["NewMemberScope"].value;
      var count			= 0;
      
      if (newScope != "")
      {
      	if (rowCheckBoxStatus() == 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)
			         {
	                  if (document.forms[0].elements[i].checked)
			            {
		                  var memberName		= parseName(document.forms[0].elements[i].value);
	                     var memberScope	= parseScope(document.forms[0].elements[i].value);
		                  
	                     // If no scope exists add to array
	                     if (memberScope == null || memberScope == "" || newScope != memberScope)
	                     {
                           modifyScopeArray[count] = osEscape(memberName) + ";" + osEscape(newScope) + "$";
	                     }
	                    
		                  count++;
			            }
                  }
		         }      
		      }
	         
            if (modifyScopeArray.length > 0)
		      {
               document.AssignScopesForm.elements["ModifyScopeList"].value=modifyScopeArray.join("$");
               document.AssignScopesForm.elements["merge"].value="fw.AssignScopes";
				   document.AssignScopesForm.elements["nextState"].value="addScopeToTableMember";
				   document.AssignScopesForm.submit();
		      }
	      }
         else
         {
         	alert("<%= c.toScript(c.string("AssignScopes.CheckNameBoxes")) %>");
            setFocusToFormElement();
         }
      }
      else
      {
      	alert("<%= c.toScript(c.string("AssignScopes.EnterScope")) %>");
         setFocusToFormElement();
      }
   }

   
   function parseScope(scopeAndName)
   {
      var nameScopeArray = scopeAndName.split(":");
   	return nameScopeArray[0];
   }
   
   
   function parseName(scopeAndName)
   {
      var nameScopeArray = scopeAndName.split(":");
   	return nameScopeArray[1];
   }
   
   
   function modifyScopeSelected(memberName, scopeName)
   {
      document.AssignScopesForm.elements["ModifyMemberFullName"].value=memberName;
      document.AssignScopesForm.elements["ModifyMemberOldScope"].value=scopeName;
      
      if (!myWindow || myWindow.closed)
      {
         myWindow = window.open("webacc?taskId=dev.Empty&merge=fw.ModifyRoleScopeWizard&User.context=<%= c.var("User.context") %>&ScopeSearchTypes=<%= c.toHTML(c.var("ScopeSearchTypes")) %>", "", "toolbar=no,location=no,directories=no,menubar=no,scrollbars=yes,resizable=yes,width=520,height=330");
      }
      else
      {
      	myWindow.focus();
      }

   }
   
      
   // 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 inheritanceCheckboxChange(fullName, scopeName, checkedValue)
   {
      var checkboxValue 		= "false";
      var stringInArray 		= false;
      var newNameScope 			= osEscape(fullName) + ";" + osEscape(scopeName);
      var newInheritanceValue = checkedValue.toString();
      
      // Convert boolean to string
      if (checkedValue == true)
      {
         checkboxValue = "true";
      }
      else
      {
      	checkboxValue = "false";
      }
      
      if (inheritanceKeyArray != null) // Global array of Names and scopes used as key
      {
         // Check array for existing memberId
         for (var i = 0; i < inheritanceKeyArray.length; i++)
         {
            var arrayNameScopeString = inheritanceKeyArray[i];
            
            // Compare new name and scope to existing name and scope key
            if (arrayNameScopeString == unescape(newNameScope))
            {
               stringInArray = true;
               
               // compare
               inheritanceValueArray[i] = newInheritanceValue;
               break;
            }
         }
         
         // Not in the array 
         if (stringInArray == false)
         {
         	inheritanceKeyArray[inheritanceKeyArray.length] = newNameScope;
            inheritanceValueArray[inheritanceValueArray.length] = newInheritanceValue;
         }
         
         for (var j = 0; j < inheritanceKeyArray.length; j++)
         {
            combinedInheritanceArray[j] = inheritanceKeyArray[j] + ";" + inheritanceValueArray[j]; 
         }
         
         document.AssignScopesForm.elements["MemberInheritanceChanged"].value="true";
      	document.AssignScopesForm.elements["MemberInheritanceList"].value=combinedInheritanceArray.join("$");
      }
   }
   

	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="AssignScopesForm" method=post action="webacc" onsubmit="return(checkNullInput())">
   <input type=hidden name="taskId" value="<%= c.var("taskId") %>">
   <input type=hidden name="merge" value="fw.CrtRoleSummary">
   <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="getSummaryInfo">
   <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="ModifyMemberFullName" value="">
   <input type=hidden name="ModifyMemberOldScope" value="">
   <input type=hidden name="ModifyScopeList" value="">
   <input type=hidden name="ModifyMemberId" value="">
   <input type=hidden name="MemberInheritanceChanged" value="false">
   <input type=hidden name="MemberInheritanceList" value="">
   <input type=hidden name="RowCheckBoxStatus" value="false">
   <input type=hidden name="CreateRolePreviousPage" value="AssignScopes">
   <!-- End of hidden tags -->
   
<% if (session.getAttribute("DeviceType").equals("browser")) { %>
	<DIV id="Layer2" style="position:relative; left:6px; top:4px;">   
<% } %>
	<TABLE width="100%" border="0" cellspacing="0" cellpadding="3">
    	<TR> 
	      <TH bgcolor="#458ab9" colspan="7"> 
	    		<DIV align="left" class="formhead1"><font color="#FFFFFF"><b><%= c.toHTML(c.string("CreateRole.WizardTitle")) %></b></font></DIV>
	      </TH>
    	</TR>
    	<TR valign="baseline" bgcolor="#EFEEE9"> 
      	<TD height="18" 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="18" width="33%" align="left" valign="middle" class="tablesubhead1"><b><%= c.toHTML(c.string("AssignScopes.SetScope")) %></b></TD>
      	<TD height="18" width="62%" align="left" valign="middle">&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("AssignScopes.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("AssignScopes.SetTheScope")) %><BR><%= c.toHTML(c.string("AssignScopes.EnterOrBrowse")) %></TD>
    	</TR>
      <TR bgcolor="#FFFFFF">
      	<TD colspan="4" nowrap valign="top"><BR><%= c.toHTML(c.string("AssignScopes.ScopeName")) %></TD>
      </TR>
      <TR bgcolor="#FFFFFF">
      	<DIV align="left">
         	<TD colspan="4" nowrap valign="top" height="9">
			      <input type=text name="NewMemberScope" value="" size=<% if (c.var("Browser.name").equals("NSN") && c.var("Browser.mjrVer").equals("4")) { %>18<% } else { %>35<% } %> maxlength=64>
               <% c.set("OS.TypeFilter", "[containers],[root]"); 
			   	   c.set("OS.Control", "NewMemberScope"); %>
			   	<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>
               <BR><DIV class="instructions"><%= c.toHTML(c.string("AssignScopes.ScopeHelpMessage")) %></DIV>
            </TD>
   		</DIV>
      </TR>
      <TR bgcolor="#FFFFFF"> 
      	<TD colspan="4" valign="top">&nbsp;</TD>
    	</TR>
   </TABLE>
 	<TABLE width="100%" border="0" cellspacing="0" cellpadding="3">
   	<TR>
      	<TD colspan="4">
         	<DIV align="left"> &nbsp;
            	<a href="#" onClick="javascript:removeScopeButtonSelected();return false"><IMG src="<%= c.getModulesUrl() + "/dev/images/" + c.string("Button.RemoveMember.image") %>" alt="<%= c.toHTML(c.string("Button.RemoveScope.alt")) %>" border=0></a>
            </DIV>
         </TD>
      </TR>
      <TR  bgcolor="#DFDDD5" > 
      	<TD width="9%" height="24"> 
        		<DIV align="center"><input type="checkbox" name="NameCheckBox" value="checkbox" onclick="setRowCheckBoxValues(this.checked)"></DIV>
      	</TD>
   		<TD width="38%" height="24" class="tablecolumnhead1"> 
     			<DIV align="left"><font color="#FFFFFF"><b><font color="#000000"><%= c.toHTML(c.string("AssignScopes.Name")) %></font></b></font></DIV>
   		</TD>
   		<TD width="28%" height="24" class="tablecolumnhead1"> 
     			<DIV align="left"><font color="#FFFFFF"><b><font color="#000000"><%= c.toHTML(c.string("AssignScopes.Scope")) %></font></b></font></DIV>
   		</TD>
   		<TD width="17%" height="24" class="tablecolumnhead1"> 
     			<DIV align="center"><font color="#FFFFFF"><b><font color="#000000"><%= c.toHTML(c.string("AssignScopes.Inheritable")) %></font></b></font></DIV>
   		</TD>
 		</TR>
      
    	<!-- Load table info -->
      <% c.set("i", "0"); 
         c.set("color", "#FFFFFF"); 
   	   c.set("previousName", ""); 
         while (c.lt(c.var("i"), c.var("Role.Member.Count"))) { %>
	      <TR bgcolor=<%= c.var("color") %>>
	         <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")+ ".ScopeFullName")) %>:<%= c.toHTML(c.var("Role.Member."+c.var("i")+ ".FullName")) %>" title="<%= c.toHTML(c.var("Role.Member."+c.var("i")+ ".FullName")) %>">
               </DIV>
		      </TD>
            <TD width="40%" height="17">
            	<% if (c.var("Role.Member."+c.var("i")+ ".FullName").equals(c.var("previousName"))) { %>
                	<%= c.toHTML(c.var("Role.Member.EmptyName")) %>
               <% } else { %>
               	<%= c.toHTML(c.var("Role.Member."+c.var("i")+ ".FullName")) %>
               <% } 
                  c.set("previousName", c.var("Role.Member."+c.var("i")+ ".FullName")); %>
            </TD>
            <% if (c.var("Role.Member."+c.var("i")+ ".ScopeFullName").equals("") == false) { %>
            	<TD width="40%" height="17">
                  	<a href="#" onClick="javascript:modifyScopeSelected('<%= c.toHTML((c.toScript(c.var("Role.Member."+c.var("i")+ ".FullName")))) %>','<%= c.toHTML((c.toScript(c.var("Role.Member."+c.var("i")+ ".ScopeFullName")))) %>');return false"><%= c.toHTML(c.var("Role.Member."+c.var("i")+ ".ScopeDisplayName")) %></a>
            	</TD>
               <TD width="10%" height="17">
               <DIV align="center">
               	<input type="checkbox" name="<%= c.toHTML(c.var("Role.Member."+c.var("i")+ ".FullName")) %>,<%= c.toHTML(c.var("Role.Member."+c.var("i")+ ".ScopeFullName")) %>" value="<%= c.toHTML(c.var("Role.Member."+c.var("i")+ ".Inheritance")) %>"
               <% if (c.toHTML(c.var("Role.Member."+c.var("i")+ ".Inheritance")).equals("true")) { %>
            		CHECKED
               <% } %>
                  width="10%" height="16" disabled>
            	</DIV>
               </TD>
            <% } else { %>
            	<TD width="40%" height="17">&nbsp;</TD>
   				<TD width="10%" height="17">&nbsp;</TD>
            <% } %>
		   </TR>
	   	<% c.inc("i"); 
	         if (c.var("Role.Member."+c.var("i")+ ".FullName").equals(c.var("previousName")) == false) {  
	            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>
