// --- globals ---
var b_ns6 = (document.getElementById) ? true : false;
var b_msie = (document.all) ? true : false;
var b_ie4 = false;
var b_ie5 = false;
var b_ie6 = false;
var b_gecko = (navigator.userAgent.indexOf('Gecko') != -1 && navigator.userAgent.indexOf('Safari') == -1);
var gecko_version = (b_gecko ? navigator.userAgent.substr(navigator.userAgent.lastIndexOf('Gecko/')+6, 8) : -1);
var b_quirks = false;

if(b_msie) {
	if(b_ns6) b_ns6 = false;
	if(navigator.userAgent.indexOf('MSIE 6') > 0) b_ie6 = true;
	else if(navigator.userAgent.indexOf('MSIE 5') > 0) b_ie5 = true;
	else b_ie4 = true;
}

if(b_msie || b_ns6) b_quirks = (document.compatMode=='BackCompat') ? true : false;


// --- document and form functions ---
function $(id) {
	if(document.getElementById)
		return document.getElementById(id);
	return document.all[id];
}

function hide(id) {
        if($(id))
                $(id).style.display = "none";
}

function show(id) {
        if($(id))
                $(id).style.display = "block";
}

function vhide(id) {
        if($(id))
                $(id).style.visibility = 'hidden';
}

function vshow(id) {
        if($(id))
                $(id).style.visibility = 'visible';
}

function value_get(e) {
	if(e.type == "checkbox")
		return e.checked? "true" : "false";

	if(e.type == "text" || e.type == "file" || e.type == "textarea" || e.type == "hidden" ||
	   e.type == "submit" || e.type == "button" || e.type == "reset")
		return e.value;
		
	if(e.type == "select-one")
		return e.options[e.selectedIndex].value;

	if(e.type == "select-multiple") {
		var rs = [];
		for(var i=0; i<e.length; i++)
			if(e.options[i].selected)
				rs.push(e.options[i].value);
		return rs;
	}

	if(e.length) {
		if(e[0].type == "radio") {
			for(var i=0; i<e.length; i++)
				if(e[i].checked)
					return e[i].value;
			return "";
		}
	} else if(e.type == "radio") // single radio
		return e.checked? e.value : "";

	throw "Unknown input field '"+e.name+"', please contact support!";
}


// --- string functions ---
function hq(str) {
	return str.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g, '&quot;');
}


// --- window functions ---
function open_window(url,w,h,target="",options="") {
	if(!options) { // center window
		var dx = 200;
		var dy = 200;
		var browser_w = window.innerWidth;
		var browser_h = window.innerHeight;
		var browser_y = window.screenY;
		var browser_x = window.screenX;

		if(browser_w > w + 2*dx) {
			var left  = (browser_w - w) / 2 + browser_x;
			options += 'screenX='+left;
		} else if(browser_w) {
			width = browser_w - 2*dx;
			left  = dx + browser_x;
			options += 'screenX='+left;
		}

		if(browser_h > h + 2*dy) {
			var top  = (browser_h - h) / 2 + browser_y;
			options += (options ? ',' : '') + 'screenY='+top;
		} else if(browser_h) {
			height = browser_h - 2*dy;
			top  = dy + browser_y;
			options += (options ? ',' : '') + 'screenY='+top;
		}
	}

	 var w = open(url, target, "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width="+w+",height="+h + (options ? ","+options : ""));
        if(w.opener==null)
                w.opener = self;
	w.focus();
}

function emitMl(a,b,c,d) {
	let s = b+(c?'.'+c:'')+(d?'.'+d:'');
	document.write('<a href="mailto:'+a+'@'+s+'">'+a+'<i class="fal fa-at"></i>'+s+'</a>'); 
}
