// sestaveni a zobrazení dialogu pro výběr hlavičky výsledků
function selectHeaderForResult(rid,element,typ){

  

 // lolkánlí funkce pro určení  polohy X/Y předaného elementu
  function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }


   // funkce pro zpracování a zobrazení dat poslané ajaxem 
  function responseShowHeaders (response){
     // naparsovani parametru z PHP skriptu
    var myResponse = Json.evaluate(response);
    // kontrola předání dat
    if(myResponse.data!= false){
     
     //posun dialogu pod odkaz "výsledky"
    var X=180+findPosX(document.getElementById (element));
    var Y=findPosY(document.getElementById (element));  

    
    //podklad
			dialog='<div class="in"><div class="in2">';
			//zaviraci krizek
			dialog+='<span class="close" onclick="closeInWindow(\'hlavicky\')">zavřít</span>';
			//titulek
			dialog+='<br /><h2>Zvolte hlavičku:</h2><ul>';
			
			 for (x=0;lessthan(x,myResponse.data.length);x++) {
          // sesatavení odkazu na vysvedceniPDF.php 
          dialog+='<li><a href="./vyhodnoceni/vysvedceniPDF.php?'+typ+'id='+element+'&hid='+myResponse.data[x]['hid']+'" target="_blank" title=" Výsledky vyhodnocení">'+myResponse.data[x]['nazev']+'</a></li> ';
         // dialog+='<li>nic</li> ';
      
      } 
      dialog+='</ul>';
        
        if (myResponse.data.length==0){
         
        }
        // pokud je nalezena jen jedna hlavička je vybrána automaticky a dialog pro výběr se nezobrazí se nezobrazi
        if (myResponse.data.length==1) {var link ="./vyhodnoceni/vysvedceniPDF.php?"+typ+"id="+element+"&hid="+myResponse.data[0]['hid'];
        //window.location.replace(link); 
          window.open(link, "vysledky", "menubar=yes,resizable=yes,left=0,top=0");
        } else {
      
		
			
			//konec in2
			dialog+='</div></div>';
			// nastavení umístění dialogo a jeho zobrazení 
      document.getElementById('hlavicky').innerHTML=dialog; 
      document.getElementById('hlavicky').style.left=X+'px';
      document.getElementById('hlavicky').style.top=Y+'px';
      document.getElementById('hlavicky').style.display='block';
        }
        
        
        
      
    }else{
    // pokus není nalezena žádná přidělené hlavička je automaticky zvolena standartní hlavička RCV s id=1 
    var link ="./vyhodnoceni/vysvedceniPDF.php?"+typ+"id="+element+"&hid=1";
       // window.location.replace(link);
       window.open(link, "vysledky", "menubar=yes,resizable=yes,left=0,top=0");
    }
             
  }
  
  
   new Ajax('./AJAXPHP/client.ajax.selectheader.php', {
		method: 'post',
		postBody: {'rid' :rid},
		onComplete: responseShowHeaders.bind(this)
	}).request();	      
}


function supported(supported){

      if(supported=='not'){
      if(confirm('Používáte nepodporovaný prohlířeč!! \n Některé funkce aplikace mohou pracovat nekorektně \n \n Chcete přesměrovat na stránky novějšího prohlížeče?')){
        location.replace('http://www.microsoft.com/cze/windows/products/winfamily/ie/default.mspx')
      }
     } 
		}
function arrSearch(arr, searchItem) {
  var foundItems = [];
  var index = arr.indexOf(searchItem)
  while (index != -1) {
    foundItems.push(index);
    index = arr.indexOf(searchItem, ++index);
  }
  
  return foundItems;
  
}






Array.prototype.find = function(searchStr) {
  var returnArray = false;
  for (i=0; i<this.length; i++) {
    if (typeof(searchStr) == 'function') {
      if (searchStr.test(this[i])) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    } else {
      if (this[i]===searchStr) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    }
  }
  return returnArray;
}

function a() {
  alert('asss');
}

