
/* JS: mod_form_main*/
function validate_form(formular) {
  try {
    // kvuli captcha je zde skip_validation
	  if (skip_validation) {
		  return true;
	  }
	} catch(e) {
	
	var inputs = formular.elements;
    var error = -1; //-1 = OK, jinak index prvniho chybneho inputu
    var tp,val,errorMsg = "";
    for (var i = 0; i < inputs.length; i++) {
		if (inputs[i].nodeName.toLowerCase() == 'textarea') {
			tp = 'text';
		} else if (inputs[i].nodeName.toLowerCase() == 'select') {
			tp = 'select';
		} else {
			tp = inputs[i].type;
		}
		//nesmi se pouzit 2,n protoze zasrany zkurveny svine
		mailRegExp = new RegExp('^[^@]{2,}@[_a-zA-Z0-9\.\-]{2,}\.[a-zA-Z]{2,4}$');
    	switch (tp) {
			case 'checkbox':
				if (inputs[i].className.match('mandatory') && !inputs[i].checked) {
					if (error == -1) {error = i;}
					errorMsg += "Prosím vyplňte položku "+inputs[i].title+". \n";
				}
			case 'password':
			case 'file':
			case 'text':
				if (inputs[i].nodeName.toLowerCase() == 'textarea') {
				    val = inputs[i].innerHTML;
				} else {
				    val = inputs[i].value;
				}
				if (inputs[i].className.match('mandatory') && (inputs[i].value == '')) {
            		if (error == -1) {error = i;}
					errorMsg += "Prosím vyplňte položku "+inputs[i].title+". \n";
        		}
        		if (inputs[i].className.match('email') && (inputs[i].value != '') && (!inputs[i].value.match(mailRegExp))) {
					if (error == -1) {error = i;}
            		errorMsg += "Prosím vyplňte platný email "+"\n";
        		}
        		if (inputs[i].className.match('number') && (inputs[i].value != '') && (!inputs[i].value.match("^[0-9]+$"))) {
					if (error == -1) {error = i;}
            		errorMsg += "Prosím vyplňte platné číslo "+"\n";
        		}
			case 'select':
			    if (inputs[i].className.match('mandatory') && (inputs[i].selectedIndex == 0)) {
            		if (error == -1) {error = i;}
					errorMsg += "Prosím vyplňte položku "+inputs[i].title+". \n";
				}
        }
    }
    if (error != -1) {
    	alert(errorMsg);
        inputs[error].focus();
        return false;
    } else {
		return true;
	}

	}
}

/* JS: mod_form_fill*/
//http://dean.edwards.name/weblog/2006/06/again/
// Dean Edwards/Matthias Miller/John Resig
function init() {
  // quit if this function has already been called
  if (arguments.callee.done) return;

  // flag this function so we don't do the same thing twice
  arguments.callee.done = true;

  // kill the timer
  if (_timer) clearInterval(_timer);

  // do stuff
  fillForm();
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
  document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
  var script = document.getElementById("__ie_onload");
  script.onreadystatechange = function() {
    if (this.readyState == "complete") {
      init(); // call the onload handler
    }
  };
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
  var _timer = setInterval(function() {
    if (/loaded|complete/.test(document.readyState)) {
      init(); // call the onload handler
    }
  }, 10);
}

/* for other browsers */
window.onload = init;

// vycisti formular - zrusi vyber u checkboxu, radiobuttonu a optionu
function clearForm() {
  var inputs = document.getElementsByTagName('input');
  for(var i=0; i<inputs.length; i++) {
    if (inputs[i].form.id.indexOf('mod_form') != -1 && (inputs[i].type=='checkbox' || inputs[i].type=='radio'))
      inputs[i].checked = false;
  }
  var options = document.getElementsByTagName('option');
  for(var i=0; i<options.length; i++) {
    if (options[i].form.id.indexOf('mod_form') != -1)
      options[i].selected = false;
  }
}

