/** MOSTRA E ESCONDE DIVS **/
var lnk = null;
function find(id)
{
    return document.getElementById(id);
}

function getStyle(id){
    var obj = find(id);
    if(obj != null)
        return find(id).style;    
    else
        return obj;
}

function HideLayer(id){
    var objStyle = getStyle(id);
    if(objStyle!=null)
    {
	    getStyle(id).visibility = "hidden";
	    getStyle(id).display = "none";
	}
}
function ShowLayer(id){	
    var objStyle = getStyle(id);
    if(objStyle!=null)
    {
	    getStyle(id).visibility = "visible";
	    getStyle(id).display = "block";
	}
}

/** TROCA CLASSE **/

function toggleSub(obj)
{
    var objLista = document.getElementById('subMenuEixo');
    if(objLista.style.display=='none')
    {
        objLista.style.display='block';
        obj.innerHTML = '<img src="../App_Themes/front/img/icoMenos.gif" style="margin: 0pt 2px 0pt 0pt;" align="absmiddle">Fechar';
        obj.parentNode.parentNode.parentNode.style.background="#f5fafd";
    }else
    {
        objLista.style.display='none';
        obj.innerHTML = '<img src="../App_Themes/front/img/icoMais.gif" style="margin: 0pt 2px 0pt 0pt;" align="absmiddle">Categorias';
        obj.parentNode.parentNode.parentNode.style.background="#e6f2f9";
    }
    
}

function change(id, newClass) {
	var i;
	var botoes=new Array("imgIgredientes","imgAplicacao");
	for (i=0;i<=1;i++) {		
		identity=document.getElementById("bt_"+i);
		identity.className=botoes[i];
	}
	identity=document.getElementById(id);
	identity.className=newClass;    
}


function MakeActive(num){
    	if(document.getElementById){
    	    if(lnk != null)
    	    {
        	    for(i=0;i<lnk.length;i++){
        		    lnk[i].style.background = "#63b2f3";
        		    lnk[num].style.background = "#1287e8";
        	    }
        	}
    	}
}

function toggleMenuOVER(menu)
{
    var objMenu = document.getElementById(menu);
    if(objMenu.style.display=='none'){
        objMenu.style.display='block';}
}

function toggleMenuOUT(menu)
{
    var objMenu = document.getElementById(menu);
    if(objMenu.style.display!='none'){
        objMenu.style.display='none';}
}

function MakeActiveInit() {
    	if(document.getElementById){
        	lnk = document.getElementById("Rosto").getElementsByTagName("a");
        	if(lnk != null)
        	{
        	    for(i=0;i<lnk.length;i++){
        		    lnk[i].onfocus=new Function("if(this.blur)this.blur()");
        		    lnk[0].style.background = "#63b2f3";
		        }
		    }
        }	
}
function ValidaText(obj,Css,CssErro)
{
    var ret = true;
    if (obj.value == "") {
        if(document.getElementById(obj.name + 'Obrig') != null)
            document.getElementById(obj.name + 'Obrig').style.display = "inline-block";
        ret = false;
        obj.className = Css + " " + CssErro;
    } else {
        if (document.getElementById(obj.name + 'Obrig') != null)
            document.getElementById(obj.name + 'Obrig').style.display = "none";
        obj.className = Css;
    }
    return ret;
}

function ComparaText(obj,objComp,Css,CssErro)
{
    if(ValidaText(objComp,Css,CssErro))
    {
        var ret = true;
        if(obj.value!=objComp.value){
            ret = false;
            objComp.className = Css + " " + CssErro;
        }else{objComp.className = Css;}
        return ret;
    }else
    {
        return false;
    }
}

function ValidaEmail(obj,Css,CssErro)
{
    if(ValidaText(obj,Css,CssErro))
    {
        var ret = true;
        er = /^[a-zA-Z0-9][a-zA-Z0-9\._-]+@([a-zA-Z0-9\._-]+\.)[a-zA-Z-0-9]{2}/;
        if (er.exec(obj.value)) {
          document.getElementById(obj.name + 'Obrig').style.display = "none";
          obj.className = Css;
      } else {
          document.getElementById(obj.name + 'Obrig').style.display = "inline-block";
          ret = false;
         obj.className = Css + " " + CssErro;
        }
        return ret;
    }else
    {
        return false;
    }
}

function checkDate(dia, mes, ano) {
    var dateRegExp = /^(19|20)\d\d-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$/;
    if (!dateRegExp.test(ano + "-" + mes + "-" + dia)) {
        document.getElementById('nascimentoObrig').style.display = "inline-block";
        return false; // formato invÃ¡lido
    }
    if (dia == 31 && (/^0?[469]$/.test(mes) || mes == 11)) {
        document.getElementById('nascimentoObrig').style.display = "inline-block";
        return false; // dia 31 de um mes de 30 dias
    } else if (dia >= 30 && mes == 2) {
        document.getElementById('nascimentoObrig').style.display = "inline-block";
        return false; // mais de 29 dias em fevereiro
    } else if (mes == 2 && dia == 29 && !(ano % 4 == 0 && (ano % 100 != 0 || ano % 400 == 0))) {
        document.getElementById('nascimentoObrig').style.display = "inline-block";
        return false; // dia 29 de fevereiro de um ano não bissexto
    } else {
        document.getElementById('nascimentoObrig').style.display = "none";
        return true; // Data válida
    }
}



