var d = "/";

/**************************************/
/* Pomocne funkce pro praci s prvky ***/
/**************************************/
function e(id){
	return document.getElementById(id);
}
function ev(id){
	return document.getElementById(id).value;
}


/**************************************/
/* Zobrazovani prvku ******************/
/**************************************/
function show(id){
	document.getElementById(id).style.display="inline";
}
function showb(id){
	document.getElementById(id).style.display="block";
}
function hide(id){
	document.getElementById(id).style.display="none";
}
function shwhd(id){
	if(document.getElementById(id).style.display=="block"){
		hide(id);
	} else {
		showb(id);
	}
}
function hdshw(id){
	if(document.getElementById(id)!=null)if((document.getElementById(id).style.display=="none")||(document.getElementById(id).style.display=="")){
		showb(id);
	} else {
		hide(id);
	}
}
function shwhdx(id){
	if(document.getElementById(id)!=null)if(document.getElementById(id).style.display=="block"){
		hidex(id);
	} else {
		showb(id);
	}
}
function hif(chb,co){
	if(chb.checked=="")
		document.getElementById(co).style.display="block";
	else
		document.getElementById(co).style.display="none";
}
function iShow(id, src, title){
	document.getElementById(id+'Img').src=src;
	document.getElementById(id+'Title').innerHTML=title;
	show(id);
}
function iHide(id){
	document.getElementById(id).src="";
	hide(id);
}

/**************************************/
/* Pouzivani povolenych znaku v input */
/**************************************/
function numKey(ev){
	var kx=getKey(ev);
	if(!ev.ctrlKey)if((kx>57)&&((kx<96)||(kx>105)))return false;
	return true;
}
function chch(obj){
	if(obj.value.indexOf(' ')>0){
		alert('V řetězci se nesmí objevit mezera.');
		obj.focus();
		return false;
	} else return true;
}
function pscKey(ev){
	var kx=getKey(ev);
	if(!ev.ctrlKey)if((kx>57)&&((kx<96)||(kx>105)))return false;
	return true;
}
function telKey(ev){
	var kx=getKey(ev);
	if(!ev.ctrlKey)if((kx>57)&&(kx!=109)&&(kx!=107)&&((kx<96)||(kx>105)))return false;
	return true;
}
function getKey(e){
	var code;
	if(e.keyCode)code=e.keyCode;	// IE a Mozilla
	else if(e.which)code=e.which;	// NN4
	return code;
}


function chForm2(param, submitID){
	if((chForm(param)) || (document.getElementById(submitID).disabled)){
		document.getElementById(submitID).disabled = true;
		document.getElementById(submitID).value='Odesílám...';
		return true;
	} else {
		return false;
	}

}

function chForm(param) {
	fields=param.split(",");
	for(var i=0;i<fields.length;i+=2){
                if (fields[i+1] != 'usr_login') {
                    if(document.getElementById(fields[i+1]).value==""){
                            alert(fields[i]+" je povinná položka!");
                            document.getElementById(fields[i+1]).focus();
                            return false;
                    }
                }
	}
	return true;
}

function zj_prohlizec(){
	this.ver=navigator.appVersion;
	this.dom=document.getElementById?1:0;
	this.ns4=(document.layers&&!this.dom)?1:0;
	this.ns6=(this.dom && parseInt(this.ver)>=5)?1:0;
	this.minIE4=(document.all)?1:0;
}

function souradniceKurzoru(e){
	if(prohlizec.ns4|prohlizec.ns6){
		mouseX=e.pageX;
		mouseY=e.pageY;
	}
/*	if(prohlizec.minIE4){
		mouseX=event.clientX+document.body.scrollLeft;
		mouseY=event.clientY+document.body.scrollTop;
	}*/
	return true;
}

var prohlizec=new zj_prohlizec();
var mouseX=0,mouseY=0,clickX=0,clickY=0,mv=false;
var prohlizec=new zj_prohlizec();
document.onmousemove=souradniceKurzoru;
var cDate=new Date();


function swAJAX(file, value, param, id, typ){
	if(document.getElementById(id).innerHTML=="")
		getAJAX(file, value, param, id, typ);
	else
		document.getElementById(id).innerHTML="";
}

/*
file = used ajax script
value = 1st parameter
param = 2nd parameter
val = HTML element (innerHTML)
typ = 0 or 1
*/
function getAJAX(file, value, param, val, typ) {
	var http_request = false;
	var request = "param="+value+"&param2="+param;
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (eror) {
			http_request = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	http_request.onreadystatechange = function() { returnValidate(http_request, file, val, typ); };
	http_request.open('POST', file, true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send(request);
}

function returnValidate(http_request, url, val, typ) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			if(val!=null)
				if(typ==0)document.getElementById(val).innerHTML = http_request.responseText;
				else
				if(typ==1)val = http_request.responseText;
		} else {
			if(http_request.status == 0){
				http_request.open("GET", url, true);
				http_request.send(null);
			} else alert('AJAX Chyba: '+http_request.status);
		}
	}
}

function isObject(a){
	return (typeof a == 'object');
}

function sw(id){
	if(document.getElementById(id).style.display=="block")
		document.getElementById(id).style.display="none";
	else
		document.getElementById(id).style.display="block";
}

