function showHide (ID) {
if (document.getElementById(ID).style.display == "block") {
   document.getElementById(ID).style.display = "none";
}
else {
   document.getElementById(ID).style.display = "block";
}
}

function hide(ID) {
    document.getElementById(ID).style.display = "none";
}

function somenteNumeros(e) {
    var code;
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    if (!(code == 8 || code == 9 || code > 47 && code < 58))
    { return false; }
}

function validarData(control) {
    var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
    spn = document.getElementById(control.id + "Obrig");
    if (!(control.value.match(expReg)) && (control.value != '')) {
    	spn.innerHTML = "Data invalida";
        return false;
    }
    spn.innerHTML = "";
    return true;
}

function formatarData(e, control) {
    var conteudo;
    var code;
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
        
    conteudo = control.value;
    if (code == 8 || code == 9)
    { }
    else {
    	if (code > 47 && code < 58) {
	        if (conteudo.length == 2) {
	            conteudo = conteudo + "/";
	            control.value = conteudo;
	        }
	        else if (conteudo.length == 5) {
	            conteudo = conteudo + "/";
	            control.value = conteudo;
	        }
    	}
    	else
    		return false;
    }
}

function formatarCPF(e) {
    var code;
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    if (code == 8 || code == 9)
    { }
    else {
        if (code > 47 && code < 58) {
            var conteudo;
            conteudo = document.getElementById("cpf").value;
            if (conteudo.length == 3) {
                conteudo = conteudo + ".";
                document.getElementById("cpf").value = conteudo;
            }
            else if (conteudo.length == 7) {
                conteudo = conteudo + ".";
                document.getElementById("cpf").value = conteudo;
            }
            else if (conteudo.length == 11) {
                conteudo = conteudo + "-";
                document.getElementById("cpf").value = conteudo;
            }
        }
        else {
            return false;
        }
    }
}

function validarCPF() {
    var i;
    s = document.getElementById("cpf").value.replace(".", "").replace(".", "").replace("-", "");
    spn = document.getElementById(document.getElementById("cpf").id + "Obrig");
    if (s == "") { spn.innerHTML = "Campo obrigat&oacute;rio"; return false; }
    var c = s.substr(0, 9);
    var dv = s.substr(9, 2);
    var d1 = 0;
    for (i = 0; i < 9; i++) {
        d1 += c.charAt(i) * (10 - i);
    }
    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(0) != d1) {
        spn.innerHTML = "CPF invalido";
        return false;
    }
    d1 *= 2;
    for (i = 0; i < 9; i++) {
        d1 += c.charAt(i) * (11 - i);
    }
    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(1) != d1) {
        spn.innerHTML = "CPF invalido";
        return false;
    }
    spn.innerHTML = "";
    return true;
}

function formatarCNPJ(e) {
    var code;
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    if (code == 8 || code == 9)
    { }
    else {
        if (code > 47 && code < 58) {
            var conteudo;
            conteudo = document.getElementById("cpf").value;
            if (conteudo.length == 2) {
                conteudo = conteudo + ".";
                document.getElementById("cpf").value = conteudo;
            }
            else if (conteudo.length == 6) {
                conteudo = conteudo + ".";
                document.getElementById("cpf").value = conteudo;
            }
            else if (conteudo.length == 10) {
                conteudo = conteudo + "/";
                document.getElementById("cpf").value = conteudo;
            }
            else if (conteudo.length == 15) {
                conteudo = conteudo + "-";
                document.getElementById("cpf").value = conteudo;
            }
        }
        else {
            return false;
        }
    }
}

