var a_AK = new Array();
var a_AA = new Array();

var h_Lieferung_spaetestens = new Hash({ });

var Kunden_Tabelle_Zaehler = 0;
var Artikel_Tabelle_Zaehler = 0;
var Ausgewaehlte_Artikel_Tabelle_Zaehler = 0;

Ajax.Responders.register({
	onCreate: show_loader,
	onComplete: hide_loader
});

function show_loader()
{
	document.getElementById('loading').style.display = 'block';
}

function hide_loader()
{
	document.getElementById('loading').style.display = 'none';
}

function remove_all_child_nodes(id)
{
	if (document.getElementById(id).hasChildNodes())
	{
		while (document.getElementById(id).hasChildNodes())
		{
			document.getElementById(id).removeChild(document.getElementById(id).firstChild);
		}
	}
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - www.quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function ZeigeSeite(Seite)
{
	switch(Seite)
	{
		case 'Kundenauswahl':
			window.scrollTo(0,0);
			remove_all_child_nodes('Bestellung_abgeschlossen');
			remove_all_child_nodes('Kunden_Liste');
			remove_all_child_nodes('Artikel_Liste');
			if(undefined != a_AK[0])
			{
				Kunde_auswaehlen(a_AK[0]);
			}
			Kunden_Suchformular_erstellen();
			break;
		case 'Artikel':
			window.scrollTo(0,0);
			remove_all_child_nodes('Bestellung_abgeschlossen');
			remove_all_child_nodes('Kunden_Liste');
			remove_all_child_nodes('Kunden_Suchformular');
			Ausgewaehlte_Kunde_Tabelle_erstellen();
			Artikel_Tabelle_erstellen();
			break;
		case 'Ausgewaehlte_Artikel':
			window.scrollTo(0,0);
			remove_all_child_nodes('Bestellung_abgeschlossen');
			remove_all_child_nodes('Kunden_Liste');
			remove_all_child_nodes('Kunden_Suchformular');
			remove_all_child_nodes('Artikel_Liste');
			Ausgewaehlte_Kunde_Tabelle_erstellen();
			Ausgewaehlte_Artikel_Tabelle_erstellen();
			break;
		case 'Bestellung_abgeschlossen':
			window.scrollTo(0,0);
			remove_all_child_nodes('Bestellung_abgeschlossen');
			remove_all_child_nodes('Ausgewaehler_Kunde');
			remove_all_child_nodes('Kunden_Liste');
			remove_all_child_nodes('Kunden_Suchformular');
			remove_all_child_nodes('Artikel_Liste');
			/* ---------- GESPEICHERTE DATEN ZURÜCKSETZEN ---------- */
				a_AK = new Array();
				a_AA = new Array();
			/* ---------- /GESPEICHERTE DATEN ZURÜCKSETZEN ---------- */
			div = document.createElement('div');
			strong = document.createElement('strong');
			strong.appendChild(document.createTextNode('Bestellung abgeschlossen'));
			div.appendChild(strong);
			div.appendChild(document.createElement('br'));
			div.appendChild(document.createElement('br'));
			em = document.createElement('em');
			em.appendChild(document.createTextNode('Wichtiger Hinweis: Diese Seite kann nicht erneut aufgerufen werden, da die Bestellung bereits abgeschlossen wurde.'));
			div.appendChild(em);
			div.appendChild(document.createElement('br'));
			div.appendChild(document.createElement('br'));
			div.appendChild(document.createTextNode('Ihre Bestellung wurde an die Winkels Getränke Logistik GmbH versendet.'));
			div.appendChild(document.createElement('br'));
			a = document.createElement('a');
			a.setAttribute('href','Druckversion.php');
			a.setAttribute('target','_blank');
			a.appendChild(document.createTextNode('Druckversion der Bestellung aufrufen.'));
			div.appendChild(a);
			div.appendChild(document.createElement('br'));
			div.appendChild(document.createElement('br'));
			a = document.createElement('a');
			a.setAttribute('href','#');
			a.appendChild(document.createTextNode('Neue Bestellung'));
			a.onclick = function() { ZeigeSeite('Kundenauswahl') }
			div.appendChild(a);
			document.getElementById('Bestellung_abgeschlossen').appendChild(div);
			break;
	}
}

/* ---------- FORMS ---------- */

	function Login_Formular_erstellen()
	{
		remove_all_child_nodes('login');

		form = document.createElement('form');
		form.onsubmit = function() { ajax_login_submit(); return false; }

		fieldset = document.createElement('fieldset');

		legend = document.createElement('legend');
		legend.appendChild(document.createTextNode('Login'));
		fieldset.appendChild(legend);

		fieldset.appendChild(document.createElement('br'));

		div = document.createElement('div');
		div.setAttribute('id','message');
		fieldset.appendChild(div);

		p = document.createElement('p');
		label = document.createElement('label');
		label.htmlFor = 'Benutzername';
		label.appendChild(document.createTextNode('Benutzername: '));
		p.appendChild(label);
		input = document.createElement('input');
		input.setAttribute('type','text');
		input.setAttribute('id','Benutzername');
		p.appendChild(input);
		fieldset.appendChild(p);
		
		p = document.createElement('p');
		label = document.createElement('label');
		label.htmlFor = 'Kennwort';
		label.appendChild(document.createTextNode('Kennwort: '));
		p.appendChild(label);
		input = document.createElement('input');
		input.setAttribute('type','password');
		input.setAttribute('id','Kennwort');
		p.appendChild(input);
		fieldset.appendChild(p);

		p = document.createElement('p');
		p.className = 'buttons';
		label = document.createElement('label');
		label.appendChild(document.createTextNode('.'));
		p.appendChild(label);

		input = document.createElement('input');
		input.setAttribute('type','submit');
		input.setAttribute('value','Einloggen');
		input.onclick = function() { ajax_login_submit() }
		p.appendChild(input);
		fieldset.appendChild(p);

		form.appendChild(fieldset);

		document.getElementById('login').appendChild(form);
		$('login').setStyle( {display: 'block'} );
	}

	function Kunden_Suchformular_erstellen()
	{
		remove_all_child_nodes('Kunden_Suchformular');

		form = document.createElement('form');
		form.onsubmit = function() { Kunden_suchen(); return false; }

		fieldset = document.createElement('fieldset');

		legend = document.createElement('legend');
		legend.appendChild(document.createTextNode('Kundensuche'));
		fieldset.appendChild(legend);

		fieldset.appendChild(document.createElement('br'));

		div = document.createElement('div');
		div.setAttribute('id','message');
		fieldset.appendChild(div);

		p = document.createElement('p');
		label = document.createElement('label');
		label.htmlFor = 'Kundennummer';
		label.appendChild(document.createTextNode('Kunden-Nr.: '));
		p.appendChild(label);
		input = document.createElement('input');
		input.setAttribute('type','text');
		input.setAttribute('id','Kundennummer');
		p.appendChild(input);
		p.appendChild(document.createTextNode(' (beginnt mit)'));
		fieldset.appendChild(p);

		p = document.createElement('p');
		label = document.createElement('label');
		label.htmlFor = 'KundenName';
		label.appendChild(document.createTextNode('Name: '));
		p.appendChild(label);
		input = document.createElement('input');
		input.setAttribute('type','text');
		input.setAttribute('id','KundenName');
		p.appendChild(input);
		p.appendChild(document.createTextNode(' (enthält)'));
		fieldset.appendChild(p);

		p = document.createElement('p');
		label = document.createElement('label');
		label.htmlFor = 'KundenStrasse';
		label.appendChild(document.createTextNode('Staße: '));
		p.appendChild(label);
		input = document.createElement('input');
		input.setAttribute('type','text');
		input.setAttribute('id','KundenStrasse');
		p.appendChild(input);
		p.appendChild(document.createTextNode(' (enthält)'));
		fieldset.appendChild(p);

		p = document.createElement('p');
		label = document.createElement('label');
		label.htmlFor = 'KundenPLZ';
		label.appendChild(document.createTextNode('PLZ: '));
		p.appendChild(label);
		input = document.createElement('input');
		input.setAttribute('type','text');
		input.setAttribute('id','KundenPLZ');
		p.appendChild(input);
		p.appendChild(document.createTextNode(' (beginnt mit)'));
		fieldset.appendChild(p);

		p = document.createElement('p');
		label = document.createElement('label');
		label.htmlFor = 'KundenOrt';
		label.appendChild(document.createTextNode('Ort: '));
		p.appendChild(label);
		input = document.createElement('input');
		input.setAttribute('type','text');
		input.setAttribute('id','KundenOrt');
		p.appendChild(input);
		p.appendChild(document.createTextNode(' (enthält)'));
		fieldset.appendChild(p);

		p = document.createElement('p');
		p.className = 'buttons';
		label = document.createElement('label');
		label.appendChild(document.createTextNode('.'));
		p.appendChild(label);

		input = document.createElement('input');
		input.setAttribute('type','submit');
		input.setAttribute('value','Suchen');
		input.onclick = function() { Kunden_suchen() }
		p.appendChild(input);
		if (undefined != a_AK[0])
		{
			input = document.createElement('input');
			input.setAttribute('type','button');
			input.setAttribute('value','Abbrechen');
			input.onclick = function() { ZeigeSeite('Artikel') }
			p.appendChild(input);
		}
		fieldset.appendChild(p);

		form.appendChild(fieldset);

		document.getElementById('Kunden_Suchformular').appendChild(form);
	}

/* ---------- /FORMS ---------- */

function ajax_login_submit()
{
	var Benutzername = document.getElementById('Benutzername').value;
	var Kennwort = document.getElementById('Kennwort').value;

	/* ---------- HANDLE EMPTY FIELDS ---------- */
		remove_all_child_nodes('message');
		if (Benutzername.empty())
		{
			span = document.createElement('span');
			span.className = 'messageError';
			span.appendChild(document.createTextNode('Bitte geben Sie Ihren Benutzernamen ein.'));
			document.getElementById('message').appendChild(span);
			document.getElementById('message').appendChild(document.createElement('br'));
		}
		if (Kennwort.empty())
		{
			span = document.createElement('span');
			span.className = 'messageError';
			span.appendChild(document.createTextNode('Bitte geben Sie Ihr Kennwort ein.'));
			document.getElementById('message').appendChild(span);
			document.getElementById('message').appendChild(document.createElement('br'));
		}
		if (Benutzername.empty() || Kennwort.empty())
		{
			document.getElementById('message').appendChild(document.createElement('br'));
		}
	/* ---------- /HANDLE EMPTY FIELDS ---------- */

	if (!Benutzername.empty() && !Kennwort.empty())
	{
		var url = '/js/ajax_Login.php';
		var data = 'Benutzername='+encodeURIComponent(Benutzername)+'&Kennwort='+encodeURIComponent(calcMD5(Kennwort));
		var myAjax = new Ajax.Request(url, {method: 'post', parameters: data, onComplete: ajax_login_submit_response});
	}
}

function ajax_login_submit_response(response)
{
	result = response.responseText.strip();

	if (result == 1)
	{
		location.replace('index.php');
	}
	else
	{
		remove_all_child_nodes('message');

		span = document.createElement('span');
		span.className = 'messageError';
		span.appendChild(document.createTextNode('Benutzername oder Kennwort falsch.'));
		document.getElementById('message').appendChild(span);
		document.getElementById('message').appendChild(document.createElement('br'));
		document.getElementById('message').appendChild(document.createElement('br'));
	}
}

function ajax_logout_submit()
{
	var url = '/js/ajax_Logout.php';

	new Ajax.Request(url, {
			method: 'post',
			onComplete: function(response) {
			location.replace('index.php?logout');
		}
	});
}

function Ausgewaehlter_Kunde_Speichern(Nummer)
{
	var url = '/js/ajax_Ausgewaehlter_Kunde_Speichern.php';
	var data = 'Nummer='+encodeURIComponent(Nummer);

	var myAjax = new Ajax.Request(url, {method: 'post', parameters: data});
}

function Kunden_suchen()
{
	Suche_Nummer = document.getElementById('Kundennummer').value.strip();
	Suche_Name = document.getElementById('KundenName').value.strip().toLowerCase();
	Suche_Strasse = document.getElementById('KundenStrasse').value.strip().toLowerCase();
	Suche_PLZ = document.getElementById('KundenPLZ').value.strip().toLowerCase();
	Suche_Ort = document.getElementById('KundenOrt').value.strip().toLowerCase();

	/* ---------- HANDLE EMPTY/IN-VALID FIELDS ---------- */
		remove_all_child_nodes('message');

		var FehlerMeldungen = new Array();
		if (Suche_Nummer.length < 2 && Suche_Nummer.length < 2 && Suche_Name.length < 2 && Suche_Strasse.length < 2 && Suche_PLZ.length < 2 && Suche_Ort.length < 2)
		{
			FehlerMeldungen.push('Bitte geben Sie mindestens ein Suchkriterium mit mindestens 2 Zeichen an.');
		}
		if (Suche_Nummer != '' && isNaN(Suche_Nummer))
		{
			FehlerMeldungen.push('Die Kunden-Nr. muss eine Zahl sein.');
		}
		if (Suche_PLZ != '' && isNaN(Suche_PLZ))
		{
			FehlerMeldungen.push('Die PLZ muss eine Zahl sein.');
		}
		if (FehlerMeldungen.length > 0)
		{
			if (FehlerMeldungen.length == 1)
			{
				var message = 'Bitte beachten Sie bie Ihrer Eingabe folgenden Hinweis:';
			}
			else
			{
				var message = 'Bitte beachten Sie bie Ihrer Eingabe folgende Hinweise:';
			}
			span = document.createElement('span');
			span.className = 'messageError';
			span.appendChild(document.createTextNode(message));
			document.getElementById('message').appendChild(span);
			
			ul = document.createElement('ul');
			ul.className = 'messageError';
			for (var i = 0; i < FehlerMeldungen.length; i++)
			{
				li = document.createElement('li');
				li.appendChild(document.createTextNode(FehlerMeldungen[i]));
				ul.appendChild(li);
			}
			document.getElementById('message').appendChild(ul);
		}
	/* ---------- /HANDLE EMPTY/IN-VALID FIELDS ---------- */

	if (FehlerMeldungen.length == 0)
	{
		// stretch overlay to fill page
		var arrayPageSize = getPageSize();
		$('overlay').style.height = arrayPageSize[1] + 'px'; // TODO: JS-Fehler im IE wenn über document.getElementByID

		document.getElementById('overlay').setStyle({display: 'block'});
		window.setTimeout('Kunden_Tabelle_erstellen(Suche_Nummer,Suche_Name,Suche_Strasse,Suche_PLZ,Suche_Ort)', 5);
	}
}

function Kunden_Tabelle_erstellen(Suche_Nummer,Suche_Name,Suche_Strasse,Suche_PLZ,Suche_Ort)
{
	var Treffer = 0;

	Kunden_Tabelle_Zaehler++;
	Kunden_Tabelle_ID = 'Kunden_Tabelle'+Kunden_Tabelle_Zaehler;

	table = document.createElement('table');
	table.setAttribute('id',Kunden_Tabelle_ID);
	table.className = 'Kunden_Tabelle sortable';

	thead = document.createElement('thead');

	tr = document.createElement('tr');

	th = document.createElement('th');
	th.setAttribute('id','Kunden_Tabelle_Kundennummer');
	th.className = 'number';
	th.appendChild(document.createTextNode('Kunden-Nr.'));
	tr.appendChild(th);

	th = document.createElement('th');
	th.setAttribute('id','Kunden_Tabelle_Name');
	th.className = 'text sortfirstasc';
	th.appendChild(document.createTextNode('Name'));
	tr.appendChild(th);

	th = document.createElement('th');
	th.setAttribute('id','Kunden_Tabelle_Strasse');
	th.className = 'text';
	th.appendChild(document.createTextNode('Straße'));
	tr.appendChild(th);

	th = document.createElement('th');
	th.setAttribute('id','Kunden_Tabelle_PLZ');
	th.className = 'number';
	th.appendChild(document.createTextNode('PLZ'));
	tr.appendChild(th);

	th = document.createElement('th');
	th.setAttribute('id','Kunden_Tabelle_Ort');
	th.className = 'text';
	th.appendChild(document.createTextNode('Ort'));
	tr.appendChild(th);

	thead.appendChild(tr);
	table.appendChild(thead);

	tbody = document.createElement('tbody');

	for (var i = 0; i < a_K.length; ++i)
	{
		show_entry = true;

		Nummer = a_K[i][0];
		Name = a_K[i][1];
		Strasse = a_K[i][2];
		PLZ = a_K[i][3];
		Ort = a_K[i][4];

		if(show_entry != false && Suche_Nummer != '' && !Nummer.startsWith(Suche_Nummer))
		{
			show_entry = false;
		}
		if(show_entry != false && Suche_Name != '' && !Name.toLowerCase().include(Suche_Name))
		{
			show_entry = false;
		}
		if(show_entry != false && Suche_Strasse != '' && !Strasse.toLowerCase().include(Suche_Strasse))
		{
			show_entry = false;
		}
		if(show_entry != false && Suche_PLZ != '' && !PLZ.startsWith(Suche_PLZ))
		{
			show_entry = false;
		}
		if(show_entry != false && Suche_Ort != '' && !Ort.toLowerCase().include(Suche_Ort))
		{
			show_entry = false;
		}

		if (show_entry == true)
		{
			Treffer++;
			tr = document.createElement('tr');
			tr.setAttribute('title','Diesen Kunden auswählen');
			tr.setAttribute('id','tr'+Nummer);
			tr.onclick = function() { Kunde_auswaehlen(this.id.substr(2)); ZeigeSeite('Artikel') }

			td = document.createElement('td');
			td.appendChild(document.createTextNode(Nummer));
			tr.appendChild(td);

			td = document.createElement('td');
			td.appendChild(document.createTextNode(Name));
			tr.appendChild(td);

			td = document.createElement('td');
			td.appendChild(document.createTextNode(Strasse));
			tr.appendChild(td);

			td = document.createElement('td');
			td.appendChild(document.createTextNode(PLZ));
			tr.appendChild(td);

			td = document.createElement('td');
			td.appendChild(document.createTextNode(Ort));
			tr.appendChild(td);

			tbody.appendChild(tr);
		}
	}
	table.appendChild(tbody);

	remove_all_child_nodes('Kunden_Liste');

	if (Treffer > 0)
	{
		document.getElementById('Kunden_Liste').appendChild(document.createTextNode('Klicken Sie auf einen Kunden um ihn auszuwählen.'));
		document.getElementById('Kunden_Liste').appendChild(document.createElement('br'));
		document.getElementById('Kunden_Liste').appendChild(document.createElement('br'));
		document.getElementById('Kunden_Liste').appendChild(table);
		TableKit.Sortable.init(Kunden_Tabelle_ID);
	}
	else
	{
		span = document.createElement('span');
		span.appendChild(document.createTextNode('Es konnten keine Kunden mit den von Ihnen angegebenen Kriterien gefunden werden.'));
		document.getElementById('Kunden_Liste').appendChild(span);
	}

	document.getElementById('overlay').setStyle({display: 'none'});
}

function Kunde_auswaehlen(Kundennummer)
{
	for (var i = 0; i < a_K.length; ++i)
	{
		if (a_K[i][0] == Kundennummer)
		{
			a_AK = a_K[i];
			break;
		}
	}

	Ausgewaehlter_Kunde_Speichern(Kundennummer);
}

function Ausgewaehlte_Kunde_Tabelle_erstellen()
{
	remove_all_child_nodes('Kunden_Suchformular');
	remove_all_child_nodes('Kunden_Liste');
	remove_all_child_nodes('Ausgewaehler_Kunde');

	span = document.createElement('span');
	strong = document.createElement('strong');
	strong.appendChild(document.createTextNode('Ausgewählter Kunde '));
	span.appendChild(strong);
	span.appendChild(document.createTextNode('('));
	a = document.createElement('a');
	a.setAttribute('href', '#');
	a.onclick = function() { ZeigeSeite('Kundenauswahl') }
	a.appendChild(document.createTextNode('ändern'));
	span.appendChild(a);
	span.appendChild(document.createTextNode(')'));
	span.appendChild(document.createElement('br'));
	document.getElementById('Ausgewaehler_Kunde').appendChild(span);

	table = document.createElement('table');
	table.className = 'Ausgewaehler_Kunde_Tabelle';
	thead = document.createElement('thead');
	tr = document.createElement('tr');

	th = document.createElement('th');
	th.setAttribute('id','Ausgewaehler_Kunde_Tabelle_Kundennummer');
	th.className = 'text';
	th.appendChild(document.createTextNode('Kunden-Nr.'));
	tr.appendChild(th);

	th = document.createElement('th');
	th.setAttribute('id','Ausgewaehler_Kunde_Tabelle_Name');
	th.className = 'text';
	th.appendChild(document.createTextNode('Name'));
	tr.appendChild(th);

	th = document.createElement('th');
	th.setAttribute('id','Ausgewaehler_Kunde_Tabelle_Strasse');
	th.className = 'text';
	th.appendChild(document.createTextNode('Straße'));
	tr.appendChild(th);

	th = document.createElement('th');
	th.setAttribute('id','Ausgewaehler_Kunde_Tabelle_PLZ');
	th.className = 'text';
	th.appendChild(document.createTextNode('PLZ'));
	tr.appendChild(th);

	th = document.createElement('th');
	th.setAttribute('id','Ausgewaehler_Kunde_Tabelle_Ort');
	th.className = 'text';
	th.appendChild(document.createTextNode('Ort'));
	tr.appendChild(th);

	thead.appendChild(tr);
	table.appendChild(thead);
	tbody = document.createElement('tbody');
	tr = document.createElement('tr');

	td = document.createElement('td');
	td.appendChild(document.createTextNode(a_AK[0]));
	tr.appendChild(td);

	td = document.createElement('td');
	td.appendChild(document.createTextNode(a_AK[1]));
	tr.appendChild(td);

	td = document.createElement('td');
	td.appendChild(document.createTextNode(a_AK[2]));
	tr.appendChild(td);

	td = document.createElement('td');
	td.appendChild(document.createTextNode(a_AK[3]));
	tr.appendChild(td);

	td = document.createElement('td');
	td.appendChild(document.createTextNode(a_AK[4]));
	tr.appendChild(td);

	tbody.appendChild(tr);
	table.appendChild(tbody);

	document.getElementById('Ausgewaehler_Kunde').appendChild(table);
}

function Artikel_Tabelle_erstellen()
{
	remove_all_child_nodes('Artikel_Liste');

	span = document.createElement('span');
	strong = document.createElement('strong');
	strong.appendChild(document.createTextNode('Artikelauswahl'));
	span.appendChild(strong);
	span.appendChild(document.createElement('br'));
	span.appendChild(document.createElement('br'));
	document.getElementById('Artikel_Liste').appendChild(span);

	span = document.createElement('span');
	span.appendChild(document.createTextNode('Bitte beachten Sie, dass für "Bestellmenge" und "Menge Gratisbestellung" nur positive Zahlenwerte erlaubt sind.'));
	span.appendChild(document.createElement('br'));
	document.getElementById('Artikel_Liste').appendChild(span);

	if (a_A.length > 0)
	{
		Artikel_Tabelle_Zaehler++;
		Artikel_Tabelle_ID = 'Artikel_Tabelle'+Artikel_Tabelle_Zaehler;

		table = document.createElement('table');
		table.setAttribute('id',Artikel_Tabelle_ID);
		table.className = 'Artikel_Tabelle sortable';
		thead = document.createElement('thead');

		tr = document.createElement('tr');

		th = document.createElement('th');
		th.setAttribute('id', 'Artikel_Tabelle_WinkelsArtikelnummer');
		th.className = 'number';
		th.appendChild(document.createTextNode('Winkels- Artikelnummer'));
		tr.appendChild(th);

		th = document.createElement('th');
		th.setAttribute('id', 'Artikel_Tabelle_LieferantenArtikelnummer');
		th.className = 'number sortfirstasc';
		th.appendChild(document.createTextNode('Lieferanten- Artikelnummer'));
		tr.appendChild(th);

		th = document.createElement('th');
		th.setAttribute('id', 'Artikel_Tabelle_Bezeichnung');
		th.className = 'text';
		th.appendChild(document.createTextNode('Bezeichnung'));
		tr.appendChild(th);

		th = document.createElement('th');
		th.setAttribute('id', 'Artikel_Tabelle_Gebinde');
		th.className = 'number';
		th.appendChild(document.createTextNode('Gebinde'));
		tr.appendChild(th);

		th = document.createElement('th');
		th.setAttribute('id', 'Artikel_Tabelle_Inhalt');
		th.className = 'number';
		th.appendChild(document.createTextNode('Inhalt'));
		tr.appendChild(th);

		th = document.createElement('th');
		th.setAttribute('id', 'Artikel_Tabelle_Sortengruppe');
		th.className = 'text';
		th.appendChild(document.createTextNode('Sortengruppe'));
		tr.appendChild(th);

		th = document.createElement('th');
		th.setAttribute('id', 'Artikel_Tabelle_Bestellmenge');
		th.className = 'nosort';
		th.appendChild(document.createTextNode('Bestellmenge'));
		tr.appendChild(th);

		th = document.createElement('th');
		th.setAttribute('id', 'Artikel_Tabelle_MengeGratisbestellung');
		th.className = 'nosort';
		th.appendChild(document.createTextNode('Menge Gratisbestellung'));
		tr.appendChild(th);

		thead.appendChild(tr);
		table.appendChild(thead);

		tbody = document.createElement('tbody');

		for (var i = 0; i < a_A.length; ++i)
		{
			tr = document.createElement('tr');

			td = document.createElement('td');
			td.appendChild(document.createTextNode(a_A[i][0]));
			td.className = 'r';
			tr.appendChild(td);

			td = document.createElement('td');
			td.appendChild(document.createTextNode(a_A[i][1]));
			td.className = 'r';
			tr.appendChild(td);

			td = document.createElement('td');
			td.appendChild(document.createTextNode(a_A[i][2]));
			tr.appendChild(td);

			td = document.createElement('td');
			td.appendChild(document.createTextNode(a_A[i][3]));
			td.className = 'r';
			tr.appendChild(td);

			td = document.createElement('td');
			td.appendChild(document.createTextNode(a_A[i][4]));
			td.className = 'r';
			tr.appendChild(td);

			td = document.createElement('td');
			td.appendChild(document.createTextNode(a_A[i][5]));
			td.className = 'r';
			tr.appendChild(td);

			td = document.createElement('td');
			td.className = 'Artikel_Tabelle_Bestellmenge';
			input = document.createElement('input');
			input.setAttribute('type','text');
			input.setAttribute('id','Artikel_Tabelle_Bestellmenge'+String(a_A[i][0])+String(a_A[i][1]));
			input.className = 'r';
			input.onkeyup = function() {
				if(this.value != '' && (isNaN(this.value) || this.value < 0))
				{
					this.style.borderColor = '#FF0000';
				}
				else
				{
					this.style.borderColor = '#009DCA';
				}
			}
			td.appendChild(input);
			tr.appendChild(td);

			td = document.createElement('td');
			td.className = 'Artikel_Tabelle_MengeGratisbestellung';
			input = document.createElement('input');
			input.setAttribute('type','text');
			input.setAttribute('id','Artikel_Tabelle_MengeGratisbestellung'+String(a_A[i][0])+String(a_A[i][1]));
			input.className = 'r';
			input.onkeyup = function() {
				if(this.value != '' && (isNaN(this.value) || this.value < 0))
				{
					this.style.borderColor = '#FF0000';
				}
				else
				{
					this.style.borderColor = '#009DCA';
				}
			}
			td.appendChild(input);
			tr.appendChild(td);

			tbody.appendChild(tr);
		}
		table.appendChild(tbody);

		form = document.createElement('form');
		form.onsubmit = function() { Artikel_auswaehlen(); return false; }
		form.appendChild(table);

		div = document.createElement('div');
		div.setAttribute('id','message');
		form.appendChild(div);

		p = document.createElement('p');
		p.className = 'buttons';
		input = document.createElement('input');
		input.setAttribute('type','submit');
		input.setAttribute('value','weiter');
		input.onclick = function() { Artikel_auswaehlen(); return false; }
		p.appendChild(input);
		input = document.createElement('input');
		input.setAttribute('type','reset');
		input.setAttribute('value','zurücksetzen');
		p.appendChild(input);

		form.appendChild(p);

		document.getElementById('Artikel_Liste').appendChild(form);

		TableKit.Sortable.init(Artikel_Tabelle_ID);

		window.setTimeout('Artikel_Tabelle_Values()', 5);
	}
	else
	{
		document.getElementById('Artikel_Liste').appendChild(document.createElement('br'));
		document.getElementById('Artikel_Liste').appendChild(document.createTextNode('Es stehen keine Artikel zur Auswahl bereit.'));
	}
}

function Ausgewaehlte_Artikel_Tabelle_erstellen()
{
	remove_all_child_nodes('Artikel_Liste');

	if (a_AA.length <= 0)
	{
		span = document.createElement('span');
		strong = document.createElement('strong');
		strong.appendChild(document.createTextNode('Ausgewählte Artikel'));
		span.appendChild(strong);
		span.appendChild(document.createElement('br'));
		span.appendChild(document.createElement('br'));
		span.appendChild(document.createTextNode('Sie haben noch keine Artikel ausgewählt. ('));
		a = document.createElement('a');
		a.setAttribute('href','#');
		a.appendChild(document.createTextNode('Artikel auswählen'));
		a.onclick = function() { ZeigeSeite('Artikel') }
		span.appendChild(a);
		span.appendChild(document.createTextNode(')'));

		document.getElementById('Artikel_Liste').appendChild(span);
	}

	if (a_AA.length > 0)
	{
		Ausgewaehlte_Artikel_Tabelle_Zaehler++;
		Ausgewaehlte_Artikel_Tabelle_ID = 'Ausgewaehlte_Artikel_Tabelle'+Ausgewaehlte_Artikel_Tabelle_Zaehler;

		span = document.createElement('span');
		strong = document.createElement('strong');
		strong.appendChild(document.createTextNode('Ausgewählte Artikel'));
		span.appendChild(strong);
		span.appendChild(document.createTextNode(' ('));
		a = document.createElement('a');
		a.setAttribute('href','#');
		a.onclick = function() { ZeigeSeite('Artikel') }
		a.appendChild(document.createTextNode('ändern'));
		span.appendChild(a);
		span.appendChild(document.createTextNode(')'));
		span.appendChild(document.createElement('br'));
		document.getElementById('Artikel_Liste').appendChild(span);

		table = document.createElement('table');
		table.setAttribute('id',Ausgewaehlte_Artikel_Tabelle_ID);
		table.className = 'Ausgewaehlte_Artikel_Tabelle sortable';

		thead = document.createElement('thead');
		tr = document.createElement('tr');

		th = document.createElement('th');
		th.setAttribute('id','Ausgewaehlte_Artikel_Tabelle_WinkelsArtikelnummer');
		th.className = 'number';
		th.appendChild(document.createTextNode('Winkels- Artikelnummer'));
		tr.appendChild(th);

		th = document.createElement('th');
		th.setAttribute('id','Ausgewaehlte_Artikel_Tabelle_LieferantenArtikelnummer');
		th.className = 'number sortfirstasc';
		th.appendChild(document.createTextNode('Lieferanten- Artikelnummer'));
		tr.appendChild(th);

		th = document.createElement('th');
		th.setAttribute('id','Ausgewaehlte_Artikel_Tabelle_Bezeichnung');
		th.className = 'text';
		th.appendChild(document.createTextNode('Bezeichnung'));
		tr.appendChild(th);

		th = document.createElement('th');
		th.setAttribute('id','Ausgewaehlte_Artikel_Tabelle_Gebinde');
		th.className = 'number';
		th.appendChild(document.createTextNode('Gebinde'));
		tr.appendChild(th);

		th = document.createElement('th');
		th.setAttribute('id','Ausgewaehlte_Artikel_Tabelle_Inhalt');
		th.className = 'number';
		th.appendChild(document.createTextNode('Inhalt'));
		tr.appendChild(th);

		th = document.createElement('th');
		th.setAttribute('id','Ausgewaehlte_Artikel_Tabelle_Sortengruppe');
		th.className = 'text';
		th.appendChild(document.createTextNode('Sortengruppe'));
		tr.appendChild(th);

		th = document.createElement('th');
		th.setAttribute('id','Ausgewaehlte_Artikel_Tabelle_Bestellmenge');
		th.className = 'number';
		th.appendChild(document.createTextNode('Bestell- menge'));
		tr.appendChild(th);

		th = document.createElement('th');
		th.setAttribute('id','Ausgewaehlte_Artikel_Tabelle_MengeGratisbestellung');
		th.className = 'number';
		th.appendChild(document.createTextNode('Menge Gratisbestellung'));
		tr.appendChild(th);

		thead.appendChild(tr);
		table.appendChild(thead);

		tbody = document.createElement('tbody');

		for (var i = 0; i < a_AA.length; ++i)
		{
			for (var j = 0; j < a_A.length; ++j)
			{
				if (a_AA[i][0] == a_A[j][0] && a_AA[i][1] == a_A[j][1])
				{
					tr = document.createElement('tr');

					td = document.createElement('td');
					td.appendChild(document.createTextNode(a_A[j][0]));
					td.className = 'r';
					tr.appendChild(td);

					td = document.createElement('td');
					td.appendChild(document.createTextNode(a_A[j][1]));
					td.className = 'r';
					tr.appendChild(td);

					td = document.createElement('td');
					td.appendChild(document.createTextNode(a_A[j][2]));
					tr.appendChild(td);

					td = document.createElement('td');
					td.appendChild(document.createTextNode(a_A[j][3]));
					td.className = 'r';
					tr.appendChild(td);

					td = document.createElement('td');
					td.appendChild(document.createTextNode(a_A[j][4]));
					td.className = 'r';
					tr.appendChild(td);

					td = document.createElement('td');
					td.appendChild(document.createTextNode(a_A[j][5]));
					td.className = 'r';
					tr.appendChild(td);

					td = document.createElement('td');
					td.appendChild(document.createTextNode(a_AA[i][2]));
					td.className = 'r';
					tr.appendChild(td);

					td = document.createElement('td');
					td.appendChild(document.createTextNode(a_AA[i][3]));
					td.className = 'r';
					tr.appendChild(td);

					tbody.appendChild(tr);

					break;
				}
			}
		}
		table.appendChild(tbody);
		document.getElementById('Artikel_Liste').appendChild(table);
		TableKit.Sortable.init(Ausgewaehlte_Artikel_Tabelle_ID);

		form = document.createElement('form');

		p = document.createElement('p');
		p.appendChild(document.createTextNode('Um den Bestellungsvorgang fortzusetzen überprüfen Sie bitte die ausgewählten Artikel sorgfältig und klicken dann auf den "Weiter"-Button.'));
		form.appendChild(p);

		p = document.createElement('p');
		p.className = 'buttons';
		input = document.createElement('input');
		input.setAttribute('type','button');
		input.setAttribute('value','weiter');
		input.onclick = function() { document.getElementById('Artikel_Liste').removeChild(this.parentNode.parentNode); Zusaetzliche_Bestellangaben_Formular_erstellen(); }
		p.appendChild(input);
		form.appendChild(p);

		document.getElementById('Artikel_Liste').appendChild(form);
	}
}

function Artikel_Tabelle_Values()
{
	for (i = 0; i < a_AA.length; ++i)
	{
		$('Artikel_Tabelle_Bestellmenge'+String(a_AA[i][0])+String(a_AA[i][1])).value = a_AA[i][2];
		$('Artikel_Tabelle_MengeGratisbestellung'+String(a_AA[i][0])+String(a_AA[i][1])).value = a_AA[i][3];
	}
}

function Artikel_auswaehlen()
{
	var Fehler = 0;
	for (var i = 0; i < a_A.length; ++i)
	{
		Bestellmenge = document.getElementById('Artikel_Tabelle_Bestellmenge'+String(a_A[i][0])+String(a_A[i][1])).value;
		MengeGratisbestellung = document.getElementById('Artikel_Tabelle_MengeGratisbestellung'+String(a_A[i][0])+String(a_A[i][1])).value;

		if ((Bestellmenge != '' && (isNaN(Bestellmenge) || Bestellmenge < 0)) || (MengeGratisbestellung != '' && (isNaN(MengeGratisbestellung) || MengeGratisbestellung < 0)))
		{
			Fehler++;
		}
	}

	if (Fehler == 0)
	{
		a_AA.clear();

		for (var i = 0; i < a_A.length; ++i)
		{
			Bestellmenge = document.getElementById('Artikel_Tabelle_Bestellmenge'+String(a_A[i][0])+String(a_A[i][1])).value;
			MengeGratisbestellung = document.getElementById('Artikel_Tabelle_MengeGratisbestellung'+String(a_A[i][0])+String(a_A[i][1])).value;

			if (!Bestellmenge.empty() || !MengeGratisbestellung.empty())
			{		
				a_AA.push([a_A[i][0],a_A[i][1],Bestellmenge,MengeGratisbestellung]);
			}
		}
	
		if (a_AA.length > 0)
		{
			Ausgewaehlte_Artikel_Speichern(a_AA);
			window.scrollTo(0,0);
			window.setTimeout('ZeigeSeite(\'Ausgewaehlte_Artikel\')', 5);
		}
		else
		{
			alert('Sie haben noch keine Artikel ausgewählt.');
		}
	}
	else // HANDLE INVALID FIELDS
	{
		remove_all_child_nodes('message');
		span = document.createElement('span');
		span.className = 'messageError';
		span.appendChild(document.createTextNode('Bitte beachten Sie, dass für "Bestellmenge" und "Menge Gratisbestellung" nur positive Zahlenwerte erlaubt sind.'));
		span.appendChild(document.createElement('br'));
		span.appendChild(document.createTextNode('Die fehlerhaften Eingabefelder sind mit einem roten Rahmen gekennzeichnet.'));
		span.appendChild(document.createElement('br'));
		document.getElementById('message').appendChild(span);
		document.getElementById('message').appendChild(document.createElement('br'));

		var arrayPageSize = getPageSize();
		window.scrollTo(0,arrayPageSize[1]);
	}
}

function Ausgewaehlte_Artikel_Speichern(a_AA)
{
	var data = '';
	for (var i = a_AA.length-1; i >= 0 ; --i)
	{
		data += i+'[0]='+a_AA[i][0]+'&'+i+'[1]='+a_AA[i][1]+'&'+i+'[2]='+a_AA[i][2]+'&'+i+'[3]='+a_AA[i][3];
		data += (i != 0 ? '&' : '');
	}
	var url = '/js/ajax_Ausgewaehlte_Artikel_Speichern.php';
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: data});
}

