function soNumero(e){
    if (!e) e = window.event; 
    if (e.keyCode) var tecla = e.keyCode;      
    else if (e.which) var tecla = e.which;
    else if (e.charCode) var tecla = e.charCode;

    if ((tecla == 37) || (tecla == 39) || (tecla == 9) || (tecla > 47 && tecla < 58)) {
        return true;
    }
    else {
        if ((tecla == 13)||(tecla==9)) {
            return false;
    }
    if (tecla != 8) {
        return false;
    }
    else
        return true;
    }
}

function soLetra(elem) {
    //padrao=/[^a-zA-Ză\s\.]/g;
    //if (padrao.test(elem)) {
//        return false;
    //}
    //else {
        return true;
    //}
}

function abreSub(qual) {
    document.getElementById(qual).style.display="block";
}

function mudaQtde(qual, valor) {
	var elem = document.getElementById("qtde_" + qual);
	if (parseInt(elem.value)) {
		var total = parseInt(elem.value) + parseInt(valor);
		if (total > 0) elem.value = total;
	}
	else elem.value = 1;
}

function validaEmail(str) {
	var at = "@";
	var dot = ".";
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);
	if (str.indexOf(at)== -1) return false;
	if (str.indexOf(at)== -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) return false;
	if (str.indexOf(dot)== -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) return false;
	if (str.indexOf(at,(lat+1)) != -1) return false;
	if (str.substring(lat-1,lat) == dot || str.substring(lat+1,lat+2) == dot) return false;
	if (str.indexOf(dot,(lat+2)) == -1) return false;
	if (str.indexOf(" ") != -1) return false;
	return true;			
}

function validaCPF(cpf){
    var i;
    var soma = soma2 = 0;
    if (!(cpf.match(/^[0-9]{3,3}[.]{0,1}[0-9]{3,3}[.]{0,1}[0-9]{3,3}[-]{0,1}[0-9]{2,2}$/))) {
        return false;
    }
    cpf = retiraCaracter(retiraCaracter(cpf, '.'), '-');
    for(i=0;i<10;i++) {
        if (i<9) {
            soma += cpf.charAt(i) * (10 - i);
        }
        soma2 += cpf.charAt(i) * (11 - i);
    }
    if(((soma % 11) < 2 ? 0 : 11 - (soma  % 11 )) != cpf.charAt(9)) {
        return false;
    }
    return (((soma2 % 11 ) < 2 ? 0 : 11 - (soma2 % 11 )) != cpf.charAt(10)) ? false : true;
}

function retiraCaracter(string, caracter) {
    var i = 0;
    var final = '';
    while (i < string.length) {
        if (string.charAt(i) == caracter) {
            final += string.substr(0, i);
            string = string.substr(i+1, string.length - (i+1));
            i = 0;
        }
        else {
            i++;
        }
    }
    return final + string;
}

function mudacampo(campo, quantidade, destino, tecla) {
	ultimo = campo.value.substring(campo.value.length-1, campo.value.length);
	if (!isNumber(ultimo)) campo.value = campo.value.substring(0, campo.value.length-1);
	if (campo.value.length == quantidade && tecla != 37 && tecla != 39) destino.focus();
}

function checaSlct(elem) {
    if (elem.selectedIndex == -1 || elem[elem.selectedIndex].value == "")
        return false;
    else
        return true;
}

function isNumber(numero){
	var CaractereInvalido = false;
	
	for (i=0; i < numero.length; i++){
		var Caractere = numero.charAt(i);
		if (isNaN(parseInt(Caractere))) CaractereInvalido = true;
	}
	return !CaractereInvalido;
}

function checaEnquete(f) {
    var marcado = false;
	for (i = 0; i < f.opcao.length; i++)
		if (f.opcao[i].checked)
			marcado = true;
	if (!marcado) {
        alert('Selecione uma alternativa!');
        return false;
	}
	else
	    return true;
}

function checaBusca(f) {
    if (!f.busca.value) {
        alert('Voce deve digitar alguma palavra para fazer a busca!');
        f.busca.focus();
        return false;
    }
    if (f.busca.value.length < 3) {
        alert('A palavra deve ter pelo menos 3 digitos para fazer a busca!');
        f.busca.focus();
        return false;
    }
    return true;
}

function indique(url) {
	window.open('Indique.aspx?url='+url,'indique','width=375,height=368,menubar=no,toolbar=no,scrollbars=no');
}

function comentarios(codigo) {
	window.open('Comentarios.aspx?codigo='+codigo,'comentarios','width=392,height=368,menubar=no,toolbar=no,scrollbars=yes');
}

function comocomprar(content,larg,alt) {
	winleft = (screen.width-larg)/2;
	wintop  = (screen.height-alt)/2;
 	top.consoleRef=window.open(content,'comocomprar','width='+ larg+',height='+ alt +',resizable=no,scrollbars=no');
	top.consoleRef.window.moveTo(winleft,wintop);
	top.consoleRef.focus();
}

function ovatel(content,larg,alt) {
	winleft = (screen.width-larg)/2;
	wintop  = (screen.height-alt)/2;
 	top.consoleRef=window.open(content,'comocomprar','width='+ larg+',height='+ alt +',resizable=no,scrollbars=no');
	top.consoleRef.window.moveTo(winleft,wintop);
	top.consoleRef.focus();
}

function DoAddBookmark(title,url) {
	if (window.sidebar)	{
		window.sidebar.addPanel(title, url,"");
	}
	else if (document.all)	{
		window.external.AddFavorite( url, title);
	}
	else {
		return true;
	}
}

function popup(nome,url,wi,he) {
	winleft = (screen.width-wi)/2;
	wintop  = (screen.height-he)/2;
	janela = window.open(url,nome,'width=' + wi + ', height=' + he + ', toolbar=no, statusbar=no, scrollbars=auto');
	janela.window.moveTo(winleft,wintop);
}

$(document).ready(function(){
	var menu = $("#menu-lateral");
	var options = menu.find("h2 a").filter(function(index){
		return ($(this).attr("class") != "link-aberto")
	});

	options.click(function(){
		var parent = $(this).parent();
		var list = parent.next();

		    $.each(parent.parent().find("h2"), function(index, item){
		        var css = $(this).attr("class");		        
		        $(this).attr("class",css.replace("-on","-off"));
		    });		

		    parent.attr("class",parent.attr("class").replace("-off", "-on"));
			menu.find("ul").filter(function(index){
				return ($(this).attr("class") != "opt-aberto")
			}).slideUp(700);			

		if ((list[0].tagName.toLowerCase()) == "ul") {			
			list.slideDown(800);			
		}

		return false;
	});
});