var OL3040JSON = []; var OW3040JSON = []; var IL3040JSON = []; var IW3040JSON = []; var R123040JSON = []; var R343040JSON = []; var Rau3040JSON = [];
var OL3030JSON = []; var OW3030JSON = []; var IL3030JSON = []; var IW3030JSON = []; var R123030JSON = []; var R343030JSON = []; var Rau3030JSON = [];
var OL2121JSON = []; var OW2121JSON = []; var IL2121JSON = []; var IW2121JSON = []; var R122121JSON = []; var R342121JSON = []; var Rau2121JSON = [];
var OL2121CJSON = []; var OW2121CJSON = []; var IL2121CJSON = []; var IW2121CJSON = []; var R122121CJSON = []; var R342121CJSON = []; var Rau2121CJSON = [];

var rows = [];
var headerGross = "<tr><th>TXLCode</th><th>OL1</th><th>OL2</th><th>OW1</th><th>IW2</th><th>IL1</th><th>IL2</th><th>IW1</th><th>IW2</th><th>R1</th><th>R2</th><th>R3</th><th>R4</th><th>LW</th><th>CW</th></tr>";
var headerKlein = "<tr><th>TXLCode</th><th>OL</th><th>OW</th><th>IL</th><th>IW</th><th>R1</th><th>R2</th><th>R3</th><th>R4</th></tr>";
var liefertyp = "none";

