/* Consulta Remédios */

$(document).ready(function(){
    $('img.btmenu').click(function(){
    	$('.menu').fadeIn(10);
    })

    $('.menu').mouseleave(function(){
    	$('.menu').fadeOut(10);
    })
    
    $('.form-busca').click(function(){
    	$('#topo-b').slideDown('fast');
        $('.menu').css('display', 'none');
    })

    $('.form-busca').click(function(){
    	$('#principal').css('margin-top','170px');
    })
    
    $('#post').getTwitter({
    	userName: 'c_remedios',
    	numTweets: 1, 
    	loaderText: 'Carregando Tweets...',
    	slideIn: true,
    	slideDuration: 750,
    	showHeading: true,
    	showProfileLink: true,
    	showTimestamp: true
    });
    
    $('.menu a, #inicio').click(function(abrir){
        $('.menu').css('display', 'none');
    });
    
    $("#marcar_subativas").click(function(){
       marcar("#lista-subativas", this.checked);
    });

    $("#marcar_apr").click(function(){
       marcar("#lista-apr", this.checked);
    });

    $("#marcar_fabs").click(function(){
       marcar("#lista-fabs", this.checked);
    });

	$("#f-enviar-comentario").click(function(){
		$(".f-comentarios").slideDown();
	})
    
    // Comparativo de Preços
	$("a.mudar-tipo-list").toggle(function(){
		$(this).addClass("swap"); 
		$("div.list-busca").fadeOut("fast", function() {
			$(this).fadeIn("fast").addClass("b"); 
		});
	
	}, function () {
		$(this).removeClass("swap");
		$("div.list-busca").fadeOut("fast", function() {
			$(this).fadeIn("fast").removeClass("b");
		});
	});
});

function marcar(idsel, valor){
    if (idsel == null || idsel == undefined )
    return;
    $(idsel + " input[type=checkbox]").each(function(){
        this.checked = valor;
    });
}

function trim(str){
    return str.replace(/^\s+|\s+$/g,"");
}

function checkMail(mail) {
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);

    if(typeof(mail) == "string"){
        if(er.test(mail)){ return true; }
    } else if (typeof(mail) == "object") {
        if(er.test(mail.value)) { 
            return true; 
        }
    }else {
        return false;
    }   
}

function aviso(t){
    return "Consulta Remédios           \n\nAviso\n\n"+t;
}

function checarConsulta(f){
    if ( (trim(f.nome.value) == "" ) || (trim(f.nome.value) == "Pesquisa pelo nome, substância ou código" ) ){
        alert(aviso("Digite algum nome para iniciar a Consulta."));
        f.nome.focus();
        return (false);
    }
    f.submit;
}

function checarfaleconosco(f){
    if (trim(f.nome.value) == "" ) {
        alert(aviso("Informe seu nome."));
        f.nome.focus();
        return (false);
    }

    if (!checkMail(trim(f.email.value))) {
        alert(aviso("Informe seu e-mail corretamente."));
        f.email.focus();
        return (false);
    }

    if (trim(f.cidade.value) == 0 ) {
        alert(aviso("Informe sua cidade."));
        f.cidade.focus();
        return (false);
    }

    if (trim(f.mensagem.value) == "" ) {
        alert(aviso("Informe sua mensagem."));
        f.mensagem.focus();
        return (false);
    }

    f.submit;
}

this.vtip = function(){    
    this.xOffset = -35;
    this.yOffset = 20;       
    $(".vtip").unbind().hover(
        function(e){
            this.t = this.title;
            this.title = ''; 
            this.top = (e.pageY + yOffset); this.left = (e.pageX + xOffset);
            $('body').append( '<p id="vtip"><img id="vtipArrow" />' + this.t + '</p>' );
            $('p#vtip #vtipArrow').attr("src", '/imagens/tpa.png');
            $('p#vtip').css("top", this.top+"px").css("left", this.left+"px").fadeIn("slow");
        },
        function(){
            this.title = this.t;
            $("p#vtip").fadeOut("slow").remove();
        }
    ).mousemove(
        function(e){
            this.top = (e.pageY + yOffset);
            this.left = (e.pageX + xOffset);
            $("p#vtip").css("top", this.top+"px").css("left", this.left+"px");
        }
    );            
};
jQuery(document).ready(function($){vtip();})

/* Anunciante */
function limpaString(s) {
	var digitos = "0123456789";
	var temp = "";
	var digito = "";

    for (var i=0; i<s.length; i++){
		digito = s.charAt(i);
		if (digitos.indexOf(digito)>=0){temp=temp+digito}
	}
	return temp
}

