var focus_changing = false;
var dont_check = false;

function kraje_okresy(f, form) {
	form['okres'].length = 1;
	form['okres'].options[0].text = '(vyberte)';
	for (var key in okresy[f]) {
		var o = document.createElement('option');
		o.value = key;
		o.appendChild(document.createTextNode(okresy[f][key]));
		form['okres'].appendChild(o);
	}
}
function kategorie_podkategorie(f, form) {
	form['podkategorie'].length = 1;
	form['podkategorie'].options[0].text = '(vyberte)';
	for (var key in podkategorie[f]) {
		var o = document.createElement('option');
		o.value = key;
		o.appendChild(document.createTextNode(podkategorie[f][key]));
		form['podkategorie'].appendChild(o);
	}
}

// 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;
}

function check_fields(frm) {
    for (i=1; i < arguments.length; i++) {
        if (frm[arguments[i]].value == '') {
            frm[arguments[i]].focus();
            alert('Vyplňte všechny položky označené hvězdičkou.');
            return false;
        }
    }
    return true;
}

// 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;
}

// 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);
}

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;

function kosik(id, typ) {
    var img = document.getElementById('kosik-' + typ + '-' + id);
    var puvodni = img.src;
    img.src = '/img/' + typ + '-proces.gif';
    return send_xmlhttprequest(function (xmlhttp) {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			var reg = new RegExp(typ + '-v-kosiku');
			if (reg.test(puvodni)) {
				img.src = '/img/' + typ + '-do-kosiku.gif';
			} else {
				img.src = '/img/' + typ + '-v-kosiku.gif';
				alert(typ == 'akce' ? 'Akce byla vložena do košíku' : 'Obrázek byl vložen do košíku');
			}
        }
    }, 'GET', '/js/kosik.php?' + typ + '=' + id);
}

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(string) {
document.write(' \&\#8226\; \074a href=\"javascript:goprn();\"\076' + string + '\074/a\076');
}
function goprn() {
window.print();
}
function fav(string) {
if ((opera) && (win)) return false;
else if ((ie) && (win)) document.write(' \&\#8226\; \074a href=\"javascript:gofav();\"\076' + string + '\074/a\076');
}
function gofav() {
window.external.addFavorite('http://www.netreporters.org','NetReporters.org');
}
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!";
	}
}