// warning xrst code for pick is not yet included


  function fixLoot(iDiff, iVer) {
    // recompute loots for diff level iDiff=0/1/2
    // recompute loots for version=0/1 (TDP/TG)
    var l = spLoot;
    var imax = l.length;
    var loot = new Array(0,0,0); // three types of loots
    var chkp = new Array();
    var sDiff = "NHE".substr(iDiff,1);
    var sVer = iVer?"TG":"TDP"; // variable length
    var li;
    var patch = new Array();
    var patchptr = 0;
    var patchptr2;
    var patchptr3;
    var x;
    var totalLoot="";
    var totalseq=-1; // location of total=display, can fix one only.
    var patchid;
    var npatches;
    // if there is one spLoot, l is the loot object and l.length is undefined
    // if more than one, l is an array and each l[i] is an loot object
    if (imax) {
      patchptr=1;
      patch[0] = x = new Object;
      x.seq = 0;
      x.id = "MainLoop";
      x.waiting = true;
      x.value = new Array(0,0,0);
      npatches=1;
      var killer=3;
      while (npatches > 0) {
        for (patchptr3 = 0; patchptr3 < patchptr; patchptr3++) {
          x = patch[patchptr3];
          if (x.waiting && (x.value)) {
            loot[0] = x.value[0];
            loot[1] = x.value[1];
            loot[2] = x.value[2];
            patchid=x.id;
            x.waiting = false;
            npatches--;
            break
          }
        }
        // if (!--killer) return;
        for (var i = x.seq; i < imax; i++) {
          li = l[i];
          // must not direct copy the array!
          if (li.chkp) {
            chkp[li.chkp] = new Array(loot[0],loot[1],loot[2]);
            for (patchptr2 = 0; patchptr2 < patchptr; patchptr2++) {
              if (patch[patchptr2].id == li.chkp) {
                patch[patchptr2].value = chkp[li.chkp];
                break;
              }
            }
          }
          if (li.xrst) {
            // if xrst is applicable to current diff/ver
            if (!fixSupp(li,sDiff,sVer)) {
              if (chkp[li.xrst]) {
                x = chkp[li.xrst];
                loot[0] = x[0];
                loot[1] = x[1];
                loot[2] = x[2];
              } else {
                patch[patchptr++] = x = new Object;
                x.seq = i;
                x.id = li.xrst;
                x.waiting = true;
                npatches++;
              }
            }
          }
          if ((li.patchend == patchid) && !fixSupp(li,sDiff,sVer)) break;
          if (li.nop) continue;
          if (li.total) {
            if (li.total == "display") {
              totalseq=i;
              li.innerText = (totalLoot == "") ? (totalLoot=fixLootFormat(loot)) : totalLoot;
            } else { // set
              if (!fixSupp(li,sDiff,sVer)) totalLoot = fixLootFormat(loot);
            }
          } else fixLoot2(li,loot,sDiff,sVer); 
        }
        if (totalseq>-1) l[totalseq].innerText = (totalLoot == "") ? (totalLoot=fixLootFormat(loot)) : totalLoot;
      }
    } else {
      if (l.total) l.innerText = "no";
      else fixLoot2(l,loot,sDiff,sVer); 
    }
  }
  // loot is an array, and is passed by reference
  function fixLoot2(li,loot,sDiff,sVer) {
    if (li.originalInnerText) {
      var lii = li.originalInnerText;
    } else {
      var lii = li.innerText;
      li.originalInnerText = lii;
    }
    var liis = lii.split(";"); // split into the three types
    var mi=li.copies;

    if (fixSupp(li,sDiff,sVer)) {
      if (mi) {
        li.innerText = mi + "*" + fixLootFormat2(liis[0],liis[1],liis[2],mi*(parseInt(liis[0])+parseInt(liis[1])+parseInt(liis[2]))) + fixSuppStr(li,sDiff,sVer);
      } else {
        li.innerText = fixLootFormat2(liis[0],liis[1],liis[2],parseInt(liis[0])+parseInt(liis[1])+parseInt(liis[2])) + fixSuppStr(li,sDiff,sVer);
      }
    } else {
      var i;
      if (mi) {
        for (i=0; i<3; i++) loot[i] += parseInt(liis[i]) * mi;
        li.innerText = "+" + mi + "*{" + lii + "}, total " + fixLootFormat(loot) + fixSuppStr(li,sDiff,sVer);
      } else {
        for (i=0; i<3; i++) loot[i] += parseInt(liis[i]);
        li.innerText = "+{" + lii + "}, total " + fixLootFormat(loot) + fixSuppStr(li,sDiff,sVer);
      }
    }
  }
  function fixLootFormat(v) {
    // formats the loot array into "(x;y;z)=l"
    // return "{" + v[0] + ";" + v[1] + ";" + v[2] + "}=" + (parseInt(v[0])+parseInt(v[1])+parseInt(v[2]));
    return fixLootFormat2(v[0],v[1],v[2],v[0]+v[1]+v[2]);
  }
  function fixLootFormat2(v0,v1,v2,vall) {
    // formats the loot array into "(x;y;z) or l"
    return "{" + v0 + ";" + v1 + ";" + v2 + "} or " + vall;
  }
  function fixSupp(li,sDiff,sVer) {
    if (suppAux > 0) {
      if (li.suppAux1) {
        if (suppAux1Test(li.suppAux1)) return true;
      }
      if (suppAux > 1 && (li.suppAux1) && suppAux2Test(li.suppAux2)) return true;
    }
    // returns true if li is not applicable to current diff/ver
    return ((li.only)?(li.only.indexOf(sDiff) == -1):false) || ((li.ver)?(li.ver != sVer):false);
  }

  function fixSuppStr(li,sDiff,sVer) {
    var only3=li.only;
    var x="";
    if (only3) {
      if (only3.length == 2) only3 = only3.substr(0,1) + "/" + only3.substr(1,1); // insert separator
      x= ", diff " + only3 + " only";
    } 
    if (li.ver) {
      x+=", " + li.ver + " only";
    }
    if (suppAux > 0) {
      if (li.suppAux1) {
        x+=", " + suppAux1Str(li.suppAux1);
      }
    }
    if (suppAux > 1 && (li.suppAux2)) x+= ", " + suppAux2Str(li.suppAux2);

    return x;
  }
  
  
  function fixPick(iDiff,iVer) {
    var l=spPick;
    var imax = l.length;
    var sDiff = "NHE".substr(iDiff,1);
    var sVer = iVer?"TG":"TDP"; // variable length
    var pick = 0;
    var li;
    var chkp = new Array();
    if (imax) for (var i = 0; i < imax; i++) {
      li = l[i];
      if (li.chkp) chkp[li.chkp] = pick;
      if (li.xrst) pick = chkp[li.xrst];
      else pick=fixPick2(li,pick,sDiff,sVer);
    } else pick=fixPick2(l,0,sDiff,sVer);
  }

  function fixPick2(li,pick,sDiff,sVer) {
    if (li.total == "display") {
      li.innerText = (pick ? (pick==1?"one pick":pick + " picks") : "no pickpockets");
      return pick;
    }
    if (li.originalInnerText) {
      var lii = li.originalInnerText;
    } else {
      var lii = li.innerText;
      li.originalInnerText = lii;
    }
    var mi=li.copies;
    if (fixSupp(li,sDiff,sVer)) {
      if (mi) {
        li.innerText = lii + " (" + mi + " picks" + fixSuppStr(li,sDiff,sVer) + ")";
      } else {
        li.innerText = lii + " (one pick" + fixSuppStr(li,sDiff,sVer) + ")";
      }
    } else {
      var mi=li.copies;
      if (mi) {
        mi = parseInt(mi);
        var liit = lii + " (pick";
        for (var j=1; j<mi; j++) liit += (" #" + ++pick + ","); // loop mi-1 times
        li.innerText = liit + " #" + ++pick + fixSuppStr(li,sDiff,sVer) + ")";
      } else {
        li.innerText = lii + " (pick #" + ++pick + fixSuppStr(li,sDiff,sVer) + ")";
      }
    }
    return pick;
  }

function diffChange() {
  // var iDiff=seDiff.options(seDiff.selectedIndex).value;
  var iDiff=seDiff.selectedIndex;
  var iVer=1;
  if (document.all) {
    if (document.all.seVer) iVer=seVer.selectedIndex;
    if (document.all.spLoot) fixLoot(iDiff, iVer); 
    if (document.all.spPick) fixPick(iDiff, iVer);
  }
}

if (document.all) {
  if (typeof(suppAux) == "undefined") suppAux = 0;
  if (document.all.seDiff) seDiff.selectedIndex = seDiff.options.length - 1;
  if (document.all.seVer) seVer.selectedIndex = seVer.options.length - 1;
  diffChange();
}


