function del(odkaz) // funkce osetrujici smazani zaznamu pomoci odkazu (paramert funkce)
{
var x=window.confirm("Opravdu chcete tento záznam smazat?")
if (x)
window.location.href=odkaz;
else
return false;

}


function show(co)
{
 if (document.getElementById(co).style.display == "none")
 	document.getElementById(co).style.display = "";
 else		
 	document.getElementById(co).style.display = "none"	
}

 

function zaznam(objectName, count){
  
  //zobrazeni pozice kurzoru ve strance
  document.getElementById("aktivni").innerHTML=count;
  //zmena hidden pole zaznam, ktere urcuje pozici pri prechodu na zalozku
  document.getElementById("zaznam").value=idZaznam[count-1];
  
  
    if (count != lastIndex) {
        rowName = objectName + "Row" + count;
        lastRowName = objectName + "Row" + lastIndex;

        // odznačení původního záznamu
        if (lastRowName != null) {
            elements = document.getElementsByName(lastRowName);
            for (i = 0; i < elements.length; i++) {
                setElClass(elements[i], lastRowClass);
            }
        }

        // označení nového záznamu
        
        elements = document.getElementsByName(rowName);
        lastIndex = count;
        lastRowClass = getElClass(elements[0]);
        for (i = 0; i < elements.length; i++) {
            setElClass(elements[i], "tdata_radek_aktivni");
        }

    }
}
  

function keyPress(e){ 
      
if(e.keyCode==13) //sipka dolu 
		{
		 document.getElementById('zalozka').submit()
		}

  if(e.keyCode==40) //sipka dolu 
		{
		 if (lastIndex!=radku ) 
		 zaznam("table", lastIndex+1)
		}
	if(e.keyCode==38) //sipka nahoru 
		{
		 if (lastIndex!=1 )
		 
	   zaznam("table", lastIndex-1)
		}

}

function buttonup(){
  
 if (lastIndex!=1 )
		 
	   zaznam("table", lastIndex-1)
		}

  

  function buttondown(){
  
  if (lastIndex!=radku ) 
		 zaznam("table", lastIndex+1)
		}
  




function povinnaPole() { //kontola vyplneni povinnych poli 
  
  check = true;
  elements = documentAll();
  for (i = 0; i < elements.length; i++) {
      el = elements.item(i);
      if (el.tagName == "INPUT") {
          if (isElClass(el, "inputpole_povinne") && trim(el.value).length < 1) {
              check = false;
              break;
          }
      }
      
      if (el.tagName == "TEXTAREA") {
          if (isElClass(el, "inputpole_povinne") && trim(el.value).length < 1) {
              check = false;
              break;
          }
      }
      
      if (el.tagName == "SELECT") {
          if (isElClass(el, "inputpole_povinne") && trim(el.options[el.selectedIndex].value).length < 1) {
              check = false;
              break;
          }
      }
  }

  if (! check) {
      pole = "";
      if (el.title != "") {
          pole = el.title;
      } else {
          if (el.parentNode != null && el.parentNode.title != "") {
              pole = el.parentNode.title;
          }
      }

      if (pole == "") {
          alert("Vyplňte, prosím, povinnou hodnotu.");
      } else {
          alert("Vyplňte, prosím, povinnou hodnotu pole '" + pole + "'."); //pole - title u prvku
      }
      el.focus();
  }
  return check;
}


function trim(inputString) {
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   return retValue; // Return the trimmed string back to the user
}



var ie = (document.all) ? true : false;
function documentAll() {
    if (ie) {
        return document.all;
    } else {
        return document.getElementsByTagName('*');
    }
}


function isElClass (el, className) {
    result = false;
    for (var i = 0; i < el.attributes.length; i++) {
        if (el.attributes.item(i).nodeName == 'class') {
           if (el.attributes.item(i).nodeValue == className) {
               result = true;
               break;
           }
        }
    }
    return result;
}

function getElClass (el) {
    result = null;
    for (var i = 0; i < el.attributes.length; i++) {
        if (el.attributes.item(i).nodeName == 'class') {
           result = el.attributes.item(i).nodeValue;
           break;
        }
    }
    return result;
}

function setElClass (el, className) {
    for (var i = 0; i < el.attributes.length; i++) {
        if (el.attributes.item(i).nodeName == 'class') {
           el.attributes.item(i).nodeValue = className;
           break;
        }
    }
}


// *********************** kontrola a prevod data ******************************

function isInteger(s){
    var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

// prevod dd.mm.yy na dd.mm.yyyy
function unifyDateString(a) {
    if (! checkDateString(a)) return a;
    pointFirst = a.indexOf(".");
    pointSecond = a.indexOf(".", pointFirst + 1);
    yyyy = a.substring(pointSecond + 1, a.length) // year
    if (yyyy >= 100) return a;

    if (yyyy >= 0 && yyyy < 50) yyyy = parseInt(yyyy) + parseInt(2000);
    if (yyyy >= 50 && yyyy < 100) yyyy = parseInt(yyyy) + parseInt(1900);
    return a.substring(0, pointSecond + 1) + yyyy;
}

// minimalni format:    d.m.yy
// maximalni format:    dd.mm.yyyy
function checkDateString(a) {
    var check = true;

    if (a.length < 6 || a.length > 10) check = false;
    pointFirst = a.indexOf(".");
    pointSecond = a.indexOf(".", pointFirst + 1);
    
    dd = a.substring(0, pointFirst) // day
    c = a.substring(pointFirst, pointFirst + 1) // '.'
    mm = a.substring(pointFirst + 1, pointSecond) // month
    e = a.substring(pointSecond, pointSecond + 1) // '.'
    yyyy = a.substring(pointSecond + 1, a.length) // year

    // integer checking
    if (!isInteger(dd)) check = false;
    if (!isInteger(mm)) check = false;
    if (!isInteger(yyyy)) check = false;

    // zapis 'yy' prevedu na zapis 'yyyy'
    if (yyyy >= 0 && yyyy < 50) yyyy = parseInt(yyyy) + parseInt(2000);
    if (yyyy >= 50 && yyyy < 100) yyyy = parseInt(yyyy) + parseInt(1900);

    //basic error checking
    if (mm < 1 || mm > 12) check = false;
    if (c != '.') check = false;
    if (dd < 1 || dd > 31) check = false;
    if (e != '.') check = false;
    if (yyyy < 1900 || yyyy > 2100) check = false;
    
    //advanced error checking

    // months with 30 days
    if (mm==4 || mm==6 || mm==9 || mm==11) {
        if (dd==31) check = false;
    }

    // february, leap year
    if (mm==2) {
        // feb
        var g = parseInt(yyyy/4)
        if (isNaN(g)) {
            check = false;
        }

        if (dd>29) check = false;
        if (dd==29 && ((yyyy/4)!=parseInt(yyyy/4))) check = false;
    }

    return check;
}

function checkDateInput(el) {
    if (el.value.length < 1) {
        return true;
    }

    if (! checkDateString(el.value)) {
        alert('Vložte, prosím, správné datum.');
        el.focus();
        return false;
    }
    // prevod dd.mm.yy na dd.mm.yyyy
    el.value = unifyDateString(el.value);
    return true;
}

// cisla




function checkNumberInput(el) {
    if (el.value.length < 1) {
        return true;
    }
  
    if (! isInteger(el.value)) {
        alert('Vložte, prosím, číslo.');
        el.value="";
        el.focus();
        return false;
    }
     return true;
}
