function Mascara_Eris(Campo, Tecla) {
    var tecla = Tecla.keyCode;
    var vr = new String(Campo.value);

    vr = vr.replace("-", "");
    vr = vr.replace("/", "");
    vr = vr.replace(".", "");
    tam = vr.length;

    if (tecla != 9 && tecla != 8 && tecla != 46) {
        if (tam > 7 && tam < 9) {
            Campo.value = vr.substr(0, 7) + '-' + vr.substr(7, tam);
        }
        if (tam >= 9 && tam < 12) {
            Campo.value = vr.substr(0, 7) + '-' + vr.substr(7, 2) + '/' + vr.substr(9, 3);
        }
        if (tam >= 12) {
            Campo.value = vr.substr(0, 7) + '-' + vr.substr(7, 2) + '/' + vr.substr(9, 3) + '/' + vr.substr(13, 5);
        }
    }
}

//Para usar 
//<input type="text" 
//onBlur="javascript:this.value=FormataNumero(this.value,2);">
function FormataNumero(numero, casas) {
    numero = numero.replace(",", "");
    numero = numero.replace(".", "");

    negativo = false;

    if (numero == "" || numero == "0")
        return "0,00";

    numero = numero.replace(".", "");

    //if (numero.indexOf(',')==-1) numero+= ",00";

    num = numero.split(",");

    aux = "";
    y = 1;
    alert(num[0].length);

    for (x = num[0].length; x > 0; x--) {
        aux += num[0].substr(x - 1, 1);
        if (y % 3 == 0 && x > 1)
            aux += ".";
        y++;
    }
    ret = "";
    for (x = aux.length; x > 0; x--)
        ret += aux.substr(x - 1, 1);

    if (casas > 0)
        ret += ",";

    if (num[1].length >= casas)
        len = casas
    else
        len = num[1].length;

    for (x = 0; x < len; x++)
        ret += num[1].substr(x, 1);

    for (y = x; y < casas; y++)
        ret += "0";

    if (negativo)
        ret = "-" + ret;

    return ret;
}

//Uso: onkeypress="javascript:AtualizaCampo(valorZ, 12);"
function AtualizaCampo(id, valor) {
    $(id).innerText = formatCurrencyToStr(valor);
}

function AtualizaCampoPercentualValue(id, id2, valor, percentual, percentual2) {
    val = $(valor).value;

    val = val.toString().replace(/[.]/g, '');
    val = val.replace(/,/g, '.');

    perc = $(percentual).value;

    perc = perc.toString().replace(/[.]/g, '');
    perc = perc.replace(/,/g, '.');

    result = (val * perc) * 0.01;

    perc2 = $(percentual2).value;

    perc2 = perc2.toString().replace(/[.]/g, '');
    perc2 = perc2.replace(/,/g, '.');

    result2 = (val * perc2) * 0.01;

    result = result.toString().replace(/[.]/g, ',');
    $(id).innerText = formatCurrencyToStr(result);
    result2 = result2.toString().replace(/[.]/g, ',');
    $(id2).innerText = formatCurrencyToStr(result2);
}

function AtualizaCampoPercentualObjetos(id, valor, percentual) {
    val = $(valor).value;

    val = val.toString().replace(/[.]/g, '');
    val = val.replace(/,/g, '.');

    perc = $(percentual).value;

    perc = perc.toString().replace(/[.]/g, '');
    perc = perc.replace(/,/g, '.');

    result = (val * perc) * 0.01;

    result = result.toString().replace(/[.]/g, ',');
    $(id).innerText = formatCurrencyToStr(result);
}

function AtualizaCampoPercentualTotalEngineeringFee(id, val, percentual, id2, percentual2) {
    val = val.toString().replace(/[.]/g, '');
    val = val.replace(/,/g, '.');

    perc = $(percentual).value;

    perc = perc.toString().replace(/[.]/g, '');
    perc = perc.replace(/,/g, '.');

    result = (val * perc) * 0.01;

    perc2 = $(percentual2).innerText;

    perc2 = perc2.toString().replace(/[.]/g, '');
    perc2 = perc2.replace(/,/g, '.');

    result2 = (result * perc2) * 0.01;

    result = result.toString().replace(/[.]/g, ',');
    $(id).innerText = formatCurrencyToStr(result);
    result2 = result2.toString().replace(/[.]/g, ',');
    $(id2).innerText = formatCurrencyToStr(result2);
}

function AtualizaNetPremiumTotal(id, id2, grosspremium, brokerage, percentual, technicalpremium) {
    grosspremium = grosspremium.toString().replace(/[.]/g, '');
    grosspremium = grosspremium.replace(/,/g, '.');

    brokerage = brokerage.toString().replace(/[.]/g, '');
    brokerage = brokerage.replace(/,/g, '.');

    perc = $(percentual).value;

    perc = perc.toString().replace(/[.]/g, '');
    perc = perc.replace(/,/g, '.');

    perc = (perc * 1) + (brokerage * 1);

    perc = (grosspremium * perc) * 0.01;

    result = grosspremium - perc;

    technicalpremium = technicalpremium.toString().replace(/[.]/g, '');
    technicalpremium = technicalpremium.replace(/,/g, '.');

    result2 = result * 100, 00;

    result2 = (result2) / (technicalpremium);

    result = result.toString().replace(/[.]/g, ',');
    $(id).innerText = formatCurrencyToStr(result);
    result2 = result2.toString().replace(/[.]/g, ',');
    $(id2).innerText = formatCurrencyToStr(result2);
}