function validarCNPJ() {
    CNPJobj = document.getElementById("cpf");
    CNPJ = CNPJobj.value;
    erro = new String;

    spn = document.getElementById(CNPJobj.id + "Obrig");
    if (CNPJ == "") { spn.innerHTML = "Campo obrigat&oacute;rio"; return false; }
    
    //substituir os caracteres que nao sao numeros
    if (document.layers && parseInt(navigator.appVersion) == 4) {
        x = CNPJ.substring(0, 2);
        x += CNPJ.substring(3, 6);
        x += CNPJ.substring(7, 10);
        x += CNPJ.substring(11, 15);
        x += CNPJ.substring(16, 18);
        CNPJ = x;
    } else {
        CNPJ = CNPJ.replace(".", "");
        CNPJ = CNPJ.replace(".", "");
        CNPJ = CNPJ.replace("-", "");
        CNPJ = CNPJ.replace("/", "");
    }
    var a = [];
    var b = new Number;
    var c = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
    for (i = 0; i < 12; i++) {
        a[i] = CNPJ.charAt(i);
        b += a[i] * c[i + 1];
    }
    if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11 - x }
    b = 0;
    for (y = 0; y < 13; y++) {
        b += (a[y] * c[y]);
    }
    if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11 - x; }
    if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])) {
        erro += "CNPJ invalido";
    }
    if (erro.length > 0) {
        spn.innerHTML = erro;
        return false;
    }
    spn.innerHTML = "";
    return true;
}

function formatarTelefone(e, control) {
    var code;
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    if (code == 8 || code == 9)
    { }
    else {
        if (code > 47 && code < 58) {
            var conteudo;
            conteudo = document.getElementById(control.id).value;
            if (conteudo.length == 0) {
                conteudo = conteudo + "(";
                document.getElementById(control.id).value = conteudo;
            }
            else if (conteudo.length == 3) {
                conteudo = conteudo + ") ";
                document.getElementById(control.id).value = conteudo;
            }
            else if (conteudo.length == 9) {
                conteudo = conteudo + "-";
                document.getElementById(control.id).value = conteudo;
            }
        }
        else {
            return false;
        }
    }
}

function formatarCEP(e, control) {
    var code;
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    if (code == 8 || code == 9)
    { }
    else {
        if (code > 47 && code < 58) {
            var conteudo;
            conteudo = document.getElementById(control.id).value;
            if (conteudo.length == 5) {
                conteudo = conteudo + "-";
                document.getElementById(control.id).value = conteudo;
            }
        }
        else {
            return false;
        }
    }
}


function validarEmail() {
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);

    mail = document.getElementById("email");
    index = mail.value.indexOf("@");
    spn = document.getElementById(mail.id + "Obrig");
    if (mail.value == "") {spn.innerHTML = "Campo obrigat&oacute;rio";  return false;}
    if (!er.test(mail.value) || mail.value.charAt(index + 1) == ".") {        
        spn.innerHTML = "Email invalido";
        return false;
    }
    spn.innerHTML = ""; 
    return true;
}

function confirmarSenha() {
    senha = document.getElementById("senha");
    confirmaSenha = document.getElementById("confirmaSenha");
    spn = document.getElementById(confirmaSenha.id + "Obrig");
    if (senha.value != confirmaSenha.value) {
        spn.innerHTML = "Senhas n&atilde;o conferem";
        return false;
    }
    spn.innerHTML = "";
    return true;
}

function validarForm(form) {
    var elem = form.elements;
    var valid = true;
    var validCPF = true;
    var validCNPJ = true;
    var validEmail = true;
    for (var i = 0; i < elem.length; i++) {
        spn = document.getElementById(elem[i].id + "Obrig");
        if (spn != null) {
            if (elem[i].value == "") {
                spn.innerHTML = "Campo obrigat&oacute;rio";
                valid = false;
                elem[i].onblur = function() { validarControle(this); }
            }
        }
    }

    validEmail = validarEmail();

	document.getElementById("email").onblur = function() { validarEmail(); }
    return (valid && validEmail);
}

function validarFormContato(form) {
    var elem = form.elements;
    var valid = true;
    var validEmail = true;
    for (var i = 0; i < elem.length; i++) {
        spn = document.getElementById(elem[i].id + "Obrig");
        if (spn != null) {
            if (elem[i].value == "") {
                spn.innerHTML = "Campo obrigat&oacute;rio";
                valid = false;
                elem[i].onblur = function() { validarControle(this); }
            }
        }
    }

    validEmail = validarEmail();

    document.getElementById("email").onblur = function() { validarEmail(); }

    return (valid && validEmail);
}

