function importy(dir){

      var dir = dir;
      //rand v url je aby to ten blbec explorer nekesoval
      url = "/index.php/importer/import/" + dir + "/" + Math.random();
      var XHR = new SZN.HTTPRequest();
      XHR.setMethod("get");
      XHR.setFormat("xml");
      XHR.setMode("async");
      XHR.send(url, window, "importyCall");

}
function importyCall(xml, status){
      if (status == 200){
            var directory = xml.getElementsByTagName("directory").item(0).firstChild.data;
            var info =  xml.getElementsByTagName("info").item(0).firstChild.data;
            if(info != "end"){
                  //schovat link
                  SZN.gEl("startLink").style.display = 'none';
                  SZN.gEl("importUpozorneni").style.display = 'block';


                  var object = xml.getElementsByTagName("imageTag").item(0).firstChild.data ;
                  if(info  == 'ok') {
                        //posunout okno nahoru
                        window.scrollTo(0,0);

                        var parentEl = SZN.gEl('imm') ;

                        //vytvorit element ktery se bude vkladat
                        var newElement = document.createElement('div');
                        newElement.setAttribute("style","float:left; border:1px solid black; margin:2px");
                        newElement.innerHTML = object ;

                        //vlozit
                        addBefore(parentEl, newElement);

                        SZN.gEl('loader').innerHTML = "<img src='/kohana/design/ajax-loader.gif' /> import obrázku č.";

                        //pocet
                        var pocet = parseInt(SZN.gEl('num').innerHTML);
                        if(isNaN(pocet)) pocet = 0;
                        pocet = pocet + 1;
                        SZN.gEl('num').innerHTML = pocet;

                        importy(directory);
                  }
            }else{
                  var celkem = SZN.gEl('num').innerHTML;
                  if(celkem < 1) celkem = 0;
                  SZN.gEl('loader').innerHTML = "Bylo importováno " + celkem + " obrázků.";
                  SZN.gEl('num').innerHTML = '';
                  SZN.gEl("importUpozorneni").style.display = 'none';
            }
      }
}


function addBefore(parentElement, insertElement) {
      //odkaz na element do ktereho se bude vkladat
      var container = parentElement;

      //vytvorit element ktery se bude vkladat
      var newElement = insertElement;

      //pokud je prazdny, vytvorit element
      if(container.childNodes.length < 1){
            container.appendChild(newElement);
      }else{
            //pokud uz v nem neco je, vlozi se novy element pred nej
            container.insertBefore(newElement,container.childNodes.item(0));
      }


}


function showWindow(name, text , width, height) {
      var name = name;
      var width= width;
      var height=height;
      var text = text;

      try{
            //vytvoreni okna
            var params = {
                  imagePath: "/kohana/design/shadow/shadow-", 
                  sizes: [0,0,0,0]
            };
            w = new SZN.Window(params);
            //nastaveni obsahu
            var button = SZN.cEl('input','closeButton');
            var lista = SZN.cEl('div','popupLista');
            var popContent = SZN.cEl('div','contentPopup');
            var nameLista = SZN.cTxt(name);
            lista.appendChild(nameLista);
            button.type="button";
            button.value="zavřít";
            button.onclick=windowClose;
            w.content.appendChild(lista);
            popContent.style.height = parseInt(height - 21 - 8) + "px";
            w.content.appendChild(popContent);
            w.content.appendChild(button);
            SZN.Dom.addClass(lista, "alertLista");
            popContent.innerHTML = text;


            w.show();

            w.container.style.position = 'fixed';

            w.content.style.width = width + "px";
            w.content.style.height = height + "px";

            var pos1 = SZN.Dom.getBoxPosition(SZN.gEl('windowPlaceholder'));


            w.container.style.top = (SZN.Dom.getDocSize().height / 2) - (height / 2) + 'px';
            w.container.style.left = (SZN.Dom.getDocSize().width / 2) - (width / 2) + 'px';

            SZN.gEl('windowPlaceholder').appendChild(w.container);
      }
      catch(e)
      {
            alert(e);
            return false;
      }

}

function windowClose(){
      w.hide();
}

