/*
if (top!=self) {
  window.location = "UseLink.html";
}
*/
var ff = 0;
var frame = 0;
var path = window.location.pathname.split('/');
var page = path[2];
if (window.innerWidth) { // Non-IE
  var fullwidth = window.innerWidth;
}
else if (document.documentElement.clientWidth) { // IE 6+ in 'standards compliant mode'
  var fullwidth = document.documentElement.clientWidth;
}
else if (document.body.clientWidth) { // IE 4 compatible
  var fullwidth = document.body.clientWidth;
}

// Page-load changes if JS is enabled
function pageload() {
  if (ff) {
    document.removeEventListener("DOMContentLoaded", pageload, false);
  }
  // Set menu to click-only, rather than no-JS hover-based (remove the :hover rule)
  if (document.getElementsByTagName('style')) {
    var styletag = document.getElementsByTagName('style').item(0);
    var styletext = "#navcontainer .submenu { display: none; }";
    try {
      styletag.innerHTML = styletext;
    }
    catch (error) { // IE fix
      styletag.styleSheet.cssText = styletext;
    }
  }
  // Page-specific changes
  switch (page) {
    case "NDDC.php":
      // If checkallbox exists, there is no text input, so don't need selectall onclick overwriting this
      if (document.getElementById('checkallbox')) {
        document.getElementById('jshidden').style.display = 'none';
        document.getElementById('checkallbox').onclick = checkall;
        displayClock();
        return;
      }
      break;
    case "Extras.php":
      document.getElementById('jshidden').style.display = 'none';
      submitcheck();
      break;
    case "Stats.php":
      if (document.getElementsByTagName('select').item(0)) {
        disableselect();
        displayswitch();
        displayClock();
        return; // Kills the click-to-highlight, but allows the display toggle to work
      }
      break;
    case "Sig.php":
      if (document.getElementById('jshidden')) {
        document.getElementById('jshidden').style.display = 'none';
        var paletteinfo = document.getElementsByName('paletteinfo');
        for (var i=0; i<paletteinfo.length; i++) {
          paletteinfo.item(i).innerHTML = "<br>&nbsp;(Click the palette for presets)";
        }
        styleswitch();
        sigoptions();
        displayClock();
        // Values for color selection functions; can't be defined until DOM is loaded
        window.scrollbar = scrollBarWidth;
        window.imgmap = document.getElementById('colormap_back');
        window.color = document.getElementsByName('color').item(0);
        document.getElementById('colormap_form').onsubmit = hidemap;
        return; // There is text input, but sigoptions are more important; don't want selectall onclick.
      }
    case "Trackers.php":
      if (document.getElementsByTagName('select').item(0) && document.getElementsByTagName('select').item(0).getAttribute('name')=="action") {
        var select = document.getElementsByTagName('select').item(0);
        select.options[0].disabled = true;
        select.options[0].style.color = "graytext";
        var form = document.getElementsByTagName('form').item(0);
        form.onsubmit = nosubmit;
      }
      // If tztoggle exists, there is no text input, so don't need selectall onclick overwriting this
          // Technically there is text input now, but oh well.  tztoggle is more important.
      else if (document.getElementById('tztoggle')) {
        tztoggler();
        document.getElementById('tztoggle').onclick = tztoggler;
        // Check for influence's target column, and disable the text boxes until track column is checked
        var inputs = document.getElementsByTagName('form').item(1).getElementsByTagName('input');
        window.eggcodes = new Array();
          eggcodes['update'] = new Array();
          eggcodes['target'] = new Array();
        var n = 0;
        for (var i=0; i<inputs.length; i++) {
          if (inputs.item(i).getAttribute('type')=="text") {
            inputs.item(i).disabled = true;
            eggcodes['update'][n] = inputs.item(i-1);
            eggcodes['target'][n] = inputs.item(i);
            n++;
            inputs.item(i-1).onchange = noeggs;
          }
        }
/*
  if the page has a text input field and/or if it has input fields named 'target[*]'
    disable the 'target[*]' boxes
    set all 'update[*]' checkboxes to track onchange/check-uncheck

if (eggcodes) {
  for (i=0; i<eggcodes['update'].length; i++) {
    eggcodes['target'][i].disabled = true;
    eggcodes['update'][i].onchange = noeggs;
  }
}

  if a checkbox is changed
    if it's now checked, enable the matching 'target[*]' box
    else, disable the matching 'target[*]' box

var els = this.form.elements;
var x, nextEl;
for (var i=0; i<els.length; i++){
x = els[i];
if (el == x && (nextEl = els.item(i+1))){

*/
/* if (target/text) { *
        var eggcodes = document.getElementsByName('update'.'*');
        for (e in eggcodes) {
          if (e.value=="add") {
            target = disabled;
            e.onchange = noeggs;
          }
        }
* } */
        displayClock();
        /* if (document.getElementById('frzdelete')) {
          document.getElementById('frzdelete').onsubmit = confirmdelete;
        } */
        return;
      }
      break;
    case "Alarm.php":
      unhidealert();
      addtimes();
      break;
    case "Clock.php":
      document.getElementById('noclock').style.display = 'none';
      break;
  }
  displayClock();
  selectcheck();
}

