﻿Funcoes = function() { }

Funcoes.prototype = {
    FomatoMoeda: function (valor) {
        return 'R$ ' + parseFloat(valor).toFixed(2).replace(".", ",");
//        var valSplit = valor.toString().split('.');
//        var valInt = valSplit[0];
//        var valDec = ((valSplit.length > 1) ? valSplit[1] : '00');

//        if (valDec.toString().length == 1)
//            valDec = valDec.toString() + '0';
//        else if (valDec.length > 2)
//            valDec = valor.toFixed(2).split('.')[1];
//        
//        return 'R$ ' + valInt.toString() + ',' + valDec.toString();
    },
    CarregaArquivoXML: function (url) {
        var xmlDoc;
        if (window.XMLHttpRequest) {
            var xmlDoc = new XMLHttpRequest();
            xmlDoc.open("GET", url, false);
            xmlDoc.send(null);
            return xmlDoc.responseXML;
        } else if (window.ActiveXObject) {
            var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
            xmlDoc.async = false;
            xmlDoc.load(url);
            return xmlDoc;
        }
    },
    HtmlEncode: function (str) {
        str = new String(str);
        var strRetorno = new String();

        for (var i = 0; i < str.length; i++) {
            var s = str.substring(i, i + 1);

            switch (s) {
                case "<":
                    strRetorno += "&lt;";
                    break;
                case ">":
                    strRetorno += "&gt;";
                    break;
                case "'":
                    strRetorno += "&quot;";
                    break;
                case "\"":
                    strRetorno += "&quot;";
                    break;
                case "&":
                    strRetorno += "&amp;";
                    break;
                default:
                    strRetorno += s;
            }
        }

        return strRetorno;
    },
    HtmlDecode: function (str) {
        str = new String(str);
        var strRetorno = new String();
        var valores = str.split(";");

        for (var i = 0; i < valores.length; i++) {
            var s = valores[i];

            if (s.indexOf("&lt") > -1) {
                s = s.replace("&lt", "<");
            }
            else if (s.indexOf("&gt") > -1) {
                s = s.replace("&gt", ">");
            }
            else if (s.indexOf("&quot") > -1) {
                s = s.replace("&quot", "\"");
            }
            else if (s.indexOf("&amp") > -1) {
                s = s.replace("&amp", "&");
            }
            else {
                s = s + ";";
            }

            strRetorno += s;
        }

        if (!valores[valores.length - 1].length > 0) {
            strRetorno = strRetorno.substring(0, strRetorno.length - 1);
        }

        return strRetorno;
    },
    ControlaTamanho: function (obj, objcont, size) {
        if (obj.value.length > size)
            obj.value = obj.value.substring(0, size);
        objcont.value = size - obj.value.length;
    }
}
