var URLEnc = new Object();

URLEnc.SAFECHARS =
 "0123456789" + // Numeric
 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
 "abcdefghijklmnopqrstuvwxyz" +
 "-_.!~*'()"; // RFC2396 Mark characters
 
URLEnc.HEX = "0123456789ABCDEF";

URLEnc.encode = function(plaintext)
{
  var charCode, ch, i, encoded = "", alerted = false;
  for (i = 0; i < plaintext.length; i++)
  {
    ch = plaintext.charAt(i);
if (ch == " ")
    {
      encoded += "+"; // x-www-urlencoded, rather than %20
    }
    else if (URLEnc.SAFECHARS.indexOf(ch) != -1)
{
      encoded += ch;
}
    else
    {
      charCode= ch.charCodeAt(0);
if (charCode > 255)
      {
        encoded += "%u";
        encoded += URLEnc.HEX.charAt((charCode >> 12) & 0xF);
        encoded += URLEnc.HEX.charAt((charCode >> 8) & 0xF);
        encoded += URLEnc.HEX.charAt((charCode >> 4) & 0xF);
        encoded += URLEnc.HEX.charAt(charCode & 0xF);
        /*
        if (!alerted)
        {
          alert( "Unicode Character '" 
                 + ch 
                 + "' cannot be encoded using standard URL encoding.\n" +
                 "(URL encoding only supports 8-bit characters.)\n" +
                 "A space (+) will be substituted." );
          alerted = true;
        }
        encoded += "+";
        */
      }
      else
      {
        encoded += "%";
        encoded += URLEnc.HEX.charAt((charCode >> 4) & 0xF);
        encoded += URLEnc.HEX.charAt(charCode & 0xF);
      }
    }
  }
  return encoded;
}

URLEnc.decode = function(encoded)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var ch, c1, c2, i, plaintext = "", alert = false;
   
   if ((encoded == null) || (encoded.length == 0))
   {
     return "";
   }
   
   for (i = 0; i < encoded.length; i++)
   {
     ch = encoded.charAt(i);
if (ch == "+")
     {
       plaintext += " ";
     }
     else if (ch == "%")
     {
       if (i < (encoded.length - 2))
       {
         c1 = URLEnc.HEX.indexOf(encoded.charAt(i+1).toUpperCase());
c2 = URLEnc.HEX.indexOf(encoded.charAt(i+2).toUpperCase());
if ((c1 != -1) && (c2 != -1))
{
           plaintext += String.fromCharCode((16 * c1) + c2);
           i += 2;
}
         else
         {
           if (!alerted)
           {
             alert( 'Bad escape combination near ...' + encoded.substr(i) );
             plaintext += "%[ERROR]";
             alerted = true;
}
         }
       }
     }
     else
     {
       plaintext += ch;
}
   }
   return plaintext;
}


URLEnc.decodeSearchStrHash = function(search)
{
   var i, pairs, vals;
   var r = new Hash();
   
   if (search.substring(1) == '?')
   {
     search = search.substring(1);
}
   
   if (search.length)
   {
     pairs = search.split("&");
for (i = 0; i < pairs.length; i++)
     {
       vals = pairs[i].split("=");
r.put(URLEnc.decode(vals[0]), URLEnc.decode(vals[1]));
     }
   }
   return r;
}

URLEnc.decodeSearchStr = function(search)
{
   var i, pairs, vals;
   var get = new Array();
   
   if (search.substr(0, 1) == '?')
   {
     search = search.substr(1);
}
   if (search.length)
   {
     pairs = search.split("&");
for (i = 0; i < pairs.length; i++)
     {
       vals = pairs[i].split("=");
get[URLEnc.decode(vals[0])] = URLEnc.decode(vals[1]);
}
   }
   return get;
}


function HTag()
{
  this.stack = new Array();
  this.state = 'cdat';
  this.document = document;
this.buffer = "";
}
new HTag();

HTag.lt = "<";
HTag.gt = ">";

HTag.prototype.st = function(tag)
{
  this.closetag();
  this.document.write(HTag.lt+tag);
  this.stack.push(tag);
  this.state = 'intag';
}

HTag.prototype.att = function(key, val)
{
  this.document.write(" "+key+"=\""+val+"\"");
}

HTag.prototype.atts = function(key)
{
  this.document.write(" "+key);
}

HTag.prototype.et = function()
{
  this.closetag();
  this.document.write(HTag.lt+"/"+this.stack.pop()+HTag.gt);
}

HTag.prototype.net = function()
{
  this.closetag();
  this.stack.pop();
}

HTag.prototype.cd = function(txt)
{
  this.closetag();
  this.document.write(txt);
}

HTag.prototype.closetag = function()
{
  if (this.state == 'intag')
  {
    this.document.write(HTag.gt);
    this.state = 'cdat';
  }
}

HTag.prototype.n = function()
{
  this.document.write("\n");
}

HTag.prototype.debug = function()
{
  HTag.lt = "&lt;";
  HTag.gt = "&gt;";
}

HTag.prototype.useBuffer = function()
{
  this.document = this;
}

HTag.prototype.write = function(str)
{
  this.buffer += str;
}


function jumpToSelected(pulldown)
{
  var URL;
  var idx = pulldown.selectedIndex;
  if (pulldown.options)
  {
    URL = pulldown.options[idx].value;
    if (URL != "")
    {
      /* START: ph 2007-06-29 clear the selection to allow re-selection */
      pulldown.options[0].selected = true;
      /* END: ph 2007-06-29 clear the selection to allow re-selection */
      window.location.href = URL;
    }
  }
}

/* START: ph@littletype.com 2007-06-07 */
function mailpage()
{
  //mail_str = "mailto:?subject="+encodeURIComponent("Defenders.org: "+document.title);
  //mail_str += "&body="+encodeURIComponent("Please see the following page: "+location.href);
  var widgets = 
   "toolbar=no," +
   "location=no," +
   "directories=no," +
   "status=no," +
   "menubar=no," +
   "scrollbars=yes," +
   "resizable=yes," +
   "width=500," +
   "height=500";
  var location = window.location.href.toLowerCase().replace(/^https?:\/\/[^\/]+/, "");
  var url = "/send_this_page/index.php?sendpage="+encodeURIComponent(location)+"&title="+encodeURIComponent(document.title);
  popupWin = window.open(url, "SendThisPage", widgets);
  popupWin.focus();
}
/* START: ph@littletype.com 2007-06-07 */