function isToken(text)
{
   var semiIndex = text.indexOf(';');

   if (semiIndex < 0)
      return false;

   var temp = text.slice(0,semiIndex + 1);

   if (temp == '&amp;')
      return true;

   if (temp == '&lt;')
      return true;

   if (temp == '&gt;')
      return true;

   if (temp == '&quot;')
      return true;

   if (temp == '&#39;')
      return true;

   return false;
}

function fixString(text)
{
   if (text == null)
      return "";

   var result = text;

   if (text.indexOf('&') >= 0 || text.indexOf('<') >= 0 || text.indexOf('>') >= 0 || text.indexOf('"') >= 0 || text.indexOf("'") >= 0)
   {
      var temp = new String();

      for (var i = 0; i < text.length; i++)
      {
         var ch = text.charAt(i);

         if (ch == '&' && !isToken(text.slice(i)))
            temp += "&amp;";
         else if (ch == '<')
            temp += "&lt;";
         else if (ch == '>')
            temp += "&gt;";
         else if (ch == '"')
            temp += "&quot;";
         else if (ch == "'")
            temp += "&#39;";
         else
            temp += ch;
      }

      result = temp;
   }

   return result;
}

function translateMessage(msg,arg0,arg1,arg2,arg3,arg4)
{
   // return empty string if null
   if (msg == null)
      return "";

   // fix special html characters
   while (msg.indexOf('&#39;') >= 0)
      msg = msg.replace("&#39;","'");

   while (msg.indexOf('&lt;') >= 0)
      msg = msg.replace("&lt;","'");

   while (msg.indexOf('&gt;') >= 0)
      msg = msg.replace("&gt;","'");

   while (msg.indexOf('&amp;') >= 0)
      msg = msg.replace("&amp;","'");

   while (msg.indexOf('&quot;') >= 0)
      msg = msg.replace("&quot;","'");

   // if there are no parameter markers, then we are done
   if (msg.indexOf('{') < 0)
      return msg;

   // perform parameter substitution
   if (arg0 != null && msg.indexOf("{0}") >= 0)
   {
      msg = msg.replace("{0}",arg0.toString());
   }

   if (arg1 != null && msg.indexOf("{1}") >= 0)
   {
      msg = msg.replace("{1}",arg1.toString());
   }

   if (arg2 != null && msg.indexOf("{2}") >= 0)
   {
      msg = msg.replace("{2}",arg2.toString());
   }

   if (arg3 != null && msg.indexOf("{3}") >= 0)
   {
      msg = msg.replace("{3}",arg3.toString());
   }

   if (arg4 != null && msg.indexOf("{4}") >= 0)
   {
      msg = msg.replace("{4}",arg4.toString());
   }

   return msg;
}

function confirmPrompt(prompt,item)
{
   return confirm(translateMessage(prompt,item));
}

function replaceNonWordChars(text,character)
{
	var string = new String(text);
	return string.replace(/\W/ig,character);
}