// Analog Clock (http://javascript.about.com/library/blclock1.htm)
// copyright Stephen Chapman, 19th November 2005, 28th September 2008
// you may copy this clock provided that you retain the copyright notice

// Clock Parameters (dctime and dst in NDER2.js.php)
var clocksize = 140;
var mindivsize = 110;
var usedwidth = 550;
var availwidth = (fullwidth - usedwidth) / 2;
if (availwidth >= mindivsize) {
  var cshow = 1;
  if (availwidth < clocksize && availwidth >= 120) {
    var cwidth = Math.floor(availwidth / 10) * 10;
    clocksize = cwidth;
  }
  else if (availwidth < 120) clocksize = 120;
}
var background = 'images/Clockbg.gif';
var bgwidth = '100px';
var bgheight = '100px';
var colnumbers = '000000';
var colseconds = 'FF0000';
var colminutes = '088130'; // 2FB157
var colhours = '0171BB';
var numstyle = 0;
var font_family = 'helvetica,arial,sans-serif';
var localZone = 0; // If reset to 1, remove updateClock()-parenthetical, "start", and "dctime" stuff.
var mytimezone = -5;
var start = 1;
var city = 'Dragon Cave';
var country = 'Time';
var fix = 0; // If reset to 1, probably remove positionClock() and all availwidth-related stuff.
var xpos = 0;
var ypos = 0;
if (page=="Clock.php") {
  var cshow = 1;
  var clocksize = 225;
  var availwidth = (fullwidth * 0.8) - 8; // Width of div.inner
}

// Clock Functionality
var dayname = new Array ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
var am = 'AM'; var pm = 'PM';
var pi = Math.PI;
var d = document;
var pi2 = pi/2;
var rad = (+clocksize) / 2;
var ctrX = (+xpos) + rad;
var ctrY = (+ypos) + rad;
var hourln = 1;
var minln = secln = 8;
for(var i = 0; i < (rad / 2) + (rad / 16); i++) {hourln += 1;}
for(var i = 0; i < (rad / 2) - (rad / 8); i++) {minln += 2;secln += 2;}
var font_size = rad / 4;
var offset = 16; 
var clocknum = [[,1,2,3,4,5,6,7,8,9,10,11,12],[,'I','II','III','IIII','V','VI','VII','VIII','IX','X','XI','XII'],[,'·','·','-','·','·','<span style="font-size:60%">|</span>','·','·','-','·','·','<span style="font-size:60%">||</span>']];
if (numstyle < 0 || numstyle > 2) numstyle = 0;

