$(document).ready(function () {
	$('#lb_boxen').attr("style", "width: 150px");
	$('#lb_boxen').html("<option>noch nichts gewählt</option>");
	
	
	$("body").off("click", "#btn_getBoxen3040")
	$("body").off("click", "#btn_getBoxen3030")
	$("body").off("click", "#btn_getBoxen2121")
	$("body").off("change", "select#lb_boxen")
	
	
	
	$("body").on("click", "#btn_getBoxen3040", function() {
			result = DB.getAllBoxNr("3040");
			boxArr = result.split(";");
			$('#lb_boxen').html("");
			$.each(boxArr, function(index, boxNr) {
					$('#lb_boxen').append($('<option>', {
							value: boxNr,
							text: boxNr
					}));
			});
	});
	
	$("body").on("click", "#btn_getBoxen3030", function() {
			result = DB.getAllBoxNr("3030");
			boxArr = result.split(";");
			$('#lb_boxen').html("");
			$.each(boxArr, function(index, boxNr) {
					$('#lb_boxen').append($('<option>', {
							value: boxNr,
							text: boxNr
					}));
			});
	});
	
	$("body").on("click", "#btn_getBoxen2121", function() {
			result = DB.getAllBoxNr("2121");
			boxArr = result.split(";");
			$('#lb_boxen').html("");
			$.each(boxArr, function(index, boxNr) {
					$('#lb_boxen').append($('<option>', {
							value: boxNr,
							text: boxNr
					}));
			});
	});

	$("body").on("click", "#btn_getBoxen2121C", function() {
			result = DB.getAllBoxNr("2121C");
			boxArr = result.split(";");
			$('#lb_boxen').html("");
			$.each(boxArr, function(index, boxNr) {
					$('#lb_boxen').append($('<option>', {
							value: boxNr,
							text: boxNr
					}));
			});
	});


	$("body").on("change", "select#lb_boxen", function() {



		var tbody = $('#tbodyMesswerte');
		var boxNr = $(this).val();
		var tr;
		var objMesswerte = $.parseJSON(DB.getBoxMesswerte("3040", boxNr));
		console.log(objMesswerte);

		$.each(objMesswerte, function(txlcode) {
			actMessObj = $(this);
			m = actMessObj[0];

			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;

			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> \
					</tr>";
		}); // end each
	$("#tbodyMesswerte").html(tr);
	$("#theadMesswerte").show();
	//});
	
	});

	
	
});