////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// global.js
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

String.format = function()
{
  var str = null;
  if (arguments.length > 0)
  {
    var str = arguments[0];
    for (var i = 1; i < arguments.length; i++)
    {
      var re = new RegExp('\\{' + (i-1) + '\\}', 'gm');
      str = str.replace(re, arguments[i]);
    }
  }
  return str;
}
String.prototype.stripTags = function()
{
  var str = new String(this);
  str = str.replace(/<br>/ig, '\r\n');
  var re = new RegExp('<[^>]+>', 'gm');
  return str.replace(re, '');
}
String.prototype.convertEscapeChars = function()
{
  var str = new String(this);

  var m = str.match(/&#[0-9]+;/ig);
  if (m != null)
  {
    for (var i = 0; i < m.length; i++)
    {
      str = str.replace(m[i], Convert.codeToSymbol(m[i]));
    }
  }

  m = str.match(/&[a-zA-Z]+;/ig);
  if (m != null)
  {
    for (var i = 0; i < m.length; i++)
    {
      str = str.replace(m[i], Convert.entityToSymbol(m[i]));
    }
  }

  return str;
}
function $getElement(e)
{
  if (document.getElementById)
  {
    e = document.getElementById(e);
  }
  else if (document.all)
  {
    e = document.all.item(e);
  }
  else
  {
    e = null;
  }
  return e;
}
function $getElementsByClassName(cn, node, tag)
{
  var classElements = new Array();

  if (!node)
  {
    node = document;
  }
  if (!tag)
  {
    tag = '*';
  }
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp('(^|\\s)' + cn + '(\\s|$)');
  for (var i = 0, j = 0; i < elsLen; i++)
  {
    if (pattern.test(els[i].className))
    {
      classElements[j++] = els[i];
    }
  }

  return classElements;
}
function $createElement(t)
{
  return document.createElement(t);
}
function $addElement(p, e)
{
  p.appendChild(e);
}
function $removeElement(e)
{
   e = $getElement(e);
   e.parentNode.removeChild(e);
}
function $getCurrentStyle(e)
{
  var b = (e.ownerDocument ? e.ownerDocument : e.documentElement).defaultView; 
  return (b && e !== b && b.getComputedStyle ? b.getComputedStyle(e, null) : e.style);
}
function $flashInstalled()
{
/*
  var flashInstalled = false;

  if (navigator.plugins && navigator.plugins.length)
  {
    x = navigator.plugins["Shockwave Flash"];
    if (!x)
    {
      x = navigator.plugins["Shockwave Flash 2.0"];
    }
  }
  else if (navigator.mimeTypes && navigator.mimeTypes.length)
  {
    x = navigator.mimeTypes['application/x-shockwave-flash'];
    if (x && x.enabledPlugin)
      flashinstalled = 2;
    else
      flashinstalled = 1;
  }
  else
    MSDetect = "true";
*/
}
function $copyToClipboard(e)
{
  var fcId = 'flashcopier';
  var fc = $getElement(fcId);
  if (!fc)
  {
    fc = document.createElement('div');
    fc.id = fcId;
    with (fc.style)
    {
      left = top = 0;
      position = 'absolute';
      display = 'inline';
      visibility = 'hidden';
    }
    document.body.appendChild(fc);
  }
  var text2copy = e.innerHTML.stripTags();
  try { text2copy = text2copy.convertEscapeChars(); } catch (ex) {}
  fc.innerHTML = '<embed src="http://blogs.incyclesoftware.com/swf/_clipboard.swf" FlashVars="clipboard=' +
  escape(text2copy) + '" width="0" height="0" type="application/x-shockwave-flash"></embed>';
}
function $openWindow(newURL, newName, newFeatures, orgName)
{
  if (!newFeatures)
  {
    newFeatures = 'channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0';
  }
  var remote = window.open(newURL, newName, newFeatures);
  // If something went wrong.
  if (remote.opener == null)
  {
    remote.opener = window;
  }
  if (orgName)
  {
  	remote.opener.name = orgName;
  }
  return remote;
}