function displayClock() {
  if (!d.getElementById) return;
  positionClock();
  var ctx = document.createElement('div');
  if (fix) {
    ctx.style.position = 'relative';
    ctx.style.margin = 'auto';
    ctx.style.width = (clocksize + offset * 2) + 'px';
    ctx.style.height = (clocksize + offset * 2) + 'px';
    ctx.style.overflow = 'visible';
  }
  var cn = [];
  for (var i = 12; i > 0; i--) {
    cn[i] = document.createElement('div');
    cn[i].id = 'cnum' + i;
    commonClock(cn[i]);
    cn[i].style.width = (offset * 2) + 'px';
    cn[i].style.height = (offset * 2) + 'px';
    cn[i].style.fontFamily = font_family;
    cn[i].style.fontSize = font_size + 'px';
    cn[i].style.color = '#' + colnumbers;
    cn[i].style.textAlign = 'center';
    cn[i].style.paddingTop = '5px';
    cn[i].style.zIndex = 6;
    cn[i].innerHTML = clocknum[numstyle][i];
    ctx.appendChild(cn[i]);
  }
  var hr = [];
  for (i = hourln; i > 0; i--) {
    hr[i] = document.createElement('div');
    hr[i].id = 'chour' + i;
    commonClock(hr[i]);
    hr[i].style.width = '3px';
    hr[i].style.height = '3px';
    hr[i].style.fontSize = '3px';
    hr[i].style.backgroundColor = '#' + colhours;
    hr[i].style.zIndex = 7;
    ctx.appendChild(hr[i]);
  }
  var mn = [];
  for (i = minln; i > 0; i--) {
    mn[i] = document.createElement('div');
    mn[i].id = 'cmin' + i;
    commonClock(mn[i]);
    mn[i].style.width = '2px';
    mn[i].style.height = '2px';
    mn[i].style.fontSize = '2px';
    mn[i].style.backgroundColor = '#' + colminutes;
    mn[i].style.zIndex = 8;
    ctx.appendChild(mn[i]);
  }
  var sc = [];
  for (i = secln; i > 0; i--) {
    sc[i] = document.createElement('div');
    sc[i].id = 'csec' + i;
    commonClock(sc[i]);
    sc[i].style.width = '1px';
    sc[i].style.height = '1px';
    sc[i].style.fontSize ='1px';
    sc[i].style.backgroundColor = '#' + colseconds;
    sc[i].style.zIndex = 9;
    ctx.appendChild(sc[i]);
  }
  var am = document.createElement('div');
  am.id = 'ampm';
  commonClock(am);
  am.style.width = ((xpos + rad) * 2) + 'px';
  am.style.fontFamily = font_family;
  am.style.fontSize = (font_size * 2 / 3) + 'px';
  am.style.color = '#' + colnumbers;
  am.style.textAlign = 'center';
  am.style.paddingTop = '5px';
  am.style.zIndex = 5;
  ctx.appendChild(am);
  var zn = document.createElement('div');
  zn.id = 'zone';
  commonClock(zn);
  zn.style.width = ((xpos + rad) * 2) + 'px';
  zn.style.fontFamily = font_family;
  zn.style.fontSize = (font_size * 2 / 3) + 'px';
  zn.style.color = '#' + colnumbers;
  zn.style.textAlign = 'center';
  zn.style.paddingTop = '5px';
  zn.style.zIndex = 5;
  ctx.appendChild(zn);
  d.getElementById('clock_a').appendChild(ctx);
  for (var i = 12; i > 0; i--) {
    d.getElementById('cnum' + i).style.top = (ctrY - offset + rad * Math.sin(i * pi / 6 - pi2))+'px';
    d.getElementById('cnum' + i).style.left = (ctrX - offset + rad * Math.cos(i * pi / 6 - pi2))+'px';
    d.getElementById('cnum' + i).style.visibility = 'visible';
  }
  updateClock();
}

function positionClock() {
  var cdiv = document.getElementById('clock');
  if (cshow) {
    cdiv.style.width = availwidth + 'px';
    var cmain = document.getElementById('clock_a');
    cmain.style.width = clocksize + 'px';
    if (page!='Clock.php') {
      var bg = document.createElement('img');
      bg.src = background;
      bg.style.position = 'relative';
      bg.style.top = -9 + 'px';
      bg.style.width = clocksize + 14 + 'px';
      bg.style.height = clocksize + 19 + 'px';
      bg.style.zIndex = 4;
      cdiv.appendChild(bg);
      var curl = document.getElementById('clock_u');
      curl.style.background = 'url(' + background + ') -' + bgheight + ' -' + bgwidth + ' no-repeat';
      curl.style.width = clocksize + 14 + 'px';
      curl.style.height = clocksize + 19 + 'px';
      curl.style.zIndex = 10;
    }
    if (availwidth < 120) {
      var adjust = (clocksize - availwidth) / 2;
      cdiv.style.right = adjust + 'px';
    }
  }
  else cdiv.style.display = 'none';
}

