function toggle_on_off(f, a, hide_text) {
	f.style.display = (f.style.display == 'none' ? '' : 'none');
	if (a) {
		if (f.style.display == 'none') {
			a.innerHTML = a.hide_text;
		} else {
			a.hide_text = a.innerHTML;
			a.innerHTML = hide_text;
		}
	}
	return true;
}

function toggle_form_on_off(ch, f) {
	f.style.display = (ch.checked ? '' : 'none');
	return true;
}

function send_xmlhttprequest(obsluha, method, url, content, headers) {
    var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
    if (!xmlhttp) {
        return false;
    }
    xmlhttp.open(method, url);
    xmlhttp.onreadystatechange = function() {
        obsluha(xmlhttp);
    };
    if (headers) {
        for (var key in headers) {
            xmlhttp.setRequestHeader(key, headers[key]);
        }
    }
    xmlhttp.send(content);
    return true;
}

var timeout;

version = navigator.appVersion.toLowerCase();
agent = navigator.userAgent.toLowerCase();
ie = (version.indexOf('msie')>-1);
win = (version.indexOf('windows')>-1);
opera = (agent.indexOf('opera')>-1);
ie50 = (version.indexOf('msie 5.0')>-1);

function prn() {
document.write(' \&\#8226\; \074a href=\"javascript:goprn();\"\076Tisk\074/a\076');
}
function goprn() {
window.print();
}
function fav() {
if ((opera) && (win)) return false;
else if ((ie) && (win)) document.write(' \&\#8226\; \074a href=\"javascript:gofav();\"\076přidej mezi oblíbené\074/a\076');
}
function gofav() {
window.external.addFavorite('http://www.meritis.cz','Meritis');
}
function text_count(v, maxChars) {
	document.getElementById("count").innerHTML = "Můžete ještě napsat " + (maxChars - v.length) + " znaků.";
	if (v.length > maxChars) {
		document.getElementById('count').innerHTML = "Napsali jste více znaků než je povoleno!";
	}
}
var focus_changing = false;
var dont_check = false;

// Focus into field f, display message s and return false
function focus_alert(s, f) {
	if (!focus_changing) {
		focus_changing = true;
		f.focus();
		alert(s);
		focus_changing = false;
	}
	return false;
}

// Check fields in the form
// s is warning message, f is pointer to <form>, next arguments are names of mandatory fields
// example of use: <form onSubmit="return form_correct('Fill-in all marked fields!', this, 'name', 'password');">
function form_correct(s, f) {
	// correctness of each field
	for (var i=0; i < f.elements.length; i++) {
		if (f.elements[i].onblur && !f.elements[i].onblur()) {
			return false;
		}
	}
	
	// non-emptiness of selected fields
	fields_loop: for (var i=2; i < arguments.length; i++) {
		if (f[arguments[i]].length && !f[arguments[i]].options) {
			for (var j=0; j < f[arguments[i]].length; j++) {
				if (f[arguments[i]][j].checked) { //! suppose only multiple <input type="checkbox|radio">
					continue fields_loop;
				}
			}
			return focus_alert(s, f[arguments[i]][0]);
		} else if (f[arguments[i]].value == '') {
			return focus_alert(s, f[arguments[i]]);
		}
	}
	return true;
}

function checkbox_correct(s, f) {
	if (f.checked) {
		return true;
	}
	return focus_alert(s, f);
}

// Check if field matches ereg
// example of use: <input onblur="return ereg_correct('Enter a date!', this, /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/);">
function ereg_correct(s, f, regexp) {
	if (f.value == '' || regexp.test(f.value)) {
		return true;
	}
	return focus_alert(s, f);
}

// Check if there is a number in the field
// s is warning message, f is input field, boolean negative allows negative values, d1 is number of digits before decimal point, optional d2 is maximum count of digits after decimal point (empty - none, d1 - unlimited)
// example of use: <input onblur="return number_correct('Enter a positive number!', this, false, 5);">
function number_correct(s, f, negative, d1, d2) {
	f.value = f.value.replace(',', '.');
	return ereg_correct(s, f, new RegExp('^'+ (negative ? '-?' : '') +'[0-9]{0,'+ (d1 ? d1 : '') +'}'+ (d2 ? '([.][0-9]{1,'+ d2 +'})?' : '') +'$'));
}

// Check if the field contains number between 1901 and 2155
// example of use: <input onblur="return year_correct('Enter a year!', this);">
function year_correct(s, f) {
	return ereg_correct(s, f, /^(19(0[1-9]|[1-9][0-9])|20[0-9][0-9]|21[0-4][0-9]|215[0-5])$/);
}

// Check if there is an e-mail in the field, simplified!
// example of use: <input onblur="return email_correct('Enter an e-mail!', this);">
function email_correct(s, f) {
	return ereg_correct(s, f, /^[^ @]+@[^ @]+\.[^ @]+$/);
}

// Check if there is a URL in the field, simplified!
// example of use: <input onblur="return url_correct('Enter a URL!', this);">
function url_correct(s, f) {
	return ereg_correct(s, f, /^https?:\/\/[^ \/]+\.[^ ]+/);
}

// Check if f1.value equals f2.value
// example of use: <input type="password" name="a"><input type="password" onblur="return equal_correct('Passwords differ!', this.form['a'], this);">
function equal_correct(s, f1, f2) {
	if (f1.value == f2.value) {
		return true;
	}
	return focus_alert(s, f1);
}

// Open image window
function img_open(filename, w, h, title) {
	var wnd = window.open('', '', (w && h ? 'width=' + w + ',height=' + h : 'width=300,height=300'));
	if (!wnd) {
		return false;
	}
	wnd.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n<html xmlns="http://www.w3.org/1999/xhtml">\n');
	if (title) {
		wnd.document.write('<head>\n<title>' + title + '</title>\n</head>\n');
	}
	wnd.document.write('\n<body style="margin: 0;">\n\n');
	wnd.document.write('<img src="' + filename + '"' + (w && h ? ' width="' + w + '" height="' + h + '"' : '') + ' alt="' + (title ? title : '') + '" style="margin: 0;" />');
	wnd.document.write('\n</body>\n</html>\n');
	wnd.document.close();
	return true;
}

// Remove Czech diacricit and replace non-alphanumeric sequences by dash
function make_url(s) {
	var nodiac = { 'á': 'a', 'č': 'c', 'ď': 'd', 'é': 'e', 'ě': 'e', 'í': 'i', 'ň': 'n', 'ó': 'o', 'ř': 'r', 'š': 's', 'ť': 't', 'ú': 'u', 'ů': 'u', 'ý': 'y', 'ž': 'z' };
    return s.toLowerCase().replace(/[áčďéěíňóřšťúůýž]/g, function (str) {
		return nodiac[str];
	}).replace(/[^a-z0-9_]+/g, '-').replace(/^-|-$/g, '');
}

// Copy table row for referenced editable tables
function row_copy(row) {
	var tags = row.parentNode.appendChild(row.cloneNode(true)).getElementsByTagName("*");
	for (var i=0; i < tags.length; i++) {
		if (/^(input|textarea|select)$/i.test(tags[i].tagName)) {
			tags[i].name = tags[i].name.replace(/\[-/, "[-1");
			if (tags[i].tagName == "select") {
				tags[i].selectedIndex = 0;
			} else {
				tags[i].value = "";
			}
		}
	}
}
