<%@ 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 getDistributorOptions(HttpSession session) throws Exception{
	StringBuffer sb = new StringBuffer("");
	DistributorMin[] pms = FormHelper.getDistMin();

	for (int i = 0; i < pms.length; i++) {
		String selected="";
		if (getVal("downline", session).equals("") && pms[i].id.equals("0")) selected = " selected";
		else if (getVal("downline", session).equals(pms[i].id)) selected=" selected";
		sb.append("<option"+selected+" value=\""+ pms[i].id + "\">"+pms[i].fullname+"</option>");
	}
	return sb.toString();
}

public String generateSortByOptions(HttpSession session) {
	StringBuffer sb = new StringBuffer();
	
	String[] sels = new String[] {"", "", "", ""};
	
	String sortb = getVal("sortBy", session);
	
	if (sortb.equals("1")) sels[0] = " selected";
	if (sortb.equals("2")) sels[1] = " selected";
	if (sortb.equals("3")) sels[2] = " selected";
	if (sortb.equals("4")) sels[3] = " selected";
	
	sb.append("<option"+sels[0]+" value=\"1\">Distr_Id</option>");
	sb.append("<option"+sels[1]+" value=\"2\">Nachname</option>");
	sb.append("<option"+sels[2]+" value=\"3\">Vorname</option>");
	sb.append("<option"+sels[3]+" value=\"4\">Anzahl</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("new_member.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();
  }
 }
 
  function downline_changed() {
	dfv = document.forms[0].downline
	document.forms[0].downline_name.value = dfv.options[dfv.selectedIndex].value
}

  function downline_name_changed() {
	dfv = document.forms[0].downline
	for (i = 0; i < dfv.length; i++) {
		if (dfv.options[i].value == document.forms[0].downline_name.value) {
			dfv.selectedIndex = i
			return
		}
	}
	alert("Ungltige Distributornummer")
	document.forms[0].downline_name.focus()
	document.forms[0].downline_name.select()
   }
 
// -->
</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/new_member.jsp" method="get">
<br><input type="hidden" name="saveState"/>
<input type="hidden" name="pageAction"/>
<input type="hidden" name="link" value="process/new_member.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>
<label><fmt:message key="ROOT_ID"/></label>
<input name="downline_name" id="medium" value="<%=(getVal("downline_name", session)==null || "".trim().equals(getVal("downline_name", session)))?"0":getVal("downline_name", session)%>" onChange="downline_name_changed();"/>
<select name="downline" onChange="downline_changed();">
        <%=getDistributorOptions(session)%>
        </select><br>
<label>&nbsp;</label>
<a onClick="javascript:return check();" href="javascript:top.mainFrame.document.forms[0].pageAction.value = 'show_nm'; link('process/new_member.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_nm'; link('process/new_member.jsp');">
<fmt:message key="PRINT"/></a><br>
</form>
<% if (session.getAttribute("SHOW_NM")!=null) {
	String dateFrom = (String)session.getAttribute("nm_dateFrom");
	String dateTo = (String)session.getAttribute("nm_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;
	}
	
	Integer root = (Integer)session.getAttribute("nm_root");
	
	
	String sb = (String)session.getAttribute("nm_sortBy");
	int sortBy = new Integer(sb).intValue();
	if (ok) {%>	
	<%=com.etixpert.evolution.app.statistics.NewMember.generateHTML(df,dt,sortBy, root, pageContext.getServletContext().getRealPath("WEB-INF"))%>
	<%} else {%>
	<%=exc%>	
	<%}
	session.removeAttribute("SHOW_NM");
}%>

</body></html>