kittyfluff / gaia bid verifier

// ==UserScript==
// @name gaia bid verifier
// @namespace kittyfluff
// @version 0.36
// @match *.gaiaonline.com/marketplace/userstore/*
// ==/UserScript==

// dynamically updating the "place bid"-button with users entered bid-value

var fields = document.getElementsByClassName("marketplaceInputField"),
    buttons = [],
    maxUserGold = 0,
    minBidIncr = document.getElementsByClassName("divpad2"),
    bidInfo = "",
    curBid = 0,
    reqBidSum = 0;
    bidIncr = 0;

if (minBidIncr.length) {
// only if bid increment is specified
// replace incr with total bid sum required
    
// can be dblclicked and just pasted into the box :D

  bidInfo = document.getElementById("transactionWindowInfoTopRight").innerHTML.split("<strong>");
  curBid = parseInt(bidInfo[3].split(/>/)[1].replace(/(,|\s.*)/g, ""), 10);
  bidIncr = parseInt(bidInfo[5].split(/>/)[1], 10); // above 0 if we are in here
  reqBidSum = curBid + bidIncr;
  minBidIncr[0].innerHTML = "Minimum bid now is " + reqBidSum + " gold";
  if (reqBidSum > 999) {
    minBidIncr[0].innerHTML += " (" + numToWord(String(reqBidSum)) + ")";
  }
}

if (fields.length === 1 && fields[0].name === "amount") { // verify if bid-page and not a shop or the confirmation page
    fields[0].addEventListener("keyup", verifyBidInput, false);

// fetch users current gold amount from header
    maxUserGold = parseInt(document.getElementById("go1d_amt").getElementsByTagName("span")[0].innerHTML.replace(/,/g, ""), 10);

// new styling needed for "place bid"-button since changing innerhtml is messing up the gradient background
    buttons = document.getElementsByTagName("button");
    buttons[0].innerHTML = "Place Bid";
    buttons[0].style.backgroundImage = "none";
    buttons[0].style.background = "#7f7f7f";
    buttons[0].style.color = "#ffffff";
    buttons[0].style.borderRadius = "5px";
    buttons[0].style.border = "1px solid #3f3f3f";
    buttons[0].style.paddingLeft = "5px";
}

function numToWord (str) {
  var numWords = ["thousand", "million", "billion", "trillion", "quadrillion", "quintillion"],
      decValue = 0,
      res = "",
      segments = str.replace(/\B(?=(\d{3})+(?!\d))/g, ",").split(",");
// adds numerical word to a value
  if (segments.length > 1) {
    decValue = segments.shift();
    if (decValue.length === 1 && ! segments[0].match(/0{3}/)) {
      decValue += "." + segments[0].replace(/0*$/, "");
    }
    if (! segments.join("").match(/^0*$/)) {
      decValue = "~" + decValue;
    }
    res = decValue + " " + numWords[(segments.length - 1)];
  }

  return res;
}

function verifyBidInput () {
  var curValue = 0,
      buttonMsg = "Place Bid";
// updating button with numeric word after each change to input field
  if (this.value.match(/^[^0]\d+$/)) {
    curValue = parseInt(this.value, 10);
    if (curValue > maxUserGold) {
      buttonMsg += " (Not Enough Gold)";
    }
    else if (curValue > 999) {
      buttonMsg += " (" + numToWord(this.value) + ")";
    }
  } // add word suffix only if valid positive number
  document.getElementsByTagName("button")[0].innerHTML = buttonMsg;
}