/* Copyright 2010, Mark B. Rosenthal.  All rights reserved.  Mark
 * B. Rosenthal owns all right, title, and interest in this listing.
 * This listing is confidential and proprietary. Any unauthorized use,
 * reproduction, altering, distribution, or transmission of this
 * listing, or any part thereof, in any form or by any means,
 * electronic or mechanical, is strictly prohibited without a license
 * from Mark B. Rosenthal.
 */
/* Author: Mark B. Rosenthal <mbr@arlsoft.com>, Arlington Software Enterprises */

function initAddrs()
{
  var elements = findAllTagsOfType("*");
  for (var i = 0; i < elements.length; i++)
    {
      var element = elements[i];
      if (element.tagName.toLowerCase() == "a")
	{
	  if (element.id !== undefined)
	    {
	      var id_parts = element.id.split("_");
	      if (id_parts[0] == "addr")
		{
		  element.href = munscramble(id_parts[1]);
		  textNode = document.createTextNode(munscramble(id_parts[2]));
		  element.appendChild(textNode);
/*
debugVar("element", element, -1,
{nodeName:1, nodeValue:1, nodeType:1, tagName:1, id:1, className:1, childNodes:1, attributes:1},
true);
*/
		}
	    }
	}
      else
	{
	  if (element.id !== undefined)
	    {
	      var id_parts = element.id.split("_");
	      if (id_parts[0] == "attr")
		{
/*
debugVar("element(id=attr)", element, -1,
{nodeName:1, nodeValue:1, nodeType:1, tagName:1, id:1, className:1, childNodes:1, attributes:1},
true);
debugVar("element.attributes", element.attributes, 2, null, true);
*/
		  for (var j = 1; j < id_parts.length; j++)
		    {
		      var attr_parts = munscramble(id_parts[j]).split("=", 2);
		      var name = attr_parts[0];
		      var val = attr_parts[1];
		      element.setAttribute(name, val);
		    }
/*
debugVar("element", element, -1,
{nodeName:1, nodeValue:1, nodeType:1, tagName:1, id:1, className:1, childNodes:1, attributes:1},
true);
debugVar("element.attributes", element.attributes, 2, null, true);
*/
		}
	    }
	}
    }
}

function munscramble(s)
{
  s = unhex(s);
  var offset = s.charCodeAt(0) - 64;
  var retval = "";
  for (var i = 1; i < s.length; i++)
    {
      offset = (offset + 1) % 64;
      retval += String.fromCharCode(s.charCodeAt(i) - offset);
    }
  return retval;
}

function unhex(s)
{
  if (s.length % 2 == 1)
    s += '0';
  var retval = "";
  for (var i = 0; i < s.length; i+=2)
    {
      var i0 = intFromHexChar(s.charAt(i));
      var i1 = intFromHexChar(s.charAt(i+1));
      var code = (i0 << 4) + i1;
      var c = String.fromCharCode(code);
      retval += c;
    }
  return retval;
}

function intFromHexChar(c)
{
  var code_0 = String('0').charCodeAt(0);
  var code_9 = String('9').charCodeAt(0);
  var code_a = String('a').charCodeAt(0);
  var code_f = String('f').charCodeAt(0);

  var code = c.toLowerCase().charCodeAt(0);
  if (code >= code_0 && code <= code_9)
    return code - code_0;
  if (code >= code_a && code <= code_f)
    return code - code_a +0xa;
  return 0;
}
