$(document).ready(function(){

	$('.cnpj').mask('99.999.999/9999-99');
	$('#telefone').mask('(99) 9999-9999');
	$('#cep').mask('99999-999');
	
	$("#send").click(function(){
		var msgerro = '';
		if($("input[@name='email']").val().length <= 3)
			msgerro += 'Favor preencher o email.\n';
		if($("input[@name='nome']").val().length <= 3)
			msgerro += 'Favor preencher seu nome.\n';
		if(!validaEmail($("input[@name='email']").val()))
			msgerro += 'O e-mail informado não é válido.\n';
		if(msgerro.length > 0){
			alert(msgerro);
			return false;
		}else{
			return true;
		}
    });
	
	$('#produto').change(function(){
		if(this.value=='Outros'){
			$('#outros-campo').show();
		}else{
			$('#outros-campo').hide();
		}
	});
	
	
	$(function() {
	$('a[@rel*=lightbox]').lightBox();
	});
	$("select[@name='estado']").change(function(){
		$.post(
			URL+'ajax',{
				estado: $("select[@name='estado']").val(),
				tipo: 'estado'
			},
			function(response){
				$("select[@name='cidade']")
                    .find('option')
                        .remove()
                    .end()
                    .append(response)
			}
		)
	})
	
	
	
});

function validaCNPJ(CNPJ) {
	erro = new String;
	if (CNPJ.length < 18) erro += "É necessario preencher corretamente o número do CNPJ! \n\n";
	if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
		if (erro.length == 0) erro += "É necessário preencher corretamente o número do CNPJ! \n\n";
	}
	//substituir os caracteres que não são números
	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 nonNumbers = /\D/;
	if (nonNumbers.test(CNPJ)) erro += "A verificação de CNPJ suporta apenas números! \n\n";
	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 +="Dígito verificador com problema!";
	}
	if (erro.length > 0){
		return false;
	}
	return true;
}

function validaEmail(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;
        }
}

