<%@ include file="header.jsp" %>
<%@ page  errorPage="errorPage.jsp" language="java" session="true" contentType="text/html;charset=UTF-8" %>
<%@ page pageEncoding="UTF-8"%>

<%!
public String getVal(String name, HttpSession session) {
	Map m = (Map)session.getAttribute("user_detail_address.jsp");
	if (m == null) return "";
	String[] vals = (String[])m.get(name);
	if (vals != null && vals.length>0 && vals[0] != null) return vals[0];
	return "";
}

public String getInternVal(String name, HttpSession session) {
	Map m = (Map)session.getAttribute("user_detail_int.jsp");
	if (m == null) return "";
	String[] vals = (String[])m.get(name);
	if (vals != null && vals.length>0 && vals[0] != null) return vals[0];
	return "";
}

%>
<%
 String prefix = AboHelper.getPrefix(request);
%>
<html>
<head>
<title>Untitled Document</title>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">
<link href="main.css" rel="stylesheet" type="text/css">

<SCRIPT LANGUAGE="JavaScript">
<!-- JavaScript code hiding
	function simpleLink(origLink) {
		parent.mainFrame.location = origLink;
	}

 
 function link(origLink, page_action){
		if (top.mainFrame.document.forms.length == 0) {
			simpleLink(origLink);
			return;
		}
		else {
			
			
			top.mainFrame.document.forms[0].saveState.value = '<%=prefix%>detail_abo.jsp'
			top.mainFrame.document.forms[0].link.value = origLink;
			top.mainFrame.document.forms[0].pageAction.value = page_action;
			top.mainFrame.document.forms[0].submit();
		}
	}
	
 function link2(origLink1, page_action1){
		if (top.mainFrame.document.forms.length == 0) {
			simpleLink(origLink1);
			return;
		}
		else {
			
			var ok = false;
		
			if (top.mainFrame.document.forms[1].del.length == undefined) {
			  if (top.mainFrame.document.forms[1].del.checked) ok = true;
			}
			else
			 for (var i=0; i<top.mainFrame.document.forms[1].del.length; i++) 
				if (top.mainFrame.document.forms[1].del[i].checked) ok = true;
			
			if (!ok) {
			  alert('Wählen Sie bitte zuerst ein Abo aus!');
			  return;
			}
			if (page_action1=='del_abo') {
				if (!confirm('Sind Sie sicher, dass Sie die ausgewählten Abos löschen möchten?'))
					return;
			}
			
			
			top.mainFrame.document.forms[1].saveState.value = top.mainFrame.self.location.pathname;
			top.mainFrame.document.forms[1].link.value = origLink1;
			top.mainFrame.document.forms[1].pageAction.value = page_action1;
			top.mainFrame.document.forms[1].submit();
		}
	}
	
	
// -->
</SCRIPT>
</head>
<%
	boolean admin = false;
	boolean distributor = false;
	boolean customer = false;
	if	(session.getAttribute("admin")!=null) admin = true;
	if	(session.getAttribute("customer")!=null) customer = true;
		if	(session.getAttribute("distributor")!=null) distributor = true;
	
	
	boolean[] rights = AboHelper.getRights(session, prefix, 
							AboHelper.MASK_CREATE | AboHelper.MASK_VIEW | AboHelper.MASK_DELETE);
%>

<% if (prefix.equals("customer_")) { %>
 <body bgcolor="#e6e6ee">
<% } else { %>
 <body>
<% } %>

<% if (prefix.equals("customer_")) { %>
 <div width="800" height="30" align="left" class="menu"><%=FormHelperC.generateCustomerDataHeaderString(pageContext)%>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class="menu" href="process/customer_selection.jsp">Anderen Kunde auswählen</a></div>
<% } else { %>
  <div width="800" height="30" align="left" class="menu"><%=FormHelper.generateUserDataHeaderString(pageContext)%>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class="menu" href="process/user_selection.jsp">Anderen Vetriebspartner auswählen</a></div>
<% } %>
<table border="1" id="userTable" class="menu">
<%
	String self_id = "1250";
	String parent_id = "1200";
	
	if (prefix.equals("customer_")) {
		self_id = "2250";
		parent_id = "2200";
	
	}
%>
<!--<jsp:include page="MenuHelper.jsp">
	<jsp:param name="self_id" value="<%=self_id%>"/>
	<jsp:param name="parent_id" value="<%=parent_id%>"/>	
</jsp:include>-->
</table>
<form name="abo_form" action="process/new_abo.jsp?prefix=<%=prefix%>" method="POST">
 <input type="hidden" name="saveState"/>
 <input type="hidden" name="link"/>
 <input type="hidden" name="pageAction"/>


	<%
	
		if (!(prefix.equals("user_") && "300".equals(session.getAttribute("original_level_id")))) {
	
	%>



 <% if (session.getAttribute(prefix+"loading_abo_error")!=null) { %>
    
    <p style="color:red"><%=session.getAttribute(prefix+"loading_abo_error") %></p>
 
 <% 
    session.removeAttribute(prefix+"loading_abo_error"); 
   }
  %>
 	
 	
 <table width="800">
	<% if (rights[AboHelper.INDEX_CREATE]) { %>
	<tr>
		<td height="100" width="200" id="userTableContentTitles"> <a href="javascript:link('process/new_abo.jsp?prefix=<%=prefix%>', 'start_new_abo');">Neues Abo anlegen</a></td>
		<td>&nbsp;</td>
	<tr>
	<%}%>
</table>
</form>
<hr>

<form name="abo_del" action="process/detail_abo.jsp?prefix=<%=prefix%>">
 <input type="hidden" name="saveState"/>
 <input type="hidden" name="link"/>
 <input type="hidden" name="pageAction"/>
 <input type="hidden" name="prefix" value="<%=prefix%>"/>
 <fmt:message key="COMMENT" var="comment"/>

<%

 Transaction tx = null;
 try {
  if (rights[AboHelper.INDEX_VIEW]) {
   Session hsession = HibernateUtil.currentSession(); 
   tx= hsession.beginTransaction();
   Connection conn = hsession.connection();
  
   String id = null;
   if (prefix.equals("customer_"))
    id = (String) session.getAttribute("CURRENT_CUSTOMER_ID");
   else
    id = (String) session.getAttribute("CURRENT_DISTRIBUTOR_ID");
   
   List list = AboHelper.listAbos(conn, new Integer(id));
  
   
   out.println(AboHelper.generateAboListHeader(pageContext, prefix));
   out.println(AboHelper.generateAboListData(session, prefix+"detail_abo.jsp", list, prefix));
   tx.commit();
   HibernateUtil.closeSession();
  }
 } catch (Exception e) {
 	
 	if (tx!=null)
 		tx.rollback();
 	throw e;	
 }
%>
 <% if (rights[AboHelper.INDEX_DELETE] && rights[AboHelper.INDEX_VIEW]) { %>
  <table width="300">
	<tr>
		<td height="100" width="100" id="userTableContentTitles"> <a href="javascript:link2('process/detail_abo.jsp?prefix=<%=prefix%>', 'del_abo');">Löschen</a></td>
		<td>&nbsp;</td>
	<tr>
 </table>
<%}%>

</from>

<%
	} else {
%>
	<table>
		<tr>
    		<td id="userTableContentMainTitles2" height="70">
				<fmt:message key="NO_ABO_FOR_DEALERS"/>
			</td>
		</tr>
	</table>
<%
	}
%>



</body>
</html>