﻿function isDefined(obj) {
    try {
        if ((obj || ((typeof obj == "number") && (obj != 1))) && (obj != null)) return true;
        else return (obj.isEmpty());
    } catch (err) { return false; }
}

function parseBinary(v) { return parseInt(v, 2); }
function parseDecimal(v) { return parseInt(v, 10); }
function parseHexaecimal(v) { return parseInt(v, 16); }

//Retreives a reference to a given object on a page by it's ID tag name.
function getElementByID() { return (arguments.length > 1) ? getElementFromID(arguments[0], arguments[1]) : getElementFromID(arguments[0]); }

function getElementFromID() {
    var objDocument, idName;
    
    switch (arguments.length) {
        case 0: return null;
        case 1: objDocument = document; idName = arguments[0]; break;
        case 2: objDocument = arguments[0]; idName = arguments[1]; break;
    }

    if (objDocument.getElementById) return objDocument.getElementById(idName);
    else if (objDocument.all) return objDocument.all[idName];
    else if (objDocument.layers) return objDocument.layers[idName];

    return null;
}

//Method retrieves the cookie value if there is one
function findCookie(name) {
    var dc = document.cookie;
    var cname = name + "=";
    var clen = dc.length;
    var cbegin = 0;

    while (cbegin < clen) {
        var vbegin = cbegin + cname.length;

        if (dc.substring(cbegin, vbegin) == cname) {
            var vend = dc.indexOf(";", vbegin);
            if (vend == -1)
                vend = clen;
            return unescape(dc.substring(vbegin, vend));
        }
        cbegin = dc.indexOf(" ", cbegin) + 1;

        if (cbegin == 0)
            break;
    }
    return null;
}

/*****************************************/
// String object extensions
/*****************************************/
String.prototype.Length = function() { return isDefined(this) ? this.length : 0; }
String.prototype.Split = String.prototype.split;
String.prototype.Substring = String.prototype.substring;
String.prototype.MakeWebSafe = function() { return this.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\34/g, "&quot;").replace(/\39/g, "&#039;"); }
String.prototype.parseInt = function() { return parseDecimal(this); }
String.prototype.parseFloat = function() { return parseFloat(this); }
String.prototype.trim = function() { return this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""); }
String.prototype.Trim = String.prototype.trim;
String.prototype.ltrim = function() { return this.replace(/^\s+/g, ""); }
String.prototype.LTrim = String.prototype.ltrim;
String.prototype.rtrim = function() { return this.replace(/\s+$/g, ""); }
String.prototype.RTrim = String.prototype.rtrim;
String.prototype.startsWith = function(str) { return (this.match("^" + str) == str); }
String.prototype.startsWithSpecial = function(str) { return (this.match("^" + str) == str.replace("?", "\\?").replace("^", "\\^").replace("*", "\\*").replace("+", "\\+").replace("$", "\\$").replace(".", "\\.").replace("[", "\\[").replace("]", "\\]").replace("{", "\\{").replace("(", "\\(").replace(")", "\\)").replace("<", "\\<").replace(">", "\\>")); }
String.prototype.StartsWith = String.prototype.startsWith;
String.prototype.endsWith = function(str) { return (this.match(str + "$") == str); }
String.prototype.endsWithSpecial = function(str) { return (this.match(str + "$") == str.replace("?", "\\?").replace("^", "\\^").replace("*", "\\*").replace("+", "\\+").replace("$", "\\$").replace(".", "\\.").replace("[", "\\[").replace("]", "\\]").replace("{", "\\{").replace("(", "\\(").replace(")", "\\)").replace("<", "\\<").replace(">", "\\>")); }
String.prototype.EndsWith = String.prototype.endsWith;
String.prototype.contains = function(s) { return (this.indexOf(s) > -1); }
String.prototype.Contains = String.prototype.contains;
String.prototype.isEmpty = function(s) { return (this == ""); }
String.prototype.IsEmpty = String.prototype.isEmpty;
String.prototype.left = function(i) { if (this.length > (i - 2)) return this.substring(0, i); else return this; }
String.prototype.Left = String.prototype.left;
String.prototype.right = function(i) { var r = (this.length - i); if (r > 0) return this.substring(r); else return this; }
String.prototype.Right = String.prototype.right;

