<%@ page language="java" session="true" contentType="text/html;charset=ISO-8859-2" %>
<%@ page import="com.etixpert.evolution.gui.servlets.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.net.*"%>
<%@ page pageEncoding="UTF-8"%>
<%@ page import="com.etixpert.evolution.gui.servlets.*"%>
<%@ page import="com.etixpert.evolution.*"%>
<%@ include file="header.jsp" %>
<%!
public String getVal(String name, HttpSession session) {
	Map m = (Map)session.getAttribute("new_order_person_select.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 getPMOptions(HttpSession session) throws Exception {
	StringBuffer sb = new StringBuffer("");
	PM[] pms = FormHelper.getPMs();
	
	for (int i = 0; i < pms.length; i++) {
		String selected="";
		if (getVal("pay_method_id", session).equals(pms[i].id)) selected=" selected";	
		sb.append("<option"+selected+" value=\""+ pms[i].id + "\">"+pms[i].value+"</option>");
	}
	return sb.toString();
}
%>
<%

	boolean fo = OrderHelper.isFirstOrder(request);
	
	boolean cancelAnotherOrder = false;

	if (fo && session.getAttribute("new_fo")==null)
		cancelAnotherOrder = true;
	
	if (!fo && session.getAttribute("new_fo")!=null)
		cancelAnotherOrder = true;
	
	if (cancelAnotherOrder) {
		session.setAttribute("new_order.jsp", null);
		Map m = (Map)session.getAttribute("new_order_details");

		session.removeAttribute("NEW_ORDER_STARTED");

		if (m!=null) {
			m.clear();
			m.put("count", new Integer(0));
		}
	}
	
	
	session.removeAttribute("new_fo");
	if (fo)
		session.setAttribute("new_fo", "true");	
		
	response.setContentType("text/html; charset=UTF-8");
    request.setCharacterEncoding("UTF-8");
	
	if (session.getAttribute("USER_LOGGED_ID")==null) {
		response.sendRedirect("expired.jsp");
		return;
	
	} else if (session.getAttribute("new_order.jsp")!=null) {
		response.sendRedirect("process/new_order.jsp");
		return;
	}
	
	if (session.getAttribute("SELF_REDIRECT_STATIC")!=null) {
		session.removeAttribute("SELF_REDIRECT_STATIC");
		session.removeAttribute("ORDER_ADDED");
		response.sendRedirect("order_detail_static.jsp");
		return;
	}
	 
	System.out.println(session.getAttribute("USER_LOGGED_ID").getClass().getName());
	int currentUserId = Integer.valueOf( (String) session.getAttribute("USER_LOGGED_ID")).intValue();
	
	
	ObjectSearch os = new ObjectSearch();
	List l = os.search("select level_id, order_create from distributor d, general_rights r where d.level_id=r.role_id and d.id=?", 
					new Object[]{new Integer(currentUserId)});
	
	
	if (l.size()==0) {
		
		String message=URLEncoder.encode("Distributor mit ungültiger Stufe","UTF-8");
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/ShowMessage.jsp?msg="+message);
		dispatcher.forward(request, response);
		//response.sendRedirect("ShowMessage.jsp?msg="+message);
	} 
	else { 
	 PersistableObject po = (PersistableObject) l.get(0);
	 String right = (String) po.get("order_create");
	 if (right.equalsIgnoreCase("x")) {
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/ShowMessage.jsp?msg="+URLEncoder.encode("Zugriff auf diese Seite verweigert"));
		dispatcher.forward(request, response);
		//response.sendRedirect("ShowMessage.jsp?msg="+URLEncoder.encode("Zugriff auf diese Seite verweigert"));
	 } else if (right.equalsIgnoreCase("s")) {
	 	response.sendRedirect("process/new_order.jsp?pageAction=start_new_order&saveState=nothing&link=process/new_order.jsp&cnr="+session.getAttribute("USER_LOGGED_ID"));
	 	
	 } else {
     
 %>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="css/evolution.css" rel="stylesheet" type="text/css">
<SCRIPT LANGUAGE="JavaScript">
<!-- JavaScript code hiding
	function changeSel(which){
		window = open("leftMenu.jsp?sel="+which, "_self", "");
	}

	function simpleLink(origLink) {
		parent.mainFrame.location = origLink;
	}

function link(origLink, page_action){
		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].pageAction.value = page_action;
			top.mainFrame.document.forms[0].submit();
		}
	}
// -->
</SCRIPT>
<script language="JavaScript">
	
	function FillText() {
		var sel = document.select_person.dist_id2;
		document.select_person.cnr.value=sel.options[sel.selectedIndex].value;
	}

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");
	document.forms[0].cnr.focus();
	document.forms[0].cnr.select();
}

function check() {
	if (document.forms[0].cnr.value == '') {
		alert("Ungültige DistID");
		return false;
	}
	return true;
}

</script>
</head>
<html>
  <body onLoad="backButtonOverride()">
  		<%=OrderHelper.getMessage(request) %>
  			<%

  			%>

    	<form  name="select_person" action="process/new_order.jsp" method="post">
       	    <input type="hidden" name="link" value="process/new_order.jsp"/>
    		<input type="hidden" name="saveState"/>
    		<input type="hidden" name="pageAction" value="start_new_order"/>
  			<label><fmt:message key="<%=fo?"HANDLER_FIRST_ORDER": "DISTRIBUTOR_CUSTOMER"%>"/>:</label>
  			<input type="text" size="8" name="cnr" onchange="dist_id2_changed()"/>
  			<select name="dist_id2" onchange="cnr_changed()" onblur="cnr_changed()">
				<option value="" selected></option>  				
				<%
				
					
  					List l1 = new ArrayList();
  					l1.add("id");
  					os = new ObjectSearch("distributor",l1);
  					os.setCondition("active=true and id in (select * from {fn "+right.toLowerCase()+"("+currentUserId+")})" + (fo?" and (level_id = 300)":""));
  					os.setOrderBy(new String[]{"lastname","firstname"});
  					l = os.search();
  					int did = -1;
  					if (!getVal("dist_id2", session).equals("")) 
  						did = Integer.valueOf(getVal("dist_id2", session)).intValue();
  						
  						
  					for (int i=0; i<l.size(); i++) {
  					  po = (PersistableObject) l.get(i);
  				%>  
  				
  					<option value="<%=((Integer) po.get("id")).intValue()%>"				 	
  					 <% if (getVal("dist_id2", session).equals("")) {
  					 	   if (  (( Integer)po.get("id")).intValue()== currentUserId) { %>
  					 	 		"" 
  							 <%} 
  					    } else 
  					    	if (did == ((Integer) po.get("id")).intValue()) {%><% } %>
  					 	 
  					><%=po.get("lastname")%> <%=po.get("firstname")%> </option>
  				<%
  				  out.println("\n");	}
  				%>
  			  </select><br>
  			  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="return check();" href="javascript:link('process/new_order.jsp', 'start_new_order');"><fmt:message key="NEXT"/></a>
    	</form>

  </body>
</html>
<% }} %>