function hoverImage(img, state) {
    if (img.src.indexOf("_hover") == -1)
      img.src = img.src.substring(0, img.src.lastIndexOf(".")) + "_hover" + img.src.substring(img.src.lastIndexOf("."));
    else if (img.src != "")
      img.src = img.src.replace("_hover", "");
}

function hoverMsg(msg, img) {
  if (!msg)
    msg = '';

  document.getElementById('msgBar').innerHTML = msg;
}

function processLogin() {
  document.getElementById('password').value = hex_md5(document.getElementById('p').value + hex_md5(document.getElementById('p').value));
  document.getElementById('password').name = 'password';
  document.getElementById('p').value = '';
  document.getElementById('p').name = 'p';
}

function showNewsItem(id) {
  document.getElementById("article_" + id).style.display = "block";
}

function edit(cmd, val) {
  s.select();

  if (cmd == "FontSize" && !val)
    fontSize(event.x, event.y);
  else if (cmd == "ForeColor" && !val)
    fontColor(event.x, event.y);
  else if (cmd == "InsertImage" && !val)
    imageUpload(event.x, event.y);
  else
    document.execCommand(cmd, false, val);
}

function fontSize(x,y) {
  if (document.getElementById('fontSize'))
    document.body.removeChild(document.getElementById('fontSize'));

  var fs = document.createElement('div');
  fs.id = 'fontSize';
  fs.style.cssText = 'position: absolute; text-align: center;';
  fs.style.top = y + document.body.scrollTop;
  fs.className = 'decorative';
  var html = '<font class="smaller">Font Size:</font><br>\n';
  html += '<select style="margin: 2px;" onclick="s.select()"  onchange="edit(\'FontSize\',this.value);">\n';
  html += '  <option value="">\n';
  html += '  <option value="1">8\n';
  html += '  <option value="2">12\n';
  html += '  <option value="3">18\n';
  html += '  <option value="4">24\n';
  html += '  <option value="5">36\n';
  html += '  <option value="6">48\n';
  html += '  <option value="7">72\n';
  html += '</select><br><br>\n';
  html += '<input type="button" value="Done" onclick="s.select(); this.parentNode.style.display = \'none\';" class="decorative smaller" style="padding: 0px;">';

  fs.innerHTML = html;

  document.body.appendChild(fs);

  fs.style.left = x - fs.offsetWidth / 2;
}

var s;

function fontColor(x,y) {
  if (document.getElementById('fontColor'))
    document.body.removeChild(document.getElementById('fontColor'));

  var r = 0;
  var g = 0;
  var b = 0;

  var fs = document.createElement('div');
  fs.id = 'fontColor';
  fs.style.cssText = 'position: absolute; text-align: center;';
  fs.style.top = y + document.body.scrollTop;
  fs.className = 'decorative';

  var html = '<font class="smaller">Font Color:<br><font class="smaller">Click to lock color</font></font><br><br>\n';
  while (r < 255) {
    html += '<div style="float:left; width: 65px; margin-right: -4px;">'
    while (g < 255) {
      while (b < 255) {
        var color = RGBtoHex(r)+RGBtoHex(g)+RGBtoHex(b);
        html += '<div style="float:left; width:10; height:10; margin-right: 1px; overflow: hidden; background-color: #'+color+'; cursor: pointer;" onmouseover="s.select(); edit(\'ForeColor\', \'#'+color+'\'); document.selection.empty();"  onclick="s.select(); this.parentNode.parentNode.style.display = \'none\';"></div>';
        b += 51;
      }
      b = 0;
      g += 51;
    }
    b = 0;
    g = 0;
    r += 51;

    html += '</div>';
  }

  fs.innerHTML = html;

  document.body.appendChild(fs);

  fs.style.left = x - fs.offsetWidth / 2;

}