function loadHTMLe(obj, val, edit){
	if(edit>0)document.getElementById(obj.id).contentWindow.document.designMode='on';
	document.getElementById(obj.id).contentWindow.document.body.innerHTML=val;
	document.getElementById(obj.id+'_value').value=val;

}
function sr(str, id){
	document.getElementById(id).value=str;
	document.getElementById(id).focus();
	document.getElementById(id+'_div').style.display='none';
	return true;
}
function srAjax(obj, inp){
	if(document.getElementById(inp+'_div').style.display!='block')document.getElementById(inp+'_div').style.display='block';
	getAJAX('/test.php', obj.value, inp, inp+'_div', 0);
	return true;
}
function srAjax2(obj, inp, event){
	if(getKey(event)==13)return true;
	if(document.getElementById(inp+'_div').style.display!='block')document.getElementById(inp+'_div').style.display='block';
	getAJAX('/test.php', obj.value, inp, inp+'_div', 0);
	return true;
}

function imgWin(src, width, height){
	var top, left;
	top = (screen.height-height)/2;
	left = (screen.width-width)/2;
	return !window.open(src, '', 'width='+(width+20)+', height='+(height+20)+', top='+top+', left='+left);
}

function sendForm(obj, event){
	if((event.ctrlKey)&&(getKey(event)==13))obj.form.submit();
}

/// Jump to next field after the strlen in field reaches 3 (maxlength) - jako pri vyplnovani serioveho cisla
/// Usage: <input size="3" tabindex="1" maxlength="3" onkeyup="toUnicode(this,this.value)">
function toUnicode(elmnt,content){
	if (content.length==elmnt.maxLength){
		next=elmnt.tabIndex
		if (next<document.forms[0].elements.length){
			document.forms[0].elements[next].focus()
		}
	}
}

/// je-li v odkaze k obrazku URL obrazku
function darkimg(obj){
	//alert(navigator.appVersion);
	if(document.getElementById('cf').style.display=="block"){
		document.getElementById('cf').style.display="none";
		document.getElementById('full').innerHTML='<td><img src="/img/loading.png" id="fullimg" alt="" /></td>';
	} else {
		document.getElementById('full').innerHTML='<td><img src="/img/loading.png" id="fullimg" alt="" /></td>';
		if(isObject(obj))document.getElementById('fullimg').src=obj.href;else document.getElementById('fullimg').src=obj;
		document.getElementById('cf').style.display="block";
		document.getElementById('fullimg').style.height = screen.availHeight/2;
		//alert(document.getElementById('fullimg').style.height);
	}
	return true;
}

/// je-li v odkaze k obrazku URL img.php?xy
function darkbox(str){
	if(parseInt(navigator.appVersion)>4){
		if(document.getElementById('cf').style.display=="block"){
			document.getElementById('cf').style.display="none";
			document.getElementById('fullC').innerHTML='<img src="/img/loading.png" id="fullimg" alt="" />';
		} else {
			getAJAX(str, '', '', 'fullC', 0);
			document.getElementById('cf').style.display="block";
		}
		return true;
	} else {
		str = str.toString();
		var path = new Array();
		path = str.split("&");
		window.open(str+'&win=1','','top=200,left=200,'+path[2]+', '+path[3]);
		return true;
	}
}

function passwordStrength(password){
	var score = 0;

	//password length
	score += password.length * 4;
	score += ( checkRepetition(1,password).length - password.length ) * 1;
	score += ( checkRepetition(2,password).length - password.length ) * 1;
	score += ( checkRepetition(3,password).length - password.length ) * 1;
	score += ( checkRepetition(4,password).length - password.length ) * 1;

	if (password.match(/(.*[0-9].*[0-9].*[0-9])/))  score += 5; //password has 3 numbers
	if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) score += 5; //password has 2 sybols
	if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))  score += 10; //password has Upper and Lower chars
	if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))  score += 15; //password has number and chars
	if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/))  score += 15; //password has number and symbol
	if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/))  score += 15; //password has char and symbol
	if (password.match(/^\w+$/) || password.match(/^\d+$/) )  score -= 10; //password is just a nubers or chars

	//verifing 0 < score < 100
	if ( score < 0 )  score = 0;
	if ( score > 100 )  score = 100;

	return score;
}

function regPS(pass, login){
	var score = passwordStrength(pass.value);
	var x = document.getElementById('strength');

	x.style.textAlign='left';
	x.style.color='#808080';
	x.style.fontWeight='normal';

	if (pass.value.length < 4 ) { x.value='Heslo musí mít nejméně 4 znaky'; return false;} //password < 4
	if (pass.value.toLowerCase()==login.toLowerCase()) {return 'Shoda s přihlašovacím jménem!'; return false;} //password == username

	x.style.fontWeight='bold';

	if (score < 34 ){
		x.value='Slabé';
		x.style.color='#ff0000';
	} else
	if (score < 68 ) {
		x.value='Střední';
		x.style.color='#ffa600';
		x.style.textAlign='center';
	}
	else {
		x.value='Silné ';
		x.style.color='#1d6a1c';
		x.style.textAlign='right';
	}
}

function checkRepetition(pLen,str) {
	res = ""
	for ( i=0; i<str.length ; i++ ) {
		repeated=true
		for (j=0;j < pLen && (j+i+pLen) < str.length;j++)
			repeated=repeated && (str.charAt(j+i)==str.charAt(j+i+pLen))
		if (j<pLen) repeated=false
		if (repeated) {
			i+=pLen-1
			repeated=false
		}
		else {
			res+=str.charAt(i)
		}
	}
	return res
}

function checkMail(id){
	var x = document.getElementById(id).value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if ((filter.test(x))||(x=="")) return true;
	else return false;
}

function validateEmail(email){
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
	return email.match(re)
}

function getLibraArchiv(id, year){
	getAJAX(d + "library_archiv.php", id, year, "libraryArchiv", 0);
	return false;
}

function getLibraDetail(id){
	getAJAX(d + "library_detail.php", id, "", "libraryDetail", 0);
	return false;
}


