/**
 * Biblioteca extensiva JavaScript do TRE-SE.
 *
 * Autor: Diego Medeiros (diego@tre-se.gov.br)
 */

window.jsx = {
    "DEVELOPMENT" : true
};

function assert(x) {

    if (jsx.DEVELOPMENT && x !== true) {
        throw new Error("Assertion failure");
    }

}

function runtimeAssert(x) {

    if (x !== true) {
        throw new Error("RuntimeAssertion failure");
    }

}

function getElementById(sId){ 
  
  //return (document.getElementById) ? document.getElementById(sId) : document.all[sId];
  return document.getElementById(sId);
                                   
} 

function isNull(x) {
    
    return x === null;
    
}

function isBoolean(x) {

    var r =
        (typeof(x) !== "undefined")
        &&
        (x !== null)
        &&
        (x.constructor === Boolean);
        
    return r;

}

function isString(x) {

    var r =
        (typeof(x) !== "undefined")
        &&
        (x !== null)
        &&
        (x.constructor === String);
        
    return r;

}

function isNumber(x) {

    var r =
        (typeof(x) !== "undefined")
        &&
        (x !== null)
        &&
        (x.constructor === Number);

    return r;

}

function isNumeric(x) {

    try {
    
        var r =
            (typeof(x) !== "undefined")
            &&
            (x !== null)
            &&
            (
                (x.constructor === Number)
                ||
                (
                    isString(x)
                    &&
                    isNumber(x.toNumber())
                )
            );
            
    }
    catch (e) {
        r = false;
    }
    
    return r;

}

function isInteger(x) {

    var r =
        (typeof(x) !== "undefined")
        &&
        (x !== null)
        &&
        (x.constructor === Number)
        &&
        (x === parseInt(x));
        
    return r;

}



// STRING //////////////////////////////////////////////////////////////////////

if (!String.prototype.trimLeft) {
    String.prototype.trimLeft = function() {

        return this.replace(/^\s+/, '');

    };
}

if (!String.prototype.trimRight) {
    String.prototype.trimRight = function() {

        return this.replace(/\s+$/, '');

    };
}

if (!String.prototype.trim) {
    String.prototype.trim = function() {

        return this.replace(/^\s+/, '').replace(/\s+$/, '');

    };
}

String.prototype.toNumber = function(silent) {
    
    var result;
    
    //debugger;
    
    result = parseFloat(this);
    
    if (isNaN(result)) {
    
        if (silent) {
            result = null;
        }
        else {
            throw new Error("A String não pode ser convertida em Number.");
        }
    }
    
    return result;

};




// NUMBER //////////////////////////////////////////////////////////////////////

Number.valueOf = function(x, silent) {

    //TODO validar x
    
    silent = !!silent;
    
    if (isString(x)) {        
        return x.toNumber(silent);
    }
    
    return NaN;
    
};

/*Number.valueOfString = function(s) {
    
    var s = s.trim().replace(",", ".");
    var r = "";    
    
    var i;
    var c;
    var foundSeparator = false;
    
    for (i = 0; i < s.length; i++) {
        c = s.charCodeAt(i);
        
        // Se for sinal no primeiro caractere
        if (i == 0 && (c == 43 || c == 45)) {
            r += s.substr(i, 1);
        }
        // Se for numero
        else if (c >= 48 && c <= 57) {
            r += s.substr(i, 1);
            continue;
        }
        // Se ainda nao tiver achado separador decimal
        else if (!foundSeparator && c == 46) {
            r += s.substr(i, 1);
            foundSeparator = true;
        }
        else {
            throw "Cannot parse string to number";
        }
    }
    
    return parseFloat(r);
    
}*/

// QUERYSTRING /////////////////////////////////////////////////////////////////

/*
jsx.URL = function() {

    this.url = "";
    this.parameters = {};
    this.total = 0;

};

jsx.URL.fromURL = function(url) {

    var qs = new jsx.URL();
    qs.setUrl();
    qs.setParameter();    
    return qs;

};

jsx.URL.prototype.toString = function() {

    //TODO encode parameters
    
    var result = this.url;
    
    if (this.total > 0) {
    
        var i = 0;
        var aux;
        var para;
        
        for (para in this.parameters) {
            
            if (!this.parameters.hasOwnProperty(para)) {
                continue;
            }
                    
            if (i === 0) {
                aux = "?";
            }
            else {
                aux = "&";
            }
                                    
            result += aux + para + "=" + this.parameters[para];
            
            i++;
            
        }
    
    }

};

jsx.URL.getURL = function() {

    return this.url;

};

jsx.URL.setURL = function(url) {

    //TODO validar
    
    this.url = url;

};

jsx.URL.setParameter = function(parameter, value) {

    //TODO validar
    
    this.parameters[parameter] = value;
    this.total++;

};

jsx.URL.getParameter = function(parameter) {

    //TODO validar
    
    return this.parameters[parameter];

};

jsx.URL.removeParameter = function(parameter) {

    //TODO validar
    
    delete this.parameters[parameter];
    this.total--;

};*/