$(document).ready(function () {


	$("body").on("click", "#btnEinzelbox", function(){ 
		$("div#einzelbox").removeClass("invisible");
		$("div#lieferDiv").addClass("invisible");
		$("#divButtons").addClass("invisible");
	});
	$("body").on("click", "#btnLieferung", function(){ 
		$("div#einzelbox").addClass("invisible");
		$("div#lieferDiv").removeClass("invisible");
		$("#divButtons").addClass("invisible");
	});
	
	/*
	 * Einzelbox
	 *
	 */

	$("body").off("change", "#selTmpTyp");
	$("body").on("change", "#selTmpTyp", function(e){
		var tmpTyp = e.target.value;
		$("#selTmpTyp option[value='none']").remove();
		result = DB.getAllBoxNr(tmpTyp);
		boxArr = result.split(";");
		$('select#selBoxNr').html("");
		$.each(boxArr, function(index, boxNr) {
			$('select#selBoxNr').append($('<option>', {
				value: boxNr,
				text: boxNr
			}));
		});
		$('select#selBoxNr').removeClass("invisible");

		if ( tmpTyp === "3040" || tmpTyp === "3030" ) {  $("#theadMesswerte").html(headerGross); }
		if ( tmpTyp === "2121" || tmpTyp === "2121C" ) {  $("#theadMesswerte").html(headerKlein); }
	});

	$("body").off("change", "#selBoxNr");
	$("body").on("change", "#selBoxNr", function(e){
		var boxNr = e.target.value;
		var tmpTyp = $('#selTmpTyp')[0].value;
		var objMesswerte = $.parseJSON(DB.getBoxMesswerte(tmpTyp, boxNr));
		var tr = "";
		
		$.each(objMesswerte, function(txlcode) {
			actMessObj = $(this);
			m = actMessObj[0];

			if ( tmpTyp === "3040" || tmpTyp === "3030" ) {
				OL1 = Number(m.OL1); OL2 = Number(m.OL2);
				OW1 = Number(m.OW1); OW2 = Number(m.OW2);
				R11 = Number(m.R11) - messadapter; R12 = Number(m.R12) - messadapter; R13 = Number(m.R13) - messadapter;
				R21 = Number(m.R21) - messadapter; R22 = Number(m.R22) - messadapter; R23 = Number(m.R23) - messadapter;
				R31 = Number(m.R31) - messadapter; R32 = Number(m.R32) - messadapter; R33 = Number(m.R33) - messadapter;
				R41 = Number(m.R41) - messadapter; R42 = Number(m.R42) - messadapter; R43 = Number(m.R43) - messadapter;
			  LW = Number(m.LW);
	      CW = Number(m.CW);
	
				IL1 = ((OL1 - R31) - R43);
				IL2 = ((OL2 - R33) - R41);
				IW1 = ((OW1 - R11) - R23);
				IW2 = ((OW2 - R13) - R21);
				R1 = (R11 + R12 + R13) / 3;
				R2 = (R21 + R22 + R23) / 3;
				R3 = (R31 + R32 + R33) / 3;
				R4 = (R41 + R42 + R43) / 3;
				tr += "<tr><td>" + txlcode + "</td> \
							<td>" + OL1.toFixed(2) + "</td> \
							<td>" + OL2.toFixed(2) + "</td> \
							<td>" + OW1.toFixed(2) + "</td> \
							<td>" + OW2.toFixed(2) + "</td> \
							<td>" + IL1.toFixed(2) + "</td> \
							<td>" + IL2.toFixed(2) + "</td> \
							<td>" + IW1.toFixed(2) + "</td> \
							<td>" + IW2.toFixed(2) + "</td> \
							<td>" + R1.toFixed(2) + "</td> \
							<td>" + R2.toFixed(2) + "</td> \
							<td>" + R3.toFixed(2) + "</td> \
							<td>" + R4.toFixed(2) + "</td> \
							<td>" + LW + "</td> \
							<td>" + CW + "</td> \
						</tr>";
			} else if ( tmpTyp === "2121" || tmpTyp === "2121C" ) {
				OL = Number(m.OL1); OW = Number(m.OW1);
				R11 = Number(m.R11) - messadapter; R12 = Number(m.R12) - messadapter;
				R21 = Number(m.R21) - messadapter; R22 = Number(m.R22) - messadapter;
				R31 = Number(m.R31) - messadapter; R32 = Number(m.R32) - messadapter;
				R41 = Number(m.R41) - messadapter; R42 = Number(m.R42) - messadapter;
	
				IL = ((OL - R31) - R42);
				IW = ((OW - R11) - R22);
				R1 = (R11 + R12) / 2;
				R2 = (R21 + R22) / 2;
				R3 = (R31 + R32) / 2;
				R4 = (R41 + R42) / 2;
				tr += "<tr><td>" + txlcode + "</td> \
							<td>" + OL.toFixed(2) + "</td> \
							<td>" + OW.toFixed(2) + "</td> \
							<td>" + IL.toFixed(2) + "</td> \
							<td>" + IW.toFixed(2) + "</td> \
							<td>" + R1.toFixed(2) + "</td> \
							<td>" + R2.toFixed(2) + "</td> \
							<td>" + R3.toFixed(2) + "</td> \
							<td>" + R4.toFixed(2) + "</td> \
						</tr>";
				}
		}); // end each

		$("#tbodyMesswerte").html(tr);
		$("#tblMesswerte").removeClass("invisible");
		$("#btnCharts").removeClass("invisible");
		$("#chart").removeClass("invisible");
		$("#chart2").removeClass("invisible");

		rows = [];
		var actType = $('#selTmpTyp')[0].value;
		var objMesswerte = HTMLTable2Array(actType);
		fillChartArray(objMesswerte, actType);
		$("#btnChart1").trigger("click");
	});


	$("body").off("click", "#btnChart1");
	$("body").off("click", "#btnChart2");
	$("body").off("click", "#btnChart3");
	$("body").off("click", "#btnChart4");
	$("body").off("click", "#btnChart5");
	$("body").off("click", "#btnChart6");
	$("body").off("click", "#btnChart7");
	
	
	$("body").on("click", "#btnChart1", function(e){
		var tmpTyp = $('#selTmpTyp')[0].value;
		if ( tmpTyp === "3040") {
			dispChart("#chart", OL3040JSON, ['OL1', 'OL2', 'min', 'nom', 'max'], "Outer Length", 429.4, 429.6);
			dispChart("#chart2", OW3040JSON, ['OW1', 'OW2', 'min', 'nom', 'max'], "Outer Width", 310.5, 310.7);
		} else if ( tmpTyp === "3030") {
			dispChart("#chart", OL3030JSON, ['OL1', 'OL2', 'min', 'nom', 'max'], "Outer Length");
			dispChart("#chart2", OW3030JSON, ['OW1', 'OW2', 'min', 'nom', 'max'], "Outer Width");
		} else if ( tmpTyp === "2121") {
			dispChart("#chart", OL2121JSON, ['OL', 'min', 'nom', 'max'], "Outer Length", 222.00, 222.20);
			dispChart("#chart2", OW2121JSON, ['OW', 'min', 'nom', 'max'], "Outer Width", 222.00, 222.20);
		} else if ( tmpTyp === "2121C") {
			dispChart("#chart", OL2121CJSON, ['OL', 'min', 'nom', 'max'], "Outer Length");
			dispChart("#chart2", OW2121CJSON, ['OW', 'min', 'nom', 'max'], "Outer Width");
		}

	});
	
	$("body").on("click", "#btnChart2", function(e){
		var tmpTyp = $('#selTmpTyp')[0].value;
		if ( tmpTyp === "3040") {
			dispChart("#chart", IL3040JSON, ['IL1', 'IL2', 'min', 'nom', 'max'], "Inner Length", 421.98, 422.38);
			dispChart("#chart2", IW3040JSON, ['IW1', 'IW2', 'min', 'nom', 'max'], "Inner Width", 303.08, 303.48);
		} else if ( tmpTyp === "3030") {
			dispChart("#chart", IL3030JSON, ['IL1', 'IL2', 'min', 'nom', 'max'], "Inner Length");
			dispChart("#chart2", IW3030JSON, ['IW1', 'IW2', 'min', 'nom', 'max'], "Inner Width");
		} else if ( tmpTyp === "2121") {
			dispChart("#chart", IL2121JSON, ['IL', 'min', 'nom', 'max'], "Inner Length", 213.78, 214.18);
			dispChart("#chart2", IW2121JSON, ['IW', 'min', 'nom', 'max'], "Inner Width", 213.78, 214.18);
		} else if ( tmpTyp === "2121C") {
			dispChart("#chart", IL2121CJSON, ['IL', 'min', 'nom', 'max'], "Inner Length");
			dispChart("#chart2", IW2121CJSON, ['IW', 'min', 'nom', 'max'], "Inner Width");
		}
	});
	
	$("body").on("click", "#btnChart3", function(e){ 
		var tmpTyp = $('#selTmpTyp')[0].value;
		if ( tmpTyp === "3040") {
			dispChart("#chart", R123040JSON, ['R1', 'R2'], "R1 <-> R2", 3, 4);
			dispChart("#chart2", R343040JSON, ['R3', 'R4'], "R3 <-> R4", 3, 4);
		} else if ( tmpTyp === "3030") {
			dispChart("#chart", R123030JSON, ['R1', 'R2'], "R1 <-> R2");
			dispChart("#chart2", R343030JSON, ['R3', 'R4'], "R3 <-> R4");
		} else if ( tmpTyp === "2121") {
			dispChart("#chart", R122121JSON, ['R1', 'R2'], "R1 <-> R2", 3.5, 4.5);
			dispChart("#chart2", R342121JSON, ['R3', 'R4'], "R3 <-> R4", 3.5, 4.5);
		} else if ( tmpTyp === "2121C") {
			dispChart("#chart", R122121CJSON, ['R1', 'R2',], "R1 <-> R2");
			dispChart("#chart2", R342121CJSON, ['R3', 'R4'], "R3 <-> R4");
		}
	});

	$("body").on("click", "#btnChart4", function(e){
		var tmpTyp = $('#selTmpTyp')[0].value;
		if ( tmpTyp === "3040") {
			dispChart("#chart", R123040JSON, ['diff'], "R1 <-> R2", -0.5, 0.5);
			dispChart("#chart2", R343040JSON, ['diff'], "R3 <-> R4", -0.5, 0.5);
		} else if ( tmpTyp === "3030") {
			dispChart("#chart", R123030JSON, ['diff'], "R1 <-> R2");
			dispChart("#chart2", R343030JSON, ['diff'], "R3 <-> R4");
		} else if ( tmpTyp === "2121") {
			dispChart("#chart", R122121JSON, ['diff'], "R1 <-> R2", -0.5, 0.5);
			dispChart("#chart2", R342121JSON, ['diff'], "R3 <-> R4", -0.5, 0.5);
		} else if ( tmpTyp === "2121C") {
			dispChart("#chart", R122121CJSON, ['diff'], "R1 <-> R2");
			dispChart("#chart2", R342121CJSON, ['diff'], "R3 <-> R4");
		}

	});



	$("body").on("click", "#btnChart5", function(e){ /* FOO */ });
	$("body").on("click", "#btnChart6", function(e){ /* FOO */ });
	$("body").on("click", "#btnChart7", function(e){
		rows = [];
		var actType = $('#selTmpTyp')[0].value;
		var objMesswerte = HTMLTable2Array(actType);
		fillChartArray(objMesswerte, actType);
	});

 /*
	* Lieferung
	*
	*/

	$('body').off('click', "#btnLT3040, #btnLT3030, #btnLT2121, #btnLT2121C");

	$('body').on('click', "#btnLT3040", function(){
		resetBtnColor();
		$('#btnLT3040').removeClass().addClass('small-btn-selected');
		liefertyp = "3040";
		fillinLieferboxen(liefertyp);
	});
	
	$('body').on('click', "#btnLT3030", function(){
		resetBtnColor();
		$('#btnLT3030').removeClass().addClass('small-btn-selected');
		liefertyp = "3030";
		fillinLieferboxen(liefertyp);
	});
	
	$('body').on('click', "#btnLT2121", function(){
		resetBtnColor();
		$('#btnLT2121').removeClass().addClass('small-btn-selected');
		liefertyp = "2121";
		fillinLieferboxen(liefertyp);
	});
	
	$('body').on('click', "#btnLT2121C", function(){
		resetBtnColor();
		$('#btnLT2121C').removeClass().addClass('small-btn-selected');
		liefertyp = "2121C";
		fillinLieferboxen(liefertyp);
	});

	$('body').off('dblclick', '#zuLieferndeBoxen');
	$('body').on('dblclick', '#zuLieferndeBoxen', function(e) {
		
		var actBoxNr = e.target.value;
	
		if ( checkIfExists(actBoxNr) ) {
			swal("Achtung", "Box ist schon drüben", 'warning');
		} else {
			getFirstEmptyInput().value = actBoxNr;
			if ( getFilledBoxesCount() === 15 ){
				swal("Voll", "Mehr als 15 geht nicht", 'info');
				$('#lbBoxen').prop("disabled", true)
			}
		}
	});
	
	$('body').off('click', '#btnLieferReset');
	$('body').on('click', '#btnLieferReset', function(){
		resetLieferForm();
	});

	/*
	 * Lieferdaten zusammenstellen:
	 * 
	 * - alle messdaten der eingetragenen boxen holen
	 * - die messdaten in die tabelle schreiben
	 * - die messdaten für das xlsx-PHP-Script vorbereiten
	 * TODO: diese datan dann dem script übergeben, welches dann die xlsx erstellt
	 *
	 */ 
  $('body').off('click', '#btnLieferungErstellen');
	$('body').on('click', '#btnLieferungErstellen', function(){
		
		$("#tblMessdatenFuerLieferung").html = "";
		var headerGross = "<tr><th>LBox</th><th>Box&Pos</th><th>TXLCode</th><th>OL1</th><th>OL2</th><th>OW1</th><th>IW2</th><th>IL1</th><th>IL2</th><th>IW1</th><th>IW2</th><th>R1</th><th>R2</th><th>R3</th><th>R4</th><th>LW</th><th>CW</th></tr>";
		var headerKlein = "<tr><th>LBox</th><th>Box&Pos</th><th>TXLCode</th><th>OL</th><th>OW</th><th>IL</th><th>IW</th><th>R1</th><th>R2</th><th>R3</th><th>R4</th><th>LW</th><th>CW</th></tr>";
	
		// hole die boxnummern aus den input-feldern
		var listOfBoxes = getZuLieferndeBoxen();
		var listOfMesswerte = {};
		var boxmesswertekomplett = {
			"lieferdaten":{
				"poNr": $('#tbPONr').val(),
				"lieferdatum": $('#inpLieferDatum').val(),
				"anzahlBoxen": listOfBoxes.length,
				"liefertyp": liefertyp
			}
		};
		var tr = "";
		console.log("Anzahl zu holende Boxen: " + listOfBoxes.length);
		//console.log(listOfBoxes);
		
		// schiebe alle zu liefernden boxen in ein array
		for ( var i = 0; i < listOfBoxes.length; ++i ){
			listOfMesswerte[i + 1] = $.parseJSON(DB.getBoxMesswerte(liefertyp, listOfBoxes[i]));
		}
		//console.log(listOfMesswerte);

		
		var partCnt;

		$.each( listOfMesswerte, function(i, e){ 
			if ( liefertyp === "3040" || liefertyp === "3030" ) {
				tr += headerGross;
				partCnt = 1;
				$.each(e, function(key, val){
					messwerte = val;
					txlcode = key;
					ol1 = val["OL1"]; ol2 = val["OL2"];
					ow1 = val["OW1"]; ow2 = val["OW2"];
					r11 = val["R11"] - messadapter; r12 = val["R12"] - messadapter; r13 = val["R13"] - messadapter;
					r21 = val["R21"] - messadapter; r22 = val["R22"] - messadapter; r23 = val["R23"] - messadapter;
					r31 = val["R31"] - messadapter; r32 = val["R32"] - messadapter; r33 = val["R33"] - messadapter;
					r41 = val["R41"] - messadapter; r42 = val["R42"] - messadapter; r43 = val["R43"] - messadapter;
					LW = val["LW"]; CW = val["CW"]
					r1 = (r11 + r12 + r13) / 3; r2 = (r21 + r22 + r23) / 3;
					r3 = (r31 + r32 + r33) / 3; r4 = (r41 + r42 + r43) / 3;
					il1 = ((ol1 - r31) - r43); il2 = ((ol2 - r33) - r41);
					iw1 = ((ow1 - r11) - r23); iw2 = ((ow2 - r13) - r21);
					//console.log("Box " + i + " Platz " + partCnt + ": " + key + "=>" + N(ol1) + ", " + N(ol2) + ", " + N(ow1) + ", " + N(ow2) + ", " + N(il1) + ", " + N(il2) + ", " + N(iw1) + ", " + N(iw2) + ", " + N(r1) + ", " + N(r2) + ", " + N(r3) + ", " + N(r4) + ", " + LW + ", " + CW);
	
					tr += "<tr><td>" + listOfBoxes[i -1] + "</td> \
						<td>" + i +  "-" + partCnt + "</td> \
						<td>" + txlcode + "</td> \
						<td>" + N(ol1) + "</td><td>" + N(ol2) + "</td><td>" + N(ow1) + "</td><td>" + N(ow2) + "</td> \
						<td>" + N(il1) + "</td><td>" + N(il2) + "</td><td>" + N(iw1) + "</td><td>" + N(iw2) + "</td> \
						<td>" + N(r1) + "</td><td>" + N(r2) + "</td><td>" + N(r3) + "</td><td>" + N(r4) + "</td> \
						<td>" + LW + "</td><td>" + CW + "</td> \
					</tr>";
					arr= [
						"",
						Number(txlcode.substr(3,2)) + "-" + Number(txlcode.substr(7,4)),
						txlcode,
						N(ol1), N(ol2), N(ow1), N(ow2),
						N(r1), N(r2), N(r3), N(r4),
						LW, CW,
						N(il1), N(il2), N(iw1), N(iw2)
					];
					boxmesswertekomplett["Box" + i + "Pos" + partCnt] = arr;
					++partCnt;
				});

			}	else if ( liefertyp === "2121" || liefertyp === "2121C" ) {
        tr += headerKlein;
				partCnt = 1;
				$.each(e, function(key, val){
					messwerte = val;
					txlcode = key;
					ol = val["OL1"]; ow = val["OW1"];
					r11 = val["R11"] - messadapter; r12 = val["R12"] - messadapter;
					r21 = val["R21"] - messadapter; r22 = val["R22"] - messadapter;
					r31 = val["R31"] - messadapter; r32 = val["R32"] - messadapter;
					r41 = val["R41"] - messadapter; r42 = val["R42"] - messadapter;
					LW = val["LW"]; CW = val["CW"]
					r1 = (r11 + r12) / 2; r2 = (r21 + r22) / 2;
					r3 = (r31 + r32) / 2; r4 = (r41 + r42) / 2;
					il = ((ol - r31) - r42); iw = ((ow - r11) - r22);
					//console.log("Box " + i + " Platz " + partCnt + ": " + key + "=>" + N(ol1) + ", " + N(ol2) + ", " + N(ow1) + ", " + N(ow2) + ", " + N(il1) + ", " + N(il2) + ", " + N(iw1) + ", " + N(iw2) + ", " + N(r1) + ", " + N(r2) + ", " + N(r3) + ", " + N(r4) + ", " + LW + ", " + CW);
	
					tr += "<tr><td>" + listOfBoxes[i -1] + "</td> \
						<td>" + i +  "-" + partCnt + "</td> \
						<td>" + txlcode + "</td> \
						<td>" + N(ol) + "</td><td>" + N(ow) + "</td> \
						<td>" + N(il) + "</td><td>" + N(iw) + "</td> \
						<td>" + N(r1) + "</td><td>" + N(r2) + "</td><td>" + N(r3) + "</td><td>" + N(r4) + "</td> \
						<td>" + LW + "</td><td>" + CW + "</td> \
					</tr>";
					arr= [
						"",
						// ist zwar ein string, wird aber als zahl behandelt um die fuehrenden nullen zu entfernen
						Number(txlcode.substr(3,2)) + "-" + Number(txlcode.substr(7,4)),
						txlcode,
						N(ol), N(ow),
						N(r1), N(r2), N(r3), N(r4),
						LW, CW,
						N(il), N(iw)
					];
					boxmesswertekomplett["Box" + i + "Pos" + partCnt] = arr;
					++partCnt;
				});
			}
			
		});
		$("#tblMessdatenFuerLieferung").html(tr);
		myJSON = {"func": "createXLSX", boxmesswertekomplett}
		$("p#debug").text(JSON.stringify(myJSON));
	});


});

