// WoD-Shop-Select user script
// version 1.0.0 BETA!
// 2005-12-18
// Copyright (c) 2005, Jens Rupp
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.  To install it, you need
// Greasemonkey 0.6.4 or later: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "WoD-Shop-Select", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          WoD-Shop-Select
// @description   Selektiert bei Shops automatisch Gegenstände
// @include       http*://*.world-of-dungeons.de/wod/spiel/wodpartner/partners.php*
                  http://a3.world-of-dungeons.de/wod/spiel/wodpartner/partners.php?KEY=wod_partner&HELPID=WoD-Gesch%E4fte
// ==/UserScript==

//GM_log("Start");

function selectItems(event)
{
  var shop=event.target.name.substring(4,event.target.name.length-1);
  //GM_log(shop);

  filter=document.getElementById('filter['+shop+']');
  //GM_log(filter.value);


  var allSelects, thisSelect,  thisType='', sentOptions;
  sentOptions=Array();
  allSelects = document.getElementsByTagName('select');
  var found = false;
  for (var i = 0; i < allSelects.length; i++)
  {
      thisSelect = allSelects[i];
      //GM_log(thisSelect.name);

      if(!found){
              for(n=0; n<thisSelect.length; n++){
                      sentOptions[n]=Array();
                      //sentOptions[n]['value']=thisSelect.options[n].value;
                      sentOptions[n]=thisSelect.options[n].text;
                      //GM_log(thisSelect.options[n].value+" "+thisSelect.options[n].text);
              }
              found = true;
          }
  }

  var select=-1,n1=0, n2=1, p,v;


  while ((select<allSelects.length)  )
  {
    //eintrag finden
    found=false;
    while ((!found) && (n1<sentOptions.length))
    {
        p=sentOptions[n1].indexOf('#');
        if (p>=0)
        {
          v=parseInt(sentOptions[n1].substr(p+1));
          //GM_log('Multi: '+v);
          if (n2<v)
          {
            n2=n2+1;
          } else
          {
            n1=n1+1;
            n2=1;
          }

        } else
        {
          n2=1;
          n1=n1+1;
        }
      if  ( (n1<sentOptions.length) &&  ( (filter.value == '') || (sentOptions[n1].indexOf(filter.value) != -1)))
      {
        found=true;
      }  
    }

    //naechstes select finden
    do
    {
      select=select+1;
    } while ((select<allSelects.length) && (allSelects[select].name.indexOf(shop)==-1));

    //beides ok?
    if ( found && (select<allSelects.length) && (n1<sentOptions.length))
    {
      allSelects[select].selectedIndex=n1;
    } else if (select<allSelects.length)
      allSelects[select].selectedIndex=-1;

  }

}

var allButtons, thisButton, newButton, newEdit;
allButtons = document.getElementsByTagName('input');
for (var i = 0; i < allButtons.length; i++)
{
    thisButton = allButtons[i];
    if(thisButton.name.indexOf('visit')!=-1)
    {
       //GM_log(thisButton.name.substring(6,thisButton.name.length-1));

	    newButton = document.createElement('input');
	    newButton.setAttribute('type', 'button');
	    newButton.setAttribute('name', 'set['+thisButton.name.substring(6,thisButton.name.length-1)+']');
	    newButton.setAttribute('value', 'Setzen');
            newButton.addEventListener('click',selectItems,false);
	    thisButton.parentNode.insertBefore(newButton, thisButton.nextSibling);
            //GM_log("insert");

	    newEdit = document.createElement('input');
	    newEdit.setAttribute('type', 'edit');
	    newEdit.setAttribute('name', 'filter['+thisButton.name.substring(6,thisButton.name.length-1)+']');
	    newEdit.setAttribute('id', 'filter['+thisButton.name.substring(6,thisButton.name.length-1)+']');

	    newEdit.setAttribute('value', '');
	    thisButton.parentNode.insertBefore(newEdit, thisButton.nextSibling);

	    thisButton.parentNode.insertBefore(document.createTextNode(' Auswahl: '), thisButton.nextSibling);
    }

}




