function show_photo(text,file,width,height){
	no=window.open("", "zoom", "left=5, top=5, width="+width+", height="+height+", toolbar=no, location=no, status=no, scrollbars=auto");
	with (no.document)
	{
		open();
		writeln('<body style="margin: 0px;"><a href="javascript:void window.close();"><img src="'+file+'" style="border: 0px;" /></a></body>');
		close();
		no.focus();
	}
}


function oznac(akce,name){ 
  oElement = document.getElementsByName(name); 
  if(oElement.length) { 
	for(i = 0; i < oElement.length; i++){ 
		if (akce=='check') oElement[i].checked = true;
		if (akce=='uncheck') oElement[i].checked = false;
	}
	} 
}

function toggleCheckedId(oElement){ 
	oForm = oElement.form; 
  oElement = oForm.elements[oElement.id]; 
  if(oElement.length) 
  { 
    bChecked = oElement[0].checked; 
    for(i = 1; i < oElement.length; i++) 
      oElement[i].checked = bChecked; 
  } 
}

function check(id){
	document.getElementById(id).checked = true;	
}

function toggleIndeterminate(oElement) { 
  oForm = oElement.form; 
  oElement = oForm.elements[oElement.id]; 
  if(oElement.length) { 
    bIndeterminate = false; 
    bChecked = true; 
    nChecked = 0; 
    for(i = 1; i < oElement.length; i++) 
      if(oElement[i].checked) 
        nChecked++; 
    if(nChecked < oElement.length - 1) { 
      if(nChecked) 
        bIndeterminate = true; 
      else { 
        bIndeterminate = false; 
        bChecked = false; 
      } 
    } 
    else { 
      bIndeterminate = false; 
    } 
    oElement[0].indeterminate = bIndeterminate; 
    oElement[0].checked = bChecked; 
  } 
}

function ShowDialog( theInput,theInput1,theInput2 ) {
	//if (!formik){formik = "vyber";}
	//window.alert(formik);
	theField = eval( "document.getElementById(theInput)");
    //window.alert(theField);
	//theField.value = "";
    theField1 = eval( "document.getElementById(theInput1)");
    //theField1.value = "";
	theField2 = eval( "document.getElementById(theInput2)");
    //theField2.value = "";*/
    dialogWindow=window.open("/admin/kalendar_popup.html","MyDialog",
            "dependent=yes, menubar=no, location=no, resizable=no, width=320, height=205, toolbar=no, screenX=100, screeny=100" );
}

function transferValue (field,val){
	window.top.opener.document.form.idkatalogtree.value=val;
	window.top.close() ;
	
}

function transferFormValues (string){
        prvky = string.split('|');
        for(i = 0; i < prvky.length; i++){
      prvek = prvky[i].split('*');
      nazev_pole = prvek[0];
      hodnota = prvek[1];

          if (window.top.opener.document.getElementById(nazev_pole)) {
            //alert(nazev_pole+' '+hodnota);
                window.top.opener.document.getElementById(nazev_pole).value=hodnota;
          }
        }
        window.top.close() ;

}

function Transfer( theValue ) {
    if( theField != null ) theField.value = theValue;
	theField.focus();
}

function DoubleTransfer( theValue1,theValue2) {
	//window.alert(theField);
	if( theField1 != null ) theField1.value = theValue1;
	if( theField2 != null ) theField2.value = theValue2;
	//document.vyber.datum_od.value = theValue1;
	//document.vyber.datum_do.value = theValue2;
	theField.focus();
	//document.formik.submit();
	//submit();
}
function openWindow(text,file,width,height,type){
	if (type=='img'){
		no=window.open("", "zoom", "left=5, top=5, width="+width+", height="+height+", toolbar=no, location=no, status=no, scrollbars=auto");
		with (no.document)
		{
			open();
			writeln('<body style="margin: 0px;"><a href="javascript:void window.close();"><img src="'+file+'" alt="'+text+'"style="border: 0px;" /></a></body>');
			close();
			no.focus();
		}
	}
	if (type=='file'){
		window.open(file, "zoom", "left=5, top=5, resizable,toolbar=no, location=no, status=no, scrollbars=yes");
		
		
	}
	return false;
}