function ValidaRadio(name)
{
    var Objs = document.getElementsByName(name);
    for(var i =0;i<=Objs.length-1;i++) {
        if(Objs[i].checked) {
            return true;
        }
    }    
    return false;
}

function ValidaSelect(obj,Css,CssErro)
{
    if (obj.selectedIndex < 1) {
        if(document.getElementById(obj.name + 'Obrig') != null)
            if (document.getElementById(obj.name + 'Obrig') != null)
                document.getElementById(obj.name + 'Obrig').style.display = "inline-block";

        obj.className = Css + ' ' + CssErro;
        return false;
    } else {
        if (document.getElementById(obj.name + 'Obrig') != null)
            document.getElementById(obj.name + 'Obrig').style.display = "none";
        obj.className = Css;
        return true;
    }
}

function ValidaTelefone(obj,Css,CssErro)
{
    if(ValidaText(obj,Css,CssErro))
    {
        if(!validaChars(obj.value,' 0123456789-()'))
        {
            obj.className = Css + ' ' + CssErro;
            return false;
        }else
        {
            obj.className = Css;
            return true;
        }
    }
    else
    {
        return false;
    }
}

function ValidaCPF(obj,Css,CssErro)
{
    if(ValidaText(obj,Css,CssErro))
    {
        if(!validaChars(obj.value,' 0123456789-.')) {
            if (document.getElementById(obj.name + 'Obrig') != null)
                document.getElementById(obj.name + 'Obrig').style.display = "inline-block";
            
            obj.className = Css + ' ' + CssErro;
            return false;
        }else
        {
            if(!validaCPF(obj)) {
                obj.className = Css + ' ' + CssErro;
                return false;
            }
            obj.className = Css;
            return true;
        }
    }
    else
    {
        return false;
    }
}

function ValidarCPF(obj) {
    var i;
    s = obj.value.replace(".", "").replace(".", "").replace("-", "");
    spn = document.getElementById(obj.name + "Obrig");
    if (s == "") {
        spn.style.display = "inline-block";
        return false;
    }
    if (s == "00000000000" || s == "11111111111" || s == "22222222222" || s == "33333333333" || s == "44444444444" || s == "55555555555" || s == "66666666666" || s == "77777777777" || s == "88888888888" || s == "99999999999") {
        spn.style.display = "inline-block";
        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.style.display = "inline-block";
        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.style.display = "inline-block";
        return false;
    }
    spn.style.display = "none";
    return true;
}



function validaChars(sText,chars)

{
    var ValidChars = chars;
    var IsNumber=true;
    var Char;


    for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
    return IsNumber;

}

function popTermos(mypage, myname, w, h, chscroll) {
 var winLargura = (screen.width - w) / 2;
 var winAltura = (screen.height - h) / 2;
 winprops = 'height='+h+',width='+w+',top='+winAltura+',left='+winLargura+',scrollbars='+chscroll+',resizable'
 win = window.open(mypage, myname, winprops)

 if (parseInt(navigator.appVersion) >= 4){
 win.window.focus();
 }
}

function SomenteNumero(e){
    var tecla=(window.event)?event.keyCode:e.which;
    if((tecla > 47 && tecla < 58)) return true;
    else{
    if (tecla != 8) return false;
    else return true;
    }
}

function validaCPF(obj) {
cpf = obj.value.toString().replace("-", "").replace(".","").replace(".","");
erro = new String;
if (cpf.length < 11) erro += "Sao necessarios 11 digitos para verificacao do CPF! \n\n"; 
var nonNumbers = /\D/;
if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! \n\n"; 
if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
erro += "Numero de CPF invalido!"
}
    var a = [];
    var b = new Number;
    var c = 11;
    for (i=0; i<11; i++){
        a[i] = cpf.charAt(i);
        if (i < 9) b += (a[i] * --c);
    }
    if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
    b = 0;
    c = 11;
    for (y=0; y<10; y++) b += (a[y] * c--); 
    if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
    if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
    erro +="Digito verificador com problema!";
    }
    if (erro.length > 0){
        return false;
    }
    return true;
}

function mascaraTexto(e, mascara, somenteNumeros) {

    var campo, valor, i, tam, caracter;

    if (somenteNumeros) {
        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) {
                if (document.all) // Internet Explorer  
                    campo = e.srcElement;
                else // Nestcape, Mozzila  
                    campo = e.target;

                valor = campo.value;
                tam = valor.length;

                for (i = 0; i < mascara.length; i++) {
                    caracter = mascara.charAt(i);
                    if (caracter != "9")
                        if (i < tam & caracter != valor.charAt(i))
                            campo.value = valor.substring(0, i) + caracter + valor.substring(i, tam);
                }
            }
            else {
                return false;
            }
        }
    }
    else {
        if (document.all) // Internet Explorer  
            campo = e.srcElement;
        else // Nestcape, Mozzila  
            campo = e.target;

        valor = campo.value;
        tam = valor.length;

        for (i = 0; i < mascara.length; i++) {
            caracter = mascara.charAt(i);
            if (caracter != "9")
                if (i < tam & caracter != valor.charAt(i))
                    campo.value = valor.substring(0, i) + caracter + valor.substring(i, tam);
        }
    }
} 
