/* Vraci set vsech elementu daneho name v danem node */
function getElByName(node, name) {
	return node.getElementsByTagName(name);
}
/* Vraci text obsazeny v elementu s textovym obsahem */
function getContent(node) {
	return node.firstChild.data;
}
/* Metoda pro oterverni noveho okna */
function openMatchWindow(url,typ){
   var xwidth = window.screen.width - 10;
   var wopen = window.open(url+'?typ='+typ, "_blank", "width=" + xwidth + ", height=600,resizable=yes,scrollbars=yes,left=0,top=0");
   wopen.focus();
}
/* Metoda pro zavreni okna */
function closeMatchWindow(){
   window.close();
}
/* Metoda pro tisk okna */
function printMatchWindow(){
   window.print();
}

/* Odstranit polozku pro porovnani */
function unsetMatchItemAll(urlmodul,typ,msg){
   if(confirm(msg)){
      var url = urlmodul;
      var timeStamp = "casotisk=" + new Date().getTime();
      var urlPost = url + '?' + timeStamp;
      var postData = 'typ=' + typ;

      // Nastavit pocet prvku
      $('app_info_text').set('text','Odebírám ze srovnání');
      // Zobrazit info box
      ShowAppBox.showBox($('app_info'));
      /* Zobrazit info */
      ViewPort.centerObject($('app_info'), 300, 100);

      var myAjax = new Request({method: 'post', url: urlPost,data:postData,onComplete: resultUnsetMatchItemAll}).send();
   }
}
/* Zpracovat pozadavek */
function resultUnsetMatchItemAll(){
   //alert(this.response.text);
   workUnsetMatchItemAll(this.response.xml);
}
/* Zpracovat pozadavek */
function workUnsetMatchItemAll(xmlDoc){
   // Zobrazit info box
   ShowAppBox.showBox($('app_info'));

   var itemType = xmlDoc.getElementsByTagName('type')[0].firstChild.data;
   var isCheck = xmlDoc.getElementsByTagName('ischeck')[0].firstChild.data;

   // Zrusit ul
   $('id_wmatchul_' + itemType).destroy();
   // Vytvorit ul
   createElementUl('id_wmatchdiv_' + itemType,'id_wmatchul_' + itemType);
   // Schovat tlacitko
   $('id_wmatchsub_' + itemType).addClass('item_hidden');
   $('id_wmatchsubu_' + itemType).addClass('item_hidden');
   // Schovat box
   $('id_wmatchsubu_' + itemType).addClass('item_hidden');
   $('id_wmatchbox_' + itemType).addClass('item_hidden');

   // Odskrtnout programy pokud nejake jsou vybrane
   if(isCheck == 1){
      // Ziskat polozky
      var items = xmlDoc.getElementsByTagName("hosting");
      //projit polozky
      for(var i=0;i < items.length;i++){
         var itemId = getContent(getElByName(items[i], 'id')[0]);

         if($('id_wmatchbut'+itemId+'_' + itemType).hasClass('tl_abuthost')){
            $('id_wmatchbut'+itemId+'_' + itemType).removeClass('tl_abuthost');
         }
         if(!$('id_wmatchbut'+itemId+'_' + itemType).hasClass('tl_buthost')){
            $('id_wmatchbut'+itemId+'_' + itemType).addClass('tl_buthost');
         }
      }
   }

}

