// store func
function filtrar() {
	$('#formFiltros').submit();
}
function getAddress(cep) {
	$.getJSON('_ajax/correios_address.php?cep=' + cep, function(data) {
		$('#logradouro').val(data.tipo_logradouro);
		$('#endereco').val(data.logradouro);
		$('#cidade').val(data.cidade);
		$('#bairro').val(data.bairro);
		$('#estado').val(data.uf);
	});
}
function desejosIncluir(p) {
	addLoading('desejoIncluir');
	$('#desejoIncluir').load('_ajax/loja_favoritos.php?p='+p);
}
function storeFreteCalc(d, id) {
	if (d.value.length == 8) {
		addLoading('valorFrete');
		var tipoFrete = 'S';
	
		$.getJSON('_ajax/loja_frete.php?cep='+d.value, function(data) {
			$('#valorFrete').html(data.price);
			if (data.msg.length == 0)
				hideTr('trFreteParceiro');
			else
				showTr('trFreteParceiro');
			$('#freteMsg').html(data.msg);
	
			setTimeout('storeTotalCalc()', 500);
		});
	}
}
function storeCupomCalc(d) {
	addLoading('valorCupom');
	$('#valorCupom').load('_ajax/loja_cupom.php?id='+d.value, function() {
		storeTotalCalc();
	});
}
function storeTotalCalc(d) {
	addLoading('valorTotal');
	// prices
	var produtos = $('#valorProdutos').html().replace('R$ ','');
	//var cupom = $('#valorCupom').html().replace('R$ ','');
	var frete = $('#valorFrete').html().replace('R$ ','');
	$('#valorTotal').load('_ajax/loja_total.php?p='+produtos+'&f='+frete);
}
function addLoading(div) {
	if (div) {
		$('#' + div).html("<img src='_images/loader.gif' border='0'>");
	}
}
function delLoading(div) {
	if (div) {
		$('#' + div).html("");
		$('#' + div).hide();
	}
}
function showHide(div) {
	if ($("#" + div).css("display") == "none" || $("#" + div).css("display") == "") {
		$("#" + div).slideDown('slow');
	} else {
		$("#" + div).slideUp('slow');
	}
}
function showHideFiltros(div) {
   if ($("#" + div).css("display") == "none" || $("#" + div).css("display") == "") {
      $("#" + div).slideDown('slow');
      $('#labelFiltros').html('+ Ocultar Filtros');
   } else {
      $("#" + div).slideUp('slow');
      $('#labelFiltros').html('+ Mostrar Filtros');
   }
}
function showHideTr(div) {
   if ($("#" + div).css("display") == "none" || $("#" + div).css("display") == "") {
      showTr(div);
   } else {
      hideTr(div);
   }
}
function showTr(div) {
	if ($.browser.msie)
		document.getElementById(div).style.display = 'table-row';
	else
		$("#" + div).css("display", "table-row");
}
function hideTr(div) {
	if ($.browser.msie)
		document.getElementById(div).style.display = 'none';
	else
		$("#" + div).css("display","none");
}
function txtBox(box,tipo) {
	if (box) {
		if (tipo == 'in') {
			if (box.value == box.title) {
				box.value = '';
			}
		} else {
			if (box.value == '') {
				box.value = box.title;
			}
		}
	}
}
Number.prototype.formatMoney = function(c, d, t){
    var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "",
    i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t)
    + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};
function trocaImagemProduto(d) {
	$('#productImageFull').attr('src', d);
}
function validarIndique() {
	if ($('#nome').val() == '') {
		$('#nome').focus();
		alert('Preencha corretamente o seu nome.');
		return false;
	}
	for(var i=1; i <= 5; i++) {
		if ($('#email' + i).val() == '' || !emailCheck($('#email' + i).val())) {
			$('#email' + i).focus();
			alert('Preencha corretamente todos os e-mails.');
			return false;
		}
	}
	return true;
}
function emailCheck(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 rememberPassword() {
	addLoading('r_loading');
	var email = $('#rp_email').attr('value');
	$.get("cadastro_lembrarsenha_do.php?e="+email, function(data){
		if (data == '0') {
			alert('Email não encontrado.');
		} else {
			alert('Uma nova senha foi enviada para seu email');
			tb_remove();
		}
	});
}
function blog_send() {
	var nome = $('#nome').val();
	var email = $('#email').val();
	var to_nome = $('#to_nome').val();
	var to_email = $('#to_email').val();
	var id = $('#c').val();
	if (nome.length == 0 || email.length == 0 || to_nome.length == 0 || to_email.length == 0) {
		alert('Preencha todos os dados');
		return false;
	}
	addLoading('r_loading');
	$.get("blog_enviar_do.php?e="+email+"&n="+nome+"&tn="+to_nome+"&te="+to_email+"&id=" + id, function(data){
		if (data == '0') {
			alert('Erro enviando indicação. Tente novamente em alguns instantes.');
		} else {
			alert('Indicação enviada com sucesso!');
			tb_remove();
		}
	});
}
function setPerson(t) {
	$('#cpf').unmask();
	if (t == 'F') {
		$('#cpf').mask('999.999.999-99');
		$('#cpfLabel').html('CPF');
		$('#nomeLabel').html('Nome');
		$('#trRazao').hide();
		$('#trIE').hide();
		$('#trContato').hide();
		$('#trSobrenome').show();
		
		$('#box_sexo').show();
		$('#box_nascimento').show();
		
		// validation
		$("#nome").rules("add", { required: true, messages: { required: "*" } });
		$("#sobrenome").rules("add", { required: true, messages: { required: "*" } });
		$("#contato").rules("remove", "required");
		$("#ie").rules("remove", "required");
		$("#razao").rules("remove", "required");
	} else {
		$('#cpf').mask('99.999.999/9999-99');
		$('#cpfLabel').html('CNPJ');
		$('#nomeLabel').html('Nome Fantasia');
		$('#trRazao').show();
		$('#trIE').show();
		$('#trContato').show();
		$('#trSobrenome').hide();
		
		$('#box_sexo').hide();
		$('#box_nascimento').hide();
		
		// validation
		$("#nome").rules("remove", "required");
		$("#sobrenome").rules("remove", "required");
		
		$("#contato").rules("add", { required: true, messages: { required: "*" } });
		$("#ie").rules("add", { required: true, messages: { required: "*" } });
		$("#razao").rules("add", { required: true, messages: { required: "*" } });
	}
}
function selectPerson() {
	if (document.getElementById('tipo_f').checked)
		setPerson('F');
	else
		setPerson('J');
}
function validateComment() {
	if ($('#f_nome').val() == '') {
		$('#f_nome').focus();
		alert('Preencha o nome');
		return false;
	} else if ($('#f_email').val() == '' || !isValidEmail($('#f_email').val())) {
		$('#f_email').focus();
		alert('Preencha o e-mail corretamente.');
		return false;
	} else if ($('#f_msg').val() == '') {
		$('#f_msg').focus();
		alert('Preencha a mensagem');
		return false;
	} else {
		addLoading('formLoading');
		return true;
	}
}
function isValidEmail(email) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email))
		return false;
	else
		return true;
}
function showResponse(responseText, statusText) {
	delLoading('formLoading');
	alert(responseText);
}