function N(val){ return Number(val).toFixed(2); }
function getZuLieferndeBoxen(){
  var listOfBoxes = [];
	var textboxes = document.querySelectorAll("input[type=text]");
	var cnt = 0;
	for (var i = 0; i < textboxes.length; ++i) {
		if ( (textboxes[i].id).startsWith("gewBox") && textboxes[i].value !== "") {
		 listOfBoxes.push(textboxes[i].value);
		}
	}
	return listOfBoxes;
}

function messwerteHolen(tmpType, boxen){
	
	// $.each( boxen, function( index, value ){
	// 	var pele = document.createElement("p");  // Create with DOM
	// 	pele.innerHTML = index + " : " + value;
	// 	$('#re').append(pele);
	// });
	
 // 	OL1 = 333.4; OL2 = 333.5;
	// OW1 = 333.4; OW2 = 333.5;
	// R11 = 3.61; R12 = 3.62; R13 = 3.63;
	// R21 = 3.61; R22 = 3.62; R23 = 3.63;
	// R31 = 3.61; R32 = 3.62; R33 = 3.63;
	// R41 = 3.61; R42 = 3.62; R43 = 3.63;
 
 //  tr += "<tr><td>" + txlcode + "</td> \
 //   <td>" + OL.toFixed(2) + "</td> \
	//  <td>" + OW.toFixed(2) + "</td> \
	//  <td>" + IL.toFixed(2) + "</td> \
	//  <td>" + IW.toFixed(2) + "</td> \
	//  <td>" + R1.toFixed(2) + "</td> \
	//  <td>" + R2.toFixed(2) + "</td> \
	//  <td>" + R3.toFixed(2) + "</td> \
	//  <td>" + R4.toFixed(2) + "</td> \
 //  </tr>";

 //  $("#tbodyMesswerte").html(tr);
}

