let VERBOSE = false;
let DEBUG = true;
let DEBUGmany = true;
let messadapter = 38.242;

$(document).ready(function () {  });

// Boxenerstellung - globals
var BE = {};
BE.flavToFill = "empty";
BE.partCnt = 0;
BE.boxInhalte3040 = ["foo", "bar"];
BE.boxInhalte3030 = ["foo", "bar"];
BE.boxInhalte2121 = ["foo", "bar"];
BE.boxInhalte2121C = ["foo", "bar"];
BE.running = false;
BE.reset = function(verbose = 0) {
	BE.flavToFill = "empty";
	BE.partCnt = 0;
	BE.boxInhalte3040 = ["foo", "bar"];
	BE.boxInhalte3030 = ["foo", "bar"];
	BE.boxInhalte2121 = ["foo", "bar"];
	BE.boxInhalte2121C = ["foo", "bar"];
	$("#tbTXLCode").val("");
	$("#tbTXLCode").removeAttr("disabled");
	$("#btnCreateBox").attr("disabled", "true");
	$("#tbBoxNr").removeAttr("disabled");
	if ( verbose === 1 ) {
		console.log("BE.flavToFill: " + BE.flavToFill);
		console.log("BE.partCnt: " + BE.partCnt );
		console.log("boxInhalte3040: " + BE.boxInhalte3040 );
		console.log("boxInhalte3030: " + BE.boxInhalte3030 );
		console.log("boxInhalte2121: " + BE.boxInhalte2121 );
		console.log("boxInhalte2121C: " + BE.boxInhalte2121C);
	}
}



function loadMM3040() {
	$.ajax({
		url: 'html/messmaske3040.html', dataType: 'html', timeout: 1000,
		success: function (html) {
			$("#mainContainer").html(html);
			// disableAllMessTBs();
			setTimeout(function() { 
				$("#btn_mm3040_check").prop("disabled", true); 
				$("#btn_mm3040_eintragen").prop("disabled", true);
				$("#tb_mm3040_tool").prop("disabled", false).focus();
			}, (200));
		}
	});
	//reload_js('js/messmasken.js');
}
function loadMM3030() {
	$.ajax({
		url: 'html/messmaske3030.html', dataType: 'html', timeout: 1000,
		success: function (html) {
			$("#mainContainer").html(html);
			// disableAllMessTBs();
			setTimeout(function() { $("#btn_mm3030_check").prop("disabled", true); }, (30));
			setTimeout(function() { $("#btn_mm3030_eintragen").prop("disabled", true); }, (30));
			setTimeout(function() { $("#tb_mm3030_tool").prop("disabled", false).focus(); }, (30));
		}
	});
	//reload_js('js/messmasken.js');
}
function loadMM2121() {
	$.ajax({
		url: 'html/messmaske2121.html', dataType: 'html', timeout: 1000,
		success: function (html) {
			$("#mainContainer").html(html);
			// disableAllMessTBs();
			$("#btn_mm2121_check").prop("disabled", true);
			$("#btn_mm2121_eintragen").prop("disabled", true);
			$("#tb_mm2121_tool").prop("disabled", false).focus();
		}
	});
	//reload_js('js/messmasken.js');
}
function loadMM2121C() {
	$.ajax({
		url: 'html/messmaske2121C.html', dataType: 'html', timeout: 1000,
		success: function (html) {
			$("#mainContainer").html(html);
			// disableAllMessTBs();
			$("#btn_mm2121C_check").prop("disabled", true);
			$("#btn_mm2121C_eintragen").prop("disabled", true);
			$("#tb_mm2121C_tool").prop("disabled", false).focus();
		}
	});
	//reload_js('js/messmasken.js');
}