function validarControle(controle) {
    spn = document.getElementById(controle.id + "Obrig");
    if(controle.value == ""){
        spn.innerHTML = "Campo obrigat&oacute;rio";
    }
    else{
        spn.innerHTML = "";
    }
}

function trataTipoPessoa() {
    pf = document.getElementById("pf");

    cpf = document.getElementById("cpf");
    cpf.value = "";   

    lblcpf_cnpj = document.getElementById("cpf_cnpj");
    lblrg_insc = document.getElementById("rg_insc");
    lblnome = document.getElementById("nome");
    if (pf.checked) {
        lblnome.innerHTML = "Nome:"
        lblcpf_cnpj.innerHTML = "CPF:"
        lblrg_insc.innerHTML = "RG:";
        cpf.maxLength = "14";
        cpf.onkeypress = function(event) { return formatarCPF(event); }
        //cpf.onblur = function() { validarCPF(); }
    }
    else {
        lblnome.innerHTML = "Raz&atilde;o social:"
        lblcpf_cnpj.innerHTML = "CNPJ:";
        lblrg_insc.innerHTML = "Inscri&ccedil;&atilde;o <abbr title='estadual/municipal'>est./muni.:</abbr>";
        cpf.maxLength = "18";
        cpf.onkeypress = function(event) { return formatarCNPJ(event); }
        //cpf.onblur = function() { validarCNPJ(); }
    }
}

function habilitaEnviar() {
    chk = document.getElementById("aceito");
    btnInscricao = document.getElementById("btnEnviar");
    if (chk.checked) {
        btnInscricao.disabled = false;
        btnInscricao.className = "botEnviar";
    }
    else {
        btnInscricao.disabled = true;
        btnInscricao.className = "botEnviarDisabled";
    }
}

function configuraListaItem(control, name) {
    nameControl = control.id.replace("chkItem", name);
    valorItem = document.getElementById(nameControl.replace(name, "valorItem"));
    valorCredito = document.getElementById("valorCredito");
    sValorCredito = document.getElementById("sValorCredito");
    
    if (control.checked) {
        document.getElementById(nameControl).style.color = "#b4ff37";
        valorCredito.value = float2Moeda(parseFloat(valorCredito.value) - parseFloat(valorItem.value));
    }
    else {
        document.getElementById(nameControl).style.color = "#bcbcbc";
        valorCredito.value = float2Moeda(parseFloat(valorCredito.value) + parseFloat(valorItem.value));
    }
    sValorCredito.innerHTML = valorCredito.value;

    div = document.getElementById(nameControl.replace("rptItemRevenda", "rptItemRevendaDescricao").replace(name, "box"));
    if (div.style.display == "block")
        div.style.display = "none";
    else
        div.style.display = "block"    
}

function verificaCheckBox(nameControl) {
    chk = document.getElementById(nameControl.replace("lblItem", 'chkItem'));
    chk.checked = (!chk.checked);
    configuraListaItem(chk, "lblItem");
}

function float2Moeda(num) {

    x = 0;

    if (num < 0) {
        num = Math.abs(num);
        x = 1;
    }
    if (isNaN(num)) num = "0";
    cents = Math.floor((num * 100 + 0.5) % 100);

    num = Math.floor((num * 100 + 0.5) / 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));
    ret = num + ',' + cents;
    if (x == 1) ret = ' - ' + ret; return ret;

}
/*
Correctly handle PNG transparency in Win IE 5.5 & 6.
*/
function fixPNG(myImage) 
{
    if ((version >= 5.5) && (version < 7) && (document.body.filters)) 
    {
       var imgID = (myImage.id) ? "id='" + myImage.id + "' " : ""
	   var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : ""
	   var imgTitle = (myImage.title) ? 
		             "title='" + myImage.title  + "' " : "title='" + myImage.alt + "' "
	   var imgStyle = "display:inline-block;" + myImage.style.cssText
	   var strNewHTML = "<span " + imgID + imgClass + imgTitle
                  + " style=\"" + "width:" + myImage.width 
                  + "px; height:" + myImage.height 
                  + "px;" + imgStyle + ";"
                  + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                  + "(src=\'" + myImage.src + "\', sizingMethod='scale');\"></span>"
	   myImage.outerHTML = strNewHTML	  
    }
}

