<%@ page errorPage="errorPage.jsp" 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="com.etixpert.evolution.gui.servlets.*"%>
<%@ 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");
%>
<%!
public String getCountryOptions(HttpSession session) throws Exception{
	StringBuffer sb = new StringBuffer("");
	Country[] countries = FormHelper.getCountries();
	
	String sel0 = "";
	if (getVal("countryID", session).equals("null")) sel0=" selected";
	sb.append("<option"+sel0+" value=\"null\">Gesamt</option>");
	
	
	for (int i = 0; i < countries.length; i++) {
		String selected="";
		if (getVal("countryID", session).equals(countries[i].country_code)) selected=" selected";
		sb.append("<option"+selected+" value=\""+ countries[i].country_code + "\">"+countries[i].name+"</option>");
		
	}
	return sb.toString();
}

public String getDefaultRootID(HttpSession session) {
	String loggedID = (String)session.getAttribute("USER_LOGGED_ID");
	int id = Integer.parseInt(loggedID);
	if (id<1000) return "0";
	else return loggedID;
}

public String getRootID(HttpSession session) {
	if (session.getAttribute("SHOW_UMSATZ1")!=null) return getVal("rootID", session);
	else return getDefaultRootID(session);
}

public java.sql.Date getNormalDate(String value) throws Exception {
	java.sql.Date date;
	java.text.DateFormat df = java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM, Locale.GERMAN);
	java.util.Date d = df.parse(value);
	date = new java.sql.Date(d.getTime());		
	return date;
}

public java.sql.Date getDate(String value) throws Exception {
	java.sql.Date date;
	java.text.DateFormat df = java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM, Locale.GERMAN);
	String value2 = "";
	
		String year = value.substring(0,4);
		String month = value.substring(4);
		
		value2 = "01."+month+"."+year;
		
		java.util.Date d = df.parse(value2);
		date = new java.sql.Date(d.getTime());
		
		return date;
}

public String getVal(String name, HttpSession session) {
	Map m = (Map)session.getAttribute("umsatzproduktion.jsp");
	if (m == null) return "";
	String[] vals = (String[])m.get(name);
	if (vals != null && vals.length>0 && vals[0] != null) return vals[0];
	return "";
}
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">
<link href="main_stat.css" rel="stylesheet" type="text/css">
<SCRIPT LANGUAGE="JavaScript">
<!-- JavaScript code hiding
	function isInteger(s){
		var i;
	    for (i = 0; i < s.length; i++){   
	        // Check that current character is number.
	        var c = s.charAt(i);
	        if (((c < "0") || (c > "9"))) return false;
	    }
	    // All characters are numbers.
	    return true;
	}

	function isDate2(dtStr) {
		if (dtStr.length!=6) return false
	
		var strMonth=dtStr.substring(4)
		var strYear=dtStr.substring(0,4)
	
		if (isInteger(strMonth)==false) return false;
		if (isInteger(strYear)==false) return false;
	
		if (parseInt(strMonth,10)>12) return false;
		if (parseInt(strMonth,10)<1) return false;
	
		if (parseInt(strYear,10)>2200) return false;
		if (parseInt(strYear,10)<1900) return false;
	
		return true

	}

function isDate(dtStr) {
	var dtCh = "."
	if (dtStr.length!=10) return false

	var pos1=dtStr.indexOf(dtCh)
	if (pos1==-1){
		return false
	}

	var pos2=dtStr.indexOf(dtCh,pos1+1)
	
	if (pos2==-1){
		return false
	}
	
	
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)

	if (isInteger(strDay)==false) return false;
	if (isInteger(strMonth)==false) return false;
	if (isInteger(strYear)==false) return false;

	
	if (parseInt(strDay, 10)>31) return false;
	if (parseInt(strDay, 10)<1) return false;

	if (parseInt(strMonth, 10)>12) return false;
	if (parseInt(strMonth, 10)<1) return false;

	if (parseInt(strYear, 10)>2100) return false;
	if (parseInt(strYear, 10)<1900) return false;


	return true

}	
	function check() {
	    var elem = document.forms[0].dateFrom;
	    var elem1 = document.forms[0].dateTo;

		if (!isDate(elem.value)) {
			alert("Bitte geben Sie den Zeitraum im Format TT.MM.JJJJ ein (Date von)!");
			putFocus(0,4);
			return false;
		}
		
		if (!isDate(elem1.value)) {
			alert("Bitte geben Sie den Zeitraum im Format TT.MM.JJJJ ein (Date bis)!");
			putFocus(0,5);
			return false;
		}
		
		
		return true;
	}
	
	function simpleLink(origLink) {
		parent.mainFrame.location = origLink;
	}

	function link0(origLink){
		top.mainFrame.document.forms[0].pageAction.value = '';
		if (top.mainFrame.document.forms.length == 0) {
			simpleLink(origLink);
			return;
		}
		else {
			if (top.mainFrame.document.forms[0].saveState == null) {
				simpleLink(origLink); 
				return;
			}
			top.mainFrame.document.forms[0].saveState.value = top.mainFrame.self.location.pathname;
			top.mainFrame.document.forms[0].link.value = origLink;
			top.mainFrame.document.forms[0].submit();
		}
	}

	function link(origLink){
		if (top.mainFrame.document.forms.length == 0) {
			simpleLink(origLink);
			return;
		}
		else {
			if (top.mainFrame.document.forms[0].saveState == null) {
				simpleLink(origLink); 
				return;
			}
			top.mainFrame.document.forms[0].saveState.value = top.mainFrame.self.location.pathname;
			top.mainFrame.document.forms[0].link.value = origLink;
			top.mainFrame.document.forms[0].submit();
		}
	}
 function putFocus(formInst, elementInst) {
  	if (document.forms.length > 0) {
   		document.forms[formInst].elements[elementInst].focus();
  }
 }