// fce pro kontrolu spravneho zapisu datumu
// thx to http://www.rodsdot.com/ee/dateValidate1.asp
/*
function isValidDate(date) {
  var RegExPattern = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;
  var pom;
  if (date !='' ) {
    return date.match(RegExPattern); 
  }
  else {
    return false; 
  } 
}*/
//puvodni funkce (zakomentovana) nahrazena nasledujici
function isValidDate(str) 
{ 
var regEx = /^((0?[1-9])|([1-2][0-9])|(3[01])).((0?[1-9])|(1[012])).\d{4}$/; 

if(!regEx.test(str)) 
{ 
return false; 
} 

var arr = str.split('.'); 

var d = parseInt(arr[0], 10); 
var m = parseInt(arr[1], 10); 
var y = parseInt(arr[2], 10); 

//pocet milisekund za 10 let 315619200000
//pocet milisekund za 120 let 3784233600000
var dnes= new Date();
var zadany=new Date(y,m-1,d,dnes.getHours(),dnes.getMinutes(),dnes.getSeconds(),dnes.getMilliseconds());

if((3786825600000>(dnes - zadany))&& ((dnes - zadany) >315619200000)){
  return true;
}
else{
  return false;
}


}

// fce kontroluje zda je zadany retezec skutecne email (vyuziti RE)
function isValidEmail(str) {
   return str.match(/^\w+([\.\-]\w+)*@\w+([\.\-]\w+)*\.[a-z]{2,4}$/i);
}

function lessthan(number, tocompare) {
  if (number < tocompare) return true;
  else return false;
}

function greaterthan(number, tocompare) {
  if (number > tocompare) return true;
  else return false;
}

// fce pro nahrazeni textu v nejakem tagu -> chodi pro IE i FF
function setInnerText(obj, text) {
  if(document.all){
       obj.innerText = text;
  } else{
      obj.textContent = text;
  }
}

// snaha o zobrazeni modalniho dialogu pokud to nejde tj. neni IE tak se snazi
// zobrazit co nejvice osekane okno
function showDialog(url, width, height) {

    if (window.showModalDialog) {
        var ieFeatures = "dialogHeight:" + height + "px"
                       + "dialogWidth:" + width + "px"
                       + "edge:sunken;"
                       + "help:no;"
                       + "scroll:no;"
                       + "resizable:no;"
                       + "status:no;"
                       + "unadorned:no;";
        showModalDialog(url, window, ieFeatures);
    } else {
        var windowName = "sampleDialog"
        var mozillaFeatures = "width=" + width - 20 +","
                            + "height=" + height + ","
                            + "toolbar=no,"
                            + "location=no,"
                            + "directories=no,"
                            + "status=no,"
                            + "menubar=no,"
                            + "scrollbars=no,"
                            + "resizable=no,"
                            + "modal=yes";
        window.open(url, windowName, mozillaFeatures);
    }
    return false;
}

// event handlign functions are from
// http://www.soziologie.uni-halle.de/unger/SCRIPTs/workshop_internet/txt_js_322.html
// thx

function addEvent(obj, eventType, afunction, isCapture) {
  // W3C DOM
  if (obj.addEventListener) {
     obj.addEventListener(eventType, afunction, isCapture);
     return true;
  }
  // Internet Explorer
  else if (obj.attachEvent) {
     return obj.attachEvent("on"+eventType, afunction);
  }
  else return false;
}

function removeEvent(obj, eventType, afunction, isCapture) {
  if (obj.removeEventListener) {
     obj.removeEventListener(eventType, afunction, isCapture);
     return true;
  }
  else if (obj.detachEvent) {
     return obj.detachEvent("on"+eventType, afunction);
  }
  else return false;
}

// pouzito z opor predmetu PIS
// Detekce typu prohlizece spustenem na klientovi

var browIE=0;// prohlížeče
var browNetscape=1;
var browOpera=2;
var browName=browIE;
var browType = function () {
// získání typu prohlížeče
// parametry:nejsou
// návratová hodnota: true - povedlo se

if (navigator.appName=="Microsoft Internet Explorer") { 
  browName=browIE;
}  // IE
else if (navigator.appName=="Netscape") {
  // Netscape, Mozilla Firefox
  browName=browNetscape;
} // Netscape
else if (navigator.appName=="Opera") { // Opera
  browName=browOpera;
} // Opera
  
  return true;
}; // browType

