<%@ 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 generateSortByOptions(HttpSession session) {

    String[] selected = new String[] {"", "", "", "", ""};
    if (getVal("sortBy", session)!=null) {
    	int dd = 1;
    	try {
	    	dd = Integer.parseInt((String)getVal("sortBy", session));
	    } catch (Exception e) {		
	    }
	    selected[dd-1] = " selected";
    }

	StringBuffer sb = new StringBuffer();
	sb.append("<option value=\"1\""+selected[0]+">PV</option>");
	sb.append("<option value=\"2\""+selected[1]+">EV</option>");
	sb.append("<option value=\"3\""+selected[2]+">PEV</option>");
	sb.append("<option value=\"4\""+selected[3]+">TV</option>");
	sb.append("<option value=\"5\""+selected[4]+">OV</option>");
	return sb.toString();
}
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 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 String getVal(String name, HttpSession session) {
	Map m = (Map)session.getAttribute("topten.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].saveState.value = 'nothing';
			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="putFocus(0,3)">
<form action="process/topten.jsp" method="get">
<br><input type="hidden" name="saveState"/>
<input type="hidden" name="pageAction"/>
<input type="hidden" name="link" value="process/topten.jsp"/>
<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="sortBy"><fmt:message key="SORT_BY"/>:</label>
<select name="sortBy"><%=generateSortByOptions(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 ";
        if (session.getAttribute("SHOW_TT")!=null) {
        	String[] checks = (String[])session.getAttribute("tt_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];
        	
        }%>
<label>Kunde</label><input type="checkbox" <%=checked1%> name="role" value="100">
<label>Silber Kunde</label><input type="checkbox" <%=checked2%> name="role" value="130">
<label>Gold Kunde</label><input type="checkbox" <%=checked3%> name="role" value="160"><br>
<label>Platin Kunde</label><input type="checkbox" <%=checked4%> name="role" value="200">
<label>Therapeut</label><input type="checkbox" <%=checked5%> name="role" value="300">
<label>Händler</label><input type="checkbox" <%=checked6%> name="role" value="330">
<label>Apotheker</label><input type="checkbox" <%=checked8%> name="role" value="370">
<label>Vertriebspartner</label><input type="checkbox" <%=checked7%> name="role" value="400"><br>        

<label>&nbsp;</label>
<a onClick="javascript:return check();" href="javascript:top.mainFrame.document.forms[0].pageAction.value = 'show_tt'; link('process/topten.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_tt'; link('process/topten.jsp');">
<fmt:message key="PRINT"/></a><br>
</form>
<% if (session.getAttribute("SHOW_TT")!=null) {
	String dateFrom = (String)session.getAttribute("tt_dateFrom");
	String dateTo = (String)session.getAttribute("tt_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 sb = (String)session.getAttribute("tt_sortBy");
	String[] roles = (String[])session.getAttribute("tt_roles");
	int sortBy = new Integer(sb).intValue();
	if (ok) {%>	
	<%=com.etixpert.evolution.app.statistics.TopTenMembers.generateHTML(df,dt,sortBy, roles, pageContext.getServletContext().getRealPath("WEB-INF"))%>
	<%} else {%>
	<%=exc%>	
	<%}
	session.removeAttribute("SHOW_TT");
}%>
</body></html>