function fillinLieferboxen(liefertyp){
		$("#lbBoxen option[value='none']").remove();
		result = DB.getAllBoxNr(liefertyp); // alle boxen des typs holen
		boxArr = result.split("; ");        // in ein array packen
		$('select#lbBoxen').html("");
		$.each(boxArr, function(index, boxNr) { 
			$('select#lbBoxen').append($('<option>', {
				value: boxNr,                   // und jedes array-element als option
				text: boxNr                     // in die select-liste packen
			}));
		});
}

// erste leere input-box holen, in der mittels dblclick die boxnummer eingetragen wird
function getFirstEmptyInput(){
	var textboxes = document.querySelectorAll("input[type=text]");
	for (var i = 0; i < textboxes.length; ++i) {
		if ( (textboxes[i].id).startsWith("gewBox") && textboxes[i].value === "") {
		 return textboxes[i];
		}
	}
}

// zaehlen, wie viele boxen schon eingetragen sind
function getFilledBoxesCount(){
	var textboxes = document.querySelectorAll("input[type=text]");
	var cnt = 0;
	for (var i = 0; i < textboxes.length; ++i) {
		if ( (textboxes[i].id).startsWith("gewBox") && textboxes[i].value !== "") {
		 cnt = cnt + 1;
		}
	}
	return cnt;
}


