function ShowImage(jmeno, sirka, vyska, popis) {
  wokno=window.open("", "_blank", "resizeable=yes,toolbar=no,status=no,menubar=no,scrollbars=no,directories=no,location=no,resize=yes,width="+sirka+",height="+vyska);
  wokno.document.write("<html><head><title>"+popis+"</title></head><body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">");
  wokno.document.write("<img src=\""+jmeno+"\" width="+sirka+" height="+vyska+" border=0 alt=\""+popis+"\" onClick=\"window.close();\">");
  wokno.document.write("</body></html>");
}  

function ShowPageImg(jmeno, sirka, vyska, popis) {
  wokno=window.open("", "_blank", "resizeable=yes,toolbar=no,status=no,menubar=no,scrollbars=no,directories=no,location=no,resize=yes,width="+sirka+",height="+vyska);
  wokno.document.write("<html><head><title>"+popis+"</title></head><body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">");
  wokno.document.write("<img src=\""+jmeno+"\" width="+sirka+" height="+vyska+" border=0 alt=\""+popis+"\" onClick=\"window.close();\">");
  wokno.document.write("</body></html>");
  return false;
}  

function ShowPageImgWithScrollbars(jmeno, sirka, vyska, popis) {
  wokno=window.open("", "_blank", "resizeable=yes,toolbar=no,status=no,menubar=no,scrollbars=yes,directories=no,location=no,resize=yes,width="+sirka+",height="+vyska);
  wokno.document.write("<html><head><title>"+popis+"</title></head><body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">");
  wokno.document.write("<img src=\""+jmeno+"\" width="+sirka+" height="+vyska+" border=0 alt=\""+popis+"\" onClick=\"window.close();\">");
  wokno.document.write("</body></html>");
  return false;
}

function isEmail(string) {
  if(string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
    return true;
  else return false;
}
function ValidateForm(form) {
    var ret=true;
    var mail=true;
    var help=false;
		for(i=0;i<form.length;i++) {
      var swap=form.elements[i];
      //alert(swap.name +'='+ swap.value + ' | '+ swap.type);
      if((swap.type=='text' || swap.type=='textarea') && (swap.value=='' &&  swap.name.substring(0,6)=='check_')) {	
        ret=false;
      }
      if(swap.type=='checkbox' && swap.name=='info') {
        if(!swap.checked) {
          alert('Objednávku nebylo možno dokončit, protože jste nezaškrtli souhlas s obchodními podmínkami (umístěno na konci formuláře).');
          return false;
        }
      }
      if(swap.name=='f_email' && !isEmail(swap.value)){ mail=false;}
    }
    if(!ret) alert("Formulář nebylo možno odeslat. Pravděpodobně jste nevyplnili některé povinné pole.")
    //if(help && ret) { alert("Formulář nebylo možno odeslat. Pravděpodobně jste nevyplnili některé povinné pole."); ret=false;}
    if(!mail && ret) { alert("Zadali jste špatnou emailovou adresu. Zkuste to prosím znova."); ret=false;}
    return ret;
  }
  
  
  function MenuScrollLeft(max) {
    menu=document.getElementById('menublock');
    if(menu) {
      tmpMargin=menu.style.marginLeft;
      tmp=tmpMargin.substring(0, tmpMargin.length-2);
      tmpMargin=Number(tmp);
      if(tmpMargin!=0) menu.style.marginLeft=(tmpMargin+100)+"px";
    }
  }
  
  function MenuScrollRight(max) {
    menu=document.getElementById('menublock');
    if(menu) {
      tmpMargin=menu.style.marginLeft;
      tmp=tmpMargin.substring(0, tmpMargin.length-2);
      tmpMargin=Number(tmp);
      if(tmpMargin > -max) menu.style.marginLeft=(tmpMargin-100)+"px";
    }
  }
  
  function setElemetName(elemet, lastUsedId, name) {
    
    
  }
  
  function addCertificateRow(tableId) {
    var table = document.getElementById(tableId);
    var nextId = certificateCounter++;
    
    var trs = tableElementFactory(1, 'tr');
    for(var x in trs) {
      trs[x].setAttribute('id', "certificateName"+ nextId);
      var tds = tableElementFactory(4, 'td');
      tds[0].innerHTML = 'Jméno a příjmení:';
      tds[1].innerHTML = '<input type="text" name="certificateNames['+ nextId +'][value]" size="50" class="defedit" value="">';
      tds[2].innerHTML = advSelectBoxPatternForCertificate.replace('<select', '<select onChange="this.setAttribute(\'name\', \'certificateNames['+ nextId +'][basketItemId]\')"');
      tds[3].innerHTML = '<a href="javascript:void(0)" onClick="removeCertificateRow(\''+ tableId +'\', \'certificateName'+ nextId +'\')" title="Odstranit tento řádek."><img src="images/delete.gif" width="18" height="16" style="" alt=""></a>';
      tds[3].setAttribute('class', 'text-right');
      for(var y in tds) trs[x].appendChild(tds[y]);
      
      table.appendChild(trs[x]);
    }
    viewedItemsCertificate++;
    if(viewedItemsCertificate == basketItemCount) {
      document.getElementById('newCertificateRow').style.display = 'none';
      var tr01 = document.createElement("tr");
      tr01.setAttribute('id', 'emptyRow');
      var td05 = document.createElement("td");
      td05.setAttribute('colspan', 4);
      td05.innerHTML = '&nbsp;';
      tr01.appendChild(td05);
      table.appendChild(tr01);
    } 
    
  }
  
  function removeCertificateRow(table, rowId) {
    removeRow(table, rowId);
    if(document.getElementById('emptyRow')) removeRow(table, 'emptyRow');
    viewedItemsCertificate--;
    document.getElementById('newCertificateRow').style.display = 'table';
  }
  
  function removeRow(table, rowId) {
    var table = document.getElementById(table);
    if(document.getElementById(rowId)) table.removeChild(document.getElementById(rowId));
  }
  
  function tableElementFactory(num, element) {
    var ret = [];
    for(z = 0; z < num; z++) {
      ret[z] = document.createElement(element);
    }
    return ret;
  }
  
  // pridava do formulare dalsi blahoprani
  function addCongratulation(tableId) {
    var table = document.getElementById(tableId);
    var nextId = congratulationCounter++;
    
    var trs = tableElementFactory(4, 'tr');
    for(var x in trs) {
      trs[x].setAttribute('id', "comgratulation"+ nextId +"x"+ x);
      if(x == 0) {
        // horni odsazovaci radek
        var swap0 =tableElementFactory(1, 'td');
        swap0[0].setAttribute("style", "text-align: right;");
        swap0[0].setAttribute("colspan", "3");
        swap0[0].innerHTML = '<hr size="1" style="color: Gray;" noshade>';
        trs[x].appendChild(swap0[0]);
      } else if(x == 1) {
        // radek s oslovenim a vyberem zazitku
        var swap1 = tableElementFactory(3, 'td');
        swap1[0].innerHTML = 'Oslovení:';
        swap1[1].innerHTML = '<input type="text" name="congratulations['+ nextId +'][osloveni]" value="" size="50" class="defedit">';
        swap1[2].setAttribute('style', 'text-align: right');
        swap1[2].innerHTML = '<a href="javascript:void(0)" onClick="removeCongratulation(\''+ tableId +'\', \'comgratulation'+ nextId +'x\')" title="Odstranit toto blahopřání."><img src="images/delete.gif" width="18" height="16" style="" alt=""></a>';
        for(var y in swap1) trs[x].appendChild(swap1[y]);
      } else if(x == 2) {
        // radek s blahopranim
        swap2 = tableElementFactory(3, 'td');
        swap2[0].innerHTML = 'Blahopřání:';
        swap2[1].innerHTML = '<textarea name="congratulations['+ nextId +'][text]" rows="8" cols="50" class="defedit"></textarea>';
        swap2[2].innerHTML = advSelectBoxPatternForCongratulation.replace('<select', '<select onChange="this.setAttribute(\'name\', \'congratulations['+ nextId +'][basketItemId]\')"');
        for(var y in swap2) trs[x].appendChild(swap2[y]);
      } else if(x == 3) {
        // radek s podpisem
        swap3 = tableElementFactory(3, 'td');
        swap3[0].innerHTML = 'Podpis:';
        swap3[1].innerHTML = '<input type="text" name="congratulations['+ nextId +'][podpis]" value="" size="50" class="defedit">';
        swap3[2].innerHTML = '&nbsp;';
        for(var y in swap3) trs[x].appendChild(swap3[y]);
      }
      table.appendChild(trs[x]);
    }
    viewedItemsCongratulation++;
    // pokud bude zobrazeno tolik blahoprani, kolik je zazitku v kosiku, skovame odkaz na pridani dalsiho
    if(viewedItemsCongratulation == basketItemCount) {
      document.getElementById('newCongratulation').style.display = 'none';
    } 
  }
  
  function removeCongratulation(table, rowId) {
    for(var x = 0; x < 4; x++) {
      if(rowId == 'congratulation0x' && x == 0) continue;
      removeRow(table, rowId + x);
    }
    viewedItemsCongratulation--;
    document.getElementById('newCongratulation').style.display = 'table';
  }
  
  function EmptyField(id) {
    document.getElementById(id).value = '';
  }
  
  /* zapnuti/vypnuti dodaci adresy u objednavky  */
  function switchDeliveryAddr(checkbox) {
    disable=checkbox.checked;
    elems=Array('d_firm', 'd_name', 'd_street', 'd_city', 'd_zip', 'd_tel', 'd_email');
    for(i=0; i<checkbox.form.elements.length; i++) {
      if(elems.indexOf(checkbox.form.elements[i].name)>-1) {
        checkbox.form.elements[i].disabled=disable;
        if(disable) checkbox.form.elements[i].value='';
      }
    }
  }



  function showHidePackageForm(cnt, display) {
    for(x = 0; x < cnt; x++) {
      if(document.getElementById('packageform'+ x))
        document.getElementById('packageform'+ x).style.display=display;
    }
  }
  
  function highlightDeliveriForm(num) {
    for(x = 1; x < 8; x++) {
      if(x == num) {
        document.getElementById('id'+ x).className = 'delivery-highlight';
      } else {
        document.getElementById('id'+ x).className = '';
      }
    }
  }
  
  
  function removeContent(id, kind) {
    if(kind == 1) {
      document.getElementById(id).value = '';
    } else {
      document.getElementById('congratulationOsloveni'+ id).value = '';
      document.getElementById('congratulationText'+ id).value = '';
      document.getElementById('congratulationPodpis'+ id).value = '';
    }
  }
  
  function deleteAdvFromBasket(element) {
    if(window.confirm('Opravdu chcete položku z košíku odstranit?')) {
      document.getElementById(element).setAttribute('onSubmit', 'return true');
    } else {
      document.getElementById(element).setAttribute('onSubmit', 'return false');
    }
  }
  
  
  function formFunction() {
    document.getElementById('discussionBasicForm').action = '../discussionHandler.php';
    return true;
  }
  
  function showHideElement(id) {
    if(document.getElementById(id).style.display != 'none') document.getElementById(id).style.display='none';
    else document.getElementById(id).style.display='block';
  }
  
  function popupOdkazy() {
    if (!document.getElementsByTagName) return false;
    var links = document.getElementsByTagName("a");
    for (var i=0; i < links.length; i++) {
      if (links[i].className.match("external")) {
        links[i].onclick = function() { return !window.open(this.href); }
      }
    }
    return true;
  }
