<%@ 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" %>

<html>
<%!
public String getVal(String name, HttpSession session) {
	Map m = (Map)session.getAttribute("password_person_selection.jsp");
	if (m == null) return "";
	String[] vals = (String[])m.get(name);
	if (vals != null && vals.length>0 && vals[0] != null) return vals[0];
	return "";
}
%>
<%

	boolean admin = false;
	
	if (session.getAttribute("admin")!=null) admin = true;	
		
	response.setContentType("text/html; charset=UTF-8");
    request.setCharacterEncoding("UTF-8");
	
	if (session.getAttribute("USER_LOGGED_ID")==null) {
		response.sendRedirect("expired.jsp");
		return;
	}
	
	String ulid = (String)session.getAttribute("USER_LOGGED_ID");
	if ("15".equals(ulid)) admin = false;
	
	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");
     
 %>


<head>
<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 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>
  <body>
    	<form  name="select_person" action="process/password_change.jsp" method="post">
       	    <input type="hidden" name="link" value="process/password_change.jsp"/>
    		<input type="hidden" name="saveState"/>
    		<input type="hidden" name="pageAction" value="start_password_changing"/>
  			<label style="width:200px"><fmt:message key="CHANGE_PASSWORD_FOR"/>:</label>
  			<%
  				if (admin) {
			%>
  			<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("id in (select * from {fn "+right.toLowerCase()+"("+currentUserId+")})");
  					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>
  			  <%
  			    } else {
			   %>
			   		<%=session.getAttribute("USER_LOGGED_ID") + ", " + session.getAttribute("USER_LOGGED_LASTNAME")%>
			   		<input type="hidden" name="cnr" value="<%=session.getAttribute("USER_LOGGED_ID")%>"/>
			   <%}%>
  			  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="return check();" href="javascript:link('process/password_change.jsp', 'start_password_changing');"><fmt:message key="NEXT"/></a>
    	</form>
  </body>
</html>
<% } %>