// vyplni formular daty ulozenymi v hiddenech, ktere zacinaji _mod_form_fill_
function fillForm() {
  var inputs = document.getElementsByTagName('input');
  var count = 0;
  for (var i=0; i<inputs.length; i++) {
    // filtr hiddenu
    if (inputs[i].form.id.indexOf('mod_form') == -1 || inputs[i].type != 'hidden' ||
      inputs[i].name.indexOf('_mod_form_fill_') == -1)
      continue;
    
    // pri prvnim vyskytu spravneho hiddenu, promazeme formular
    if (!count++)
      clearForm();
    
    // nyni nam zustali jen potrebne hiddeny
    var elements = document.getElementsByName(inputs[i].name.substr(('_mod_form_fill_').length));
    var element = elements[0];
    switch(element.type) {
      case 'textarea':
        element.innerHTML = inputs[i].value; break;
      case 'text':
        element.value = inputs[i].value; break;
      case 'checkbox':
        element.checked = (inputs[i].value!='0' ? true : false); break;
      case 'radio':
        for (var j=0; j<elements.length; j++) {
          if (elements[j].value == inputs[i].value) {
            elements[j].checked = true;
            break;
          }
        }
        break;
      case 'select-one':
        var options = element.getElementsByTagName('option');
        for (var j=0; j<options.length; j++) {
          if (options[j].value == inputs[i].value) {
            options[j].selected = true;
            break;
          }
        }
        break;
      default:
    }
  }
}

/* JS: new_window*/

function openwin(elm, isImage) {

    if (!isImage) {
      window.open(elm.getAttribute("href"), "_blank");
      return true;
    }


    var msgWindow = window.open(elm.getAttribute("href"), "_blank", "toolbar=no");
    msgWindow.document.write("<?xml version='1.0' encoding='UTF-8' standalone='yes'?>");
    msgWindow.document.write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>");
    msgWindow.document.write("<html>");
    msgWindow.document.write("<meta http-equiv='content-type' content='text/html; charset=utf-8'>");
    msgWindow.document.write("<head>");
    msgWindow.document.write("<title>Obrázek</title>");
    msgWindow.document.write("<style media='screen,tv,projection' type='text/css'> html, body {margin:0;padding:0} a, img {border: 0px solid #fff}</style>");
    msgWindow.document.write("<script type='text/javascript'>");
    msgWindow.document.writeln("    function getInnerHeight() { ");
    msgWindow.document.writeln("           if (window.innerHeight) {");
    msgWindow.document.writeln("             return window.innerHeight;");
    msgWindow.document.writeln("           } else {");
    msgWindow.document.writeln("             return document.documentElement.offsetHeight;"); // document.body.clientHeight;
    msgWindow.document.writeln("           }");
    msgWindow.document.writeln("    }");
    msgWindow.document.writeln("    function getInnerWidth() { ");
    msgWindow.document.writeln("           if (window.innerWidth) {");
    msgWindow.document.writeln("             return window.innerWidth;");
    msgWindow.document.writeln("           } else {");
    msgWindow.document.writeln("             return document.documentElement.offsetWidth;"); //document.body.clientWidth;
    msgWindow.document.writeln("           }");
    msgWindow.document.writeln("    }");
    msgWindow.document.writeln("    function imgSize(controlHeight,fotoMargin) {");
    msgWindow.document.writeln("      image=document.getElementById('image_elm');");
    msgWindow.document.writeln("      if (screen.height <= (image.height + 31)) {");
    msgWindow.document.writeln("        window.resizeTo(image.width + 12,screen.height - 100);");
    msgWindow.document.writeln("      } else {");
    msgWindow.document.writeln("        window.resizeTo(image.width+2*fotoMargin,image.height+controlHeight+fotoMargin);");
    msgWindow.document.writeln("        controlx = image.width+2*fotoMargin - getInnerWidth();");
    msgWindow.document.writeln("        controly = image.height+controlHeight+fotoMargin - getInnerHeight();");
    msgWindow.document.writeln("        window.resizeTo(image.width + controlx+2*fotoMargin, image.height + controly+controlHeight+fotoMargin);");
    msgWindow.document.writeln("      }");
    msgWindow.document.writeln("    }");
    msgWindow.document.write("</script>");
    msgWindow.document.write("</head>");
    msgWindow.document.write("<body style='margin:0;padding:0;background:#fff' onload='imgSize(0,0);'>");
    msgWindow.document.write("<a style='border:0px solid #fff;' href='#' onclick='window.close();'><img src='"+elm.getAttribute("href")+"' style='border:0px solid #fff;' id='image_elm' alt=''></a>");
    msgWindow.document.write("</body>");
    msgWindow.document.write("</html>");
    msgWindow.document.close();
    msgWindow.focus();
    return true;
}

