#*
 * Aipo is a groupware program developed by Aimluck,Inc.
 * Copyright (C) 2004-2015 Aimluck,Inc.
 * http://www.aipo.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *#


<table bgcolor="#ffffff" cellpadding="5">
      #if ($msg)
      <tr>
        <td colspan="2">
          <table bgcolor="#ffffff">
            <tr>
              <td>
                $msg
              </td>
            </tr>
          </table>
        </td>
      </tr>
      #end
</table>

      #if ($mode == "delete")
      <form name="SecurityForm" method="post" action="$jslink.getPaneByName("SecurityBrowser").setAction("portlets.SecurityUpdateAction")">
      <table>
      	<tr>
      		<td colspan="2">${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_ARE_YOU_SURE_YOU_WAN_VM}</td>
      	</tr>
        <tr>
            #formReadOnlyCell ("Name" "security_name" $!entry.Name)
        </tr>
      </table>
      <table border="0"  cellspacing="0" cellpadding="5" width="30%">
      <tr>
        <td>
          <input type="submit" name="eventSubmit_doDelete" value="${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_DELETE_SECURITY_ENTR_VM}"/>
        </td>
      </tr>
      </table>
      </form>
      #elseif($mode == "insert")
      <form name="SecurityForm" method="post" action="$jslink.getPaneByName("SecurityForm").setAction("portlets.SecurityUpdateAction")">
      
      <table>
	<input type="hidden" name="mode" value="update"/>
        <tr>
          <td bgcolor="$!{skin.TitleBackgroundColor}">
            <b><font face="$ui.sansSerifFonts">${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_NAME_VM}</font></b>
          </td>
          <td bgcolor="$!{skin.TitleBackgroundColor}">
            <font face="$ui.sansSerifFonts">
              <input type="text" size="30" name="security_name" value="$!data.user.getTemp("security_name")"/>
            </font>
          </td>
        </tr>
        <tr>
          <td bgcolor="$!{skin.TitleBackgroundColor}">
            <b><font face="$ui.sansSerifFonts">${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_TITLE_VM}</font></b>
          </td>
          <td bgcolor="$!{skin.TitleBackgroundColor}">
            <font face="$ui.sansSerifFonts">
              <input type="text" size="50" name="title" value="$!data.user.getTemp("title")">
            </font>
          </td>
        </tr>
        <!--Consider making text area-->
        <tr>
	  <td bgcolor="$!{skin.TitleBackgroundColor}">
	    <b><font face="$ui.sansSerifFonts">${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_DESCRIPTION_VM}</font></b>
	  </td>
	  <td bgcolor="$!{skin.TitleBackgroundColor}">
	    <font face="$ui.sansSerifFonts">
	      <textarea rows="10" cols="50" size="50" name="description">$!data.user.getTemp("description")</textarea>
	    </font>
	  </td>
	</tr>
      </table>
      <table border="0"  cellspacing="0" cellpadding="5" width="30%">
      <tr>
        <td>
          <input type="submit" name="eventSubmit_doInsert" value="${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_INSERT_SECURITY_VM}"/>
        </td>
      </tr>
      </table>
      </form>
      #elseif($mode == "update")

      #if($subMode == "allow")
      ##Remove Form
      <form method="post" action="$jslink.getPaneByName("SecurityForm").setAction("portlets.SecurityUpdateAction")">
        <input type="hidden" name="mode" value="update"/>
        <input type="hidden" name="access_index" value="$!accessIndex"/>
        <input type="hidden" name="security_name" value="$!entry.name"/>${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_CURRENT_ACCESS_VM}$securityAccess.action
        <hr />
        <br />${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_ALLOWS_VM}<table>
  	#foreach($allow in $securityAccess.getAllows())
  	  <tr>
  	    <td><input type="checkbox" name="allow_index" value="$!velocityCount"/></td>
  	    <td>
  	    	#if($allow.user)
  	    		$allow.user
  	    	#elseif($allow.role)
  	    		$allow.role
  	    	#end
  	    </td>
  	  </tr>
  	#end
  	#if($securityAccess.getAllows().size() > 0)
  	  <tr>
  	    <td colspan="2"><input type="submit" name="eventSubmit_doRemoveallow" value="${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_REMOVE_ALLOW_VM}"/>
  	  </tr>
  	#end
  	</table>
  	</form>
  	<hr />
  	<br />${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_OWNER_ALLOWS_VM}<form method="post" action="$jslink.getPaneByName("SecurityForm").setAction("portlets.SecurityUpdateAction")">
        <input type="hidden" name="mode" value="update"/>
        <input type="hidden" name="access_index" value="$!accessIndex"/>
        <input type="hidden" name="security_name" value="$!entry.name"/>
        <input type="hidden" name="allow_type" value="owner"/>
  	<table>
  	#foreach($allow in $securityAccess.getOwnerAllows())
  	  <tr>
  	  <td><input type="checkbox" name="allow_index" value="$!velocityCount"/></td>
  	    <td>
  	    	#if($allow.user)
  	    		$allow.user
  	    	#elseif($allow.role)
  	    		$allow.role
  	    	#elseif($allow.owner)
  	    		$allow.owner
  	    	#end
  	    </td>
  	  </tr>
  	#end
  	#if($securityAccess.getOwnerAllows().size() > 0)
  	  <tr>
  	    <td colspan="2"><input type="submit" name="eventSubmit_doRemoveallow" value="${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_REMOVE_ALLOW_VM}"/>
  	  </tr>
  	#end
  	</table>
      </form>
      <hr />
      <br />
      ##Add new allow to access form
      <form method="post" action="$jslink.getPaneByName("SecurityForm").setAction("portlets.SecurityUpdateAction")">
      <input type="hidden" name="mode" value="update"/>
      <input type="hidden" name="access_index" value="$!accessIndex"/>
      <input type="hidden" name="security_name" value="$!entry.name"/>
      <table>
        <tr>
	      	  <td>${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_TYPE_VM}</td>
	      	  <td>
	      	  	<select name="allow_type">
	      	  		<option value="user">${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_USER_VM}</option>
	      	  		<option value="role">${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_ROLE_VM}</option>
	      	  		<option value="owner">${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_OWNER_VM}</option>
	      	  	</select>
	      	  </td>
	      	</tr>
	      	<tr>
	      	  <td>${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_VALUE_VM}</td>
	      	  <td><input type="text" name="allow_value" value="$!data.parameters.getString("allow_value")"/>
	      	</tr>
	      	<tr>
	      	  <td><input type="submit" name="eventSubmit_doAddallow" value="${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_ADD_ALLOW_VM}"/>
	      	</tr>
      </table>
      </form>
      
      <br />
      <form method="post" action="$jslink.getPaneByName("SecurityForm").setAction("portlets.SecurityUpdateAction")">
      	<input type="hidden" name="mode" value="update"/>
      	<input type="hidden" name="security_name" value="$!entry.name"/>
      	
      	<input type="submit" value="${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_BACK_TO_SECURITY_ENT_VM}"/>
      </form>
      #else
	      <form name="SecurityForm" method="post" action="$jslink.getPaneByName("SecurityForm").setAction("portlets.SecurityUpdateAction")">
	      
	      <table>
		<input type="hidden" name="mode" value="update"/>
	        <tr>
	          #formReadOnlyCell ("Name" "security_name" $!entry.Name)
	        </tr>
	        <tr>
	          <td bgcolor="$!{skin.TitleBackgroundColor}">
	            <b><font face="$ui.sansSerifFonts">${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_TITLE_VM}</font></b>
	          </td>
	          <td bgcolor="$!{skin.TitleBackgroundColor}">
	            <font face="$ui.sansSerifFonts">
	              <input type="text" size="50" name="title" value="$!entry.title">
	            </font>
	          </td>
	        </tr>
	        <!--Consider making text area-->
	        <tr>
		  <td bgcolor="$!{skin.TitleBackgroundColor}">
		    <b><font face="$ui.sansSerifFonts">${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_DESCRIPTION_VM}</font></b>
		  </td>
		  <td bgcolor="$!{skin.TitleBackgroundColor}">
		    <font face="$ui.sansSerifFonts">
		      <textarea rows="10" cols="50" size="50" name="description">$!entry.description</textarea>
		    </font>
		  </td>
		</tr>
	      </table>
	      <table border="0"  cellspacing="0" cellpadding="5" width="30%">
	      <tr>
	        <td>
	          <input type="submit" name="eventSubmit_doUpdate" value="${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_UPDATE_SECURITY_VM}"/>
	        </td>
	      </tr>
	      </table>
	      </form>
      
      	      <form method="post" action="$jslink.getPaneByName("SecurityForm").setAction("portlets.SecurityUpdateAction")">
      		<input type="hidden" name="mode" value="update"/>
      		<input type="hidden" name="security_name" value="$!entry.name"/>
	      <table border="1">
	        ##<th>&nbsp;</th>
	      	##<th>Action</th>
	      	##<th>Allows</th>
	      #foreach($securityAccess in $entry.getAccesses())
	      	<tr>
	      	  <td><input type="checkbox" name="access_index" value="$!velocityCount"/></td>
	      	  <td>
	      	    $securityAccess.action
	      	  </td>
	      	  <td>
	      	  	<a href="$jslink.getPaneByName("SecurityForm").setAction("portlets.SecurityUpdateAction").addQueryData("mode", "update").addQueryData("security_name",$entry.name).addQueryData("access_index",$velocityCount).addQueryData("subMode", "allow")">${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_MORE_VM}</a>
	      	  </td>
	      	</tr>
	      #end
	      #if($entry.getAccesses().size() > 0)
	      <tr>
	        <td colspan="3"><input type="submit" name="eventSubmit_doRemoveaccess" value="${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_REMOVE_ACCESS_VM}"/>
	      </tr>
	      #end
	      </table>
	      </form>
	      
	      <!--Add a new action entry-->
	      <script>
	      	
	      	var userSelect = new Array;
	      	var roleSelect = new Array;
	      	var populated = false;
	      	
	      	function populate()
	      	{	      	
		      	#foreach($user in $users)
		      	#set($myCount = $velocityCount - 1)
		      		var userOption$myCount = new Option;
		      		userOption${myCount}.value = "$user.userName";
		      		userOption${myCount}.text = "$user.userName";
		      		
		      		userSelect[$myCount] = userOption$myCount;		      		
		      	#end
		      	
		      	#foreach($role in $roles)
		      		#set($myCount = $velocityCount - 1)
		      		var roleOption$myCount = new Option;
		      		roleOption${myCount}.value = "$role.name";
		      		roleOption${myCount}.text = "$role.name";
		      		
		      		roleSelect[$myCount] = roleOption$myCount;
		      	#end
		      	
		      	populated = true;
		}
		
		
	      
	      	function changeList(selectAction)
	      	{
	      		if(!populated)
	      		{
	      			populate();
	      		}
	      		var val = selectAction.options[selectAction.selectedIndex].value;
	      		
	      		var newOpts = new Array;
	      		
	      		if(val == "user")
	      		{
	      			newOpts = userSelect;
	      		}
	      		else if(val == "role")
	      		{
	      			newOpts = roleSelect;
	      		}
	      		
	      		var select = document.NewSecurity.allow_value;
	      		
	      		for(var i=0; i<newOpts.length; i++)
	      		{
	      			select.options[i] = newOpts[i];
	      		}
	      		
	      		for(var j=newOpts.length; j<select.options.length; j++)
	      		{
	      			select.options[j] = new Option;
	      		}
	      	}
	      	
	      </script>
	      <form name="NewSecurity" method="post" action="$jslink.getPaneByName("SecurityForm").setAction("portlets.SecurityUpdateAction")">
	      <input type="hidden" name="mode" value="update"/>
	      <input type="hidden" name="security_name" value="$!entry.name"/>
	      <table>
	      	<tr>
	      	  <td>${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_ACTION_VM}</td>
	      	  <td>
	      	  	<select name="access_action" >
	      	  		<option value="*">*</option>
	      	  		#foreach($permission in $permissions)
	      	  		<option value="$!permission.name" #if($data.user.getTemp("access_action") == $permission.name) selected="true" #end>$!permission.name</option>
	      	  		#end
	      	  	</select>
	      	  </td>
	      	</tr>
	      	<tr>
	      	  <td>${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_TYPE_VM}</td>
	      	  <td>
	      	  	<select name="allow_type" onChange="changeList(this); return true;">
	      	  		<option value="owner">${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_OWNER_VM}</option>
	      	  		<option value="user">${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_USER_VM}</option>
	      	  		<option value="role">${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_ROLE_VM}</option>
	      	  	</select>
	      	  </td>
	      	</tr>
	      	<tr>
	      	  <td>${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_VALUE_VM}</td>
	      	  ##<td><input type="text" name="allow_value" value="$!data.parameters.getString("allow_value")"/></td>
	      	  
	      	  <td>
	      	  	<select name="allow_value">
	      	  	#*
	      	  	#foreach($user in $users)
	      	  		<option value="$user.name">$user.userName</option>
	      	  	#end
	      	  	*#
	      	  	</select>
	      	  </td>
	      	</tr>
	      	<tr>
	      	  <td><input type="submit" name="eventSubmit_doAddaccess" value="${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_ADD_SECURITY_ACCESS_VM}"/>
	      	</tr>
	      </table>
	     </form>
      #end
      ##ends else of subMode==allow
      
      #end
      
<table>
  <tr>
    <td>
  	<form name="BackToBrowserForm" method="post" action="$jslink.getPaneByName("SecurityBrowser").setAction("portlets.SecurityUpdateAction")">
  		<input type="submit" name="eventSubmit_doCancel" value="${l10n.SECURITY_FORM_MULTIPLE_ALLOWS_BACK_TO_BROWSER_VM}"/>
  	</form>
    </td>
  </tr>
</table>
