var xmlhttp;
var currentId;

// Konfigurowanie żądania aktualizującego dane 
function sendChange(params) {
  if (!xmlhttp) xmlhttp = aaGetXmlHttpRequest();
  var url = 'process2.php';
  xmlhttp.open('POST', url, true);
  xmlhttp.onreadystatechange = processResponse;
  xmlhttp.setRequestHeader('Content-Type',
    'application/x-www-form-urlencoded');
  xmlhttp.send(params);
}

// Przetwarzanie odpowiedzi
function processResponse() {
  if(xmlhttp.readyState == 4 && xmlhttp.status==200) {
      fadeRow();
  }
}
window.onload=function() {
   try {
   var rows = document.getElementsByTagName('tr');
   for (var i = 0; i < rows.length; i++) {
      var tds = rows[i].getElementsByTagName('td');

      // Pobieranie identyfikatora z pierwszej komórki
      if (tds.length > 1) {
         var value = tds[0].firstChild.nodeValue;
         var div = document.createElement('div');
         div.className = 'delete';
         div.id = "item" + value;
         var txt = document.createTextNode("Usuń teraz");
         div.appendChild(txt);
         var td = rows[i].insertCell(-1);
         aaManageEvent(div,'click',rowClick);
         td.appendChild(div);
      }
   }    
   } catch(e) {
     alert(e);
   } 
};

// Obsługa kliknięcia usuwającego wpis
function rowClick(evnt) {
  evnt = (evnt) ? evnt : window.event;

  var item = (evnt.currentTarget) ? evnt.currentTarget : evnt.srcElement;
  currentId = item.id.split("item")[1];
  sendChange("id=" + currentId);
}

function fadeRow(){
  Fat.fade_element("tr" + currentId,null,800,"#ff0000");
  setTimeout('deleteRow("' + currentId + '")' , 802);
}

function deleteRow(id) {
  var item = aaElem("item" + id);
  var row = item.parentNode.parentNode;
  var table = row.parentNode;
  table.deleteRow(row.rowIndex);
}
