<%@ page language="java" session="true" buffer="100kb" autoFlush="false" 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.*"%>
<%@ 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 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 simpleLink(origLink) {
		parent.mainFrame.location = origLink;
	}
<% 
boolean admin = session.getAttribute("admin")!=null;
%>

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 != '') {
		//document.forms[0].saveState.value='process/new_order';
		//document.forms[0].pageAction.value='new_orderadd';
		//document.forms[0].submit();
		return false;
	}
	if (hasChanged(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 jsp = "new_order.jsp";
	String focused = (String)session.getAttribute("to_new_pos");
	
	Map orders = (Map)session.getAttribute("new_order_details");
	String level_id = (String)orders.get("ordering_level_id");
	String level_subtype_id = (String)orders.get("ordering_level_subtype_id");
	
	if (focused == null) focused = "art_id";
%>

<%	if (session.getAttribute("ORDER_ADDED")!=null) { 
 		String orderadded = (String)session.getAttribute("ORDER_ADDED");
		session.removeAttribute("ORDER_ADDED");
		session.removeAttribute("new_order.jsp"); 	
 %>
 <body>
 <script language=javascript>
	alert('Bestellung <%=orderadded%> wurde erfolgreich angelegt!');
	parent.mainFrame.location = 'new_order_person_select.jsp';
</script>
</body></html>
<%
 } else {
 	String sponsor = OrderHelper.getVal(session, jsp, "sponsor");
 	boolean customerDiscount = "0".equals(sponsor) || "43002857".equals(sponsor) || "43005029".equals(sponsor);
 	String _tot = com.etixpert.evolution.gui.servlets.OrderHelper.generateOrderUpperTotalRow(pageContext, "new_order_details");
 	String _det = com.etixpert.evolution.gui.servlets.OrderHelper.generateOrderDetailsHeader(pageContext, "new_order_details");
 	String _dat = com.etixpert.evolution.gui.servlets.OrderHelper.generateOrderDetailsRows(session, "new_order_details");
 	String _bot = com.etixpert.evolution.gui.servlets.OrderHelper.generateOrderTotalRow(pageContext, "new_order_details");
 	out.flush();
 	if (session.getAttribute("new_order_error")!=null) {%>
		 <body onload="alert('<%=session.getAttribute("new_order_error")%>'); saveValues(document.forms[0]);" onload="showHideBankCC();">
	<% session.removeAttribute("new_order_error");
	 } else { %>
	 <body onload="document.forms[0].<%=focused%>.focus(); document.forms[0].<%=focused%>.select(); showHideBankCC(); saveValues(document.forms[0]);">
	<% } %>
<h1 align="center"><fmt:message key="NEW_ORDER"/></h1>
<form name=new_order 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"/>
    	<input type="hidden" name="pos"/>
	<input type="submit" style="width:0px; height: 0px;"/>
	<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="small1" value="<%=sponsor%>" style="border=0px;" readonly><br>

	<label for="tel"><fmt:message key="TEL"/>:</label>
	<input name="tel" class="large" value="<%=OrderHelper.getVal(session, jsp, "tel")%>" 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")%>"/>

	<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 name="delivery_postcode" maxlength="8" 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>


	<label for="shipping_descr"><fmt:message key="SHIPPING_TYPE"/>:</label>
	<select name="shipping_descr">
		<%=OrderHelper.getShippingOptions(pageContext, jsp, true)%>
	</select>
	<br>

	<label for="comment"><fmt:message key="COMMENT"/>:</label>
	<textarea name="comment" rows="3" class="very_large"><%=OrderHelper.getVal(session, jsp, "comment")%></textarea>
	<br>
	<% if (session.getAttribute("new_fo")!=null && admin) {%>
	<label for="special_discount"><fmt:message key="DISCOUNT_RATE"/>:</label>
	<% 
		String sd = (String)session.getAttribute("special_discount");
		if (sd == null) {
			session.setAttribute("special_discount", "5");
			sd = "5";
		}
	%>
	<input name="special_discount" class="little" value="<%=sd%>"/>&nbsp;&nbsp;
	<a href="javascript:link('process/new_order.jsp', 'specialdiscountchanged');">Ok</a>
	<br>
	<% }  else if ("300".equals(level_id) && admin) {%>
	<label for="special_added_discount"><fmt:message key="DISCOUNT_RATE"/>:</label>
	<% 
		String sd = (String)session.getAttribute("special_added_discount");
		if (sd == null) {
			session.setAttribute("special_added_discount", "0");
			sd = "0";
		}
	%>
	<input name="special_added_discount" class="little" value="<%=sd%>"/>&nbsp;&nbsp;
	<a href="javascript:link('process/new_order.jsp', 'specialaddeddiscountchanged');">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="special_discount_customer"><fmt:message key="DISCOUNT_RATE"/>:</label>
	<% 
		String sd = (String)session.getAttribute("special_discount_customer");
		if (sd == null) {
			session.setAttribute("special_discount_customer", "0");
			sd = "0";
		}
	%>
	<input name="special_discount_customer" class="little" value="<%=sd%>"/>&nbsp;&nbsp;
	<a href="javascript:link('process/new_order.jsp', 'specialdiscountcchanged');">Ok</a>
	<br>		
	<% } %>
	<% if ("350".equals(level_id) && admin) {%>
	<div id="sconto_erlag" style="display:none;"></div>
		<label for="special_discount_grosshandler"><fmt:message key="DISCOUNT_RATE"/>:</label>
	<% 
		String sd = (String)session.getAttribute("special_discount_grosshandler");
		if (sd == null) {
			session.setAttribute("special_discount_grosshandler", "20");
			sd = "20";
		}
	%>
	<input name="special_discount_grosshandler" class="little" value="<%=sd%>"/>&nbsp;&nbsp;
	<a href="javascript:link('process/new_order.jsp', 'specialdiscountghchanged');">Ok</a>
	<br>
	<label for="new_sconto_days"><fmt:message key="SCONTO_DAYS"/>:</label>
	<% 
		String scontodays = OrderHelper.getVal(session, jsp, "new_sconto_days");
		if (scontodays == null || "".equals(scontodays)) scontodays = "21";
	%>
	<input name="new_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="special_discount_apotheker"><fmt:message key="DISCOUNT_RATE"/>:</label>
	<% 
		String sd = (String)session.getAttribute("special_discount_apotheker");
		if (sd == null) {
			session.setAttribute("special_discount_apotheker", "0");
			sd = "0";
		}
	%>
	<input name="special_discount_apotheker" class="little" value="<%=sd%>"/>&nbsp;&nbsp;
	<a href="javascript:link('process/new_order.jsp', 'specialdiscountachanged');">Ok</a>
	<br>
	<label for="new_sconto_days"><fmt:message key="SCONTO_DAYS"/>:</label>
	<% 
		String scontodays = OrderHelper.getVal(session, jsp, "new_sconto_days");
		if (scontodays == null || "".equals(scontodays)) scontodays = "8";
	%>
	<input name="new_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="special_discount_handler"><fmt:message key="DISCOUNT_RATE"/>:</label>
	<% 
		String sd = (String)session.getAttribute("special_discount_handler");
		if (sd == null) {
			session.setAttribute("special_discount_handler", "0");
			sd = "0";
		}
	%>
	<input name="special_discount_handler" class="little" value="<%=sd%>"/>&nbsp;&nbsp;
	<a href="javascript:link('process/new_order.jsp', 'specialdiscounthchanged');">Ok</a>
	<br>
	<label for="new_sconto_days"><fmt:message key="SCONTO_DAYS"/>:</label>
	<% 
		String scontodays = OrderHelper.getVal(session, jsp, "new_sconto_days");
		if (scontodays == null || "".equals(scontodays)) scontodays = "8";
	%>
	<input name="new_sconto_days" class="little" value="<%=scontodays%>"/>&nbsp;&nbsp;
	<br>
	
	<%
	  } else { 
		boolean fo = session.getAttribute("new_fo")!=null;
		String inputName = fo? "new_fo_sconto_days" : "new_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?"new_fo_sconto_days":"new_sconto_days");
		
	if (scontodays == null || "".equals(scontodays)) scontodays = fo?"14" : "8";
	%>
	<input name="<%=inputName%>" class="little" value="<%=scontodays%>"/>&nbsp;&nbsp;
	<br>
	</div>
	<% } %>	
 <div width="800" height="30" align="left" class="menu">
 &nbsp;&nbsp;&nbsp;
 <a onClick="return checkAddresses();" href="javascript:link('process/new_order.jsp', 'new_order_add');"><fmt:message key="DO_ORDER"/></a>
 &nbsp;&nbsp;&nbsp;
  <% if (admin) {%>
 <a onClick="return checkAddresses();" href="javascript:link('process/new_order.jsp', 'new_order_add_mail');"><fmt:message key="DO_ORDER2"/></a>
 &nbsp;&nbsp;&nbsp; 
 <% }%>

 <a href="javascript:link('process/new_order.jsp', 'cancel_new_order');"><fmt:message key="CANCEL_ORDER"/></a>
 </div>
	<br>
	
	<%=_tot%>
	<%=_det	%>
	<%=_dat%>
	<%=_bot%>

	<br>
 <div width="800" height="30" align="left" class="menu">
 &nbsp;&nbsp;&nbsp;
 <a onClick="return checkAddresses();" href="javascript:link('process/new_order.jsp', 'new_order_add');"><fmt:message key="DO_ORDER"/></a>
 &nbsp;&nbsp;&nbsp;
  <% if (admin) {%>
 <a onClick="return checkAddresses();" href="javascript:link('process/new_order.jsp', 'new_order_add_mail');"><fmt:message key="DO_ORDER2"/></a>
 &nbsp;&nbsp;&nbsp; 
 <% }%>
 <a href="javascript:link('process/new_order.jsp', 'cancel_new_order');"><fmt:message key="CANCEL_ORDER"/></a>
 </div>
</form>
</body>
</html>
<script language=javascript>
document.forms[0].<%=focused%>.focus(); 
document.forms[0].<%=focused%>.select();

</script>
<%
	
}
	out.flush();
%>