<%@ page language="java" session="true" buffer="2048kb" contentType="text/html;charset=ISO-8859-2" %>
<%@ page pageEncoding="UTF-8"%>
<%@ page import="com.etixpert.evolution.gui.servlets.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.net.*"%>
<%@ page import="com.etixpert.evolution.gui.servlets.*"%>
<%@ page import="com.etixpert.evolution.*"%>
<%@ page import="java.sql.*"%>
<%@ include file="header.jsp" %>
<%@ include file="check.jsp" %>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">
<link href="css/evolution.css" rel="stylesheet" type="text/css">
<script language="JavaScript">

var elementValues = new Array ();

function getElementValue (element) {
    if (element.type == "checkbox" || element.type == "radio")
        return element.checked;
    else if (element.type.indexOf("select") >= 0)
        return element.selectedIndex;
    else if (element.type == "hidden")
        return "hidden";
    else
        return element.value;
}

function saveValues (form) {
    for (var i = 0; i < form.elements.length; i++) {
        var element = form.elements[i];
        elementValues[i] = getElementValue (element);
    }
}

function hasChanged (form) {
    if (form.elements.length != elementValues.length)
        return true;

    for (var i = 0; i < form.elements.length; i++) {
        var element = form.elements[i];
        if (element.name.indexOf('art_id')!=-1 || element.name.indexOf('quantity')!=-1)
        if (getElementValue (element) != elementValues[i])
            return true;
    }

    return false;
}




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

<% 
boolean admin = session.getAttribute("admin")!=null;
%>

function checkTotals (form) {
	if (top.mainFrame.document.forms[0].total_brutto.value < 0 && document.all['pay_method_id'].value != 4) {
		alert('Total Brutto darf nicht negative sein!');
		return false;
	}
	return true;
}

function checkAddresses() {
	if (top.mainFrame.document.forms[0].billing_postcode.value == '<PLZ>')
		top.mainFrame.document.forms[0].billing_postcode.value = '';
	if (top.mainFrame.document.forms[0].billing_city.value == '<Ort>')
		top.mainFrame.document.forms[0].billing_city.value = '';
	if (top.mainFrame.document.forms[0].billing_street.value == '<Strasse>')
		top.mainFrame.document.forms[0].billing_street.value = '';
	if (top.mainFrame.document.forms[0].delivery_postcode.value == '<PLZ>')
		top.mainFrame.document.forms[0].delivery_postcode.value = '';
	if (top.mainFrame.document.forms[0].delivery_city.value == '<Ort>')
		top.mainFrame.document.forms[0].delivery_city.value = '';
	if (top.mainFrame.document.forms[0].delivery_street.value == '<Strasse>')
		top.mainFrame.document.forms[0].delivery_street.value = '';		
	if (top.mainFrame.document.forms[0].art_id.value != '') return false;
	if (hasChanged(top.mainFrame.document.forms[0])) return false;
	if (!checkTotals(document.forms[0])) return false;

	return true;
}

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();
		}
	}
	
function showHideBankCC() {
	if (document.all['pay_method_id'].value==1) {
		 document.all['bankselect'].style.display='block';
		document.all['creditselect'].style.display='none';
		document.all['sconto_erlag'].style.display='none';
 		} else if (document.all['pay_method_id'].value==2) {
		 document.all['bankselect'].style.display='none';
		 document.all['creditselect'].style.display='block';
		 document.all['sconto_erlag'].style.display='none';
		} else {
		 document.all['bankselect'].style.display='none';
		 document.all['creditselect'].style.display='none';
		 if (document.all['pay_method_id'].value==6) {
			 document.all['sconto_erlag'].style.display='block';
		 } else
			 document.all['sconto_erlag'].style.display='none';
		}		
		
		//if (top.mainFrame.document.forms[0].total_brutto.value < 0 && document.all['pay_method_id'].value != 4) {
		//	alert('Wenn das Stück negativer Wert ist, sollte Vorauskassa auswählt werden.');
		//	document.all['pay_method_id'].value=4;
		//}
	}	
</script>
</head>

  
<%	

