// STRING //
String.prototype.trim = function() {
	return this.replace(/^\s*|\s*$/g, "")
}

String.prototype.lpad = function(pSize, pCharPad){
	var str = this
	var dif = pSize - str.length
	var ch = String(pCharPad).charAt(0)
	for (; dif>0; dif--) str = ch + str
	return (str)
}

String.prototype.finalCom = function() { //determina se existe ou não determinada extensão em um filename
	var bOk = false
	for (var i = 0; i < arguments.length; i++) {
		if (this.indexOf(arguments[i]) == this.length - arguments[i].length) {
			bOk = true
			break
		}
	}
	return bOk
}


// ARRAY //
Array.prototype.push = function(v) {
	this[this.length] = v
	return v
}


// DATE //
Date.prototype.getNomeMes = function() { // retorna o nome do mês
	return ["Janeiro","Fevereiro","Março","Abril","Maio","Junho","Julho","Agosto",
			"Setembro","Outubro","Novembro","Dezembro"][this.getMonth()]
}

Date.prototype.getHumanDateString = function() {
	return this.getMonthName() + " " + this.getDate() + ", " + this.getFullYear()
}


// OTHERS //
function url_encode(str) { 
    var hex_chars = "0123456789ABCDEF"; 
    var noEncode = /^([a-zA-Z0-9\_\-\.])$/; 
    var n, strCode, hex1, hex2, strEncode = ""; 
    for(n = 0; n < str.length; n++) { 
        if (noEncode.test(str.charAt(n))) { 
            strEncode += str.charAt(n); 
        } else { 
            strCode = str.charCodeAt(n); 
            hex1 = hex_chars.charAt(Math.floor(strCode / 16)); 
            hex2 = hex_chars.charAt(strCode % 16); 
            strEncode += "%" + (hex1 + hex2); 
        } 
    } 
    return strEncode; 
}

function url_decode(str) { 
    var n, strCode, strDecode = ""; 
    for (n = 0; n < str.length; n++) { 
        if (str.charAt(n) == "%") { 
            strCode = str.charAt(n + 1) + str.charAt(n + 2); 
            strDecode += String.fromCharCode(parseInt(strCode, 16)); 
            n += 2; 
        } else { 
            strDecode += str.charAt(n); 
        } 
    } 
    return strDecode; 
}

function mascaraValor() {
    var valor          = document.getElementById("subtotal").value;
    valor = valor.replace (".","");
    valor = valor.replace (",","");
    var tamanho        = valor.length;
    var iniciodecimais = tamanho - 3;
    if(tamanho > 3) { // então deve colocar separador
        var decimais        = valor.substr(iniciodecimais,3);
        var inteiros        = valor.substr(0,iniciodecimais);
        var stringinvertida = "";
        var tamanhoinverte = tamanho - 3;
        while (tamanhoinverte > 0) {
            tamanhoinverte -= 1;
            stringinvertida += valor.substr(tamanhoinverte,1);
        }
        var j = 0;
        var decimaisformatado = "";
        var stringformatado = "";
        for (i = 0; i <= stringinvertida.length; i++) {
            j = j + 1;
            stringformatado = stringformatado + stringinvertida.substr(i,1);
            if (j == 3 && stringformatado != "") {
                if (decimaisformatado == "") {
                    decimaisformatado = stringformatado;
                } else {
                    decimaisformatado +=  '.' + stringformatado;
                }
                stringformatado = "";
                j = 0;
            }
        }
        if (j == 2 && tamanho != 4) {
            decimaisformatado += "." + stringformatado
        } else {
            decimaisformatado += stringformatado;
        }
        
        tamanhoinverte = decimaisformatado.length;
        var stringfinal = "";
        while (tamanhoinverte > 0) {
            tamanhoinverte = tamanhoinverte - 1;
            stringfinal += decimaisformatado.substr(tamanhoinverte,1);
        }
        document.getElementById("subtotal").value = stringfinal + "," + decimais;
    }
}