function commonClock(n) {
  n.style.position = 'absolute';
  n.style.top = '0';
  n.style.left = '0';
  n.style.visibility = 'hidden';
}

function updateClock() {
  if (!start) dctime += 250;
  else start = 0;
  var now = new Date(dctime);
  var theTime = timeZone(now,localZone,mytimezone,dst);
  d.getElementById('ampm').style.top = (ypos + rad/3)+'px';
  d.getElementById('ampm').innerHTML = theTime[1] + '<br />' + dayname[theTime[0]];
  d.getElementById('ampm').style.visibility = 'visible';
  if (!localZone) {
    d.getElementById('zone').style.top = (ctrY + (rad/10))+'px';
    d.getElementById('zone').innerHTML = city + '<br />' + country;
    d.getElementById('zone').style.visibility = 'visible';
  }
  moveClock(secln, 'csec', pi * theTime[4] / 30 - pi2);
  moveClock(minln, 'cmin', pi * theTime[3] / 30 - pi2);
  moveClock(hourln, 'chour', pi * theTime[2] / 6 + pi * (+now.getMinutes())/360 - pi2);
  setTimeout('updateClock()', 250);
}

function timeZone(now,loc,mtz,dst) {
  if (loc) {
    var dow = now.getDay();
    var second = now.getSeconds();
    var minute = now.getMinutes();
    var hour = now.getHours();
  }
  else {
    now.setUTCMinutes(now.getUTCMinutes() + (mtz + dst)*60);
    var dow = now.getUTCDay();
    var second = now.getUTCSeconds();
    var minute = now.getUTCMinutes();
    var hour = now.getUTCHours();
  }
  if (hour > 11) {moa = pm; hour -= 12;}
  else moa = am;
  return [dow,moa,hour,minute,second];
}

function moveClock(l, e, f) {
  for (var i = l; i > 0; i--) {
    d.getElementById(e + i).style.top = (ctrY + i * Math.sin(f))+'px';
    d.getElementById(e + i).style.left = (ctrX + i * Math.cos(f))+'px';
    d.getElementById(e + i).style.visibility = 'visible';
  }
}

// Determine which HTML element was clicked
function getelement(e) { // Formerly "function clicked"
  if (!e) { // Get the event info (param for FF, !e for IE)
    e = window.event;
  }
  if (e.target) { // Get the element
    var elem = e.target;
  }
  else if (e.srcElement) {
    var elem = e.srcElement;
  }
  if (elem.nodeType==3) { // Defeat Safari bug (3 is the text within an element rather than the element)
    elem = elem.parentNode;
  }
  return elem;
}

// Check if the element is part of a submenu
function parent(elem) {
  var par2 = elem.parentNode.parentNode; // submenu link, sublist non-link (a > li, span > li)
  if (par2) {
    if (par2.attributes) {
      var par2c = par2.attributes.getNamedItem('class');
      if (par2c && (par2c.value=="submenu" || par2c.value=="sublist")) {
        return 0;
      }
    }
    var par3 = par2.parentNode; // sublist link (span > a > li)
    if (par3 && par3.attributes) {
      par3 = par3.attributes.getNamedItem('class');
      if (par3 && par3.value=="sublist") {
        return 0;
      }
    }
  }
  return 1; // safe to toggle submenus
}

// Submenu toggling
var menu = new Array();
  menu['E'] = new Array();
    menu['E']['toggle'] = "c";
    menu['E']['block'] = "n";
  menu['H'] = new Array();
    menu['H']['toggle'] = "c";
    menu['H']['block'] = "n";

function toggler(a) {
  var ftoggle = menu[a]['toggle'];
  if (ftoggle=="c") {
    menu[a]['toggle'] = "o";
  }
  else {
    menu[a]['toggle'] = "c";
  }
  return false;
}

function submenus() {
  var fmenu = menu;
  for (a in fmenu) {
    fmenu[a]['submenu'] = document.getElementById('submenu'+a);
    fmenu[a]['plus'] = document.getElementById('plus'+a);

    if (fmenu[a]['toggle']=="o" && fmenu[a]['block']=="n") {
      fmenu[a]['submenu'].style.display = "block";
      fmenu[a]['plus'].innerHTML = '<u>-</u>';
      menu[a]['block'] = "y";
    }
    else {
      fmenu[a]['submenu'].style.display = "none";
      fmenu[a]['plus'].innerHTML = '<u>+</u>';
      menu[a]['toggle'] = "c";
      menu[a]['block'] = "n";
    }
  }
}

