﻿String.prototype.trim = function()
{
  return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
}

String.prototype.startsWith = function(value)
{
  return (this.match("^" + value) == value);
}

String.prototype.endsWith = function(value)
{
  return (this.match(value + "$") == value);
}

String.prototype.htmlEncode = function()
{
  return this.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;"); 
}

function formatMoney(value)
{
  var result = value;
  var i;
  var isNeg;

  if (isNaN(result)) result = 0;
  isNeg = ((result < 0) ? -1 : 1);
  result = result * isNeg;

  result *= 100;
  result = Math.floor(result);
  result /= 100;

  result += "";
  i = result.indexOf(".");
  if (i == -1) result += ".00";
  if ((result.length - 1) - i == 1) result += "0";

  i = result.indexOf(".");
  while ((i -= 3) > 0)
  {
    result = result.substring(0, i) + "," + result.substring(i, result.length); 
  }

  result = "$" + result;
  if (isNeg < 0) result = "(" + result + ")";

  return result;
}

function parseMoney(value)
{
  var result = "" + value + "";
  var isNeg;

  isNeg = ((result.indexOf("(") >= 0) ? -1 : ((result.indexOf("-") >= 0) ? -1 : 1));

  result = result.replace("(", "");
  result = result.replace(")", "");
  result = result.replace("-", "");
  result = result.replace(" ", "");
  result = result.replace("$", "");
  result = result.replace(",", "");

  if (isNaN(result)) result = "";
  result = Math.floor(parseFloat(result) * 100) / 100 * isNeg;
  if (isNaN(result)) result = "";

  return result;
}