function valida_CPF(s) {
	var i;
	s = limpaString(s);
	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);
	}
	if (d1 == 0) return false;
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(0) != d1){
		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){
		return false;
	}
	return true;
}

function valida_CNPJ(cnpj){
    cnpj = cnpj.replace(/\./g, "");
    cnpj = cnpj.replace(/\-/g, "");
    cnpj = cnpj.replace(/\_/g, "");
    cnpj = cnpj.replace(/\//g, "");
    
    if(cnpj.length!=14){ var result = false; };
    
    pri = eval(cnpj.substring(0,2));
    seg = eval(cnpj.substring(3,6));
    ter = eval(cnpj.substring(7,10));
    qua = eval(cnpj.substring(11,15));
    qui = eval(cnpj.substring(16,18));
    
    var i;
    var numero;
    var situacao = ”;
    
    numero = (pri+seg+ter+qua+qui);
    
    s = numero;
    
    c = cnpj.substr(0,12);
    var dv = cnpj.substr(12,2);
    var d1 = 0;
    
    for (i = 0; i < 12; i++){
    d1 += c.charAt(11-i)*(2+(i % 8));
    }
    
    if (d1 == 0){
    var result = false;
    }
    d1 = 11 – (d1 % 11);
    
    if (d1 > 9) d1 = 0;
    
    if (dv.charAt(0) != d1){
    var result = false;
    }
    
    d1 *= 2;
    for (i = 0; i < 12; i++){
    d1 += c.charAt(11-i)*(2+((i+1) % 8));
    }
    
    d1 = 11 – (d1 % 11);
    if (d1 > 9) d1 = 0;
    
    if (dv.charAt(1) != d1){
    var result = false;
    }
    
    if (result == false) {
        return (false);
    } else {
        return (true);
    }
}

function checarAnunciante(f){
    var nome_qtd_palavras = trim(f.nome.value).split(" ");
    if ( nome_qtd_palavras.length < 2 ) {
        alert(aviso("Informe seu nome completo."));
        f.nome.focus();
        return (false);
    }

    if (!checkMail(trim(f.email.value))) {
        alert(aviso("Informe seu e-mail corretamente."));
        f.email.focus();
        return (false);
    }

    if ( (trim(f.email.value)) != (trim(f.conf_email.value)) ) {
        alert(aviso("Confirme seu e-mail corretamente."));
        f.conf_email.focus();
        return (false);
    }

    if (trim(f.telefone.value) == "" ) {
        alert(aviso("Informe seu Telefone."));
        f.telefone.focus();
        return (false);
    }
    
    if (!valida_CPF(f.cpf.value)) {
        alert(aviso("Informar o CPF corretamente."));
        f.cpf.focus();
        return (false);
    }

    /* if (trim(f.razao.value) == "" ) {
        alert(aviso("Informe a Razão Social."));
        f.razao.focus();
        return (false);
    }
    
    if (trim(f.fantasia.value) == "" ) {
        alert(aviso("Informe o Nome Fantasia."));
        f.razao.focus();
        return (false);
    }
    
/*    if (!valida_CNPJ(f.cnpj.value)) {
        alert(aviso("Informar o CNPJ corretamente."));
        f.cnpj.focus();
        return (false);
    } 

    if (trim(f.telefone_atendimento.value) == "" ) {
        alert(aviso("Informe o Telefone de Atendimento."));
        f.telefone_atendimento.focus();
        return (false);
    }

    if (trim(f.cep.value) == "" ) {
        alert(aviso("Informe o CEP."));
        f.cep.focus();
        return (false);
    }

    if (trim(f.endereco.value) == "" ) {
        alert(aviso("Informe o Endereço."));
        f.endereco.focus();
        return (false);
    }

    if (trim(f.numero.value) == "" ) {
        alert(aviso("Informe o Número."));
        f.numero.focus();
        return (false);
    }

    if (trim(f.bairro.value) == "" ) {
        alert(aviso("Informe o Bairro."));
        f.bairro.focus();
        return (false);
    }

    if (trim(f.cidade.value) == "" ) {
        alert(aviso("Informe a Cidade."));
        f.cidade.focus();
        return (false);
    }

    if (trim(f.estado.value) == "" ) {
        alert(aviso("Informe o Estado."));
        f.estado.focus();
        return (false);
    } */

    f.submit;
}