function changeImage (url,foto) {
	if (document.getElementById) {
    	//window.alert(foto);    
                document.getElementById('mainphoto').src = url;
                document.getElementById('largephoto').href = foto;
               // photo_id = fotka_id;
        }
        return false;
}

function showLargeImage(url,width,height,type) {
	if (screen.height) {
		screenY = screen.height
		screenX = screen.width
	}
	var top = ((screenY/2)-(height/2));
	var left = ((screenX/2)-(width/2));
	window.open(url,'','resizable=no,status=no,scrollbars=no,width='+width+',height='+height+',top=50,left=50');
	return false;
}

function AddFavorite(linkObj,addUrl,addTitle){
  if (document.all && !window.opera)
  {
    window.external.AddFavorite(addUrl,addTitle);
    return false;
  }
  else if (window.opera && window.print)
  {
    linkObj.title = addTitle;
    return true;
  }
  else if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function'))
  {
    if (window.confirm('Přidat oblíbenou stránku jako nový panel?'))
    {
      window.sidebar.addPanel(addTitle,addUrl,'');
      return false;
    }
  }
  window.alert('Po potvrzení stiskněte CTRL-D,\nstránka bude přidána k vašim oblíbeným odkazům.');
  return false;
}

function vycistiPlatby(n){
	oElement = document.getElementsByName(n); 
	if(oElement.length) { 
		for(i = 0; i < oElement.length; i++) 
			oElement[i].disabled = false;
	} 
}

function zakazPlatbu(n){
	if (document.getElementById(n)){
		document.getElementById(n).disabled = true;
		if (document.getElementById(n).checked) document.getElementById(n).checked = false;
	}
}

function hideBlock(block){
	document.getElementById(block).style.display = 'none';
}

function showBlock(block){
	document.getElementById(block).style.display = 'block';
}

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	
	if ( endstr == -1 ) endstr = document.cookie.length;
	return(unescape(document.cookie.substring(offset, endstr)));
}

function getCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	
	while ( i < clen ) {
		var j = i + alen;
		if ( document.cookie.substring(i, j) == arg ) return(getCookieVal (j));
		i = document.cookie.indexOf(" ", i) + 1;
		if ( i == 0 ) break;
	}
	
	return(null);
}

function startAjaxUpload(poradi){
	  if (!poradi) poradi = 0;
	
      document.getElementById('f'+poradi+'_upload_process').style.display = 'block';
      document.getElementById('f'+poradi+'_upload_form').style.display = 'none';
      return true;
}

function processAjaxUploadForms(){
	
	es = document.getElementsByClassName('ajaxuploadform'); 
  	if(es.length){ 
  		for(poradi = 1; poradi <= es.length; poradi++){
			if (getCookie('ajaxupload_success_'+poradi) == 1){
				path = getCookie('ajaxupload_destination_path_'+poradi);
				file = getCookie('ajaxupload_filename_'+poradi);
				
				document.getElementById('f'+poradi+'_upload_process').style.display = 'none';
	
				document.getElementById('f'+poradi+'_upload_form_result_succes').style.display = 'block';      
				document.getElementById('f'+poradi+'_upload_file').innerHTML = '<a href="'+path+''+file+'" target="_blank">'+file+'</a>';      
				document.getElementById('f'+poradi+'_upload_form').style.display = 'none';
			}
			else if (getCookie('ajaxupload_success_'+poradi) == 'error'){
				document.getElementById('f'+poradi+'_upload_process').style.display = 'none';
				document.getElementById('f'+poradi+'_upload_form_result_error').style.display = 'block';      
				document.getElementById('f'+poradi+'_upload_form').style.display = 'block';      
				document.cookie = 'ajaxupload_success_'+poradi+ '=0';
			}
		}
	} 
}

function deleteAjaxUploadedFile(poradi){
	var option = {
		method : "get",
		asynchronous : false,
		frequency: 1,
	    decay: 0,
		onSuccess : function(t){ 
			var xmlroot = t.responseXML.documentElement;
	    	var res = $A(xmlroot.getElementsByTagName("success"));
		    res.each( function(el){ ok = (el.firstChild.data); });
		    
			if (ok==1) {
				$('f'+poradi+'_upload_form_result_error').style.display = 'none';
				$('f'+poradi+'_upload_form_result_succes').style.display = 'none';
				$('f'+poradi+'_upload_form').style.display = 'block';
			} 	
			else {
			}		
		},
		onFailure : function(t){ }		
	};
	new Ajax.Request("index.php?pg=processdata&save_form=deleteajaxuploadedfile&poradi="+poradi, option);
}

