<%@ 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.gui.servlets.elements.*"%>
<%@ page import="com.etixpert.evolution.*"%>
<%@ page import="java.sql.*"%>
<%@ include file="header.jsp" %>
<%@ include file="check.jsp" %>
<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">
<% String prefix = AboHelper.getPrefix(request); %>
<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;
	}

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;
		return true;
}

function getNextMonth(m) {
	if (m==12) return 1;
	return m+1;
}

function getPreviousMonth(m) {
	if (m==1) return 12;
	return m-1;
}

function OTIsEarlierThanNow() {
	var ots = new Array(0, 7, 15, 23, 31);
	var day = new Date().getDate();
	var ot = top.mainFrame.document.forms[0].operating_time.value;
	if (ots[ot]<day) return true;
	else return false;	
}


function synchStartMonth() {
 var d = new Date();
 var cm = d.getMonth()+1;
 var sm = top.mainFrame.document.forms[0].start_month.value
 
 
 if (OTIsEarlierThanNow() && sm==cm) {
   top.mainFrame.document.forms[0].start_month.value=getNextMonth(cm);
 
  }
  
 if (!OTIsEarlierThanNow() && sm == getNextMonth(cm)) {
   top.mainFrame.document.forms[0].start_month.value = cm;
   
  }
 }
  
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 = '<%=prefix%>new_abo.jsp';
			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';
 		} else if (document.all['pay_method_id'].value==2) {
		 document.all['bankselect'].style.display='none';
		 document.all['creditselect'].style.display='block';
		} else {
		 document.all['bankselect'].style.display='none';
		 document.all['creditselect'].style.display='none';
		}
	}

</script>
</head>
<%
	
	String jsp = prefix+"new_abo.jsp";
	CheckBox lengthening = new CheckBox(session, jsp, "auto_extension", "true", "automatische Verlängerung nach dem Ablauf", true);
	String focused = (String)session.getAttribute(prefix+"to_new_pos_abo");
	if (focused == null) focused = "art_id";
	Map operatingTimes = new TreeMap();
	operatingTimes.put("1","01.-07.");
	operatingTimes.put("2","08.-15.");
	operatingTimes.put("3","16.-23.");
	operatingTimes.put("4","24.-31.");
	MapSelect operatingTimeSelect = new MapSelect(session, jsp, "operating_time", operatingTimes);
	operatingTimeSelect.addProperty("onclick", "synchStartMonth()");
	
	