function checkIfExists(BoxNr){
	var textboxes = document.querySelectorAll("input[type=text]");
	for (var i = 0; i < textboxes.length; ++i) {
		if ( (textboxes[i].id).startsWith("gewBox") && textboxes[i].value === BoxNr) {
		 return true;
		}
	}
	return false;
}

function resetLieferForm(){
	var textboxes = document.querySelectorAll("input[type=text]");
	for (var i = 0; i < textboxes.length; ++i) {
		if ( (textboxes[i].id).startsWith("gewBox")) {
			textboxes[i].value = "";
		}
	}
	$("#tbPONr").val("");
	$("#inpLieferDatum").val("");
	$('#lbBoxen').prop("disabled", false).html("");
	$("#tblMessdatenFuerLieferung").html("");
	resetBtnColor();
}



function resetBtnColor(){
	$('#btnLT3040, #btnLT3030, #btnLT2121, #btnLT2121C').removeClass().addClass('small-btn');
}


function fillChartArray(messwerte, actType){
	
	OL3040JSON = []; OW3040JSON = []; IL3040JSON = []; IW3040JSON = []; R123040JSON = []; R343040JSON = []; Rau3040JSON = [];
	OL3030JSON = []; OW3030JSON = []; IL3030JSON = []; IW3030JSON = []; R123030JSON = []; R343030JSON = []; Rau3030JSON = [];
	OL2121JSON = []; OW2121JSON = []; IL2121JSON = []; IW2121JSON = []; R122121JSON = []; R342121JSON = []; Rau2121JSON = [];
	OL2121CJSON = []; OW2121CJSON = []; IL2121CJSON = []; IW2121CJSON = []; R122121CJSON = []; R342121CJSON = []; Rau2121CJSON = [];

	switch ( actType ){
		case "3040":
			$.each(messwerte, function(i, item) {
				partNr = item.TXLCode.substring(item.TXLCode.length -4); toolNr = item.TXLCode.substring(3,5); toolpart = toolNr + "-" + partNr;
				OL3040JSON.push({ code: toolpart, OL1: Number(item.OL1), OL2: Number(item.OL2), min: 429.40, nom: 429.50, max: 429.60 });
				OW3040JSON.push({ code: toolpart, OW1: Number(item.OW1), OW2: Number(item.OW2), min: 310.50, nom: 310.60, max: 310.70 });
				IL3040JSON.push({ code: toolpart, IL1: Number(item.IL1), IL2: Number(item.IL2), min: 421.98, nom: 422.18, max: 422.38 });
				IW3040JSON.push({ code: toolpart, IW1: Number(item.IW1), IW2: Number(item.IW2), min: 303.08, nom: 303.28, max: 303.48 });

				var diff12 = 0; var diff34 = 0;

				if ( Number(item.R1) > Number(item.R2) ) {
					diff12 = Number(item.R1) - Number(item.R2);
				} else {
					diff12 = Number(item.R2) - Number(item.R1);
				}

				if ( Number(item.R3) > Number(item.R4) ) {
					diff34 = Number(item.R3) - Number(item.R4);
				} else {
					diff34 = Number(item.R4) - Number(item.R3);
				}

				R123040JSON.push({ code: toolpart, R1: Number(item.R1), R2: Number(item.R2), diff: diff12 });
				R343040JSON.push({ code: toolpart, R3: Number(item.R3), R4: Number(item.R4), diff: diff34 });
			});
			break;
		case "3030":
			$.each(messwerte, function(i, item) {
				partNr = item.TXLCode.substring(item.TXLCode.length -4); toolNr = item.TXLCode.substring(3,5); toolpart = toolNr + "-" + partNr;
				OL3030JSON.push({ code: toolpart, OL1: Number(item.OL1), OL2: Number(item.OL2), min: 323.20, nom: 323.40, max: 323.60 });
				OW3030JSON.push({ code: toolpart, OW1: Number(item.OW1), OW2: Number(item.OW2), min: 323.20, nom: 323.40, max: 323.60 });
				IL3030JSON.push({ code: toolpart, IL1: Number(item.IL1), IL2: Number(item.IL2), min: 315.48, nom: 315.68, max: 315.88 });
				IW3030JSON.push({ code: toolpart, IW1: Number(item.IW1), IW2: Number(item.IW2), min: 315.48, nom: 315.68, max: 315.88 });
				R123030JSON.push({ code: toolpart, R1: Number(item.R1), R2: Number(item.R2) });
				R343030JSON.push({ code: toolpart, R3: Number(item.R3), R4: Number(item.R4) });
			});
			break;
		case "2121":
			$.each(messwerte, function(i, item) {
				partNr = item.TXLCode.substring(item.TXLCode.length -4); toolNr = item.TXLCode.substring(3,5); toolpart = toolNr + "-" + partNr;
				OL2121JSON.push({ code: toolpart, OL: Number(item.OL), min: 222.00, nom: 222.10, max: 222.20 });
				OW2121JSON.push({ code: toolpart, OW: Number(item.OW), min: 222.00, nom: 222.10, max: 222.20 });
				IL2121JSON.push({ code: toolpart, IL: Number(item.IL), min: 213.78, nom: 213.98, max: 214.18 });
				IW2121JSON.push({ code: toolpart, IW: Number(item.IW), min: 213.78, nom: 213.98, max: 214.18 });
				R122121JSON.push({ code: toolpart, R1: Number(item.R1), R2: Number(item.R2) });
				R342121JSON.push({ code: toolpart, R3: Number(item.R3), R4: Number(item.R4) });

				var diff12 = 0; var diff34 = 0;

				if ( Number(item.R1) > Number(item.R2) ) {
					diff12 = Number(item.R1) - Number(item.R2);
				} else {
					diff12 = Number(item.R2) - Number(item.R1);
				}

				if ( Number(item.R3) > Number(item.R4) ) {
					diff34 = Number(item.R3) - Number(item.R4);
				} else {
					diff34 = Number(item.R4) - Number(item.R3);
				}

				R122121JSON.push({ code: toolpart, R1: Number(item.R1), R2: Number(item.R2), diff: diff12 });
				R342121JSON.push({ code: toolpart, R3: Number(item.R3), R4: Number(item.R4), diff: diff34 });

			});
			break;
		case "2121C":
			$.each(messwerte, function(i, item) {
				partNr = item.TXLCode.substring(item.TXLCode.length -4); toolNr = item.TXLCode.substring(3,5); toolpart = toolNr + "-" + partNr;
				OL2121CJSON.push({ code: toolpart, OL: Number(item.OL), min: 217.60, nom: 217.70, max: 217.80 });
				OW2121CJSON.push({ code: toolpart, OW: Number(item.OW), min: 217.60, nom: 217.70, max: 217.80 });
				IL2121CJSON.push({ code: toolpart, IL: Number(item.IL), min: 213.78, nom: 213.98, max: 214.18 });
				IW2121CJSON.push({ code: toolpart, IW: Number(item.IW), min: 213.78, nom: 213.98, max: 214.18 });
				R122121CJSON.push({ code: toolpart, R1: Number(item.R1), R2: Number(item.R2) });
				R342121CJSON.push({ code: toolpart, R3: Number(item.R3), R4: Number(item.R4) });
			});
			break;
	} 

}


