window.onload = function () {
  if (document.getElementById) {
    if (location.href.indexOf("locedit") > 0) {
      // Edit page events
      mtable = document.getElementById("machines");

      for (var i = 0; i < mtable.rows.length; ++i)
        versionChanged(mtable.rows[i].cells[0].firstChild);
    }
    else {
      // Index page events
      document.getElementById("locview").onclick = viewLoc;
      document.getElementById("state").onchange = locChange;
    }
  }
}

function viewLoc(e) {
  locview = document.getElementById("state").value;
  if (!locview)
    return false;
  else
    return true;
}

function locChange(e) {
  selectedloc = document.getElementById("state").value;
  if (selectedloc.length == 3)
    sort = "cityasc";
  else
    sort = "mixnew";

  sortlist = document.getElementById("sort");

  for (var i = 0; i < sortlist.options.length; i++)
    if (sortlist.options[i].value == sort)
      sortlist.options[i].selected = "selected";
}

function addMachine() {
  if (document.getElementById) {
    mtable = document.getElementById("machines");
    for (var i = 0; i < mtable.childNodes.length; i++) {
      if (mtable.childNodes[i].nodeName == "TR")
        break;
    }
    newrow = mtable.childNodes[i].cloneNode(true);
    mtable.appendChild(newrow);
    return false;
  }
  else
    return true;
}

function removeMachine() {
  if (document.getElementById) {
    mtable = document.getElementById("machines");
    found = 0;
    for (var i = 0; i < mtable.childNodes.length; i++) {
      if (mtable.childNodes[i].nodeName == "TR") {
        drop = i;
        ++found;
      }
    }
    if (found > 1)
      mtable.removeChild(mtable.childNodes[drop]);
    return false;
  }
  else
    return true;
}

function versionChanged(x) {
  if (x.value == -1) {
    document.location.href = "http://www.pumpituppro.com/forum/viewforum.php?f=2";
    return false;
  }
  else if (x.value == -2) {
    document.location.href = "http://www.mxpumpers.net/";
    return false;
  }
  else if (x.value == -3) {
  	document.location.href = "http://www.ddrfreak.com/locations/locations.php";
  	return false;
  }
  place = x.parentNode.parentNode.cells[1];
  currenttype = place.childNodes[2].value;
  for (var i = 0; i < place.childNodes[1].childNodes.length; ++i) {
    if (place.childNodes[1].childNodes[i].nodeName == "IMG") {
      current = place.childNodes[1].childNodes[i];
      if (current.src.indexOf("sd") > 0) {
        if (x.value != "") changeOpacity(current,100);
        else changeOpacity(current,20);
      }
      else if (current.src.indexOf("dx") > 0) {
        if (x.value >= 3) changeOpacity(current,100);
        else changeOpacity(current,20);
      }
      else if (current.src.indexOf("sx") > 0) {
        if ((x.value >= 10) && (x.value != 11)) changeOpacity(current,100);
        else changeOpacity(current,20);
      }
      else if (current.src.indexOf("gx") > 0) {
        if (x.value >= 13) changeOpacity(current,100);
        else changeOpacity(current,20);
      }
      else if (current.src.indexOf("fx") > 0) {
        if (x.value >= 18) changeOpacity(current,100);
        else changeOpacity(current,20);
      }
      else if (current.src.indexOf("na") > 0) {
        if (x.value >= 3) changeOpacity(current,100);
        else changeOpacity(current,20);
      }
    }
  }

  if ((x.value >= 3) && (currenttype == "DX")) changeType(current,"dx");
  else if (((x.value >= 10) && (x.value != 11)) && (currenttype == "SX")) changeType(current,"sx");
  else if ((x.value >= 13) && (currenttype == "GX")) changeType(current,"gx");
  else if ((x.value >= 18) && (currenttype == "FX")) changeType(current,"fx");
  else if ((x.value >= 3) && (currenttype == "NA")) changeType(current,"na");
  else if ((x.value != "") && (currenttype == "SD")) changeType(current,"sd");
  else changeType(current,"none");
}


function changeOpacity(node,percent) {
  ie = (document.all) ? 1 : 0;
  setWhere = (ie) ? "filter" : "opacity";
  setPercent = (ie) ? "alpha(opacity=" + percent + ")" : percent/100;
  node.style[setWhere] = setPercent;
}

function showTypeSelect(x) {
  if (x.nextSibling.style.display == "block")
    x.nextSibling.style.display = "none";
  else
    x.nextSibling.style.display = "block";
}

function changeType(x,t) {
  if (x == "")
    curVer = "";
  else
    curVer = x.parentNode.parentNode.parentNode.cells[0].childNodes[0].value;

  if ((curVer == "") && (t != "none")) alert("You must select a version first.");
  else if ((curVer < 3) && (t == "dx")) alert("You cannot choose the DX machine for this version.");
  else if (((curVer < 10) || (curVer == 11)) && (t == "sx")) alert("You cannot choose the SX machine for this version.");
  else if ((curVer < 13) && (t == "gx")) alert("You cannot choose the GX machine for this version.");
  else if ((curVer < 18) && (t == "fx")) alert("You cannot choose the FX machine for this version.");
  else if ((curVer < 3) && (t == "na")) alert("You must choose SD for this version.");
  else {
    x.parentNode.style.display = "none";
    if (t == "none") {
      x.parentNode.previousSibling.innerHTML = "(Select a type)";
      x.parentNode.nextSibling.value = "";
    }
    else if (t == "na") {
      x.parentNode.previousSibling.innerHTML = "Unknown";
      x.parentNode.nextSibling.value = t.toUpperCase();
    }
    else {
      x.parentNode.previousSibling.innerHTML = "Type: " + t.toUpperCase();
      x.parentNode.nextSibling.value = t.toUpperCase();
    }

    for (i = 0; i < x.parentNode.childNodes.length; i++) {
      if (x.parentNode.childNodes[i].nodeName == "IMG") {
        if (x.parentNode.childNodes[i].src.indexOf(t) > 0)
          x.parentNode.childNodes[i].style.borderColor = "#006699";
        else
          x.parentNode.childNodes[i].style.borderColor = "#ffffff";
      }
    }
  }
}