String focused = (String)session.getAttribute("to_det_pos");
if (focused == null) focused = "art_id";
if (session.getAttribute("order_error")!=null) { %>

 <body onload="alert('<%=session.getAttribute("order_error")%>'); saveValues(document.forms[0]);" onload="showHideBankCC();">

<% session.removeAttribute("order_error");
 } else if (session.getAttribute("ORDER_STATUS_CHANGED_IN_THE_BACKGROUND")!=null) { %>
 
<body onload="alert('Order modified in the background! Cannot be saved!'); saveValues(document.forms[0]);" onload="showHideBankCC();"> 
 
 <%
 session.removeAttribute("ORDER_STATUS_CHANGED_IN_THE_BACKGROUND");
 } 
 else if (session.getAttribute("ORDER_MODIFIED")!=null) { %>
 <body onload="alert('Bestellung modifiziert'); saveValues(document.forms[0]); showHideBankCC();"> 
<% session.removeAttribute("ORDER_MODIFIED");
 } else { %>
 <body onload="document.forms[0].<%=focused%>.focus(); document.forms[0].<%=focused%>.select(); saveValues(document.forms[0]); showHideBankCC(); ">
<% } %>
<%
	String jsp = "order_detail.jsp";

Map orders = (Map)session.getAttribute("order_details");
String level_id = (String)orders.get("ordering_level_id");
String level_subtype_id = (String)orders.get("ordering_level_subtype_id");
String sponsor = OrderHelper.getVal(session, jsp, "sponsor");
boolean customerDiscount = "0".equals(sponsor) || "43002857".equals(sponsor) || "43005029".equals(sponsor);

	boolean[] rights = OrderHelper.getRights(session, (String) session.getAttribute("CURRENT_ORDER_ID"), null, OrderHelper.MASK_MODIFY); 
	boolean canmod = rights[OrderHelper.INDEX_MODIFY];
	
	
%>
<!--<jsp:include page="MenuHelper.jsp">
	<jsp:param name="self_id" value="none"/>
	<jsp:param name="parent_id" value="5200"/>	
