function setCookie(name, value) {
	expires = new Date();
	expires.setTime(expires.getTime() + 1000*60*60*24);
	document.cookie = name + '=' + value + "; expires=" + expires.toGMTString() + "; path=/";
}
function getCookie(name) {
	var search = name + '=';
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = document.cookie.indexOf(';', offset);
			if (end == -1) {end = document.cookie.length;}
			return unescape(document.cookie.substring(offset, end));
		} else { return '';}
	} else { return ''; }
}
function deleteCookie(name) {
	if (getCookie(name)) { document.cookie = name + "=; path=/; expires=Thu, 01-Jan-70 00:00:01 GMT"; }
}

function mailto(before, after, do_action) {
	if (do_action == "link") {
		document.write("<a href=\"mailto:"+before+"@"+after+"\">"+before+"@"+after+"</a>");
	} else if (do_action == "text") {
		document.write(before+"@"+after);
	} else if (do_action == "open") {
		window.open("mailto:"+before+"@"+after);
	}
}

function check_form(f) {
	err = "";
	for (i=0; i<f.elements.length; i++) {
		if (f.elements[i].id != "") {
			if ($("#"+f.elements[i].id).hasClass("required") && !f.elements[i].disabled && $("#"+f.elements[i].id).css("display")!="none") {
				lbl = $("#l_"+f.elements[i].id).html();
				if (f.elements[i].type == "checkbox") {
					if (f.elements[i].checked == false) {
						err += "      - "+removeHTMLTags(lbl.replace(":*", "").replace(":", ""))+"\n";
					}
				} else {
					if (f.elements[i].value == "") {
						err += "      - "+removeHTMLTags(lbl.replace(":*", "").replace(":", ""))+"\n";
					}
				}
			}
		}
	}
	if (err != "") {
		alert("Nezadali ste tieto údaje:\n"+err);
		return false;
	} else {
		return true;
	}
}

function do_bookmark() {
	var agt=navigator.userAgent.toLowerCase();
	var major = parseInt(navigator.appVersion);
	if ( ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1)) && (major >= 4) ) {
		window.external.AddFavorite(window.top.document.location, window.top.document.title);
	} else {
		alert("Na pridanie stránky k obľúbeným stlačte Ctrl+D.");
	}
}
function do_send() {
	url = document.location.toString().replace("index.php", "content.php");
	if (url.indexOf("?") == -1) { url = url+"?action=send"; } else { url = url+"&action=send"; }
	_open(url, "SiteLabSendPage", "375", "275");
}
function do_print() {
	url = document.location.toString().replace("index.php", "content.php");
	if (url.indexOf("?") == -1) { url = url+"?action=print"; } else { url = url+"&action=print"; }
	_open(url, "_blank", "640", "480");
}

function _open(wURL, wName, wWidth, wHeight) {
	return window.open(wURL, wName, "width="+wWidth+",height="+wHeight+",top="+((screen.availHeight-wHeight)/2)+",left="+((screen.availWidth-wWidth)/2)+"");
}

function removeHTMLTags(str) {
	str = str.replace(/&(lt|gt);/g, function (strMatch, p1) {
		return (p1 == "lt")? "<" : ">";
	});
	var str = str.replace(/<\/?[^>]+(>|$)/g, "");
	return str;
}	
function to_url(str) {
	sd = "áäčďéěíĺľňóô öŕšťúů üýřžÁÄČĎÉĚÍĹĽŇÓÔ ÖŔŠŤÚŮ ÜÝŘŽ";
	bd = "aacdeeillnoo orstuu uyrzAACDEEILLNOO ORSTUU UYRZ"; 
	tx = "";
	for(i=0; i<str.length; i++) {
		if (sd.indexOf(str.charAt(i)) != -1) {
			tx += bd.charAt(sd.indexOf(str.charAt(i)));
		} else {
			tx += str.charAt(i);
		}
	}
	return tx.toLowerCase().replace(/ /g, "-").replace(/,/g, "").replace(/\./g, ""); 
}