function Ausgewaehlte_Artikel_Laden()
{
	var url = '/js/ajax_Ausgewaehlte_Artikel_Laden.php';
	var myAjax = new Ajax.Request(url, {method: 'post'});
}

function Zusaetzliche_Bestellangaben_Formular_erstellen()
{
	form = document.createElement('form');
	form.setAttribute('id','Zusaetzliche_Bestellangaben_Formular');

	fieldset = document.createElement('fieldset');

	legend = document.createElement('legend');
	legend.appendChild(document.createTextNode('Zusätzliche Bestellangaben'));
	fieldset.appendChild(legend);

	fieldset.appendChild(document.createElement('br'));

	div = document.createElement('div');
	div.setAttribute('id','message');
	fieldset.appendChild(div);

	p = document.createElement('p');
	label = document.createElement('label');
	label.htmlFor = 'Lieferung_fruehestens_Tag';
	label.appendChild(document.createTextNode('Lieferung frühestens: '));
	p.appendChild(label);
	select = document.createElement('select');
	select.setAttribute('size','1');
	select.setAttribute('id','Lieferung_fruehestens_Tag');
	select.onchange = function() { Lieferung_spaetestens_errechnen() }
	for (i = 1; i <= 31; i++)
	{
		option = document.createElement('option');
		option.setAttribute('value',(i < 10 ? String('0'+i) : i));
		if (a_Lieferung_fruehestens[0] == i)
		{
			option.setAttribute('selected','selected');
		}
		option.appendChild(document.createTextNode((i < 10 ? String('0'+i) : i)));
		select.appendChild(option);
	}
	p.appendChild(select);
	select = document.createElement('select');
	select.setAttribute('size','1');
	select.setAttribute('id','Lieferung_fruehestens_Monat');
	select.onchange = function() { Lieferung_spaetestens_errechnen() }
	for (i = 1; i <= 12; i++)
	{
		option = document.createElement('option');
		option.setAttribute('value',(i < 10 ? String('0'+i) : i));
		if (a_Lieferung_fruehestens[1] == i)
		{
			option.setAttribute('selected','selected');
		}
		option.appendChild(document.createTextNode((i < 10 ? String('0'+i) : i)));
		select.appendChild(option);
	}
	p.appendChild(select);
	select = document.createElement('select');
	select.setAttribute('size','1');
	select.setAttribute('id','Lieferung_fruehestens_Jahr');
	select.onchange = function() { Lieferung_spaetestens_errechnen() }
	jetzt = new Date();
	Jahr = jetzt.getFullYear();
	for (i = Jahr; i <= (Jahr+1); i++)
	{
		option = document.createElement('option');
		option.setAttribute('value',i);
		if (a_Lieferung_fruehestens[2] == i)
		{
			option.setAttribute('selected','selected');
		}
		option.appendChild(document.createTextNode(i));
		select.appendChild(option);
	}
	p.appendChild(select);
	p.appendChild(document.createTextNode('Der frühestmögliche Termin ist der '+a_Lieferung_fruehestens[0]+'.'+a_Lieferung_fruehestens[1]+'.'+a_Lieferung_fruehestens[2]+'.'));
	fieldset.appendChild(p);

	p = document.createElement('p');
	label = document.createElement('label');
	label.htmlFor = 'Lieferung_spaetestens_Tag';
	label.appendChild(document.createTextNode('Lieferung spätestens (optional): '));
	p.appendChild(label);
	select = document.createElement('select');
	select.setAttribute('size','1');
	select.setAttribute('id','Lieferung_spaetestens_Tag');
	option = document.createElement('option');
	option.setAttribute('value',0);
	select.appendChild(option);
	for (i = 1; i <= 31; i++)
	{
		option = document.createElement('option');
		option.setAttribute('value',(i < 10 ? String('0'+i) : i));
		option.appendChild(document.createTextNode((i < 10 ? String('0'+i) : i)));
		select.appendChild(option);
	}
	p.appendChild(select);
	select = document.createElement('select');
	select.setAttribute('size','1');
	select.setAttribute('id','Lieferung_spaetestens_Monat');
	option = document.createElement('option');
	option.setAttribute('value',0);
	select.appendChild(option);
	for (i = 1; i <= 12; i++)
	{
		option = document.createElement('option');
		option.setAttribute('value',(i < 10 ? String('0'+i) : i));
		option.appendChild(document.createTextNode((i < 10 ? String('0'+i) : i)));
		select.appendChild(option);
	}
	p.appendChild(select);
	select = document.createElement('select');
	select.setAttribute('size','1');
	select.setAttribute('id','Lieferung_spaetestens_Jahr');
	jetzt = new Date();
	Jahr = jetzt.getFullYear();
	option = document.createElement('option');
	option.setAttribute('value',0);
	select.appendChild(option);
	for (i = Jahr; i <= (Jahr+1); i++)
	{
		option = document.createElement('option');
		option.setAttribute('value',i);
		option.appendChild(document.createTextNode(i));
		select.appendChild(option);
	}
	p.appendChild(select);
	span = document.createElement('span');
	span.setAttribute('id','Lieferung_spaetestens_Select_Hinweistext');
	span.appendChild(document.createTextNode('Der frühestmögliche Termin ist der '+h_Lieferung_spaetestens['day']+'.'+h_Lieferung_spaetestens['month']+'.'+h_Lieferung_spaetestens['year']+'.'));
	p.appendChild(span);
	fieldset.appendChild(p);

	p = document.createElement('p');
	label = document.createElement('label');
	label.appendChild(document.createTextNode('Anmerkungen (optional): '));
	p.appendChild(label);

	div = document.createElement('div');
	div.setAttribute('id','Anmerkungen_Rahmen');

	/* ---------- ANMERKUNGEN ---------- */
		//	The NAME attribute cannot be set at run time on elements dynamically
		//	created with the createElement method. To create an element with a
		//	name attribute, include the attribute and value when using the createElement method.
		//	(Quelle: http://msdn2.microsoft.com/en-us/library/ms534184.aspx)

		input = document.createElement('input');
		input.setAttribute('type','radio');
		input.setAttribute('id','AnmerkungAus');
		input.setAttribute('name','Anmerkungen');
		input.setAttribute('checked','checked');
		input.className = 'Anmerkung';
		input.onclick = function() { for (j = 0; j < a_ZBA.length; j++) { document.getElementById('Anmerkung'+String(j)).checked = false; } this.checked = true; }
		div.appendChild(input);
		label = document.createElement('label');
		label.htmlFor = 'AnmerkungAus';
		label.className = 'Anmerkung';
		label.appendChild(document.createTextNode('-- keine --'));
		div.appendChild(label);
		br = document.createElement('br');
		br.className = 'cb';
		div.appendChild(br);
		div.appendChild(document.createElement('br'));


		for (i = 0; i < a_ZBA.length; i++)
		{
			input = document.createElement('input');
			input.setAttribute('type','radio');
			input.setAttribute('id','Anmerkung'+String(i));
			input.setAttribute('name','Anmerkungen');
			input.className = 'Anmerkung';
			input.onclick = function() { for (j = 0; j < a_ZBA.length; j++) { document.getElementById('Anmerkung'+String(j)).checked = false; } document.getElementById('AnmerkungAus').checked = false; this.checked = true;  }
			div.appendChild(input);
			label = document.createElement('label');
			label.htmlFor = 'Anmerkung'+String(i);
			label.className = 'Anmerkung';
			label.appendChild(document.createTextNode(a_ZBA[i]));
			div.appendChild(label);
			br = document.createElement('br');
			br.className = 'cb';
			div.appendChild(br);
		}
	/* ---------- /ANMERKUNGEN ---------- */

	p.appendChild(div);

	fieldset.appendChild(p);

	p = document.createElement('p');
	p.className = 'buttons cb';
	p.appendChild(document.createElement('br'));
	label = document.createElement('label');
	label.appendChild(document.createTextNode('.'));
	p.appendChild(label);

	input = document.createElement('input');
	input.setAttribute('type','button');
	input.setAttribute('value','Bestellung absenden');
	input.onclick = function() { Bestellung_abschliessen(); }
	p.appendChild(input);
	fieldset.appendChild(p);

	form.appendChild(fieldset);

	document.getElementById('Artikel_Liste').appendChild(form);

	var arrayPageSize = getPageSize();
	window.scrollTo(0,arrayPageSize[1]);
}

