// ------------------- login boxy

function smazat(id){
     document.getElementById(id).value = "";
}

// ------------------- externí link
function externalLinks() {
 	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}
	}
}
window.onload = externalLinks;

function otevriOkno(lURL, x, y) {
	var okno = window.open(lURL,'_blank','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width='+ x +', height='+ y);
	okno.focus();
}

function rozbalit(pObject) {
	var loTabulka = document.getElementById("polozka"+ pObject);
	if (loTabulka.style.display != "block") {
		loTabulka.style.display = "block";
	} else {
		loTabulka.style.display = "none";
	}
}

String.prototype.jeEmail = function () {
	var lAtom = "\[^\\s" + "\\(\\)><@,;:\\\\\\\"\\.\\[\\]" + "\]" + '+';
	var lWord = "(" + lAtom + "|" + "(\"[^\"]*\")" + ")";

	var lEmailPat = /^(.+)@(.+)$/;
	var lIPDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var lAtomPat = new RegExp("^" + lAtom + "$");

	var lUserPat = new RegExp("^" + lWord + "(\\." + lWord + ")*$");
	var lDomainPat = new RegExp("^" + lAtom + "(\\." + lAtom +")*$");
	var i;
	var lMatchArray = this.match(lEmailPat);
	if (lMatchArray == null) return false;

	var lUser = lMatchArray[1];
	var lDomain = lMatchArray[2];

/* hack na DEBILNÍ seznam.cz, muže mít teeku poed zavináeem...
	if (lDomain == "seznam.cz" && lUser.charAt(lUser.length - 1) == '.') {
		lUser = lUser.substr(0, lUser.length - 1);
	}*/

	for (i=0; i < lUser.length; i++) if (lUser.charCodeAt(i) > 127) return false;
	for (i=0; i < lDomain.length; i++) if (lDomain.charCodeAt(i) > 127) return false;
	if (lUser.match(lUserPat) == null) return false;

	var IPArray = lDomain.match(lIPDomainPat);
	
	if (IPArray != null) {
		for (i=1; i <= 4; i++) if (IPArray[i] > 255) return false;
		return true;
	}
	
	var lDomArr = lDomain.split(".");
	for (i=0;i<lDomArr.length;i++) if (lDomArr[i].search(lAtomPat)==-1) return false;
	var knownDomsPat =/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	if (lDomArr[lDomArr.length - 1].length != 2 && lDomArr[lDomArr.length - 1].search(knownDomsPat) == -1) return false;
	if (lDomArr.length < 2) return false;
	return true;
}


