<%@ page language="java" buffer="100kb" errorPage="/common/error.jsp"%>

<%@ page import="vkc.view.TabId" %>
<%@ page import="org.apache.struts.Globals" %>

<%@ taglib uri="struts-html" 	prefix="html"  %>
<%@ taglib uri="struts-bean" 	prefix="bean"  %>
<%@ taglib uri="struts-logic" 	prefix="logic" %>
<%@ taglib uri="vkc-taglib" 	prefix="vkc"   %>

<vkc:checkLogin />
<vkc:init/>
<html:html locale="true">
<head>

	<vkc:cssinclude />

    <meta http-equiv="content-type"  content="text/html; charset=iso-8859-1" />
    <script type="text/javascript" src="<vkc:rewrite page='/common/utils.js'/>"></script>

    <script type="text/javascript">

    window.name = "MAINVIEW";

    var aclform = null;
    var associatedTabId = <%=TabId.ADMIN_LDAP_TAB%>;
    	
    function init() {
      	var tabView = getTabView();
        var tabViewForm = tabView.document.forms[0];

        if (tabView && tabView.systemviewtype == "administrationview" && tabView.selectedTabId != associatedTabId) {
          	tabView.location.replace("<%=ctxPath%>/adminTabView.do?selectedTabId=<%=TabId.ADMIN_LDAP_TAB%>"
          						   + "&lastCategoryId=" + tabViewForm.lastCategoryId.value
          						   + "&lastFunctionalId=" + tabViewForm.lastFunctionalId.value
          						   + "&lastProjectId=" + tabViewForm.lastProjectId.value);
        }

		setAnchor();
    }

	function checkAll( mForm ) {			
		for(var i=0 ; i<mForm.elements.length ; i++){ 
			var elem = mForm.elements[i];
			if(elem.name!='allItems') {
			    elem.checked=mForm.allItems.checked;
			}
		}
	}

	function getNumberOfCheckedEntries() {
        var form  = document.forms[ 0 ];
        var field = form.selectedIds;

        if ( !field )
        	return 0;

        if ( !field.length ) {

            <%-- nur eine Checkbox auf der Seite --%>
            if ( field.checked ) {
                return 1;
            } else {
                return 0;
            }
        }

        var checked = 0;
        for ( var i=0 ; i<field.length ; i++ ) {
            if ( field[i].checked ) {
                checked++;
            }
        }
        return checked;
	}
	
	function deleteServers(elem) {
		var numChecked = getNumberOfCheckedEntries();
		
        if ( numChecked == 0 ) {
            alert( '<vkc:message javascript="true" filter="false" key="vkc.ldap.administration.select.servers"/>' );
            return;
        }

		if ( numChecked==1 ) {
            if ( !confirm( '<vkc:message javascript="true" filter="false" key="vkc.ldap.administration.confirm.delete.server"/>' ) )
                return;
		} else {
            if ( !confirm( '<vkc:message javascript="true" filter="false" key="vkc.ldap.administration.confirm.delete.servers"/>' ) )
            	return;
		}               

        var form  = document.forms[0];

        if (form.action.indexOf('?') == -1) {
          form.action = form.action + "?action.deleteServers=true";
        } else {
          form.action = form.action + "&action.deleteServers=true";
        }

        disableAllButtons(elem);
       	form.submit();
	}

	function disableServers(elem) {
		var numChecked = getNumberOfCheckedEntries();
		
        if ( numChecked == 0 ) {
            alert( '<vkc:message javascript="true" filter="false" key="vkc.ldap.administration.select.servers"/>' );
            return;
        }

		if ( numChecked==1 ) {
            if ( !confirm( '<vkc:message javascript="true" filter="false" key="vkc.ldap.administration.confirm.disable.server"/>' ) )
                return;
		} else {
            if ( !confirm( '<vkc:message javascript="true" filter="false" key="vkc.ldap.administration.confirm.disable.servers"/>' ) )
            	return;
		}               

        var form  = document.forms[0];

        if (form.action.indexOf('?') == -1) {
          form.action = form.action + "?action.disableServers=true";
        } else {
          form.action = form.action + "&action.disableServers=true";
        }

        disableAllButtons(elem);
       	form.submit();
	}

	function enableServers(elem) {
		var numChecked = getNumberOfCheckedEntries();
		
        if ( numChecked == 0 ) {
            alert( '<vkc:message javascript="true" filter="false" key="vkc.ldap.administration.select.servers"/>' );
            return;
        }

		if ( numChecked==1 ) {
            if ( !confirm( '<vkc:message javascript="true" filter="false" key="vkc.ldap.administration.confirm.enable.server"/>' ) )
                return;
		} else {
            if ( !confirm( '<vkc:message javascript="true" filter="false" key="vkc.ldap.administration.confirm.enable.servers"/>' ) )
            	return;
		}               

        var form  = document.forms[0];

        if (form.action.indexOf('?') == -1) {
          form.action = form.action + "?action.enableServers=true";
        } else {
          form.action = form.action + "&action.enableServers=true";
        }

       	disableAllButtons(elem);
        form.submit();
	}

	function modifyServers(elem) {
		var form = document.forms[ 0 ];
		var field = form.selectedCmdAction.value;

		if ( field == "noaction" || field == "noaction1" ) {
       		alert( '<vkc:message javascript="true" filter="false" key="error.noaction_choosen"/>' );
       		return false;
		}

		if ( field == "delete.servers" ) {
			deleteServers(elem);
		}

		if ( field == "disable.servers" ) {
			disableServers(elem);
		}

		if ( field == "enable.servers" ) {
			enableServers(elem);
		}
	}

    </script>