/*
	"Zusätzliche Bestellangaben" Formular:
	Berechnet das frühestmögliche Datum für "Lieferung spätestens".
	Dieses Datum muss mindestens 7 Tage nach dem Datum "Lieferung frühestens" liegen.
*/
function Lieferung_spaetestens_errechnen()
{
	Lieferung_fruehestens_Tag = document.getElementById('Lieferung_fruehestens_Tag').value;
	Lieferung_fruehestens_Monat = document.getElementById('Lieferung_fruehestens_Monat').value;
	Lieferung_fruehestens_Jahr = document.getElementById('Lieferung_fruehestens_Jahr').value;

	h_old_date = $H({ day: Lieferung_fruehestens_Tag, month: Lieferung_fruehestens_Monat, year: Lieferung_fruehestens_Jahr, hour: '0', minute: '0', second: '0' });

	var url = '/js/ajax_Add_Period.php';
	var data = h_old_date.toQueryString()+'&seconds='+(86400 * 7)+'&hash_name=h_Lieferung_spaetestens';
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: data});

	window.setTimeout('Lieferung_spaetestens_Select_Hinweistext()', 500);
}

/*
	"Zusätzliche Bestellangaben" Formular:
	Den Hinweistext für das Select-Feld "Lieferung spätestens" setzen.
*/
function Lieferung_spaetestens_Select_Hinweistext()
{
	remove_all_child_nodes('Lieferung_spaetestens_Select_Hinweistext');
	document.getElementById('Lieferung_spaetestens_Select_Hinweistext').appendChild(document.createTextNode('Der frühestmögliche Termin ist der '+h_Lieferung_spaetestens['day']+'.'+h_Lieferung_spaetestens['month']+'.'+h_Lieferung_spaetestens['year']+'.'));
}

