//.........................................................................................
//  File: RjEncDec.js                                                Date:  Oct 26, 2008
//                            Application: Javascript Tools
//  Author: R. Berry
//
//  Should probably be split up (especially the htmlEntityEncodeDecode stuff)
//
//  Functions:
//          urlencode(name)
//          urlencodeForPHP(name)
//          htmlEncode(str)
//          htmlDecode(str)
//          htmlEntityEncodeDecode(text,doEncoding)
//          htmlExtendedEncode(text,outputForTextarea)
//          htmlExtendedDecode(text,outputFromTextarea)
//
//  Copyright 2008-2009 (c) Rawdon Berry
//
//  Modified: May  3 2009 to use global replace instead of single-instance replace (by mistake)
//            Jul 12 2009 to add a missing global replace in urlencodeForPHP 
//.........................................................................................

//.........................................................................................
// URL coding + PHP url coding
//.........................................................................................
function urlencode(name)
{
  // Javascript replaces space with %20 and all punctuation with
  // hex except * + / and @
  return( (escape(name).replace(/\+/g,'%2B')).replace(/ /g,'%20'));
}

function urlencodeForPHP(name)
{
  // Javascript replaces space with %20 and all punctuation with
  // hex except * + / and @
  // PHP replaces/expects '+' for space and %2B for + and encodes all
  // punctuation in hex
  name = (escape(name).replace(/\+/g,'%2B')).replace(/%20/g,'+');
  // Now encode punctuation
  return(name.replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40'));
}

//.........................................................................................
// Text encoding / decoding
//.........................................................................................
function htmlEncode(str)
{
  // Not very smart: if stuff contains entities already, they will be
  // replaced with &amp;
  return(str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;'));
}
function htmlDecode(str)
{
  // Not very smart: does not handle all entities
  return(str.replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g,'&'));
}

/*
function replaces extended characters of 'text' with its character
entities.
call the function by default with output = false. if you switch it
to true
it will format the source code for output in a textarea.
*/
function htmlEntityEncodeDecode(text,doEncoding)
{
  // Pretty dumb getting a new array for every function call... fix this
/*
  var codec = new Array(
  '&AElig;',             '\xc6', //     '&AElig;',       'Æ',
  '&Aacute;',            '\xc1', //     '&Aacute;',      'Á',
  '&Acirc;',             '\xc2', //     '&Acirc;',       'Â',
  '&Agrave;',            '\xc0', //     '&Agrave;',      'À',
  '&Aring;',             '\xc5', //     '&Aring;',       'Å',
  '&Atilde;',            '\xc3', //     '&Atilde;',      'Ã',
  '&Auml;',              '\xc4', //     '&Auml;',        'Ä',
  '&Ccedil;',            '\xc7', //     '&Ccedil;',      'Ç',
  '&ETH;',               '\xd0', //     '&ETH;',         'Ð',
  '&Eacute;',            '\xc9', //     '&Eacute;',      'É',
  '&Ecirc;',             '\xca', //     '&Ecirc;',       'Ê',
  '&Egrave;',            '\xc8', //     '&Egrave;',      'È',
  '&Euml;',              '\xcb', //     '&Euml;',        'Ë',
  '&Iacute;',            '\xcd', //     '&Iacute;',      'Í',
  '&Icirc;',             '\xce', //     '&Icirc;',       'Î',
  '&Igrave;',            '\xcc', //     '&Igrave;',      'Ì',
  '&Iuml;',              '\xcf', //     '&Iuml;',        'Ï',
  '&Ntilde;',            '\xd1', //     '&Ntilde;',      'Ñ',
  '&Oacute;',            '\xd3', //     '&Oacute;',      'Ó',
  '&Ocirc;',             '\xd4', //     '&Ocirc;',       'Ô',
  '&Ograve;',            '\xd2', //     '&Ograve;',      'Ò',
  '&Oslash;',            '\xd8', //     '&Oslash;',      'Ø',
  '&Otilde;',            '\xd5', //     '&Otilde;',      'Õ',
  '&Ouml;',              '\xd6', //     '&Ouml;',        'Ö',
  '&THORN; ',            '\xde', //     '&THORN; ',      'Þ',
  '&Uacute;',            '\xda', //     '&Uacute;',      'Ú',
  '&Ucirc;',             '\xdb', //     '&Ucirc;',       'Û',
  '&Ugrave;',            '\xd9', //     '&Ugrave;',      'Ù',
  '&Uuml;',              '\xdc', //     '&Uuml;',        'Ü',
  '&Yacute;',            '\xdd', //     '&Yacute;',      'Ý',
  '&aacute;',            '\xe1', //     '&aacute;',      'á',
  '&acirc;',             '\xe2', //     '&acirc;',       'â',
  '&aelig;',             '\xe6', //     '&aelig;',       'æ',
  '&agrave;',            '\xe0', //     '&agrave;',      'à',
  '&aring;',             '\xe5', //     '&aring;',       'å',
  '&atilde;',            '\xe3', //     '&atilde;',      'ã',
  '&auml;',              '\xe4', //     '&auml;',        'ä ',
  '&brvbar;',            '\xa6', //     '&brvbar;',      '¦',
  '&ccedil;',            '\xe7', //     '&ccedil;',      'ç',
  '&cent;',              '\xa2', //     '&cent;',        '¢',
  '&copy;',              '\xa9', //     '&copy;',        '©',
  '&deg;',               '\xb0', //     '&deg;',         '°',
  '&eacute;',            '\xe9', //     '&eacute;',      'é',
  '&ecirc;',             '\xea', //     '&ecirc;',       'ê',
  '&egrave;',            '\xe8', //     '&egrave;',      'è',
  '&eth;',               '\xf0', //     '&eth;',         'ð',
  '&euml;',              '\xeb', //     '&euml;',        'ë',
  '&frac12;',            '\xbd', //     '&frac12;',      '½',
  '&frac14;',            '\xbc', //     '&frac14;',      '¼',
  '&frac34;',            '\xbe', //     '&frac34;',      '¾',
  '&gt;',                '\x3e', //     '&gt;',          '>',
  '&gt',                 '\x3e', //     '&gt',           '>',
  '&iacute;',            '\xed', //     '&iacute;',      'í',
  '&icirc;',             '\xee', //     '&icirc;',       'î',
  '&iexcl;',             '\xa1', //     '&iexcl;',       '¡',
  '&igrave;',            '\xec', //     '&igrave;',      'ì',
  '&iquest;',            '\xbf', //     '&iquest;',      '¿',
  '&iuml;',              '\xef', //     '&iuml;',        'ï',
  '&laquo;',             '\xab', //     '&laquo;',       '«',
  '&lt;',                '\x3c', //     '&lt;',          '<',
  '&lt',                 '\x3c', //     '&lt',           '<',
  '&mdash;',           '\x2014', //     '&mdash;',       '—',
  '&micro;',             '\xb5', //     '&micro;',       'µ',
  '&middot;',            '\xb7', //     '&middot;',      '·',
  '&ndash;',           '\x2013', //     '&ndash;',       '–',
  '&not;',               '\xac', //     '&not;',         '¬',
  '&ntilde;',            '\xf1', //     '&ntilde;',      'ñ',
  '&oacute;',            '\xf3', //     '&oacute;',      'ó',
  '&ocirc;',             '\xf4', //     '&ocirc;',       'ô',
  '&ograve;',            '\xf2', //     '&ograve;',      'ò',
  '&oslash;',            '\xf8', //     '&oslash;',      'ø',
  '&otilde;',            '\xf5', //     '&otilde;',      'õ',
  '&ouml;',              '\xf6', //     '&ouml;',        'ö',
  '&para;',              '\xb6', //     '&para;',        '¶',
  '&plusmn;',            '\xb1', //     '&plusmn;',      '±',
  '&pound;',             '\xa3', //     '&pound;',       '£',
  '&quot;',              '\x22', //     '&quot;',        '\"',
  '&raquo;',             '\xbb', //     '&raquo;',       '»',
  '&reg;',               '\xae', //     '&reg;',         '®',
  '&sect;',              '\xa7', //     '&sect;',        '§',
  '&shy;',               '\xad', //     '&shy;',         '\xAD',
  '&sup1;',              '\xb9', //     '&sup1;',        '¹',
  '&sup2;',              '\xb2', //     '&sup2;',        '²',
  '&sup3;',              '\xb3', //     '&sup3;',        '³',
  '&szlig;',             '\xdf', //     '&szlig;',       'ß',
  '&thorn;',             '\xfe', //     '&thorn;',       'þ',
  '&tilde;',           '\x02dc', //     '&tilde;',       '˜',
  '&trade;',           '\x2122', //     '&trade;',       '™',
  '&uacute;',            '\xfa', //     '&uacute;',      'ú',
  '&ucirc;',             '\xfb', //     '&ucirc;',       'û',
  '&ugrave;',            '\xf9', //     '&ugrave;',      'ù',
  '&uuml;',              '\xfc', //     '&uuml;',        'ü',
  '&yacute;',            '\xfd', //     '&yacute;',      'ý',
  '&yen;',               '\xa5', //     '&yen;',         '¥',
  '&yuml;',              '\xff'  //     '&yuml;',        'ÿ'
  );
*/
  var codec = new Array(
      '&AElig;','Æ',
      '&Aacute;','Á',
      '&Acirc;','Â',
      '&Agrave;','À',
      '&Aring;','Å',
      '&Atilde;','Ã',
      '&Auml;','Ä',
      '&Ccedil;','Ç',
      '&ETH;','Ð',
      '&Eacute;','É',
      '&Ecirc;','Ê',
      '&Egrave;','È',
      '&Euml;','Ë',
      '&Iacute;','Í',
      '&Icirc;','Î',
      '&Igrave;','Ì',
      '&Iuml;','Ï',
      '&Ntilde;','Ñ',
      '&Oacute;','Ó',
      '&Ocirc;','Ô',
      '&Ograve;','Ò',
      '&Oslash;','Ø',
      '&Otilde;','Õ',
      '&Ouml;','Ö',
      '&THORN; ','Þ',
      '&Uacute;','Ú',
      '&Ucirc;','Û',
      '&Ugrave;','Ù',
      '&Uuml;','Ü',
      '&Yacute;','Ý',
      '&aacute;', 'á',
      '&acirc;','â',
      '&aelig;','æ',
      '&agrave;','à',
      '&aring;','å',
      '&atilde;','ã',
      '&auml;','ä ',
      '&brvbar;','¦',
      '&ccedil;','ç',
      '&cent;','¢',
      '&copy;','©',
      '&deg;','°',
      '&eacute;','é',
      '&ecirc;','ê',
      '&egrave;','è',
      '&eth;','ð',
      '&euml;','ë',
      '&frac12;','½',
      '&frac14;','¼',
      '&frac34;','¾',
      '&gt;','>',
      '&gt','>',
      '&iacute;','í',
      '&icirc;','î',
      '&iexcl;','¡',
      '&igrave;','ì',
      '&iquest;','¿',
      '&iuml;','ï',
      '&laquo;','«',
      '&lt;','<',
      '&lt','<',
      '&mdash;','—',
      '&micro;','µ',
      '&middot;','·',
      '&ndash;','–',
      '&not;','¬',
      '&ntilde;','ñ',
      '&oacute;','ó',
      '&ocirc;','ô',
      '&ograve;','ò',
      '&oslash;','ø',
      '&otilde;','õ',
      '&ouml;','ö',
      '&para;','¶',
      '&plusmn;','±',
      '&pound;','£',
      '&quot;','\"',
      '&raquo;','»',
      '&reg;','®',
      '&sect;','§',
      '&shy;','\xAD',
      '&sup1;','¹',
      '&sup2;','²',
      '&sup3;','³',
      '&szlig;','ß',
      '&thorn;','þ',
      '&tilde;','˜',
      '&trade;','™',
      '&uacute;','ú',
      '&ucirc;','û',
      '&ugrave;','ù',
      '&uuml;','ü',
      '&yacute;','ý',
      '&yen;','¥',
      '&yuml;','ÿ'
  );
  var i;
  if (doEncoding)
  {
    text = text.replace(/&/g, '&amp;');
    for (i=0; i < codec.length; i=i+2)
      text = text.replace(eval('/\\'+codec[i+1]+'/g'), codec[i])
  }
  else
  {
    for (i=0; i < codec.length; i=i+2)
      text = text.replace(eval('/'+codec[i]+'/g'),String.charAt(codec[i+1],0));
    text = text.replace(/&amp;/g,'&');
  }
  return(text);
}

function htmlExtendedEncode(text,outputForTextarea)
{
  text = htmlEntityEncodeDecode(text,true);
  if (outputForTextarea)
    return(text.replace(/&/g,'&amp;'));
  return (text);
}

function htmlExtendedDecode(text,outputFromTextarea)
{
  if (outputFromTextarea)
    text = text.replace(/&amp;/g,'&');
  return(htmlEntityEncodeDecode(text,false));
}

