/**
 *  @provides string-escape
 */

function escapeURI(u)
{
    if (encodeURIComponent) {
        return encodeURIComponent(u);
    }
    if (escape) {
        return escape(u);
    }
}



/**
 *  Escape HTML characters in a string, rendering it safe for display in an
 *  HTML context.
 *
 *  @param string String to escape.
 *  @return string Escaped string.
 *
 *  @author marcel
 */
function htmlspecialchars(text) {

  if (typeof(text) == 'undefined' || !text.toString) {
    return '';
  }

  if (text === false) {
    return '0';
  } else if (text === true) {
    return '1';
  }

  return text
    .toString()
    .replace(/&/g, '&amp;')
    .replace(/"/g, '&quot;')
    .replace(/'/g, '&#039;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;');
}

function htmlize(text) {
 return htmlspecialchars(text).replace(/\n/g, '<br />');
}


/**
 *  Escape quote charcters in a string, rendering it safe for use as a parameter
 *  to a literally constructed function (e.g. an onclick handler in a link being
 *  created via innerHTML).
 *
 *  @param string String to escape.
 *  @return string Escaped string.
 *
 *  @author marcel
 */
function escape_js_quotes(text) {

  if (typeof(text) == 'undefined' || !text.toString) {
    return '';
  }

  return text
    .toString( )
    .replace(/\\/g, '\\\\')
    .replace(/\n/g, '\\n')
    .replace(/\r/g, '\\r')
    .replace(/"/g, '\\x22')
    .replace(/'/g, '\\\'')
    .replace(/</g, '\\x3c')
    .replace(/>/g, '\\x3e')
    .replace(/&/g, '\\x26');
}