function open_win_external(elm) {
    window.open(elm.getAttribute("href"), "_blank");
    return true;
}

/* JS: pulldown*/
var visibleId=new Array();
var menuDelay = 100;

if (document.getElementById) {
	document.onmousemove = openMenu;
}

function findX(obj) {
    var x = 0;
    while (obj.offsetParent) {
        x += obj.offsetLeft
        obj = obj.offsetParent;
    }
    return x;
}

function openMenu(event) {
	evnt = eval((document.all)?"window.event.srcElement":"event.target");
    if (visibleId.length>0 && // nejake submenu je viditelne
        !ancestorId(evnt, "nav")){ // mys je zcela mimo #nav
        for (i=visibleId.length;i>0;i--){
            hideEl(document.getElementById(visibleId[i-1]));
        }
        visibleId=new Array();
    }
    if (visibleId.length>0 && // je co schovavat
        ancestorId(evnt, "nav") && // mys je pres nejakeho potomka #nav
        evnt.tagName.toLowerCase() == "a" &&
        visibleId[visibleId.length-1].substring(6) != evnt.parentNode.id.substring(7) &&
        !ancestorId(evnt.parentNode, visibleId[visibleId.length-1])  // neni to ale potomek viditelneho menu
        ) {
        hideEl(document.getElementById(visibleId[visibleId.length-1]));
        visibleId = visibleId.slice(0,visibleId.length-1);
    }
    
    if (evnt.parentNode &&
        evnt.parentNode.id &&
        evnt.tagName.toLowerCase()=="a" &&
        ancestorId(evnt, "nav") &&
        evnt.parentNode.className.toLowerCase().indexOf("p_menu_item_")>=0
        ){
            x = findX(evnt.parentNode);
            li = evnt.parentNode;
            ul = document.getElementById("p_sub_"+li.id.substring(7));
            if (ul && ul.style && ul.style.visibility!="visible"){
               visibleId[visibleId.length]=ul.id;
               ul.style.visibility="visible";
               ul.style.display="block";
               if (ul.className.indexOf("p_menu_inner_0")>=0) /* vypocitat pozici prvni roletky (vnorene maji pozici relativni) */
                    ul.style.left=x-findX(document.getElementById("nav"));
            }
    }
}

function ancestorId(element, id){
   var ancestor = element;
   while (ancestor.parentNode){
      if (ancestor.id==id){
         return true;
      }
      ancestor = ancestor.parentNode;
   }
   return false;
}

function hide(elementId){
    element = document.getElementById(elementId);
    cTimer=setTimeout("hideEl(element)",menuDelay);

}

function hideEl(element){
    if (element && element.style){
        element.style.visibility="hidden";
        element.style.display="block";
    }
}

function show(elementId){
    element = document.getElementById(elementId);
    if (element && element.style){
        element.style.visibility="visible";
        element.style.display="block";
    }
}

function toggle(elementId){
    element = document.getElementById(elementId);
    if (element && element.style){
        if (element.style.visibility!="visible"){
            show(element);
        } else {
            hide(element);
        }
    }
}

