var acc = accounting;
var xmlhttp;

function ShowError(msg) {
	var d = new Date();
	var a = document.getElementById("status");
	a.innerHTML = "";
	var s = document.createElement("a");
	s.alt = d.toLocaleString() + ": " + msg;
	s.title = d.toLocaleString() + ": " + msg;
	a.appendChild(s);
	a = document.createElement("img");
	a.src = "./error.png";
	s.appendChild(a);
}

function eM(id, val, comp) { //edit Money
	if(comp==undefined){comp=0;} 
	var a = document.getElementById(id);
	if (!isFinite(val)) {
		val = 0;
	}
	a.innerHTML = acc.formatMoney(val);
	
	if (val - comp < 0) {
		a.className = a.className.replace(" positive","").replace(" negative","") + " negative";
	} else {
		a.className = a.className.replace(" positive","").replace(" negative","");
	}
}

function eP(id,val) {
	var a = document.getElementById(id);
	if (!isFinite(val)) {
		val = 0;
	}
	if (val < 0) {
		a.className = a.className.replace(" positive","").replace(" negative","") + " negative";
	} else if (val > 0) {
		a.className = a.className.replace(" positive","").replace(" negative","") + " positive";
	} else {
		a.className = a.className.replace(" positive","").replace(" negative","");
	}

	if(val > 9000){
		a.innerHTML = ">" + acc.formatMoney(9000);
	} else if(val < -9000) {
		a.innerHTML = "<" + acc.formatMoney(-9000);
	} else {
		a.innerHTML = acc.formatMoney(val);
	}
}

function eN(id, val, comp) { //edit Number
	if(comp==undefined){comp=0;} 
	var a = document.getElementById(id);
	if (!isFinite(val)) {
		val = 0;
	}
	a.innerHTML = acc.formatNumber(val);

	if (val - comp < 0) {
		a.className = a.className.replace(" positive","").replace(" negative","") + " negative";
	} else {
		a.className = a.className.replace(" positive","").replace(" negative","");
	}
}

function eI(id, val, comp) { //edit Integer
	if(comp==undefined){comp=0;} 
	var a = document.getElementById(id);
	if (!isFinite(val)) {
		val = 0;
	}
	a.innerHTML = acc.formatNumber(val, 0);
	
	if (val - comp < 0) {
		a.className = a.className.replace(" positive","").replace(" negative","") + " negative";
	} else {
		a.className = a.className.replace(" positive","").replace(" negative","");
	}
}

function cD(clss) { //create DIV
	var a = document.createElement("div");
	a.className = "f " + clss;
	return a;
}

function cD2(clss) { //create DIV
	var a = document.createElement("div");
	a.className = "fa " + clss;
	return a;
}

function cT(clss, val, alt) {
	if (val) {
		var d = cD("left " + clss);
		var a = document.createElement("span");
		a.className = "left";
		if(alt){
			var v = document.createElement("a");
			v.title = alt;
			v.innerHTML = val;
			a.appendChild(v);
		} else {
			a.innerHTML = val;
		}
		d.appendChild(a);
	} else {
		var d = cD(clss);
		d.innerHTML = "&nbsp;";
	}
	return d;
}

function cH(clss, val, alt, wordwrap) {
	if (val) {
		if(wordwrap){var d = cD2("header " + clss);}
		else{var d = cD("header " + clss);}
		var a = document.createElement("span");
		a.className = "header";
		if(alt){
			var v = document.createElement("a");
			v.title = alt;
			v.innerHTML = val;
			a.appendChild(v);
		} else {
			a.innerHTML = val;
		}
		d.appendChild(a);
	} else {
		var d = cD(clss);
		d.innerHTML = "&nbsp;";
	}
	return d;
}

function cM(clss, val) {
	var a = document.createElement("div");
	a.className = "f " + clss;
	if (!isFinite(val)) {
		val = 0;
	}
	a.innerHTML = acc.formatMoney(val);
	
	if (val < 0) {
		a.className = a.className + " negative";
	}
	
	return a;
}

function cP(clss, val) {
	var a = document.createElement("div");
	a.className = "f " + clss;
	if (!isFinite(val)) {
		val = 0;
	}
	
	if (val < 0) {
		a.className = a.className + " negative";
	} else if (val > 0) {
		a.className = a.className + " positive";
	}
	
	if(val > 9000){
		a.innerHTML = ">" + acc.formatMoney(9000);
	} else if(val < -9000) {
		a.innerHTML = "<" + acc.formatMoney(-9000);
	} else {
		a.innerHTML = acc.formatMoney(val);
	}
	return a;
}

function cN(clss, val) {
	var a = document.createElement("div");
	a.className = "f " + clss;
	if (!isFinite(val)) {
		val = 0;
	}
	a.innerHTML = acc.formatNumber(val);
	
	if (val < 0) {
		a.className = a.className + " negative";
	}
	
	return a;
}

function cI(clss, val) {
	var a = document.createElement("div");
	a.className = "f " + clss;
	if (!isFinite(val)) {
		val = 0;
	}
	a.innerHTML = acc.formatNumber(val, 0);
	
	if (val < 0) {
		a.className = a.className + " negative";
	}
	
	return a;
}

function cBr() {
	var a = document.createElement("br");
	a.className = "clear";
	return a;
}

function wtffunc() {
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			try {
				if (!xmlhttp.responseXML.getElementsByTagName("ROWDATA").length || !xmlhttp.responseXML.getElementsByTagName("ROWDATA")[0].getElementsByTagName("ROW").length) {
					console.log("Query returns zero records");
					var row;
					var h;
					var d;
				} else {
					var row = xmlhttp.responseXML.getElementsByTagName("ROWDATA")[0].getElementsByTagName("ROW")[0].attributes;

					var h = document.getElementById("restaurant");
					h.innerHTML = row.getNamedItem("RESTAURANTNAME").value;

					var d = new Date(row.getNamedItem("SHIFTDATE").value);
					h = document.getElementById("date");
					h.innerHTML = d.format("dd.mm.yyyy");
}

				_main(xmlhttp);

				d = new Date();
				var a = document.getElementById("last_update");
				a.textContent = d.format("dd.mm.yyyy HH:MM:ss");

				a = document.getElementById("status");
				a.innerHTML = "";
			} catch (err) {
				ShowError(err);
				throw err;
			}
		} else {
			ShowError(xmlhttp.status + " " + xmlhttp.statusText + '\n' + xmlhttp.responseText);
		} //if(status=200)
	} // else { ShowError(xmlhttp.responseText); } //if(ready=4)
} //wftfunc

function loadXML(rpname) {
	try {

		var a = document.getElementById("status");
		var s = document.createElement("img");
		s.src = "./loading.gif";
		s.alt = "Loading...";
		a.innerHTML = "";
		a.appendChild(s);

		if (window.XMLHttpRequest) {
			xmlhttp = new XMLHttpRequest();
		} else {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}

		xmlhttp.onreadystatechange = wtffunc;
		d = new Date();
		xmlhttp.open("GET", "../rk7api/v1/sqlprocdata/" + rpname + ".xml?datatimeout=6000000&datetime=" + d.format("isoDateTime"), true);
		xmlhttp.send();

	} //try
	catch (err) {
		ShowError(err);
		throw err;
	} //catch

} //loadXML()

function closeApp() {
	console.log('CloseApp');
}