function imageUpload(x,y) {
  if (document.getElementById('upload'))
    document.body.removeChild(document.getElementById('upload'));

  var fs = document.createElement('div');
  fs.id = 'upload';
  fs.style.cssText = 'position: absolute; text-align: center;';
  fs.style.top = y + document.body.scrollTop;
  fs.className = 'decorative';
  var html = '<font class="smaller">Please select or upload an image:</font><br>\n';
  html += '<table>\n';
  html += '<tr><td class="smaller bold">New Image</td><td class="smaller bold">Existing Image</td></tr>\n';
  html += '<tr><td class="smaller">\n';
  html += 'File<br><input type="file" name="image"><br><br>\n';
  html += '</td>\n';
  html += '<td>\n';
  html += '<iframe></iframe>\n';
  html += '</td>\n';
  html += '</tr>\n';
  html += '</table>\n';
  html += '<input type="button" value="Upload" onclick="this.parentElement.style.display = \'none\';" class="decorative smaller" style="padding: 0px;">';

  fs.innerHTML = html;

  document.body.appendChild(fs);

  fs.style.left = x - fs.offsetWidth / 2;
}

function silentHandler(sMsg,sUrl,sLine) {
   alert("An error was thrown and caught.\nError: " + sMsg + "\nLine: " + sLine + "\nURL: " + sUrl);
   return true;
}

function addChild(parent, color) {
  window.onerror = silentHandler;

  var html = '';
  var ID = generateID();
  color = (color * 1) - 70707;

  if (color < 100000) {
    alert("The menu hierarchy has reached maximum depth, you may not add any further levels.");
    return;
  }

  html += '                        <div id="'+ID+'" style="background-color: #'+color+'; margin-left: 15;" onmouseover="this.style.backgroundColor = \'#\' + ((this.style.backgroundColor.replace(\'#\',\'\') * 1) + 90909)" onmouseout="this.style.backgroundColor = \'#\' + ((this.style.backgroundColor.replace(\'#\',\'\') * 1) - 90909)">\n';
  html += '                          <div style="width: 25px; padding: 0 2 0 2; border: 1px solid #000000; background: #C0C0C0; float: left;" align="center">\n';
  html += '                            <img src="images/icons_menu_remove.jpg" onclick="remove(\''+ID+'\');" onmouseover="hoverImage(this)" onmouseout="hoverImage(this)" alt="Delete item and its contents">\n';
  html += '                            <img src="images/icons_menu_add.jpg" onclick="addChild(\''+ID+'\', \''+color+'\');" onmouseover="hoverImage(this)" onmouseout="hoverImage(this)" alt="Add new child">\n';
  html += '                          </div>\n';
  html += '                          <input type="text" name="'+ID+'_title" style="width: 100%; border: 1px ridge #'+color+';" class="emphasis invisible"><br>\n';
  html += '                          <input type="text" name="'+ID+'_description" style="width: 100%; border: 1px ridge #'+color+';" class="invisible">\n';
  html += '                        </div>\n';

  document.getElementById(parent).innerHTML += html;
  //document.getElementById(parent).innerText = document.getElementById(parent).innerHTML;
}

function remove(item) {
  if (confirm("Are you sure you want to delete this item and all of its contents?")) {
    document.getElementById(item).style.display="none";
    document.getElementById(item).innerHTML = '';
  }
}

function generateID() {
  var key = "";

  for(i = 0; i < 9; i++) {
    key += Math.floor(Math.random()*10);
  }

  return key;
}

function toUnicode(txt) {
  var chars = new Array();

  for (i = 0; i < txt.length; i++)
    chars[i] = "&#" + txt.charCodeAt(i) + ";";

  return chars.join("");
}

// Converts an RGB number (0 - 255) to Hex (00 - FF)
function RGBtoHex(n) {
  if (n > 255)
    n = 255;
  if (n < 0)
    n = 0;

  var hexb = n % 16;
  var hexa = (n - hexb) / 16;

  if (hexa > 9)
    hexa = String.fromCharCode(hexa + 87);
  if (hexb > 9)
    hexb = String.fromCharCode(hexb + 87);

  return hexa + "" + hexb;
}