</jsp:include> -->
<center><h1><fmt:message key="ORDER_MODIFY"/></h1></center>
<div width="800" height="30" align="left" class="menu">&nbsp;&nbsp;&nbsp;<a class="menu" href="process/order_selection.jsp">Andere Bestellung auswählen</a></div>
<form name=new_order action="process/order_detail.jsp" method="post">	
		<input type="hidden" name="link" value="process/order_detail.jsp"/>
    	<input type="hidden" name="saveState"/>
    	<input type="hidden" name="pageAction"/>
    	<input type="hidden" name="pos"/>
	
	<label for="ordered_by"><fmt:message key="ROOT_ID"/>:</label>
	<input name="ordered_by" class="small0" value="<%=OrderHelper.getVal(session, jsp, "ordered_by")%>" style="border=0px;" readonly>

	<label for="distr_type"><fmt:message key="DISTRIBUTOR_TYPE"/>:</label>
	<input name="distr_type" class="small1" value="<%=OrderHelper.getVal(session, jsp, "distr_type")%>" style="border=0px;" readonly>


	<label for="sponsor"><fmt:message key="SPONSOR"/>:</label>
	<input name="sponsor" class="small0" value="<%=sponsor%>" style="border=0px;" readonly><br>
	
	<input type="hidden" name="orderingCountry" value="<%=OrderHelper.getVal(session, jsp, "orderingCountry")%>"/>
	<input type="hidden" name="UID" value="<%=OrderHelper.getVal(session, jsp, "UID")%>"/>
	<input type="hidden" name="orderingLevelId" value="<%=OrderHelper.getVal(session, jsp, "orderingLevelId")%>"/>
	<input type="hidden" name="orderingLevelSubtypeId" value="<%=OrderHelper.getVal(session, jsp, "orderingLevelSubtypeId")%>"/>
	<input type="hidden" name="orderingDiscountRate" value="<%=OrderHelper.getVal(session, jsp, "orderingDiscountRate")%>"/>
	<input type="hidden" name="canmod" value="<%=OrderHelper.getVal(session, jsp, "canmod")%>"/>
	
	<input type="hidden" name="order_id" value="<%=session.getAttribute("CURRENT_ORDER_ID")%>"/>
	
	<label for="tel"><fmt:message key="TEL"/>:</label>
	<input name="tel" class="large" value="<%=OrderHelper.getVal(session, jsp, "tel")%>" style="border=0px;" readonly><br>

	<label for="delivery_title"><fmt:message key="DELIVERY_LEVEL_ADDRESS"/>:</label>
	<input name="delivery_title" class="little" value="<%=OrderHelper.getVal(session, jsp, "delivery_title")%>"><br>

	<label for="delivery_name"><fmt:message key="DELIVERY_NAME"/>:</label>
	<input maxlength="70" name="delivery_name" class="medium" value="<%=OrderHelper.getVal(session, jsp, "delivery_name")%>"><br>
	<%
		String company = OrderHelper.getVal(session, jsp, "company");
		if (company != null && !"".equals(company)) {
	%>
		<label for="company"><fmt:message key="COMPANY"/>:</label>
	<input maxlength="70" name="company" class="medium" value="<%=company%>" readonly><br>
	
	
	<% 
		}
	
	%>
	<label for="delivery_country_id"><fmt:message key="DELIVERY_ADDRESS"/>:</label>
	<%
		String del_country_id = OrderHelper.getVal(session, jsp, "delivery_country_id");
		String del_postcode = OrderHelper.getVal(session, jsp, "delivery_postcode");
		String del_city = OrderHelper.getVal(session, jsp, "delivery_city");
		String del_street = OrderHelper.getVal(session, jsp, "delivery_street");
		
		String del_focus = "";
		String del_city_focus = "";
		String del_street_focus = "";
		String del_country_focus = "";
		
		if (del_country_id.equals("") || del_country_id.equals("<Land>")) {
			del_country_focus = "this.value=''";
			del_country_id = "<Land>";
		}		
		if (del_postcode.equals("") || del_postcode.equals("<PLZ>")) {
			del_focus = "this.value=''";
			del_postcode = "<PLZ>";
		}
		if (del_city.equals("") || del_city.equals("<Ort>")) {
			del_city_focus = "this.value=''";
			del_city = "<Ort>";
		}
		if (del_street.equals("") || del_street.equals("<Strasse>")) {
			del_street_focus = "this.value=''";
			del_street = "<Strasse>";
		}
	%>
	<input name="delivery_country_id" maxlength="2" value="<%=del_country_id%>" class="small0" onFocus="<%=del_country_focus%>"/>
	<input maxlength="8" name="delivery_postcode" class="little" value="<%=del_postcode%>" onFocus="<%=del_focus%>"/>
	<input name="delivery_city" value="<%=del_city%>" onFocus="<%=del_city_focus%>"/>
	<input maxlength="50" name="delivery_street" class="medium" value="<%=del_street%>" onFocus="<%=del_street_focus%>"/><br>

	
	<label for="billing_title"><fmt:message key="BILLING_LEVEL_ADDRESS"/>:</label>
	<input name="billing_title" class="little" value="<%=OrderHelper.getVal(session, jsp, "billing_title")%>"><br>
	
	<label for="billing_name"><fmt:message key="BILLING_NAME"/>:</label>
		<%  String billingName =  OrderHelper.getVal(session, jsp, "billing_name"); 
		//if (billingName == null || " ".equals(billingName))
		if (company != null && !"".equals(company))
			billingName = company;

	%>	
	<input maxlength="70" name="billing_name" class="medium" value="<%=billingName%>">
	<br>
	
	<label for="billing_country_id"><fmt:message key="BILLING_ADDRESS"/>:</label>
	<%
		String bill_country_id = OrderHelper.getVal(session, jsp, "billing_country_id");
		String bill_postcode = OrderHelper.getVal(session, jsp, "billing_postcode");
		String bill_city = OrderHelper.getVal(session, jsp, "billing_city");
		String bill_street = OrderHelper.getVal(session, jsp, "billing_street");
		
		String bill_focus = "";
		String bill_city_focus = "";
		String bill_street_focus = "";
		String bill_country_focus = "";
		
		if (bill_country_id.equals("") || bill_country_id.equals("<LAND>")) {
			bill_country_focus = "this.value=''";
			bill_country_id = "<Land>";
		}
		if (bill_postcode.equals("") || bill_postcode.equals("PLZ")) {
			bill_postcode = "<PLZ>";
			bill_focus = "this.value=''";
		}
		if (bill_city.equals("") || bill_city.equals("<Ort>")) {
			bill_city_focus = "this.value=''";
			bill_city = "<Ort>";
		}
		if (bill_street.equals("") || bill_street.equals("<Strasse>")) {
			bill_street_focus = "this.value=''";
			bill_street = "<Strasse>";
		}
		
	%>
	<input name="billing_country_id" maxlength="2" value="<%=bill_country_id%>" class="small0" onFocus="<%=bill_country_focus%>"/>
	<input name="billing_postcode" maxlength="8" class="little" value="<%=bill_postcode%>" onFocus="<%=bill_focus%>"/>
	<input name="billing_city" value="<%=bill_city%>" onFocus="<%=bill_city_focus%>"/>
	<input maxlength="50" name="billing_street" class="medium" value="<%=bill_street%>" onFocus="<%=bill_street_focus%>"/><br>
			
	<label for="pay_method_id"><fmt:message key="PAYMENT_METHOD"/>:</label>
	<select name="pay_method_id" onchange="showHideBankCC();" onfocus="showHideBankCC();">
	<%=OrderHelper.getPaymentOptions(pageContext, jsp)%>
	</select>
	<div id="bankselect" style="display:none;">
		<select name="account_id_1">
			<%=OrderHelper.getAccountOptions(session, jsp, "bank")%>
		</select>
	</div>
	<div id="creditselect" style="display:none;">
		<select name="account_id_2">
			<%=OrderHelper.getAccountOptions(session, jsp, "credit")%>
		</select>
	</div>
	<br>
	
	<label for="shipping_descr"><fmt:message key="SHIPPING_TYPE"/>:</label>
	<select name="shipping_descr">
		<%=OrderHelper.getShippingOptions(pageContext, jsp)%>
	</select>
	<br>
	
	<label for="comment"><fmt:message key="COMMENT"/>:</label>
	<textarea name="comment" rows="3" class="large"><%=OrderHelper.getVal(session, jsp, "comment")%></textarea>
	<br>
	<% if (session.getAttribute("modify_fo")!=null && admin) {%>
		<label for="loaded_special_discount"><fmt:message key="DISCOUNT_RATE"/>:</label>
		<% 
			String sd = (String)session.getAttribute("loaded_special_discount");
			if (sd == null) sd = "5";
		%>
		<input name="loaded_special_discount" class="little" value="<%=sd%>"/>&nbsp;&nbsp;
		<a href="javascript:link('process/order_detail.jsp', 'loadedspecialdiscountchanged');">Ok</a>
		<br>
	<% } else if ("300".equals(level_id) && admin) {%>
	<label for="loaded_special_added_discount"><fmt:message key="DISCOUNT_RATE"/>:</label>
	<% 
		String sd = (String)session.getAttribute("loaded_special_added_discount");
		if (sd == null) sd = "0";
	%>
	<input name="loaded_special_added_discount" class="little" value="<%=sd%>"/>&nbsp;&nbsp;
	<a href="javascript:link('process/order_detail.jsp', 'loadedspecialaddeddiscountchanged');">Ok</a>
	<br>
		<% } else if (("100".equals(level_id) || "130".equals(level_id) || "160".equals(level_id) || "200".equals(level_id)) && customerDiscount && admin) {%>
	<label for="loaded_special_discount_customer"><fmt:message key="DISCOUNT_RATE"/>:</label>
	<% 
		String sd = (String)session.getAttribute("loaded_special_discount_customer");
		if (sd == null) {
			session.setAttribute("loaded_special_discount_customer", "0");
			sd = "0";
		}
	%>
	<input name="loaded_special_discount_customer" class="little" value="<%=sd%>"/>&nbsp;&nbsp;
	<a href="javascript:link('process/order_detail.jsp', 'loadedspecialdiscountcchanged');">Ok</a>
	<br>		
	<% }%>
		<% if ("350".equals(level_id) && admin) {%>
		<div id="sconto_erlag" style="display:none;"></div>
		<label for="loaded_special_discount_grosshandler"><fmt:message key="DISCOUNT_RATE"/>:</label>
	<% 
		String sd = (String)session.getAttribute("loaded_special_discount_grosshandler");
		if (sd == null) sd = "20";
	%>
	<input name="loaded_special_discount_grosshandler" class="little" value="<%=sd%>"/>&nbsp;&nbsp;
	<a href="javascript:link('process/order_detail.jsp', 'loadedspecialdiscountghchanged');">Ok</a>
	<br>
	<label for="modify_sconto_days"><fmt:message key="SCONTO_DAYS"/>:</label>
	<% 
		String scontodays = OrderHelper.getVal(session, jsp, "modify_sconto_days");
		if (scontodays == null || "".equals(scontodays)) scontodays = "21";
	%>
	<input name="modify_sconto_days" class="little" value="<%=scontodays%>"/>&nbsp;&nbsp;
	<br>
	<% } else if ("370".equals(level_id) && admin) {%>
		<div id="sconto_erlag" style="display:none;"></div>
		<label for="loaded_special_discount_apotheker"><fmt:message key="DISCOUNT_RATE"/>:</label>
	<% 
		String sd = (String)session.getAttribute("loaded_special_discount_apotheker");
		if (sd == null) sd = "0";
	%>
	<input name="loaded_special_discount_apotheker" class="little" value="<%=sd%>"/>&nbsp;&nbsp;
	<a href="javascript:link('process/order_detail.jsp', 'loadedspecialdiscountachanged');">Ok</a>
	<br>
	<label for="modify_sconto_days"><fmt:message key="SCONTO_DAYS"/>:</label>
	<% 
		String scontodays = OrderHelper.getVal(session, jsp, "modify_sconto_days");
		if (scontodays == null || "".equals(scontodays)) scontodays = "8";
	%>
	<input name="modify_sconto_days" class="little" value="<%=scontodays%>"/>&nbsp;&nbsp;
	<br>	
	<% } else if ("330".equals(level_id) && admin) {%>
		<div id="sconto_erlag" style="display:none;"></div>
		<label for="loaded_special_discount_handler"><fmt:message key="DISCOUNT_RATE"/>:</label>
	<% 
		String sd = (String)session.getAttribute("loaded_special_discount_handler");
		if (sd == null) sd = "0";
	%>
	<input name="loaded_special_discount_handler" class="little" value="<%=sd%>"/>&nbsp;&nbsp;
	<a href="javascript:link('process/order_detail.jsp', 'loadedspecialdiscounthchanged');">Ok</a>
	<br>
	<label for="modify_sconto_days"><fmt:message key="SCONTO_DAYS"/>:</label>
	<% 
		String scontodays = OrderHelper.getVal(session, jsp, "modify_sconto_days");
		if (scontodays == null || "".equals(scontodays)) scontodays = "8";
	%>
	<input name="modify_sconto_days" class="little" value="<%=scontodays%>"/>&nbsp;&nbsp;
	<br>
	<% } else { 
		boolean fo = session.getAttribute("modify_fo")!=null;
		String inputName = fo? "modify_fo_sconto_days" : "modify_sconto_days"; %>
	<div id="sconto_erlag" style="display:none;">
	<label for="<%=inputName%>"><fmt:message key="SCONTO_DAYS"/>:</label>
	<% 
		String scontodays = OrderHelper.getVal(session, jsp, fo?"modify_fo_sconto_days":"modify_sconto_days");
		if (scontodays == null || "".equals(scontodays)) scontodays = fo?"14" : "8";
	%>
	<input name="<%=inputName%>" class="little" value="<%=scontodays%>"/>&nbsp;&nbsp;
	<br>
	</div>
	<% } %>		