%>
<html>
<%	if (session.getAttribute(prefix+"new_abo_error")!=null) { %>
 <body onload="alert('<%=session.getAttribute(prefix+"new_abo_error")%>');saveValues(document.forms[0]);" onload="showHideBankCC();">
<% session.removeAttribute(prefix+"new_abo_error");
 } else if (session.getAttribute(prefix+"ABO_ADDED")!=null) { %>
 <body>
<script language=javascript>
	alert('Abonnement <%=session.getAttribute(prefix+"ABO_ADDED")%> wurde erfolgreich angelegt!');
	parent.mainFrame.location = 'detail_abo.jsp?prefix=<%=prefix%>';
</script>
<%
session.removeAttribute(prefix+"ABO_ADDED");
 } else { %>
 <body onload="document.forms[0].<%=focused%>.focus(); document.forms[0].<%=focused%>.select(); showHideBankCC(); saveValues(document.forms[0]);">
<% } %>
<h1 align="center"><fmt:message key="CHOOSE_ANOTHER_ABO"/></h1>
<form name="new_abo" action="process/new_abo.jsp?prefix=<%=prefix%>" method="post">
		<input type="hidden" name="link" value="process/new_abo.jsp?prefix=<%=prefix%>"/>
    	<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="distr_id"><fmt:message key="ROOT_ID"/>:</label>
	<input name="distr_id" class="small0" value="<%=OrderHelper.getVal(session, jsp, "distr_id")%>" readonly>

	<label for="sponsor"><fmt:message key="SPONSOR"/>:</label>
	<input name="sponsor" class="small0" value="<%=OrderHelper.getVal(session, jsp, "sponsor")%>" readonly><br>

	<label for="tel"><fmt:message key="TEL"/>:</label>
	<input name="tel" class="large" value="<%=OrderHelper.getVal(session, jsp, "tel")%>" readonly><br>

	<input type="hidden" name="orderingCountry" value="<%=OrderHelper.getVal(session, jsp, "orderingCountry")%>"/>
	<input type="hidden" name="orderingLevelId" value="<%=OrderHelper.getVal(session, jsp, "orderingLevelId")%>"/>
	<input type="hidden" name="UID" value="<%=OrderHelper.getVal(session, jsp, "UID")%>"/>
	
	<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>

	<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" class="little" maxlength="8" value="<%=del_postcode%>" onFocus="<%=del_focus%>"/>
	<input name="delivery_city" value="<%=del_city%>" onFocus="<%=del_city_focus%>"/>
	<input 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" type="hidden" class="little" value="<%=OrderHelper.getVal(session, jsp, "billing_title")%>" readonly>
	<%=OrderHelper.getVal(session, jsp, "billing_title")%><br>


	<label for="billing_name"><fmt:message key="BILLING_NAME"/>:</label>
	<input name="billing_name" type="hidden" class="medium" value="<%=OrderHelper.getVal(session, jsp, "billing_name")%>" readonly>
	<%=OrderHelper.getVal(session, jsp, "billing_name")%>
	<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 = "";

		if (bill_postcode.equals("") || bill_postcode.equals("PLZ")) {
			bill_postcode = "<PLZ>";
		}
		if (bill_city.equals("") || bill_city.equals("<Ort>")) {
			bill_city = "<Ort>";
		}
		if (bill_street.equals("") || bill_street.equals("<Strasse>")) {
			bill_street = "<Strasse>";
		}

	%>

	<input type="hidden" name="billing_country_id" value="<%=bill_country_id%>" class="small0" readonly>
	<input type="hidden" name="billing_postcode" class="little" value="<%=bill_postcode%>" readonly>
	<input type="hidden" name="billing_city" value="<%=bill_city%>" readonly>
	<input type="hidden" name="billing_street" class="medium" value="<%=bill_street%>" readonly>
	<%=bill_country_id+"-"+bill_postcode+", "+bill_city+", "+bill_street%><br>

	<label for="pay_method_id"><fmt:message key="PAYMENT_METHOD"/>:</label>
	<select name="pay_method_id" onchange="showHideBankCC();" onfocus="showHideBankCC();">
	<%=AboHelper.getPaymentOptions(session, jsp)%>
	</select>
	<div id="bankselect" style="display:none;">
		<select name="account_id_1">
			<%=AboHelper.getAccountOptions(session, jsp, "bank")%>
		</select>
	</div>
	<div id="creditselect" style="display:none;">
		<select name="account_id_2">
			<%=AboHelper.getAccountOptions(session, jsp, "credit")%>
		</select>
	</div>


	<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="very_large"><%=OrderHelper.getVal(session, jsp, "comment")%></textarea>
	<br>
	<br>
	<label for="start_date"><fmt:message key="START_DATE"/></label>
	<input name="start_date" class="small0" value="<%=OrderHelper.getVal(session, jsp, "start_date")%>">
	<br>
	<label for="duration"><fmt:message key="DURATION"/></label>
	<input name="duration" class="little" value="<%=OrderHelper.getVal(session, jsp, "duration")%>">
	<label for="delivery_intervall">Lieferintervall (in Monaten)</label>
	<input name="delivery_intervall" class="little" value="<%=OrderHelper.getVal(session, jsp, "delivery_intervall")%>">

<!-- 	<label for="start_month"><fmt:message key="BEGIN_MONTH"/></label>
	<input name="start_month" class="little" value="<%=OrderHelper.getVal(session, jsp, "start_month")%>"> -->
	<label for="operating_time">Bearbeitungszeitraum: </label>
	<%= operatingTimeSelect.toHTML() %>
	<br>
	<%= lengthening.toHTML() %>
	<br/>
	<%=com.etixpert.evolution.gui.servlets.AboHelper.generateOrderDetailsHeader(pageContext, prefix+"new_abo_details")	%>
	<%=com.etixpert.evolution.gui.servlets.OrderHelper.generateOrderDetailsRows(session, prefix+"new_abo_details")%>
	<%=com.etixpert.evolution.gui.servlets.AboHelper.generateOrderTotalRow(pageContext, prefix+"new_abo_details")%>
	
	<br>
 <div width="800" height="30" align="left" class="menu">
 &nbsp;&nbsp;&nbsp;
 <a onClick="return checkAddresses();" href="javascript:link('process/new_abo.jsp?prefix=<%=prefix%>', 'new_abo_add');"><fmt:message key="DO_ABO"/></a>
 &nbsp;&nbsp;&nbsp;
 <a href="javascript:link('process/new_abo.jsp?prefix=<%=prefix%>', 'cancel_new_abo');"><fmt:message key="CANCEL_ORDER"/></a>
 </div>
</form>
<body>
<html>