// Frame-busting check; back button causes problems
// BE CAREFUL: The function call may be coming from an iframe (parent.func)
function frameUpdate(e) {

// FIX THIS STUFF, FRAME-BUSTING IS GONE
  return;

//alert("onclick registered\nCode: "+e.keyCode+"\nCtrl: "+e.ctrlKey+"\nCmd: "+e.metaKey); // Mac Cmd key?
  // Check for middle button click
  if ( !e.which && e.button && (e.button & 4) ) {
    e.which = 2;
  }
//alert(e.which);
  // Check for opening in a new tab via Ctrl+Click, Cmd+Click, Middle Click; window/focus-tab Shift+Click
  if (!e.ctrlKey && !e.metaKey && e.which != 2 && !e.shiftKey) {
    frame = 1;
  }
}

// Text highlighting
function selectcheck() {
  switch (page) {
    case "Suggest.php":
      return; // Could theoretically eliminate this line and it'd be returned by Bug's return.
    case "Bug.php":
      return;
    case "Help.php":
      var textareas = document.getElementsByTagName('textarea');
      for (var t=0; t<textareas.length; t++) {
        textareas.item(t).onclick = selectall;
      }
      return;
    default: // Previously didn't exist, and section was outside/after the switch.
      var inputs = document.getElementsByTagName('input');
      for (var i=0; i<inputs.length; i++) {
        inputs.item(i).onclick = selectall;
      }
  }
}

function selectall() {
  this.select();
}

function checkall() {
//alert("checkall");
  var checkallbox = document.getElementById('checkallbox');
  var elements = document.getElementsByTagName('form').item(0).elements;
  if (checkallbox.checked) {
    for (var e=0; e<elements.length; e++) {
      elements.item(e).checked = true;
    }
  }
  else {
    for (var e=0; e<elements.length; e++) {
      elements.item(e).checked = false;
    }
  }
}

// Extras input checks
/*
function submitcheck() {
  var forms = document.getElementsByTagName('form');
  for (var f=0; f<forms.length; f++) {
    forms.item(f).onsubmit = checkinput;
  }
}

function checkinput() {
  if (this.name=="user") {
    // check for input
    if (this['scroll'].value.length < 1) {
      alert("Please check that you entered the scroll name correctly.");
      return false;
    }
  }
  else {
    var dcode = this.elements[0].value;
    // check for minimum length
    var minLength = 4;
    if (dcode.length < minLength) {
      alert("Please check that you entered the code correctly.");
      return false;
    }
    // check for alphanumeric
    else if (!alphanumeric(dcode)) { 
      alert("Please check that you entered the code correctly.");
      return false 
    }
  }
  // send to redirect page
  this.action += " ";
  return true;
}

function alphanumeric(alphane) {
  for (var j=0; j<alphane.length; j++) {
    var alphaa = alphane.charAt(j);
    var hh = alphaa.charCodeAt(0);
    if ((hh>47 && hh<58) || (hh>64 && hh<91) || (hh>96 && hh<123)) {
    }
    else {
      return false;
    }
  }
  return true;
}
*/

// Scroll-stats' basic vs detail checks
function displayswitch() {
  var radios = document.getElementsByName('display');
  for (var r=0; r<2; r++) {
    radios.item(r).onclick = disableselect;
  }
}

function disableselect() {
  var radios = document.getElementsByName('display');
  var select = document.getElementsByTagName('select').item(0);
  for (var r=0; r<2; r++) {
    if (radios.item(r).value=="detail") {
      if (radios.item(r).checked) {
        select.disabled = false;
      }
      else {
        select.disabled = true;
      }
    }
  }
}

// Sig-banner customization options
function styleswitch() {
  var radios = document.getElementsByName('style');
  for (var r=0; r<3; r++) {
    radios.item(r).onclick = sigoptions;
  }
}

function sigoptions() {
  var radios = document.getElementsByName('style');
  var styles = new Array("aond", "eds", "ewbd");
  for (var r=0; r<3; r++) {
    if (radios.item(r).checked) {
      document.getElementById(styles[r]).style.display = "";
    }
    else {
      document.getElementById(styles[r]).style.display = "none";
    }
  }
}

