String.prototype.trim = function()
{
  return this.replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1');
};


//================================================================================
String.prototype.strip_tags = function() {
        return this.replace(/<\S[^><]*>/g, ' ');
};


//================================================================================
function Common() {}


Common.loadJs = function(url) {
        var e = document.createElement("script");
        e.src = url;
        e.type="text/javascript";
        document.getElementsByTagName("head")[0].appendChild(e);
};


//================================================================================
Common.isArray = function(arr)
{
        return typeof arr == 'object' &&
                arr !== null &&
                typeof arr.length == 'number' &&
                arr.length > 0;
};

//================================================================================
Common.isString = function(str)
{
        return typeof str == 'string' &&
                str !== null &&
                str.length > 0;
};


//================================================================================
Common.isObject = function(obj)
{
        return typeof obj == 'object' &&
                obj !== null;
};


//================================================================================
Common.htmlspecialchars = function (string)
{
        if (!Common.isString(string)) return '';
        var encoded = new String(string);
        return encoded.replace(/&/g, "&amp;").
                                replace(/"/g, "&quot;").
                                replace(/'/g, "&#039;").
                                replace(/</g, "&lt;").
                                replace(/>/g, "&gt;");
};


//============================================================================================================
Common.htmlspecialchars_decode = function (string) {
        if (!Common.isString(string)) return '';
        var decoded = new String(string);
        decoded = decoded.replace(/&amp;/g,  "&");  // handle ampersand
        decoded = decoded.replace(/&quot;/g, "\""); // handle double quote
        decoded = decoded.replace(/&#039;/g, "'");  // handle single quote
        decoded = decoded.replace(/&lt;/g,   "<");  // handle less than
        decoded = decoded.replace(/&gt;/g,   ">");  // handle greater than
        return decoded;
};

//================================================================================
Common.getEnding = function(strArr, count)
{
        switch (count) {
                case 1: return strArr[1];
                case 2: return strArr[2];
                case 3:
                case 4:        return strArr[3];
                default: return strArr[0];
        }
};


//================================================================================
function debugout(message)
{
        var dbgOut = document.getElementById('debugout');
        if (dbgOut) dbgOut.value = message + '\n' + dbgOut.value;
}