/* Odstranit polozku pro porovnani */
function unsetMatchItem(urlmodul,iditem,typ){
   var url = urlmodul;
   var timeStamp = "casotisk=" + new Date().getTime();
   var urlPost = url + '?' + timeStamp;
   var postData = 'iditem=' + iditem + '&typ=' + typ;

   // Nastavit pocet prvku
   $('app_info_text').set('text','Odebírám ze srovnání');
   // Zobrazit info box
   ShowAppBox.showBox($('app_info'));
   /* Zobrazit info */
   ViewPort.centerObject($('app_info'), 300, 100);

   var myAjax = new Request({method: 'post', url: urlPost,data:postData,onComplete: resultUnsetMatchItem}).send();
}
/* Zpracovat pozadavek */
function resultUnsetMatchItem(){
   //alert(this.response.text);
   workUnsetMatchItem(this.response.xml);
}
/* Zpracovat pozadavek */
function workUnsetMatchItem(xmlDoc){
   // Zobrazit info box
   ShowAppBox.showBox($('app_info'));

   var item = xmlDoc.getElementsByTagName('item')[0].firstChild.data;
   var count = xmlDoc.getElementsByTagName('count')[0].firstChild.data;
   var itemName = xmlDoc.getElementsByTagName('name')[0].firstChild.data;
   var itemType = xmlDoc.getElementsByTagName('type')[0].firstChild.data;
   var isCheck = xmlDoc.getElementsByTagName('ischeck')[0].firstChild.data;

   // Odstranit polozku
   $('id_wmatch_' + item + '_' + itemType).destroy();
   // Pokud je nulovy pocet polozek info
   if(count == 0){
      createElementLi(itemName,'1','id_wmatchul' + '_' + itemType);
      // Schovat tlacitko
      $('id_wmatchsub_' + itemType).addClass('item_hidden')
      $('id_wmatchsubu_' + itemType).addClass('item_hidden')
   }

   // Odstranit zaskrtnuti
   if(isCheck == 1){
      if($('id_wmatchbut'+item+'_' + itemType).hasClass('tl_abuthost')){
         $('id_wmatchbut'+item+'_' + itemType).removeClass('tl_abuthost');
      }
      if(!$('id_wmatchbut'+item+'_' + itemType).hasClass('tl_buthost')){
         $('id_wmatchbut'+item+'_' + itemType).addClass('tl_buthost');
      }
   }
}
/* Nastavit hosting pro porovani */
function setMatchItem(urlmodul,iditem,typ){
   var url = urlmodul;
   var timeStamp = "casotisk=" + new Date().getTime();
   var urlPost = url + '?' + timeStamp;
   var postData = 'iditem=' + iditem + '&typ=' + typ;

   // Nastavit pocet prvku
   $('app_info_text').set('text','Přidávám do srovnání');
   // Zobrazit info box
   ShowAppBox.showBox($('app_info'));
   /* Zobrazit info */
   ViewPort.centerObject($('app_info'), 300, 100);

   var myAjax = new Request({method: 'post', url: urlPost,data:postData,onComplete: resultMatchItem}).send();
}
/* Zpracovat pozadavek */
function resultMatchItem(){
   //alert(this.response.text);
   workMatchItem(this.response.xml);
}
/* Zpracovat pozadavek */
function workMatchItem(xmlDoc){
   // Zobrazit info box
   ShowAppBox.showBox($('app_info'));

   // Pocet polozek
   var count = xmlDoc.getElementsByTagName('count')[0].firstChild.data;
   // Url adresa pro odstraneni
   var urlModel = xmlDoc.getElementsByTagName('url')[0].firstChild.data;
   // Titulek
   var itemTitle = xmlDoc.getElementsByTagName('title')[0].firstChild.data;
   // Typ hostingu
   var itemType = xmlDoc.getElementsByTagName('type')[0].firstChild.data;
   // id hostingu
   var idItem = xmlDoc.getElementsByTagName('iditem')[0].firstChild.data;

   // Zrusit ul
   $('id_wmatchul_' + itemType).destroy();
   // Vytvorit ul
   createElementUl('id_wmatchdiv_' + itemType,'id_wmatchul_' + itemType);
   // Ziskat polozky
   var items = xmlDoc.getElementsByTagName("hosting");
   //projit polozky
   for(var i=0;i < items.length;i++){
      var itemName = getContent(getElByName(items[i], 'name')[0]);
      var itemId = getContent(getElByName(items[i], 'id')[0]);
      // Vytvorit li
      createElementLi(itemName,itemId,'id_wmatchul_' + itemType,itemType);
      // Vytvorit span
      createElementSpan(' X','id_wmatch_' + itemId + '_' + itemType,itemId,urlModel,itemTitle,itemType);

      // Tridy
      $('id_wmatch_' + itemId + '_' + itemType).addClass('bold');
      $('id_wmatchs_' + itemId + '_' + itemType).addClass('box_butx');
   }

   // Zobrazit tlacitko
   if($('id_wmatchsub_' + itemType).hasClass('item_hidden')){
      $('id_wmatchsub_' + itemType).removeClass('item_hidden');
   }

   // Zobrazit tlacitko
   if($('id_wmatchsubu_' + itemType).hasClass('item_hidden')){
      $('id_wmatchsubu_' + itemType).removeClass('item_hidden');
   }

   // Zobrazit box
   if($('id_wmatchbox_' + itemType).hasClass('item_hidden')){
      $('id_wmatchbox_' + itemType).removeClass('item_hidden');
   }

   // Pridat zelene zaskrtnuti
   if(!$('id_wmatchbut'+idItem+'_' + itemType).hasClass('tl_abuthost')){
     $('id_wmatchbut'+idItem+'_' + itemType).removeClass('tl_buthost');
     $('id_wmatchbut'+idItem+'_' + itemType).addClass('tl_abuthost');
   }
}
/* Funkce pro vytvoreni ul */
function createElementUl(eparent,eid){
   //vytvorit div
   var div = document.createElement("ul");
   div.setAttribute("id",eid);
   //pridat div do div
   document.getElementById(eparent).appendChild(div);
}
/* Funkce pro vytvoreni elementu li */
function createElementLi(itemName,itemId,eparent,itemType){
   //vytvorit radek
   var row = document.createElement("li");
   row.setAttribute("id",'id_wmatch_' + itemId + '_' + itemType);
   var textUzel = document.createTextNode(itemName);
   //pridat elementu hodnotu
   row.appendChild(textUzel);
   //pridat rade do div
   document.getElementById(eparent).appendChild(row);
}
/* Funkce pro vytvoreni elementu span */
function createElementSpan(itemName,eparent,itemId,urlModel,itemTitle,itemType){
   //vytvorit radek
   var row = document.createElement("span");
   row.setAttribute("id",'id_wmatchs_' + itemId + '_' + itemType);
   //row.setAttribute("onclick","unsetMatchItem('"+urlModel+"','"+itemId+"','"+itemType+"')");
   row.onclick = new Function("unsetMatchItem('"+urlModel+"','"+itemId+"','"+itemType+"');");

   row.setAttribute("title",itemTitle);
   var textUzel = document.createTextNode(itemName);
   //pridat elementu hodnotu
   row.appendChild(textUzel);
   //pridat rade do div
   document.getElementById(eparent).appendChild(row);
}