<% if (canmod) {%>
<div width="800" height="30" align="left" class="menu">
 &nbsp;&nbsp;&nbsp;
 <a onClick="return checkAddresses();" href="javascript:top.mainFrame.document.forms[0].pageAction.value = 'modify_order'; link('process/order_detail.jsp', 'modify_order');">Modifizieren</a>
 &nbsp;&nbsp;&nbsp;
  <% if (admin) {%>
 <a onClick="return checkAddresses();" href="javascript:link('process/order_detail.jsp', 'order_add_mail');"><fmt:message key="DO_ORDER3"/></a>
 &nbsp;&nbsp;&nbsp; 
 <% }%> 
 <a href="javascript:top.mainFrame.document.forms[0].pageAction.value = 'load_order'; link('process/order_detail.jsp', 'load_order');">Aus Datenbank laden</a>
 </div>
 <% } %>
	<br>

	<%=com.etixpert.evolution.gui.servlets.OrderHelper.generateOrderUpperTotalRow(pageContext, "order_details")%>	
	<%=com.etixpert.evolution.gui.servlets.OrderHelper.generateOrderDetailsHeader(pageContext, "order_details")	%>
	<%=com.etixpert.evolution.gui.servlets.OrderHelper.generateOrderDetailsRows(session, "order_details")%>	
	<%=com.etixpert.evolution.gui.servlets.OrderHelper.generateOrderTotalRow(pageContext, "order_details")%>	

	<br>
<% if (canmod) {%>
<div width="800" height="30" align="left" class="menu">
 &nbsp;&nbsp;&nbsp;
 <a onClick="return checkAddresses();" href="javascript:top.mainFrame.document.forms[0].pageAction.value = 'modify_order'; link('process/order_detail.jsp', 'modify_order');">Modifizieren</a>
 &nbsp;&nbsp;&nbsp;
  <% if (admin) {%>
 <a onClick="return checkAddresses();" href="javascript:link('process/order_detail.jsp', 'order_add_mail');"><fmt:message key="DO_ORDER3"/></a>
 &nbsp;&nbsp;&nbsp; 
 <% }%> 
 <a href="javascript:top.mainFrame.document.forms[0].pageAction.value = 'load_order'; link('process/order_detail.jsp', 'load_order');">Aus Datenbank laden</a>
 </div>
 <% } %>
</form>
<a name="bottom"></a>
</body>
</html>