/**
 *	rozsireni knihovny prototype o mereni maximalniho poctu znaku v poli
 *	
 *	
 *	<div class="red bold maxlength" id="remain_perex"></div>
 *	 
 *	<script type="text/javascript">
	var t1 = new TextCounter('perex', 'remain_perex', 150);
	</script>
 */
  

var TextCounter = Class.create();
TextCounter.prototype = {
    initialize: function(textareaid, infoid, maxLength) {
        $(infoid).style.display = 'none';
		es = document.getElementsByClassName('maxlength'); 
	  	if(es.length) 
		{ 
			for(j = 0; j < es.length; j++) 
		  	es[j].style.display = 'none'; 
		} 
		this.maxLength = maxLength;
        this.textarea = $(textareaid);
        this.input = $(infoid);
        this.input.value = maxLength;
        this.input.readonly = true;
        this.input.disabled = true;
        Event.observe(this.textarea, 'keyup', this.checkChars.bindAsEventListener(this));
        Event.observe(this.textarea, 'keydown', this.checkChars.bindAsEventListener(this));
        this.checkChars();
    }, 
    checkChars: function(e) {
        var includeBreaksInCount = false; // false = don't count a return (\r or \n) in the count.
        var charCount = this.textarea.value.length;
        var breaks = 0;
        if (!includeBreaksInCount) {
            var lines = this.textarea.value.split('\n');
            breaks = lines.length;
            // check for /r at the end of the lines (IE)
            for (var i=0; i<lines.length; i++) {
                var line = lines[ i ];                
                if (line.charCodeAt(line.length-1) == 13)
                    breaks++;
            }
            
        }
        
        // check if over limit
        if ((charCount-breaks) > this.maxLength) {            
            this.textarea.value = this.textarea.value.substring(0, (this.maxLength + breaks) );
        }
        
        // update counter
        if (this.input && charCount) {
        	es = document.getElementsByClassName('maxlength'); 
		  	if(es.length) 
			{ 
				for(j = 0; j < es.length; j++) 
			  	es[j].style.display = 'none'; 
			} 
		
        	
			this.input.style.display = 'block';
            if ((charCount-breaks) > this.maxLength) {
                this.input.innerHTML = 'Zbývá napsat 0 znaků z '+this.maxLength+' dovolených';
            } else {
                this.input.innerHTML = 'Zbývá napsat '+((this.maxLength + breaks) - charCount)+' znaků z '+this.maxLength+' dovolených';
            }        
        }
    }
}

function toggleChecked(oElement){ 
  oForm = oElement.form; 
  oElement = oForm.elements[oElement.name]; 
  if(oElement.length) 
  { 
    bChecked = oElement[0].checked; 
    for(i = 1; i < oElement.length; i++) 
      oElement[i].checked = bChecked; 
  } 
}

function toggleCheckedId(oElement){ 
	oForm = oElement.form; 
  oElement = oForm.elements[oElement.id]; 
  if(oElement.length) 
  { 
    bChecked = oElement[0].checked; 
    for(i = 1; i < oElement.length; i++) 
      oElement[i].checked = bChecked; 
  } 
}

function check(id){
	document.getElementById(id).checked = true;	
}

function toggleIndeterminate(oElement) { 
  oForm = oElement.form; 
  oElement = oForm.elements[oElement.id]; 
  if(oElement.length) { 
    bIndeterminate = false; 
    bChecked = true; 
    nChecked = 0; 
    for(i = 1; i < oElement.length; i++) 
      if(oElement[i].checked) 
        nChecked++; 
    if(nChecked < oElement.length - 1) { 
      if(nChecked) 
        bIndeterminate = true; 
      else { 
        bIndeterminate = false; 
        bChecked = false; 
      } 
    } 
    else { 
      bIndeterminate = false; 
    } 
    oElement[0].indeterminate = bIndeterminate; 
    oElement[0].checked = bChecked; 
  } 
}