// fce na vypnuti zobrazeni elementu ve strance dle jeho ID 
function zobrazSkryj(idecko){
  el=document.getElementById(idecko).style;
  el.display=(el.display == 'block')?'none':'block';
}

// fce pro nastaveni stylu pomoci JS
function mySetStyleAttribute(element, style) {
  element.setAttribute('style', '');
  element.style.cssText = style;
}

// CODE from http://www.somacon.com/p143.php

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

  //na onloadu volam funkci ktera mi udela abz bzl kurzor vlozen v policku podle id
  function kurzor(id){
    if(document.getElementById(id)){
              document.getElementById(id).focus();
    }            
  }
            
          //funkce nastavi prvku display none
        function skryj(prvek){
            if(document.getElementById(prvek)!=null){
              document.getElementById(prvek).style.display='none';
            }
        }
                
        //funkce nastavi prvku display block
        function zobraz(prvek){
          if(document.getElementById(prvek)!=null){
            document.getElementById(prvek).style.display='block';
          }

        }

        
      
      function responsereload(respon){
		//location.reload(true);
		location.replace("index.php")
}
      
      	function readnew (id){
      	//ajax upravou zpravy
			new Ajax('./AJAXPHP/index.ajax.readnew.php', {
				method: 'post',
				postBody: {'aktid' : id}
        ,onComplete:  responsereload.bind(this)
			}).request();
		//	location.reload(true);
		  
       responsereload.bind(this);
    }
      
        function shownew(id,datum,predmet,obsah,datumprecteni){
			  
     document.getElementById("new").style.display='block';

			//nejprve vzpisu info pro spravce ye odstranuju a pak yavolam ajax mazani
			//podklad
			prvnizobrazeni='<div class="in">Zpráva: '+predmet+' ze dne: '+datum;
		  
    	 if (!(datumprecteni)){prvnizobrazeni+='<span class="button" onclick="readnew (\''+id+'\');">Přečteno</span>';}
			prvnizobrazeni+='<span class="button" onclick="document.getElementById(\'new\').style.display=\'none\';">Zavřít</span>';
			
      //zaviraci krizek
			prvnizobrazeni+='<span class="close" onclick="document.getElementById(\'new\').style.display=\'none\';">x</span>';
			prvnizobrazeni+='</div>';
      //hlaska
			prvnizobrazeni+='<div class="in2"><br /><h4>Obsah:</h4>'
			prvnizobrazeni+='<h5>'+obsah+'</h5>'
					
      //konec in2
			prvnizobrazeni+='</div>';
    
			//zobrazeni;
	     document.getElementById("new").innerHTML = prvnizobrazeni;
	     
		}
		
		
    	function echonew(pocet){  
      if (pocet){
        document.getElementById("warningnew").style.display='block';
       
        prvnizobrazeni='<div class="in">Automatické upozornění</div><div class="in2">';
			 //zaviraci krizek
			 prvnizobrazeni+='<span class="close" onclick="document.getElementById(\'warningnew\').style.display=\'none\';">x</span>';
			 //hlaska
			 prvnizobrazeni+='<br /><h3>Vítejte máte nepřečtené aktuality.</h3>'
			 prvnizobrazeni+='<h4>Počet nepřečtených aktualit je: '+pocet+'</h4> '
			 
       prvnizobrazeni+='<span class="button" onclick="document.getElementById(\'warningnew\').style.display=\'none\';">Zavřít</span>';
       //konec in2
			 prvnizobrazeni+='</div>';
    
    
			//zobrazeni;
	     document.getElementById("warningnew").innerHTML = prvnizobrazeni;
     } 
		}

function toggleDiv(element)
{
      if(document.getElementById(element).style.display == 'none')
      {
        document.getElementById(element).style.display = 'block';
      }
      else if(document.getElementById(element).style.display == 'block')
      {
        document.getElementById(element).style.display = 'none';
      }
}