// Action-trackers' features/checks
function tztoggler() {
  var rows = document.getElementsByTagName('tr');
  var first = 1;
  for (var r=0; r<rows.length; r++) {
    // Check for the jshidden rows
    if (rows.item(r).getAttribute('name')=="jshidden") {
      if (first==1) {
        if (rows.item(r).style.display=="none") {
          var d = "";
          var h = "";
          var p = "<u>-</u>";
        }
        else {
          var d = "none";
          var h = "hidden";
          var p = "<u>+</u>";
        }
        first = 0;
      }
      rows.item(r).style.display = d;
    }
    // If it's not jshidden, and "first" has already happened, all relevant rows have been covered.
    else if (first==0) {
      break;
    }
  }
  document.getElementById('jshidden2').style.visibility = h; // Leave some "padding" once all hidden
  document.getElementById('plusTZ').innerHTML = p;
}

function nosubmit() {
  var form = document.getElementsByTagName('form').item(0);
  if (!form.elements["scroll"].value) {
    return true;
  }
  var select = document.getElementsByTagName('select').item(0);
  if (select.selectedIndex==0) {
    alert("Please select an action.");
    return false;
  }
  return true;
}

function noeggs() {
  var feggcodes = eggcodes;
  for (var i=0; i<feggcodes['update'].length; i++) {
    if (this==feggcodes['update'][i]) {
      if (this.checked) {
        feggcodes['target'][i].disabled = false;
        break;
      }
      else {
        feggcodes['target'][i].disabled = true;
        break;
      }
    }
  }
}

/*
function confirmdelete() {
alert("hi!");
  return confirm("If deleted, the date(s) will be gone entirely.");
}
*/

// Unfog-alarm features
function unhidealert() {
  document.getElementById('hidden').id = 'shown';
  hiderowurl();
}

function hiderowurl() {
  var cbox = document.getElementsByName('alert').item(0);
  var row = document.getElementById('jshidden');
  if (cbox.checked==1) {
    row.style.display = "none";
  }
  else {
    row.style.display = "";
  }
}

function addtimes() {
  var curhr = document.getElementsByName('curhr').item(0);
  var curmin = document.getElementsByName('curmin').item(0);
  if (curhr.value=="") {
    var ctime = new Date();
    curhr.value = gttwe(ctime.getHours(), 'cur');
    curmin.value = ltten(ctime.getMinutes());
  }
}

function gttwe(i, w) {
  if (i>=12) {
    document.getElementsByName(w+'clock').item(0).selectedIndex = 1;
    if (i>12) {
      i -= 12;
    }
  }
  return i;
}

function ltten(i) {
  if (i<10) {
    i = "0" + i;
  }
  return i;
}

function jsalarm() {
  alert('It\'s time to unfog!');
}

function cancelalarm() {
  var millitime = new Date().getTime();
  var curtime = parseInt(millitime/1000);
  var diff = (curtime - loadtime);
  if (diff<reftime) { // && url doesn't match, maybe?
    return "The alarm will be turned off if you leave.";
  }
}

// Function calls (for events, var e=arguments[0] also works, in place of an anon-func param)
document.onclick = function(event) { // onclick does not exist/work for iframes
  var elem = getelement(event);
  if (elem.tagName.toLowerCase()=="area") { // Sig.php; image map click
    showhex(elem);
  }
  else {
    var tog = parent(elem);
    if (tog) {
      submenus();
    }
    if (page=="Alarm.php") {
      hiderowurl();
    }
  }
}

if (document.addEventListener) { // Mozilla, Opera, and webkit browsers
  document.addEventListener("DOMContentLoaded", pageload, false);
  ff = 1;
}
else if (document.attachEvent) { // IE (5+?)
  document.onreadystatechange = function() {
    if (this.readyState=="complete") {
      pageload();
    }
  }
}
else { // Fallback
  window.onload = pageload;
}

/*
window.onbeforeunload = function() {
  // Friendly frame-busting buster (AR.php, DC.php, ER.php, tablegrow.inc; viewdragons.inc, Viewer.js)
  if (frame) {
    frame = 0;
    return "DC is trying to hinder your clicking.  Do you wish to allow it?";
  }
}
*/