function formatCurrency(field) {
    num = field.value.toString();

    num = num.toString().replace(/[.]/g, '');
    num = num.replace(/,/g, '.');
    if (isNaN(num)) {
        num = "0";
    }
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + '.' +
	    num.substring(num.length - (4 * i + 3));

    field.value = (((sign) ? '' : '-') + num + ',' + cents);
}


function formatCurrencyToStr(num) {
    num = num.toString().replace(/[.]/g, '');
    num = num.replace(/,/g, '.');
    if (isNaN(num)) {
        num = "0";
    }
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + '.' +
	    num.substring(num.length - (4 * i + 3));

    return (((sign) ? '' : '-') + num + ',' + cents);
}

function txtBoxFormat(objeto, sMask, evtKeyPress) {
    var i, nCount, sValue, fldLen, mskLen, bolMask, sCod, nTecla;
    if (document.all) { //Internet Explorer
        nTecla = evtKeyPress.keyCode;
    }
    else if (document.layers) { //Nestcape
        nTecla = evtKeyPress.which;
    }
    else {
        nTecla = evtKeyPress.which;
        if (nTecla == 8) {
            return true;
        }
    }
    sValue = objeto.value;
    /* Limpa todos os caracteres de formata‡Æo que
    j  estiverem no campo*/
    sValue = sValue.toString().replace("-", "");
    sValue = sValue.toString().replace("-", "");
    sValue = sValue.toString().replace(".", "");
    sValue = sValue.toString().replace(".", "");
    sValue = sValue.toString().replace("/", "");
    sValue = sValue.toString().replace("/", "");
    sValue = sValue.toString().replace(":", "");
    sValue = sValue.toString().replace(":", "");
    sValue = sValue.toString().replace("(", "");
    sValue = sValue.toString().replace("(", "");
    sValue = sValue.toString().replace(")", "");
    sValue = sValue.toString().replace(")", "");
    sValue = sValue.toString().replace(" ", "");
    sValue = sValue.toString().replace(" ", "");
    fldLen = sValue.length;
    mskLen = sMask.length;
    i = 0;
    nCount = 0;
    sCod = "";
    mskLen = fldLen;

    while (i <= mskLen) {
        bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
        bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))
        if (bolMask) {
            sCod += sMask.charAt(i);
            mskLen++;
        }
        else {
            sCod += sValue.charAt(nCount);
            nCount++;
        }
        i++;
    }
    objeto.value = sCod;
    if (nTecla != 8) { // backspace
        if (sMask.charAt(i - 1) == "9") {
            return ((nTecla > 47) && (nTecla < 58));
        }
        else {
            return true;
        }
    }
    else {
        return true;
    }
}

function SetFocus() {
    document.Form1['TextBox1'].focus();
}

function Tecla(e) {
    var key;
    var event = e;

    if (!e) var e = window.event;
    if (e.keyCode) key = e.keyCode;
    else if (e.which) key = event.which; // Netscape 4.?               //
    else if (e.charCode) key = e.charCode; // Mozilla                  //
    else
        return true

    if (key > 47 && key < 58)  // numeros de 0 a 9
        return true;
    else {
        if (key != 8) // backspace
            return false;
        else
            return true;
    }
}

function formatar(src, mask) {
    var i = src.value.length;
    var saida = mask.substring(0, 1);
    var texto = mask.substring(i)
    if (texto.substring(0, 1) != saida) {
        src.value += texto.substring(0, 1);
    }
}

function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e) {
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.event) ? e.keyCode : e.which;
    if (whichCode == 13 || whichCode == 8) return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave    
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida    
    len = objTextBox.value.length;
    for (i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for (; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i)) != -1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0' + SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0' + SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
            objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

function pageIsValid() {
    if (HasPageValidators()) {
        //http://sandblogaspnet.blogspot.com/2009/04/calling-validator-controls-from.html
        if (Page_Validators != undefined && Page_Validators != null) {
            //Looping through the whole validation collection.
            for (var i = 0; i < Page_Validators.length; i++) {
                ValidatorValidate(Page_Validators[i]);
                //if condition to check whether the validation was successfull or not.
                if (!Page_Validators[i].isvalid) {
                    Page_IsValid = false;
                    return false;
                }
            }
        } else
            return true;
    }
    return true;
}


//Handle Page_Validators is not defined error
//http://www.velocityreviews.com/forums/t88987-pagevalidators-error.html
function HasPageValidators() {
    var hasValidators = false;
    try {
        if (Page_Validators.length > 0) {
            hasValidators = true;
        }
    }
    catch (error) { }
    return hasValidators;
}

function ShowDiv() {
    if (pageIsValid())
        document.getElementById('divFloat').style.visibility = 'visible';
}