var isIE = document.all?true:false;
var isNS = document.layers?true:false;
function onlyInteger(myfield,e) {
	var _ret = true;
	if (isIE) {
		if ((window.event.keyCode < 48 || window.event.keyCode > 57) && window.event.keyCode != 45) { window.event.keyCode = 0; _ret = false; }
		if (window.event.keyCode == 45) { if (myfield.value.indexOf("-") != -1) { window.event.keyCode = 0; _ret = false; } }
	}
	if (isNS) {
		if ((e.which < 48 || e.which > 57) && e.which != 45) { e.which = 0; _ret = false; }
		if (e.which == 45) { if (myfield.value.indexOf("-") != -1) { e.which = 0; _ret = false; } }
	}
	return (_ret); 
}
function onlyReal(myfield,e) {
	var _ret = true;
	if (isIE) {
		if ((window.event.keyCode < 48 || window.event.keyCode > 57) && window.event.keyCode != 46 && window.event.keyCode != 44) { window.event.keyCode = 0; _ret = false; }
		if (window.event.keyCode == 44) { window.event.keyCode = 46; }
		if (window.event.keyCode == 46) { if (myfield.value.indexOf(".") != -1) { window.event.keyCode = 0; _ret = false; } }
	}
	if (isNS) {
		if ((e.which < 48 || e.which > 57) && e.which != 46 && e.which != 44) { e.which = 0; _ret = false; }
		if (e.which == 44) { e.which = 46; }
		if (e.which == 46) { if (myfield.value.indexOf(".") != -1) { e.which = 0; _ret = false; } }
	}
	return (_ret); 
}

function onlyChars(myfield,e,def_only,def_chrs) {
	if (def_only) { var _ret = false; } else { var _ret = true; }
	if (isIE) { keyCode = window.event.keyCode; }
	if (isNS) { keyCode = e.which; }
	for (i=0; i<=def_chrs.length; i++) {
		if (def_only) {
			if (String.fromCharCode(keyCode) == def_chrs.substr(i,1)) { _ret = true; }
		} else {
			if (String.fromCharCode(keyCode) == def_chrs.substr(i,1)) { _ret = false; }
		}
	}
	if (isIE) { if (!_ret) { window.event.keyCode = 0; } }
	if (isNS) { if (!_ret) { e.which = 0; } }
	return (_ret); 
}

function formatNum(expr,decplaces) {
	var str = (Math.round(parseFloat(expr) * Math.pow(10,decplaces))).toString();
	while (str.length <= decplaces) { str = "0" + str; }
	var decpoint = str.length - decplaces;
	return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}

function str_pad (input, pad_length, pad_string, pad_type) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // + namespaced by: Michael White (http://getsprink.com)
    // +      input by: Marco van Oort
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: str_pad('Kevin van Zonneveld', 30, '-=', 'STR_PAD_LEFT');
    // *     returns 1: '-=-=-=-=-=-Kevin van Zonneveld'
    // *     example 2: str_pad('Kevin van Zonneveld', 30, '-', 'STR_PAD_BOTH');
    // *     returns 2: '------Kevin van Zonneveld-----'
 
    var half = '', pad_to_go;
 
    var str_pad_repeater = function (s, len) {
        var collect = '', i;
 
        while (collect.length < len) {collect += s;}
        collect = collect.substr(0,len);
 
        return collect;
    };
 
    input += '';
    pad_string = pad_string !== undefined ? pad_string : ' ';
    
    if (pad_type != 'STR_PAD_LEFT' && pad_type != 'STR_PAD_RIGHT' && pad_type != 'STR_PAD_BOTH') { pad_type = 'STR_PAD_RIGHT'; }
    if ((pad_to_go = pad_length - input.length) > 0) {
        if (pad_type == 'STR_PAD_LEFT') { input = str_pad_repeater(pad_string, pad_to_go) + input; }
        else if (pad_type == 'STR_PAD_RIGHT') { input = input + str_pad_repeater(pad_string, pad_to_go); }
        else if (pad_type == 'STR_PAD_BOTH') {
            half = str_pad_repeater(pad_string, Math.ceil(pad_to_go/2));
            input = half + input + half;
            input = input.substr(0, pad_length);
        }
    }
 
    return input;
}
