var entities[200];
var chars[200];

function fillArrays()
{
   chars[0]  = "\"";entities[0]   = "&quot;";
   chars[1]  = "'"; entities[1]   = "&apos;";
   chars[2]  = "¡"; entities[2]   = "&iexcl;";
   chars[3]  = "¢"; entities[3]   = "&cent;";
   chars[4]  = "£"; entities[4]   = "&pound;";
   chars[5]  = "¤"; entities[5]   = "&curren;";
   chars[6]  = "¥"; entities[6]   = "&yen;";
   chars[7]  = "¦"; entities[7]   = "&brvbar;";
   chars[8]  = "§"; entities[8]   = "&sect;";
   chars[9]  = "¨"; entities[9]   = "&uml;";
  chars[10]  = "©"; entities[10]  = "&copy;";
  chars[11]  = "ª"; entities[11]  = "&ordf;";
  chars[12]  = "«"; entities[12]  = "&laquo;";
  chars[13]  = "¬"; entities[13]  = "&not;";
  chars[14]  = "®"; entities[14]  = "&reg;";
  chars[15]  = "¯"; entities[15]  = "&macr;";
  chars[16]  = "°"; entities[16]  = "&deg;";
  chars[17]  = "±"; entities[17]  = "&plusmn;";
  chars[18]  = "²"; entities[18]  = "&sup2;";
  chars[19]  = "³"; entities[19]  = "&sup3;";
  chars[20]  = "´"; entities[20]  = "&acute;";
  chars[21]  = "µ"; entities[21]  = "&micro;";
  chars[22]  = "¶"; entities[22]  = "&para;";
  chars[23]  = "·"; entities[23]  = "&middot;";
  chars[24]  = "¸"; entities[24]  = "&cedil;";
  chars[25]  = "¹"; entities[25]  = "&sup1;";
  chars[26]  = "º"; entities[26]  = "&ordm;";
  chars[27]  = "»"; entities[27]  = "&raquo;";
  chars[28]  = "¼"; entities[28]  = "&frac14;";
  chars[29]  = "½"; entities[29]  = "&frac12;";
  chars[30]  = "¾"; entities[30]  = "&frac34;";
  chars[31]  = "¿"; entities[31]  = "&iquest;";
  chars[32]  = "À"; entities[32]  = "&Agrave;";
  chars[33]  = "Á"; entities[33]  = "&Aacute;";
  chars[34]  = "Â"; entities[34]  = "&Acirc;";
  chars[35]  = "Ã"; entities[35]  = "&Atilde;";
  chars[36]  = "Ä"; entities[36]  = "&Auml;";
  chars[37]  = "Å"; entities[37]  = "&Aring;";
  chars[38]  = "Æ"; entities[38]  = "&AElig;";
  chars[39]  = "Ç"; entities[39]  = "&Ccedil;";
  chars[40]  = "È"; entities[40]  = "&Egrave;";
  chars[41]  = "É"; entities[41]  = "&Eacute;";
  chars[42]  = "Ê"; entities[42]  = "&Ecirc;";
  chars[43]  = "Ë"; entities[43]  = "&Euml;";
  chars[44]  = "Ì"; entities[44]  = "&Igrave;";
  chars[45]  = "Í"; entities[45]  = "&Iacute;";
  chars[46]  = "Î"; entities[46]  = "&Icirc;";
  chars[47]  = "Ï"; entities[47]  = "&Iuml;";
  chars[48]  = "Ð"; entities[48]  = "&ETH;";
  chars[49]  = "Ñ"; entities[49]  = "&Ntilde;";
  chars[50]  = "Ò"; entities[50]  = "&Ograve;";
  chars[51]  = "Ó"; entities[51]  = "&Oacute;";
  chars[52]  = "Ô"; entities[52]  = "&Ocirc;";
  chars[53]  = "Õ"; entities[53]  = "&Otilde;";
  chars[54]  = "Ö"; entities[54]  = "&Ouml;";
  chars[55]  = "×"; entities[55]  = "&times;";
  chars[56]  = "Ø"; entities[56]  = "&Oslash;";
  chars[57]  = "Ù"; entities[57]  = "&Ugrave;";
  chars[58]  = "Ú"; entities[58]  = "&Uacute;";
  chars[59]  = "Û"; entities[59]  = "&Ucirc;";
  chars[60]  = "Ü"; entities[60]  = "&Uuml;";
  chars[61]  = "Ý"; entities[61]  = "&Yacute;";
  chars[62]  = "Þ"; entities[62]  = "&THORN;";
  chars[63]  = "ß"; entities[63]  = "&szlig;";
  chars[64]  = "à"; entities[64]  = "&agrave;";
  chars[65]  = "á"; entities[65]  = "&aacute;";
  chars[66]  = "â"; entities[66]  = "&acirc;";
  chars[67]  = "ã"; entities[67]  = "&atilde;";
  chars[68]  = "ä"; entities[68]  = "&auml;";
  chars[69]  = "å"; entities[69]  = "&aring;";
  chars[70]  = "æ"; entities[70]  = "&aelig;";
  chars[71]  = "ç"; entities[71]  = "&ccedil;";
  chars[72]  = "è"; entities[72]  = "&egrave;";
  chars[73]  = "é"; entities[73]  = "&eacute;";
  chars[74]  = "ê"; entities[74]  = "&ecirc;";
  chars[75]  = "ë"; entities[75]  = "&euml;";
  chars[76]  = "ì"; entities[76]  = "&igrave;";
  chars[77]  = "í"; entities[77]  = "&iacute;";
  chars[78]  = "î"; entities[78]  = "&icirc;";
  chars[79]  = "ï"; entities[79]  = "&iuml;";
  chars[80]  = "ð"; entities[80]  = "&eth;";
  chars[81]  = "ñ"; entities[81]  = "&ntilde;";
  chars[82]  = "ò"; entities[82]  = "&ograve;";
  chars[83]  = "ó"; entities[83]  = "&oacute;";
  chars[84]  = "ô"; entities[84]  = "&ocirc;";
  chars[85]  = "õ"; entities[85]  = "&otilde;";
  chars[86]  = "ö"; entities[86]  = "&ouml;";
  chars[87]  = "÷"; entities[87]  = "&divide;";
  chars[88]  = "ø"; entities[88]  = "&oslash;";
  chars[89]  = "ù"; entities[89]  = "&ugrave;";
  chars[90]  = "ú"; entities[90]  = "&uacute;";
  chars[91]  = "û"; entities[91]  = "&ucirc;";
  chars[92]  = "ü"; entities[92]  = "&uuml;";
  chars[93]  = "ý"; entities[93]  = "&yacute;";
  chars[94]  = "þ"; entities[94]  = "&thorn;";
  chars[95]  = "ÿ"; entities[95]  = "&yuml;";
  chars[96]  = "Œ"; entities[96]  = "&OElig;";
  chars[97]  = "œ"; entities[97]  = "&oelig;";
  chars[98]  = "Š"; entities[98]  = "&Scaron;";
  chars[99]  = "š"; entities[99]  = "&scaron;";
  chars[100] = "Ÿ"; entities[100] = "&Yuml;";
  chars[101] = "ƒ"; entities[101] = "&fnof;";
  chars[102] = "ˆ"; entities[102] = "&circ;";
  chars[103] = "˜"; entities[103] = "&tilde;";
  chars[104] = "Α"; entities[104] = "&Alpha;";
  chars[105] = "Β"; entities[105] = "&Beta;";
  chars[106] = "Γ"; entities[106] = "&Gamma;";
  chars[107] = "Δ"; entities[107] = "&Delta;";
  chars[108] = "Ε"; entities[108] = "&Epsilon;";
  chars[109] = "Ζ"; entities[109] = "&Zeta;";
  chars[110] = "Η"; entities[110] = "&Eta;";
  chars[111] = "Θ"; entities[111] = "&Theta;";
  chars[112] = "Ι"; entities[112] = "&Iota;"
  chars[113] = "Κ"; entities[113] = "&Kappa;";
  chars[114] = "Λ"; entities[114] = "&Lambda;";
  chars[115] = "Μ"; entities[115] = "&Mu;";
  chars[116] = "Ν"; entities[116] = "&Nu;";
  chars[117] = "Ξ"; entities[117] = "&Xi;";
  chars[118] = "Ο"; entities[118] = "&Omicron;";
  chars[119] = "Π"; entities[119] = "&Pi;";
  chars[120] = "Ρ"; entities[120] = "&Rho;";
  chars[121] = "Σ"; entities[121] = "&Sigma;";
  chars[122] = "Τ"; entities[122] = "&Tau;";
  chars[123] = "Υ"; entities[123] = "&Upsilon;";
  chars[124] = "Φ"; entities[124] = "&Phi;";
  chars[125] = "Χ"; entities[125] = "&Chi;";
  chars[126] = "Ψ"; entities[126] = "&Psi;";
  chars[127] = "Ω"; entities[127] = "&Omega;";
  chars[128] = "α"; entities[128] = "&alpha;";
  chars[129] = "β"; entities[129] = "&beta;";
  chars[130] = "γ"; entities[130] = "&gamma;";
  chars[131] = "δ"; entities[131] = "&delta;";
  chars[132] = "ε"; entities[132] = "&epsilon;";
  chars[133] = "ζ"; entities[133] = "&zeta;";
  chars[134] = "η"; entities[134] = "&eta;";
  chars[135] = "θ"; entities[135] = "&theta;";
  chars[136] = "ι"; entities[136] = "&iota;";
  chars[137] = "κ"; entities[137] = "&kappa;";
  chars[138] = "λ"; entities[138] = "&lambda;";
  chars[139] = "μ"; entities[139] = "&mu;";
  chars[140] = "ν"; entities[140] = "&nu;";
  chars[141] = "ξ"; entities[141] = "&xi;";
  chars[142] = "ο"; entities[142] = "&omicron;";
  chars[143] = "π"; entities[143] = "&pi;";
  chars[144] = "ρ"; entities[144] = "&rho;";
  chars[145] = "ς"; entities[145] = "&sigmaf;";
  chars[146] = "σ"; entities[146] = "&sigma;";
  chars[147] = "τ"; entities[147] = "&tau;";
  chars[148] = "υ"; entities[148] = "&upsilon;";
  chars[149] = "φ"; entities[149] = "&phi;";
  chars[150] = "χ"; entities[150] = "&chi;";
  chars[151] = "ψ"; entities[151] = "&psi;";
  chars[152] = "ω"; entities[152] = "&omega;";
  chars[153] = "ϑ"; entities[153] = "&thetasym;";
  chars[154] = "ϒ"; entities[154] = "&upsih;";
  chars[155] = "ϖ"; entities[155] = "&piv;";
  chars[156] = "–"; entities[156] = "&ndash;";
  chars[157] = "—"; entities[157] = "&mdash;";
  chars[158] = "‘"; entities[158] = "&lsquo;";
  chars[159] = "’"; entities[159] = "&rsquo;";
  chars[160] = "‚"; entities[160] = "&sbquo;";
  chars[161] = "“"; entities[161] = "&ldquo;";
  chars[162] = "”"; entities[162] = "&rdquo;";
  chars[163] = "„"; entities[163] = "&bdquo;";
  chars[164] = "†"; entities[164] = "&dagger;";
  chars[165] = "‡"; entities[165] = "&Dagger;";
  chars[166] = "•"; entities[166] = "&bull;";
  chars[167] = "…"; entities[167] = "&hellip;";
  chars[168] = "‰"; entities[168] = "&permil;";
  chars[169] = "′"; entities[169] = "&prime;";
  chars[170] = "″"; entities[170] = "&Prime;";
  chars[171] = "‹"; entities[171] = "&lsaquo;";
  chars[172] = "›"; entities[172] = "&rsaquo;";
  chars[173] = "‾"; entities[173] = "&oline;";
  chars[174] = "⁄"; entities[174] = "&frasl;";
  chars[175] = "€"; entities[175] = "&euro;";
  chars[176] = "™"; entities[176] = "&trade;";
  chars[177] = "←"; entities[177] = "&larr;";
  chars[178] = "↑"; entities[178] = "&uarr;";
  chars[179] = "→"; entities[179] = "&rarr;";
  chars[180] = "↓"; entities[180] = "&darr;";
  chars[181] = "↔"; entities[181] = "&harr;";
  chars[182] = "∂"; entities[182] = "&part;";
  chars[183] = "∏"; entities[183] = "&prod;";
  chars[184] = "∑"; entities[184] = "&sum;";
  chars[185] = "−"; entities[185] = "&minus;";
  chars[186] = "√"; entities[186] = "&radic;";
  chars[187] = "∞"; entities[187] = "&infin;";
  chars[188] = "∩"; entities[188] = "&cap;";
  chars[189] = "∫"; entities[189] = "&int;";
  chars[190] = "≈"; entities[190] = "&asymp;";
  chars[191] = "≠"; entities[191] = "&ne;";
  chars[192] = "≡"; entities[192] = "&equiv;";
  chars[193] = "≤"; entities[193] = "&le;";
  chars[194] = "≥"; entities[194] = "&ge;";
  chars[195] = "◊"; entities[195] = "&loz;";
  chars[196] = "♠"; entities[196] = "&spades;";
  chars[197] = "♣"; entities[197] = "&clubs;";
  chars[198] = "♥"; entities[198] = "&hearts;";
  chars[199] = "♦"; entities[199] = "&diams;";
}
  