function Bestellung_abschliessen()
{
	Lieferung_fruehestens_Tag = document.getElementById('Lieferung_fruehestens_Tag').value;
	Lieferung_fruehestens_Monat = document.getElementById('Lieferung_fruehestens_Monat').value;
	Lieferung_fruehestens_Jahr = document.getElementById('Lieferung_fruehestens_Jahr').value;

	Lieferung_spaetestens_Tag = document.getElementById('Lieferung_spaetestens_Tag').value;
	Lieferung_spaetestens_Monat = document.getElementById('Lieferung_spaetestens_Monat').value;
	Lieferung_spaetestens_Jahr = document.getElementById('Lieferung_spaetestens_Jahr').value;

	Lieferung_fruehestens = Lieferung_fruehestens_Tag+'.'+Lieferung_fruehestens_Monat+'.'+Lieferung_fruehestens_Jahr;
	Lieferung_spaetestens = ((Lieferung_spaetestens_Jahr != 0 && Lieferung_spaetestens_Monat != 0 && Lieferung_spaetestens_Tag != 0) ? (Lieferung_spaetestens_Tag+'.'+Lieferung_spaetestens_Monat+'.'+Lieferung_spaetestens_Jahr) : '');;

	Anmerkung = '';
	for (i = 0; i < a_ZBA.length; i++)
	{
		if (document.getElementById('Anmerkung'+String(i)).checked)
		{
			Anmerkung = a_ZBA[i];
			break;
		}
	}

	/* ---------- HANDLE IN-VALID FIELDS ---------- */
		remove_all_child_nodes('message');

		var FehlerMeldungen = new Array();
		if (parseInt(String(Lieferung_fruehestens_Jahr) + String(Lieferung_fruehestens_Monat) + String(Lieferung_fruehestens_Tag)) < parseInt(String(a_Lieferung_fruehestens[2]) + String(a_Lieferung_fruehestens[1]) + String(a_Lieferung_fruehestens[0])))
		{
			FehlerMeldungen.push('Das Datum für die frühest mögliche Lieferung muss mindestens eine Woche in der Zukunft liegen. Der frühestmögliche Termin ist der '+a_Lieferung_fruehestens[0]+'.'+a_Lieferung_fruehestens[1]+'.'+a_Lieferung_fruehestens[2]+'.');
		}
		if (Lieferung_spaetestens_Jahr != 0 || Lieferung_spaetestens_Monat != 0 || Lieferung_spaetestens_Tag != 0)
		{
			if (Lieferung_spaetestens_Jahr == 0 || Lieferung_spaetestens_Monat == 0 || Lieferung_spaetestens_Tag == 0)
			{
				FehlerMeldungen.push('Wenn Sie einen spätmöglichsten Liefertermin angeben möchten, dann müssen Sie ein vollständiges Datum angeben.');
			}
			else if (parseInt(String(Lieferung_spaetestens_Jahr) + String(Lieferung_spaetestens_Monat) + String(Lieferung_spaetestens_Tag)) < parseInt(String(Lieferung_fruehestens_Jahr) + String(Lieferung_fruehestens_Monat) + String(Lieferung_fruehestens_Tag)))
			{
				FehlerMeldungen.push('Das Datum für die spätmöglichste Lieferung kann nicht vor dem Datum der frühestmöglichen Lieferung liegen.');
			}
			else if ((Lieferung_spaetestens_Jahr != 0 && Lieferung_spaetestens_Monat != 0 && Lieferung_spaetestens_Tag != 0) && (parseInt(String(Lieferung_spaetestens_Jahr) + String(Lieferung_spaetestens_Monat) + String(Lieferung_spaetestens_Tag)) < parseInt(String(h_Lieferung_spaetestens['year']) + String(h_Lieferung_spaetestens['month']) + String(h_Lieferung_spaetestens['day']))))
			{
				FehlerMeldungen.push('Das Datum für die spätmöglichste Lieferung muss mindestens eine Woche nach dem Datum der frühestmöglichen Lieferung liegen. Der frühestmögliche Datum für das Feld \'Lieferung spätestens\' ist der '+h_Lieferung_spaetestens['day']+'.'+h_Lieferung_spaetestens['month']+'.'+h_Lieferung_spaetestens['year']+'.');
			}
		}
		if (FehlerMeldungen.length > 0)
		{
			if (FehlerMeldungen.length == 1)
			{
				var message = 'Bitte beachten Sie bie Ihrer Eingabe folgenden Hinweis:';
			}
			else
			{
				var message = 'Bitte beachten Sie bie Ihrer Eingabe folgende Hinweise:';
			}
			span = document.createElement('span');
			span.className = 'messageError';
			span.appendChild(document.createTextNode(message));
			document.getElementById('message').appendChild(span);
			
			ul = document.createElement('ul');
			ul.className = 'messageError';
			for (var i = 0; i < FehlerMeldungen.length; i++)
			{
				li = document.createElement('li');
				li.appendChild(document.createTextNode(FehlerMeldungen[i]));
				ul.appendChild(li);
			}
			document.getElementById('message').appendChild(ul);
		}
	/* ---------- /HANDLE IN-VALID FIELDS ---------- */

	if (FehlerMeldungen.length == 0)
	{
		/* ---------- ALLE DATEN SAMMELN UND SPEICHERN ---------- */
		
			var url = '/js/ajax_Bestellung_abschliessen.php';
			var data = '';

			data += 'Ausgewaehlter_Kunde[Kundennummer]='+encodeURIComponent(a_AK[0])+'&';
			data += 'Ausgewaehlter_Kunde[Name]='+encodeURIComponent(a_AK[1])+'&';
			data += 'Ausgewaehlter_Kunde[Strasse]='+encodeURIComponent(a_AK[2])+'&';
			data += 'Ausgewaehlter_Kunde[PLZ]='+encodeURIComponent(a_AK[3])+'&';
			data += 'Ausgewaehlter_Kunde[Ort]='+encodeURIComponent(a_AK[4]);

			Zaehler = 0;
			for (var i = 0; i < a_AA.length; ++i)
			{
				for (var j = 0; j < a_A.length; ++j)
				{
					if (a_AA[i][0] == a_A[j][0] && a_AA[i][1] == a_A[j][1])
					{
						data += '&';
						data += 'Ausgewaehlte_Artikel['+Zaehler+'][WinkelsArtikelnummer]='+encodeURIComponent(a_A[j][0])+'&';
						data += 'Ausgewaehlte_Artikel['+Zaehler+'][LieferantenArtikelnummer]='+encodeURIComponent(a_A[j][1])+'&';
						data += 'Ausgewaehlte_Artikel['+Zaehler+'][Bezeichnung]='+encodeURIComponent(a_A[j][2])+'&';
						data += 'Ausgewaehlte_Artikel['+Zaehler+'][Gebinde]='+encodeURIComponent(a_A[j][3])+'&';
						data += 'Ausgewaehlte_Artikel['+Zaehler+'][Inhalt]='+encodeURIComponent(a_A[j][4])+'&';
						data += 'Ausgewaehlte_Artikel['+Zaehler+'][Sortengruppe]='+encodeURIComponent(a_A[j][5])+'&';
						data += 'Ausgewaehlte_Artikel['+Zaehler+'][Bestellmenge]='+encodeURIComponent(a_AA[i][2])+'&';
						data += 'Ausgewaehlte_Artikel['+Zaehler+'][MengeGratisbestellung]='+encodeURIComponent(a_AA[i][3]);
						Zaehler++;

						break;
					}
				}
			}

			data += '&';
			data += 'Zusaetzliche_Bestellangaben[Lieferung_fruehestens]='+encodeURIComponent(Lieferung_fruehestens)+'&';
			data += 'Zusaetzliche_Bestellangaben[Lieferung_spaetestens]='+encodeURIComponent(Lieferung_spaetestens)+'&';
			data += 'Zusaetzliche_Bestellangaben[Anmerkung]='+encodeURIComponent(Anmerkung);

			var myAjax = new Ajax.Request(url, {method: 'post', parameters: data});
		/* ---------- /ALLE DATEN SAMMELN UND SPEICHERN ---------- */

		window.setTimeout('ZeigeSeite(\'Bestellung_abgeschlossen\')', 100);
		window.setTimeout('Bestellung_Druckversion_oeffnen()', 100);
	}
}

function Bestellung_Druckversion_oeffnen()
{
	var breite = 750;
	var hoehe = 500;
	var positionX = ((screen.availWidth / 2) - breite / 2);
	var positionY = ((screen.availHeight / 2) - hoehe / 2);

	popup = window.open('/Druckversion.php','Bestellung','toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width='+breite+',height='+hoehe+',top='+positionY+',left='+positionX);
}

/*
	Wenn Artikel ausgewählt wurden und die Seite verlassen werden soll,
	dann soll eine Meldung angezeigt werden.
*/

window.onbeforeunload = leaving;

function leaving()
{
	if (a_AA.length != 0)
	{
		message = "Sie haben Ihre Bestellung noch nicht abgeschlossen!\n\nSie müssen auf der Seite bleiben, bis Sie ihre Bestellung abgeschlossen haben.";
		return message;
	}
}

function ping() {
  new PeriodicalExecuter(function(pe) {
    new Ajax.Request('/js/ajax_ping.php', { method: 'post', asynchronous: false });
  }, 60);
}

Event.observe(window, 'load', ping, false);