// -->
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
<!-- JavaScript code hiding
	//top.leftFrame.d.openTo(12, true);
// -->
</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;
%>
<body onLoad="javascript:putFocus(0,3);">
<form action="process/umsatzproduktion.jsp" method="get">
<br><input type="hidden" name="saveState"/>
<input type="hidden" name="pageAction"/>
<input type="hidden" name="link" value="process/umsatzprodultion.jsp"/>
<label for="rootID"><fmt:message key="ROOT_ID"/>:</label>
<input name="rootID" value="<%=getRootID(session)%>"/><br>
<label for="dateFrom"><fmt:message key="DATE_FROM"/>:</label>
<input name="dateFrom" value="<%=getVal("dateFrom", session)%>"/><br>
<label for="dateTo"><fmt:message key="DATE_TO"/>:</label>
<input name="dateTo" value="<%=getVal("dateTo", session)%>"/><br>
<label for="dateTo"><fmt:message key="COUNTRY"/>:</label>
<select name="countryID">
          <%=getCountryOptions(session)%>
        </select><br>
 <% 
        String checked1 = " checked ";
        String checked2 = " checked ";
        String checked3 = " checked ";
        String checked4 = " checked ";
        String checked5 = " checked ";
        String checked6 = " checked ";
        String checked7 = " checked ";
        String checked8 = " checked ";
        String checked9 = " checked ";
        String checked10 = " checked ";
        String checked11 = " checked ";
        
        if (session.getAttribute("SHOW_UMSATZ1")!=null) {
        	String[] checks = (String[])session.getAttribute("up_checks");
        	checked1=checks[0];
        	checked2=checks[1];
        	checked3=checks[2];
        	checked4=checks[3];
        	checked5=checks[4];
        	checked6=checks[5];
        	checked7=checks[6];
        	checked8=checks[7];
        	checked9=checks[8];
        	checked10=checks[9];
        	checked11=checks[10];
        }%>
<label>Kunde</label><input type="checkbox" <%=checked1%> name="role" value="100:0">
<label>Silber Kunde</label><input type="checkbox" <%=checked2%> name="role" value="130:0">
<label>Gold Kunde</label><input type="checkbox" <%=checked3%> name="role" value="160:0"><br>
<label>Platin Kunde</label><input type="checkbox" <%=checked4%> name="role" value="200:0">
<label>Therapeut</label><input type="checkbox" <%=checked5%> name="role" value="300:0">
<label>Therapeut-Arzt</label><input type="checkbox" <%=checked6%> name="role" value="300:1"><br>
<label>Händler</label><input type="checkbox" <%=checked7%> name="role" value="330:0">
<label>Grosshändler</label><input type="checkbox" <%=checked8%> name="role" value="350:0">
<label>Apotheker</label><input type="checkbox" <%=checked9%> name="role" value="370:0"><br>
<label>Berater</label><input type="checkbox" <%=checked10%> name="role" value="400:0">
<label>Berater-Arzt</label><input type="checkbox" <%=checked11%> name="role" value="400:1"><br>        
        
<label>&nbsp;</label>
<a onClick="javascript:return check();" href="javascript:top.mainFrame.document.forms[0].pageAction.value = 'show_umsatz1'; link('process/umsatzproduktion.jsp');">
<fmt:message key="SHOW"/></a><br>
<label>&nbsp;</label>
<a onClick="javascript:return check();" href="javascript:top.mainFrame.document.forms[0].pageAction.value = 'print_umsatz1'; link('process/umsatzproduktion.jsp');">
<fmt:message key="PRINT"/></a><br>
</form>
<% if (session.getAttribute("SHOW_UMSATZ1")!=null) {
	String dateFrom = (String)session.getAttribute("umsatz1_dateFrom");
	String dateTo = (String)session.getAttribute("umsatz1_dateTo");
	boolean ok = true;
	String exc = "";
	java.sql.Date df = null;
	java.sql.Date dt = null;
	try {
		df = getNormalDate(dateFrom);
		dt = getNormalDate(dateTo);
	} catch (Exception e) {
		exc = dateFrom+";"+dateTo+"Error in conversion: "+e.getMessage();
		ok = false;
	}
	String id = (String)session.getAttribute("umsatz1_id");
	String country = (String)session.getAttribute("umsatz1_country");
	String[] roles = (String[])session.getAttribute("up_roles");
	if (country.equals("null")) country = null;
	
	try {
		Integer hjk = new Integer(id);
	} catch (Exception e) {
		return;
	}
	
	if (ok) {%>	
	<%=com.etixpert.evolution.app.statistics.Umsatzproduktion.generateHTML(df,dt, roles, new Integer(id), country, pageContext.getServletContext().getRealPath("WEB-INF"))%>
	<%} else {%>
	<%=exc%>	
	<%}
	session.removeAttribute("SHOW_UMSATZ1");
}%>
</body></html>