function otevritVyberSkol(lPole) {
	popUp = window.open("../vyberSkolu.asp?pole="+ lPole, "vyberSkolu", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no");
	popUp.focus();
}


function otevritVyberSkolZS(lPole) {
	popUp = window.open("vyberSkoluIndZprava.asp?pole="+ lPole, "vyberSkolu", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no");
	popUp.focus();
}


function kontrolaIndivZpravy(){
	var lChyba="";
	if (document.frm.reditelstviID.value==""){
		lChyba+="- škola\n";	
	}
	if (document.frm.email.value==""){
		lChyba+="- email\n";	
	}else{
		if (!(String(document.frm.email.value).jeEmail())){
			lChyba+="- špatně zadaný email\n";	
		}
	}
	if (document.frm.kontaktniOsoba.value==""){
		lChyba+="- kontaktni osoba\n";	
	}
	if (document.frm.telefon.value==""){
		lChyba+="- telefon\n";	
	}
	if (lChyba==""){
		document.frm.action="zpravaProSkoly.asp"
		document.frm.submit();
	}else{
		alert("Vyplňte prosím, případně opravte tyto údaje:\n"+lChyba);
	}	
}


function kontrolaPrispevkuDiskuse(formular){
	var lChyba="";
	if (!(formular.email.value.jeEmail())){
		lChyba+="Nesprávně vyplněný email.\n";
	}
	if (formular.jmeno.value==""){
		lChyba+="Není vyplněno jméno.\n";
	}
	if (formular.prijmeni.value==""){
		lChyba+="Není vyplněno príjměni.\n";
	}
	if (formular.html.value==""){
		lChyba+="Není vyplněn text příspěvku.\n";
	}
	if (lChyba!=""){
		alert(lChyba);
		return false;
	}else{
		return true;
	}
}

//funkce pro kontrolu maximalni delky textu inputu ci textarea
//	limitField - kontrolovane pole
//	limitCount - pole pro zapis zbyvajiciho poctu znaku
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function AJAX() {

	this.AJAX_createRequest = function() {
		try	{  // Firefox, Opera 8.0+, Safari  
			this.httpRequest = new XMLHttpRequest();  
		} catch (e) {  // Internet Explorer  
			try {    
				this.httpRequest = new ActiveXObject("Msxml2.XMLHTTP");    
			} catch (e) {
				try {
					this.httpRequest = new ActiveXObject("Microsoft.XMLHTTP");      
				} catch (e) {
					alert("Váš prohlížeč nepodporuje AJAX!");
					return false;      
				}    
			}  
		}
	}
	
	this.AJAX_load = function() {
		var lMistoZobrazeni = document.getElementById(this.targetID);
		lMistoZobrazeni.innerHTML = '<img src="/images/loader-small.gif" class="' + this.loaderClass +'" alt="nahrávám"/>';

		this.httpRequest.open("POST", this.url, true);
		var self = this;
		this.httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=windows-1250;");
		this.httpRequest.onreadystatechange = function() {
			if (self.httpRequest.readyState == 4) {
				if (self.httpRequest.status == 200) {
					lMistoZobrazeni.innerHTML = self.httpRequest.responseText;
				} else {
					lMistoZobrazeni.innerHTML = "Chyba pri nacitani stanky "+ self.httpRequest.status +":"+ self.httpRequest.statusText;
				}
			} 
		}
		this.httpRequest.send(this.url);
	}
}


function AJAX_nahratObsah(pIDElementu, pURL, pLoaderClass) {
	var goAjax = new AJAX();
	goAjax.targetID = pIDElementu;
	goAjax.url = pURL;
	goAjax.loaderClass = pLoaderClass;
	goAjax.AJAX_createRequest();
	goAjax.AJAX_load();
}


function NSZ_pravySloupec() {
	AJAX_nahratObsah('terminyNSZ', '/in/2vs/nsz/terminyAjax.asp', 'loader-pravy');
	AJAX_nahratObsah('loginDiv', '/prihlaseniIframe.asp', 'loader-pravy');
	AJAX_nahratObsah('aktualityDiv', '/aktualityIframe.asp?c=NSZ', 'loader-pravy');
	AJAX_nahratObsah('kosikDiv', '/1_objednavky2004/kosikIframe.asp', 'loader-pravy');
}

function odeslatFormular_Ajax(pFormID, pTargetElementID, pUrl, pClass) {
	//--- projde pole formulare, sestavi z nich query string a odesle to AJAXem podle parametru ---//
	var lQueryString = '';
	var f = document.getElementById(pFormID).elements;


	for (var i = 0; i < f.length; i++) {

	    if (f.item(i).type == "text") {
            lQueryString += f.item(i).name + "=" + upravitDiakritiku(f.item(i).value) + "&";
        }
        if (f.item(i).type == "hidden") {
            lQueryString += f.item(i).name + "=" + upravitDiakritiku(f.item(i).value) + "&";
        }

        if (f.item(i).type == "checkbox") {
            if (f.item(i).checked) {
                lQueryString += f.item(i).name + "=" + upravitDiakritiku(f.item(i).value) + "&";
            } else {
                lQueryString += f.item(i).name + "=&";
            }
        }
        if (f.item(i).type == "radio") {
            if (f.item(i).checked) {
                lQueryString += f.item(i).name + "=" + upravitDiakritiku(f.item(i).value) + "&";
            }
        }

		if (f.item(i).type == "select") {
			var sel = f.item(i);
			lQueryString += sel.name + "=" + upravitDiakritiku(sel.options(sel.selectedIndex).value) + "&";
		}

		if (f.item(i).type == "textarea") {
	        lQueryString += f.item(i).name + "=" + upravitDiakritiku(f.item(i).value) + "&";
		}
	}

	AJAX_nahratObsah(pTargetElementID, pUrl + '?' + lQueryString, pClass);
}


function selectVybratVse(pSelectBoxID, pNastavitNa) {
	// have we been passed an ID
	if (typeof pSelectBoxID == "string") {
		pSelectBoxID = document.getElementById(pSelectBoxID);
	}
	// is the select box a multiple select box?
	if (pSelectBoxID.type == "select-multiple") {
		for (var i = 0; i < pSelectBoxID.options.length; i++) {
			pSelectBoxID.options[i].selected = pNastavitNa;
		}
	}
}

function oznacVse(pID, pZahrnoutDisabled) {
	var obj = document.getElementsByName(pID), i;
	var stav = obj.length >= 0 &&  !obj[0].checked; 
	for (i=0; i < obj.length; i++) {
		if (!obj[i].disabled || pZahrnoutDisabled) {
			obj[i].checked = stav
		}
	}
}


function silaHesla(pIDelementu, pIDvysledku) {
	var lHeslo = document.getElementById(pIDelementu).value;
	var lBodu = 0
	var lSilaText = "slabé"
	var lSilaBarva = "";
	
	// delka
	if (lHeslo.length < 5) {
		lBodu += 3;
	} else if (lHeslo.length > 4 && lHeslo.length < 8) {
		lBodu += 6;
	} else if (lHeslo.length > 7 && lHeslo.length < 16) {
		lBodu += 12;
	} else if (lHeslo.length > 15) {
		lBodu += 18;
	}
	
	// obsahuje male pismeno
	if (lHeslo.match(/[a-z]/)) {
		lBodu += 1;
	}
	// obsahuje velke pismeno
	if (lHeslo.match(/[A-Z]/)) {
		lBodu += 5;
	}
	
	// obsahuje cislo
	if (lHeslo.match(/\d+/)) {
		lBodu += 5;
	}
	
	// minimalne 3 cisla
	if (lHeslo.match(/(.*[0-9].*[0-9].*[0-9])/)) {
		lBodu += 5;
	}
	
	// specialni znaky
	if (lHeslo.match(/.[!,@,#,$,%,^,&,*,?,_,~]/)) {
		lBodu += 5;
	}
	
	// dva specialni znaky
	if (lHeslo.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) {
		lBodu += 5;
	}

	// kombinace velkych a malych pismen
	if (lHeslo.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) {
		lBodu += 2;
	}

	// obsahuje mala velka i cisla
	if (lHeslo.match(/([a-zA-Z])/) && lHeslo.match(/([0-9])/)) {
		lBodu += 2;
	}

	// obsahuje cisla a specialni znaky
	if (lHeslo.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/)) {
		lBodu += 2;
	}

	//--- vyhodnoceni
	if(lBodu < 16) {
	   lSilaText = 'velmi slab&eacute;';
	   lSilaBarva = '#ff0000';
	} else if (lBodu > 15 && lBodu < 25) {
	   lSilaText = 'slab&eacute;';
	   lSilaBarva = '#ffaa00';
	} else if (lBodu > 24 && lBodu < 35) {
	   lSilaText = 'st&#345;edn&iacute;';
	   lSilaBarva = '#ffff00';
	} else if (lBodu > 34 && lBodu < 45) {
	   lSilaText = 'siln&eacute;';
	   lSilaBarva = '#8eac00';
	} else {
	   lSilaText = 'velmi siln&eacute;';
	   lSilaBarva = '#00ff00';
	}

	document.getElementById(pIDvysledku).innerHTML = lSilaText;
	document.getElementById(pIDvysledku).style.backgroundColor = lSilaBarva;
}

function zmenitDisplay(pID, pVisible) {
	pObj = document.getElementById(pID);
	if (pObj && pObj.style) pObj.style.display = (pVisible ? "block" : "none");
}

function upravitDiakritiku(pText) {
	if (navigator.appName == 'Opera') {
		var sCestinou = Array('ě', 'š', 'č', 'ř', 'ž', 'ý', 'á', 'í', 'é', 'ú', 'ů', 'ó', 'ť', 'ď', 'ň', 'Ě', 'Š', 'Č', 'Ř', 'Ž', 'Ý', 'Á', 'Í', 'É', 'Ú', 'Ů', 'Ó', 'Ť', 'Ď', 'Ň');
		var bezCestiny = Array("e", "s", "c", "r", "z", "y", "a", "i", "e", "u", "u", "o", "t", "d", "n", "E", "S", "C", "R", "Z", "Y", "A", "I", "E", "U", "U", "O", "T", "D", "N");
	//	var bezCestiny = Array('\&ecaron;', '\&scaron;', '\&ccaron;', '\&rcaron;', '\&zcaron;', '\&yacute;', '\&aacute;', '\&iacute;', '\&eacute;', '\&uacute;', '\&ucaron;', '\&oacute;', '\&tcaron;', '\&dcaron;', '\&ncaron;', '\&Ecaron;', '\&Scaron;', '\&Ccaron;', '\&Rcaron;', '\&Zcaron;', '\&Yacute;', '\&Aacute;', '\&Iacute;', '\&Eacute;', '\&Uacute;', '\&Ucaron;', '\&Oacute;', '\&Tcaron;', '\&Dcaron;', '\&Ncaron;');
		var i;

		for (i = sCestinou.length - 1; i >= 0 ; i--) {
			pText = pText.replace(new RegExp(sCestinou[i], "g"), bezCestiny[i]);
		}
	}
	return pText;
}