function checkForm(typ, e){
      //SZN.Events.cancelDef(e) ;
      var typ = typ;

      if(typ == 'tagiska'){

            if(SZN.gEl("nopismo") != null && SZN.gEl("nopismo").value == 'nenipismo') return true;

            var inputs = SZN.Dom.getElementsByClass("pismo",null,null);
            var checkinput = true;
            for(i = 0; i < inputs.length; i++){
                  if(inputs[i].checked == true){
                        checkinput = false;
                  }
            }

            if(checkinput == true){
                  showWindow("Chybí text","<br />Objednávku nelze dokončit.<br />Není vybráno žádné písmo.",350,160);
                  return false;
            }else{
                  var inputs = SZN.Dom.getElementsByClass("tagiskaInput",null,null);
                  var empty = true;
                  for(i = 0; i < inputs.length; i++){
                        if(inputs[i].value.length > 0){
                              empty = false;
                        }
                  }
                  if(empty == true) showWindow("Chybí text","<br />Objednávku nelze dokončit.<br />Všechny textové pole Tagisky jsou prázdné.<br />Doplňte text.",350,160);
                  else return true;
            }

      }
      if(typ == 'rogz'){
            var vzory = SZN.Dom.getElementsByClass("rogzvzor",null, null);

            var checked = false;
            for(i=0; i < vzory.length; i++ ){
                  if(vzory[i].checked == true) checked = true;
            }

            if(checked == false) showWindow("Vyberte vzor","<br />Objednávku nelze dokončit.<br />Není vybrán žádný vzor.",350,140);
            else return true;
      }

      if(typ == 'produkt'){
            return true;
      }
      return false;

}

function checkTextLength(element,num){

      var el = element;
      var num = num;
      var id = parseInt(element.id);
      var zbyva = parseInt(num - element.value.length);

      if(zbyva < 1){
            SZN.Dom.addClass(SZN.gEl(id + "_num"), "alert" );
      }

      if(zbyva < 0){
            element.value = element.value.substr(0,num );
            SZN.Dom.addClass(SZN.gEl(id + "_num"), "alert" );
            zbyva = 0;
      }

      if(zbyva > 0){
            SZN.Dom.removeClass(SZN.gEl(id + "_num"), "alert" );
      }

      SZN.gEl(id + "_num").innerHTML = "[" + zbyva + "]";

}

function eraseForm(formular){

      var i = 0;
      while(formular[i] != null && i < 18){
            formular[i].value = '';
            i++;
      }
}

function eraseFormKontakt(formular){

      var i = 0;
      while(formular[i] != null && i < 17){
            formular[i].value = '';
            i++;
      }
}

function addPostovne(form){
      var form = form;
      if(form.name.value == '' || form.price.value == '' || form.priceDPH.value == ''){
            alert("vyplňte všechna pole formuláře");
            return false;
      }
      else return true;
}

function sendOrderCheck(){
      var check =  SZN.gEl("souhlaOP");
      if(check.checked == false){
            alert("Před odesláním objednávky musíte souhlasit s obchodními podmínkami.");
            return false;
      }
      else
      {
            SZN.gEl('odeslatobjhiddensubmit').click();
      }

}

function openFakturaWindow(url,cisloFaktury)
{
      //if(!confirm("Opravdu chcete vytvořit fakturu?"))return false;

      var url = url;
      var cisloFaktury = cisloFaktury;
      window.open(url,"",'width=800,height=600,left=100,top=100,titlebar=no,toolbar=no');
}

/**
function checkAdproductAdminOrder(){
var valid = true;
if(parseInt(SZN.gEl("numInput").value) < 1) valid = false;
if(parseInt(SZN.gEl("priceInput").value  < 1) valid = false;
if(parseInt(SZN.gEl("priceInputDPH").value  < 1) valid = false;

if(valid == false){
alert("Zkontrolujte správné hodnoty v polích počet, cena, cena s DPH");
return false
}else return true;

}
*/

//tohle by asi melo byt ajaxem a ne natvrdo
function checkDoprava(element,act)
{
      var target = SZN.gEl("dopravadiv");
      var inner = "<select name='doprava'><option value='0'> -- vyberte způsob dopravy -- </option>";
      if(element.value == 13)
      {
            inner += "<option "+aSel(act,1)+"value=\"1\">Česká pošta</option><option "+aSel(act,25)+"value=\"25\">Kurýrní  služba</option>";
      }
      else if(element.value == 12)
      {
            inner += "<option "+aSel(act,1)+"value=\"1\">Česká pošta</option><option "+aSel(act,25)+"value=\"25\">Kurýrní  služba</option><option "+aSel(act,27)+"value='27'>Osobní odběr - platba bank. převodem</option>";
      }
      else if(element.value == 15)
      {
            inner += "<option "+aSel(act,28)+"value=\"28\">Osobní odběr - platba v hotovosti</option>";
      }

      inner += '</select>';
      target.innerHTML = inner;


}

//vlozi pri shode A a B vrati selected='true'
function aSel(A,B)
{
      if(A==B)
      {
            return " selected='true' ";
      }
      else return "";
}

/*
<option value="0"> -- vyberte způsob dopravy -- </option>
<option value="1">Česká pošta</option>
<option value="27">Osobní odběr - platba bank. převodem</option>
<option value="28">Osobní odběr - platba v hotovosti</option>
<option value="25">Kurýrní služba</option>
*/