</head>
<body onload="init();" topmargin="0" marginheight="0">
<html:form action="/ldapServerList" method="post">

<bean:define id="form" name="ldapServerListForm" type="vkc.ldap.administration.form.LdapServerListForm" toScope="request"/>
<bean:define id="selectedIds"  name="form" property="selectedIds"       type="java.lang.Integer[]"/>

<table width="100%" align="center" cellspacing="0" cellpadding="0" border="0">

  <logic:present name="<%=Globals.ERROR_KEY%>">
  <tr><td align="left" nowrap="nowrap" colspan="3"><font color="red"><b><vkc:errors/></b></font></td></tr>
  <tr><td align="left" nowrap="nowrap" colspan="3"><hr></td></tr>
  </logic:present>

  <tr>
	<td nowrap="nowrap" align="right"><vkc:link page="/newLdapServer.do"><vkc:message key="vkc.administration.ldap.newServer" /></vkc:link></td>
  </tr>
  <tr>
	<td><vkc:img page="/templates/${template.name}/gfx/trans.gif" alt="" width="5" height="8" border="0"/></td>
  </tr>
  
  <tr>
    <td class="tableLineColor">
      <table width="100%" cellspacing="1" cellpadding="2" border="0" class="tableLineColor">
 	    <tr>
	      <td class="headerSelected">&nbsp;</td>
<!--
          <td class="headerSelected"><b><vkc:message key="vkc.administration.ldap.isAlive"/></b></td>
