// ==UserScript==
// @name           AllegroTools (Archiwizator) for IE i FF
// @namespace      http://allegrotoolsie.hopto.org/
// @description    Integracja z serwisem http://archi.inos.int.pl // v0.20070815.1
// @include        http*://*allegro.pl/item*
// @include        http*://*allegro.pl/show_item.php?*
// ==/UserScript==

/////////////////////////////////////////////////////////////////
//// IE/FF compatibility
/////////////////////////////////////////////////////////////////

// In FF childNodes returns also #text nodes that represents
// the whitespaces in the HTML source. IE skips these nodes
// For IE and FF use this method instead od childNodes property
// to return all children of an element.
// NOTE: this method skips ALL #text nodes (not only whitespaces)
function getChildNodes(element)
{
  var children = new Array();

  if(element)
  {
    var child = element.firstChild;
    while(child)
    {
      if (child.nodeType == 1)
      {
        children.push(child);
      }
      child = child.nextSibling;
    }
  }

  return children;
}

// IE specific functions
function select()
{
  if (arguments.length>1) {
    var node = arguments[0];
    for (var i=1; i<arguments.length; i++) {
      if (typeof(arguments[i])=='string') {
        node = node.getElementById(arguments[i]);
      } else {
        var nodeIdx = arguments[i];
        var children = getChildNodes(node);
        if (nodeIdx<children.length) {
          node = children[nodeIdx];
        } else {
          node = null;
          break;
        }
      }
    }
  }
  return node;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


function addArchiverButton()
{
  if(document.location.href.match(/item(\d+).*?.html/) ||
    (document.location.href.match(/show_item.php\?.*?item=(\d+).*?/)))
  {
    var auctionNumber = RegExp.$1;
    
    if(auctionNumber)
    {
      var dataHeaderElement = null;
      var pagecontent = document.getElementById("pagecontent1");

      if(pagecontent)
      {
        var dataHeaderElement = select(pagecontent, 0, 3, 0, 0, 0, 0, 0, 0, 0);
        if(dataHeaderElement)
        {
          dataHeaderElement.removeAttribute('colSpan');
          dataHeaderElement.removeAttribute('width');
          var archiverFormTd = dataHeaderElement.cloneNode(true);
          archiverFormTd.align = 'right';
          archiverFormTd.innerHTML = "<form action='http://archi.inos.int.pl/add.html' " +
                                     "method='GET' name='add' target='_blank' " +
                                     "onsubmit='return confirm(\"Czy na pewno chcesz zarchiwizowac aukcje nr " + auctionNumber + " w Archi-wizatorze?\");'>" +
                                     "<input type='hidden' name='s' id='s1' value='1'>" +
                                     "<input type='hidden' name='id' value='" + auctionNumber + "'>" +
                                     "<input type='submit' name='submit' value='Archiwizuj' style='font-size=8pt' " +
                                     "title='Archiwizuje aukcje nr " + auctionNumber + " w Archi-wizatorze'>" +
                                     "&nbsp;</form>";
          dataHeaderElement.parentNode.appendChild(archiverFormTd);
        }  
      }  
    }  
  }
}

function allegrotoolsArchiver()
{
  addArchiverButton();
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Start Allegro Tools (Archiver) script
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
allegrotoolsArchiver()