function loadKF480() {
	$.ajax({ 
		url: 'html/ofenKF480.html', 
		dataType: 'html', 
		timeout: 1000, 
		success: function (html) { 
			$("#mainContainer").html(html); 
		} 
	}); 
	setTimeout(function() { $("#beladezeit").val(jetzt()); }, (100));
	setTimeout(function() { $("#beladedatum").val(heute()); }, (100));
}
function loadKF80S() {
	$.ajax({ 
		url: 'html/ofenKF80S.html', 
		dataType: 'html', 
		timeout: 1000, 
		success: function (html) { 
			$("#mainContainer").html(html); 
		} 
	}); 
	setTimeout(function() { $("#beladezeit").val(jetzt()); }, (100));
	setTimeout(function() { $("#beladedatum").val(heute()); }, (100));
	// reload_js('js/ofensachen.js');
}
function loadKF80() {
	$.ajax({ 
		url: 'html/ofenKF80.html', 
		dataType: 'html', 
		timeout: 1000, 
		success: function (html) { 
			$("#mainContainer").html(html); 
		} 
	}); 
	setTimeout(function() { $("#beladezeit").val(jetzt()); }, (100));
	setTimeout(function() { $("#beladedatum").val(heute()); }, (100));
	// reload_js('js/ofensachen.js');
}

function loadBE() {
	if ( !BE.running ) {
		if ( DEBUG ) { console.log("MENU-BE BERunning: " + BE.running); }
		$.ajax({
			url: '/html/boxenerstellen.html', dataType: 'html', timeout: 1000,
			success: function (html) {
				$("#mainContainer").html(html);
			}
		});
		setTimeout(function() { $("#btnCreateBox").removeClass().addClass('mybtn_deact'); }, (30));
		BE.running = true;
	} else {
		if ( DEBUG ) { console.log("MENU-BE BERunning: " + BE.running); }
		BE.reset();
		$.ajax({
			url: '/html/boxenerstellen.html', dataType: 'html', timeout: 1000,
			success: function (html) {
				$("#mainContainer").html(html);
			}
		});
		setTimeout(function() { $("#btnCreateBox").removeClass().addClass('mybtn_deact'); }, (30));
		BE.running = true;

	}
	
	//reload_js('js/boxenerstellung.js');
}

function loadRH() {
	$.ajax({
		url: 'html/rauheiten.html', dataType: 'html', timeout: 1000,
		success: function (html) {
			$("#mainContainer").html(html);
		}
	});
	//reload_js('js/rauheiten.js');
}

function loadAW() {
	$("#mainContainer").html("");
	$.ajax({
		url: 'html/auswertung.html', dataType: 'html', timeout: 1000,
		success: function (html) {
			$("#mainContainer").html(html);
		}
	});
}

function loadBAW() {
	$("#mainContainer").html("");
	$.ajax({
		url: 'html/auswertenEinzelneBox.html', dataType: 'html', timeout: 1000,
		success: function (html) {
			$("#mainContainer").html(html);
			setTimeout(function() { $("#theadMesswerte").hide(); }, (30));
			
		}
	});
}

function loadConverter() {
	$("#mainContainer").html("");
	$.ajax({
		url: 'html/converter.html', dataType: 'html', timeout: 1000,
		success: function (html) {
			$("#mainContainer").html(html);
		}
	});
}


function playSound(identifier){
	var audio = new Audio();
	
	switch (identifier) {
		case "ding": audio.setAttribute("src", "media/ding.wav"); break;
		case "error": audio.setAttribute("src", "media/error.mp3"); break;
		case "tada": audio.setAttribute("src", "media/owin31.wav"); break;
		default: color = "black";
	}

	audio.load();
	audio.play();
}



function heute() {
	var now = new Date();
	var day = ("0" + now.getDate()).slice(-2);
	var month = ("0" + (now.getMonth() + 1)).slice(-2);
	return now.getFullYear() + "-" + (month) + "-" + (day);
}

function jetzt() {
	var datetime = "" + new Date($.now());var arr = datetime.split(" ");
	return arr[4];
}

function showErrorModal(header, message, info){
	if ( DEBUGmany ) { console.log(header + " - " + message + " - " + info); }
	$('#modal-title').text(header);
	$('#modalMsg').text(message);
	$('#text-warning').text(info);
	$('#myModal').modal('show');
}

function reload_js(src) {
	$('script[src="boxenerstellung.js"]').remove();
	$('script[src="messmasken.js"]').remove();
	$('script[src="ofensachen.js"]').remove();
	$('script[src="rauheiten.js"]').remove();
	$('<script>').attr('src', src).appendTo('head');
}