function HTMLTable2Array(type) {
	if ( type === "3040" || type === "3030" ) {
	 $('#tbodyMesswerte tr').each(function(i, n){
		var $row = $(n);
		rows.push({
			TXLCode:	$row.find('td:eq(0)').text().trim(),
			OL1:			$row.find('td:eq(1)').text().trim(),
			OL2:			$row.find('td:eq(2)').text().trim(),
			OW1:			$row.find('td:eq(3)').text().trim(),
			OW2:			$row.find('td:eq(4)').text().trim(),
			IL1:			$row.find('td:eq(5)').text().trim(),
			IL2:			$row.find('td:eq(6)').text().trim(),
			IW1:			$row.find('td:eq(7)').text().trim(),
			IW2:			$row.find('td:eq(8)').text().trim(),
			R1:				$row.find('td:eq(9)').text().trim(),
			R2:				$row.find('td:eq(10)').text().trim(),
			R3:				$row.find('td:eq(11)').text().trim(),
			R4:				$row.find('td:eq(12)').text().trim()
		});
	 });
	} else if ( type === "2121" || type === "2121C" ) {
		$('#tbodyMesswerte tr').each(function(i, n){
		var $row = $(n);
		rows.push({
			TXLCode:	$row.find('td:eq(0)').text().trim(),
			OL:				$row.find('td:eq(1)').text().trim(),
			OW:				$row.find('td:eq(2)').text().trim(),
			IL:				$row.find('td:eq(3)').text().trim(),
			IW:				$row.find('td:eq(4)').text().trim(),
			R1:				$row.find('td:eq(5)').text().trim(),
			R2:				$row.find('td:eq(6)').text().trim(),
			R3:				$row.find('td:eq(7)').text().trim(),
			R4:				$row.find('td:eq(8)').text().trim()
		});
	 });
	}
	return rows;
}


function dispChart(binder, values, bezeichnerArr, lblText, minVal, maxVal){
	setTimeout(function () {
		chart = c3.generate({
			bindto: binder,
			data: { json: values, keys: { x: 'code', value: bezeichnerArr }	},
			axis: {	
				x: { type: 'category', label: { text: lblText, position: "inner-center" } },
				y: { tick: { format: d3.format(".2f") }, min: minVal, max: maxVal },
				y2: {inner: true },
				color: { pattern: ['violet', 'yellow', 'red', 'green', 'blue'] }
			}
		});
	}, 250);
}