function replaceCharacters()
{  
  Document.BeginUpdate();
  var s = Document.SelText;
  if (s == "") {
    Document.SelectAll;
    s = Document.SelText;
  }
  for (var i = 0; i < Length(chars); i++) {
    if (Pos(chars[i], s) > 0) {
    	s = ScriptUtils.StringReplaceAll(s, chars[i], entities[i]);
    }
  }
  Document.SelText = s;
  Document.EndUpdate();
  ShowMessage("Done!");
  f.ModalResult = mrOk;
}

function replaceEntities()
{  
  Document.BeginUpdate();
  var s = Document.SelText;
  if (s == "") {
    Document.SelectAll;
    s = Document.SelText;
  }
  for (var i = 0; i < Length(chars); i++) {
    if (Pos(entities[i], s) > 0) {
    	s = ScriptUtils.StringReplaceAll(s, entities[i], chars[i]);
    }
  }
  Document.SelText = s;
  Document.EndUpdate();
  ShowMessage("Done!");
  f.ModalResult = mrOk;
}

function exit()
{
  f.ModalResult = mrOk;
}

// Load special characters and entities to our arrays
fillArrays();

// build up main app window
f = new TForm(nil);
f.Caption = "Replace entities or special characters";
f.BorderStyle = bsDialog;
f.Position = poScreenCenter;
f.Width = 320;
f.Height = 150;

la = new TLabel(f);
la.Parent = f;
la.Name = "lblA";
la.SetBounds(30, 17, 160, 25);
la.Caption = "Entities => Characters";

r = new TButton(f);
r.Parent = f;
r.Name = "btnReplace";
r.SetBounds(200, 15, 95, 25);
r.Caption = "Replace";
r.OnClick = &replaceEntities;

lb = new TLabel(f);
lb.Parent = f;
lb.Name = "lblB";
lb.SetBounds(30, 52, 160, 25);
lb.Caption = "Characters => Entities";

h = new TButton(f);
h.Parent = f;
h.Name = "btnReplaceChars";
h.SetBounds(200, 50, 95, 25);
h.Caption = "Replace";
h.OnClick = &replaceCharacters;

c = new TButton(f);
c.Parent = f;
c.Name = "btnCancel";
c.SetBounds(200, 85, 95, 25);
c.Caption = "Cancel";
c.OnClick = &exit;

// show the main dialog
f.ShowModal;
f.Free;