-->
	      <td class="headerSelected"><b><vkc:message key="vkc.administration.ldap.serverUrl"/></b></td>
	      <td class="headerSelected"><b><vkc:message key="vkc.administration.ldap.description"/></b></td>
	      <td class="headerSelected"><b><vkc:message key="vkc.administration.ldap.authmethod"/></b></td>
	      <td class="headerSelected"><b><vkc:message key="vkc.administration.ldap.basedn"/></b></td>
	      <td class="headerSelected"><b><vkc:message key="vkc.administration.ldap.enabled"/></b></td>
        </tr>

		<logic:empty name="form" property="allLdapServers">
		<tr>
          <td class="columnUnselected"><vkc:img page="/templates/${template.name}/gfx/trans.gif" alt="" width="20" height="20" border="0"/></td>
		  <td class="columnUnselected" colspan="5" width="100%"><b><vkc:message key="vkc.administration.ldap.empty"/></b></td>
		</tr>
		</logic:empty>
		<logic:notEmpty name="form" property="allLdapServers">
	        <logic:iterate id="ldapServer" name="form" property="allLdapServers" type="vkc.ldap.db.entities.VKCLdapServer">
	        <tr>
	          <td class="columnUnselected" width="1%">
			    <html:multibox name="form" property="selectedIds" onfocus="this.blur();"><vkc:write name="ldapServer" property="ldapServerId"/></html:multibox>
			  </td>
<!--
              <logic:equal name="ldapServer" property="isAlive" value="true">
                  <td class="columnUnselected"><vkc:message key="string.yes"/></td>
              </logic:equal>
              <logic:notEqual name="ldapServer" property="isAlive" value="true">
                  <td class="columnUnselected"><font color="FF0000"><vkc:message key="string.no"/></font></td>
              </logic:notEqual>
-->
			  <td class="columnUnselected"><a href="<%=ctxPath%>/editLdapServer.do?initialize=true&serverId=<vkc:write name="ldapServer" property="ldapServerId"/>"><vkc:write name="ldapServer" property="ldapServerURL"/></a></td>
			  <td class="columnUnselected"><vkc:write name="ldapServer" property="description"/></td>
			  <td class="columnUnselected"><vkc:write name="ldapServer" property="authMethod"/></td>
			  <td class="columnUnselected"><vkc:write name="ldapServer" property="baseDN"/></td>
			  <logic:equal name="ldapServer" property="enabled" value="true">
			    <td class="columnUnselected"><vkc:message key="vkc.administration.ldap.enabled.true"/></td>
			  </logic:equal>
			  <logic:notEqual name="ldapServer" property="enabled" value="true">
			    <td class="columnUnselected"><vkc:message key="vkc.administration.ldap.enabled.false"/></td>
			  </logic:notEqual>
			</tr>
			</logic:iterate>
	    </logic:notEmpty>
 	  </table>
 	</td>
  </tr>

  <logic:notEmpty name="form" property="allLdapServers">
  <tr><td align="center"><vkc:img page="/templates/${template.name}/gfx/trans.gif" alt="" width="5" height="5" border="0"/></td></tr>
  <tr><td colspan="2">&nbsp;<input type="checkbox" name="allItems" value="allItems" onclick="javascript:checkAll(this.form);">&nbsp;<vkc:message key="vkc.string.checkAll"/></td></tr>
  <tr><td align="center"><vkc:img page="/templates/${template.name}/gfx/trans.gif" alt="" width="5" height="5" border="0"/></td></tr>
  <tr>
    <td colspan="2">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><vkc:img page="/templates/${template.name}/gfx/trans.gif" alt="" width="5" border="0"/><vkc:img page="/templates/${template.name}/gfx/function-arrow.gif" alt="-->" border="0"/></td>
          <td>
            <html:select name="form" property="selectedCmdAction" value="noaction">
	  	      <html:option value="noaction"><vkc:message key="vkc.string.chooseFunction"/></html:option>
		      <html:option value="noaction1">------------------------------------</html:option>
		      <html:option value="delete.servers"><vkc:message key="button.delete"/></html:option>
		      <html:option value="disable.servers"><vkc:message key="button.disable"/></html:option>
		      <html:option value="enable.servers"><vkc:message key="button.enable"/></html:option>
	        </html:select>
	      </td>
          <td>&nbsp;<vkc:button property="action.execute" onclick="modifyServers(this);"><vkc:message key="button.execute"/></vkc:button></td>
        </tr>
      </table>
    </td>
  </tr>
  </logic:notEmpty>
</table>

</html:form>
</body>
</html:html>
