<%@ page buffer="32kb" %>
<%@ page language="java" session="true" contentType="text/html;charset=ISO-8859-2" %>
<%@ page pageEncoding="UTF-8"%>
<%@ page import="com.etixpert.evolution.gui.servlets.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.net.*"%>
<%@ page import="com.etixpert.evolution.gui.servlets.elements.*"%>
<%@ page import="com.etixpert.evolution.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="org.hibernate.*"%>
<%@ include file="header.jsp" %>
<%@ include file="check.jsp" %>
<%

	response.setLocale(Locale.GERMAN);
	response.setContentType("text/html; charset=iso-8859-2");
	request.setCharacterEncoding("ISO-8859-2");

//response.setContentType("text/html; charset=UTF-8");
//    request.setCharacterEncoding("UTF-8");
 Transaction tx= null;
try {
	 Session hibernateSession = HibernateUtil.currentSession();
	 tx = hibernateSession.beginTransaction();
	 Connection conn = hibernateSession.connection();
	 String right = NewsletterHelper.getRight(conn, session);
	 if (right==null || right.equalsIgnoreCase("x"))
	   throw new EvolutionException("Sie haben keine Berechtigung diese Seite anzuschauen!");

	 boolean isAdmin = right.equalsIgnoreCase("A");
	 Integer levelId = new Integer((String) session.getAttribute("USER_LOGGED_LEVEL_ID"));
	 String jsp = "newsletter.jsp";

	 String from = "bestellung@evolution-international.com";
	 String address = "bestellung@evolution-international.com";
	 if (!isAdmin) from = NewsletterHelper.getFrom(conn, session);

	 Input addressElement = new Input(session, jsp, "address", address, 60);
	 Input fromElement = new Input(session, jsp, "from", from, 60);
	 Input subjectElement = new Input(session, jsp, "subject", "", 60);

	 if (from==null) from="";
	 //if (from.trim().equals(""))
	   //throw new EvolutionException("Keine Email-Adresse angegeben");


%>
<html>
<head>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<link href="main.css" rel="stylesheet" type="text/css">
<meta http-equiv="content-type" content="text/html;charset=ISO-8852-1">
<script language="JavaScript">
function Del(sn) {
	document.Form.delfile.value = sn;
	document.Form.pageAction.value='newsletter_delete';
	document.Form.submit();
}



	function link2(origLink, page_action){
			top.mainFrame.document.forms[0].saveState.value ='<%=jsp%>';
			top.mainFrame.document.forms[0].link.value = origLink;
			top.mainFrame.document.forms[0].pageAction.value = page_action;


  			if (page_action=='newsletter_send') {
     	  	   document.all['content'].style.display='none';
      	 	   document.all['wait'].style.display='block';
  			}

			top.mainFrame.document.forms[0].submit();


	}

 function cnr_changed() {
	dfv = document.forms[0].dist_id2
	document.forms[0].cnr.value = dfv.options[dfv.selectedIndex].value
}

 function dist_id2_changed() {
	dfv = document.forms[0].dist_id2
	if (document.forms[0].cnr.value == '')
		alert("Ungültige DistID");
	for (i = 0; i < dfv.length; i++) {
		if (dfv.options[i].value == document.forms[0].cnr.value) {
			dfv.selectedIndex = i
			return
		}
	}
	alert("Ungültige DistID");
	cnr_changed();
	document.forms[0].cnr.focus();
	document.forms[0].cnr.select();
}


</script>
</head>
<body style="border:0;margin:0" onload="cnr_changed()">

<div id="content" style="display:block;">

<center>
<%
 if (session.getAttribute("newsletter_error")!=null) { %>
    <p style="color:red"><small> <%=session.getAttribute("newsletter_error")%></small></p>
  <%
    session.removeAttribute("newsletter_error");
   } %>

<br>
<br>

<form name=Form method=post action="process/newsletter.jsp" ENCTYPE="multipart/form-data">
<input type=hidden name="delfile">
<input type="hidden" name="saveState" value="<%=jsp%>"/>

<input type="hidden" name="link"/>
<input type="hidden" name="pageAction"/>

<table bgcolor="#EEEEEE" border="1" style="font-size:small">

  <tr>
    <th rowspan="<%=NewsletterHelper.getRecipientHeight(conn, right, levelId)%>"><SMALL>&nbsp;<fmt:message key="RECIPIENT"/>:&nbsp;</small></th>


  <%=NewsletterHelper.generateRoles(conn, session, jsp) %>


<tr>
  <td><b><small><fmt:message key="DOWNLINE_OF"/></small></b></td>
  <td>
   <input type="text" size="8" name="cnr" onchange="dist_id2_changed()"/>
   <%=	NewsletterHelper.generateDownlineSelect(conn, session, jsp, right) %>


   </td>
</tr>
<tr>
	<th><small><fmt:message key="PARTNERSHIP"/></small></th>
	<td colspan="2" align="left">
    <%
     CheckBox[] partnerTypes = CheckBox.generateFromLookup(conn, session, jsp, "partner_type", "partner_type", "id", "descr", true);
     for (int i =0; i<partnerTypes.length; i++) {
        partnerTypes[i].setProperty("style","font-size:small");
        out.println(partnerTypes[i].toHTML());
      }
    %>

 </td>
	
</tr>
<tr>
 <th><small><fmt:message key="COUNTRIES"/></small></th>
  <td colspan="2" align="left">
    <%
     CheckBox[] countries = CheckBox.generateFromLookup(conn, session, jsp, "country", "country", "country_code", "name", true);
     for (int i =0; i<countries.length; i++) {
        countries[i].setProperty("style","font-size:small");
        out.println(countries[i].toHTML());
      }
    %>

 </td>
</tr>
  <tr>
    <td align="center"><small><b><fmt:message key="ADDRESS"/></b></small></th>
    <td><%= addressElement.toHTML()%></td>
  </tr>


  <tr>
    <th colspan=2><small>&nbsp;<fmt:message key="FROM"/>:&nbsp;</small></th>
    <td><%=fromElement.toHTML()%></td>
  </tr>
  <tr>

    <th colspan=2><small>&nbsp;<fmt:message key="ATTACHMENT"/>:&nbsp;</small></th>
    <td>
     <%=NewsletterHelper.generateFileList(pageContext)%>
    <input type=file name=attachment size="43"><input onclick="link2('process/newsletter.jsp','newsletter_attach')" type=button name=add value="<fmt:message key="ADD"/>"></td>
  </tr>
  <tr>
    <th colspan=2><small>&nbsp;<fmt:message key="SUBJECT"/>:&nbsp;</small></th>
    <td><%= subjectElement.toHTML()%></td>
  </tr>
  <tr>
    <th colspan=2><small>&nbsp;<fmt:message key="TEXT"/>:&nbsp;</small></th>
    <td><textarea name=mail rows=5 cols=45><%=Tools.getVal(session, jsp, "mail")%></textarea></td>
  </tr>
</table>
<br>
<table align="center" border="0">
 <col width="80"/><col width="80"/>
<tr>
<td align="center">
 <a href="javascript:link2('process/sent_report.jsp','newsletter_send');"><fmt:message key="SEND"/></a>
</td>
<td align="center">
  <a href="javascript:link2('process/newsletter.jsp','newsletter_cancel');"><fmt:message key="CANCEL"/></a>
</td>
</tr>
</table>

<script language="JavaScript">
//document.Form.BeraterArt.focus()
</script>
<br>
<br>

</form>
</center>
</div>
<div id="wait" style="display:none;">
 <p> <fmt:message key="SEND_STARTED"/> </p>
</div>
</body>
</html>


<% out.flush();
   tx.commit();
   HibernateUtil.closeSession();
   } catch(Exception e) {
   MainControl.logger.error("Error in newsletter", e);
 if (tx!=null)
   try { tx.rollback() ; } catch (Exception e1) {}
 HibernateUtil.closeSession();
 throw e;
} %> 