function bootstrap() {
	$('[@rel=external]').attr('target', '_blank');
	resetFontSize();
}

function checkValue(campo, tipo) {
	var check = false;
	switch (tipo) {
	case 'blank':
		check = checkBlank(campo);
		break;
	case 'mail':
		check = checkMail(campo);
		break;
	case 'zero':
		check = checkZero(campo);
		break;
	case 'cpf':
		check = checkCPF(campo);
		break;
	case 'cnpj':
		check = checkCNPJ(campo);
		break;
	}
	if (check) {
		$(campo).css('border', '1px solid #878582');
	} else {
		$(campo).css('border', '1px solid #FF8582');
	}
}

function checkBlank(campo) {
	var valor = $(campo).val();
	if (typeof(valor) == 'undefined') {
		return false;
	}
	if (valor.length == 0) {
		return false;
	}
	return true;
}

function checkMail(campo) {
	var email = $(campo).val();
	if (typeof(email) == 'undefined') {
		return false;
	}
	var rgx1 = /(\@.*\@)|(.*\.\..*)|(.*\@\..*)|(^\.)|(\.$)|(\@\/)|(.*\@\-.*)|(.*\.$)/gi;
	var rgx2 = /^[_\w\d][\w\d\_\/\-\.]*\@[\d\w\-\.]+[0-9A-z]$/gi;
	var rgx3 = /.*\@.*\.+.*/gi;
	return !email.match(rgx1) && email.match(rgx2) && email.match(rgx3);
}

function checkZero(campo) {
	var numero = $(campo).val();
	if (typeof(numero) == 'undefined') {
		return false;
	}
	numero = parseInt(numero, 10);
	if (isNaN(numero)) {
		return false;
	}
	if (numero == 0) {
		return false;
	}
	return true;
}

function checkCPF(campo) {
	var cpf = $(campo).val();
	if (typeof(cpf) == 'undefined') {
		return false;
	}
	if (cpf.length != 11) {
		return false;
	}
	var repeat = '';
	for (var i = 0; i < 11; i++) {
		repeat += cpf.charAt(0);
	}
	if (cpf == repeat) {
		return false;
	}
	var soma = 0;
	for (var i = 0; i < 9; i++) {
		soma += (cpf.charAt(i) * (i + 1));
	}
	var dv1 = soma % 11;
	if (dv1 == 10) {
		dv1 = 0;
	}
	soma = 0;
	for (var i = 1; i < 10; i++) {
		soma += (cpf.charAt(i) * i);
	}
	var dv2 = soma % 11;
	if (dv2 == 10) {
		dv2 = 0;
	}
	return (dv1 == cpf.charAt(9)) && (dv2 == cpf.charAt(10));
}

function checkCNPJ(campo) {
	var cnpj = $(campo).val();
	if (typeof(cnpj) == 'undefined') {
		return false;
	}
	if (cnpj.length != 14) {
		return false;
	}
	var c = new Array(6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2);
	var soma = 0;
	for (var i = 0; i < 12; i++) {
		soma += (cnpj.charAt(i) * c[i + 1]);
	}
	var dv1 = soma % 11;
	if (dv1 < 2) {
		dv1 = 0;
	} else {
		dv1 = 11 - dv1;
	}
	soma = 0;
	for (var i = 0; i < 13; i++) {
		soma += (cnpj.charAt(i) * c[i]);
	}
	var dv2 = soma % 11;
	if (dv2 < 2) {
		dv2 = 0;
	} else {
		dv2 = 11 - dv2;
	}
	return (dv1 == cnpj.charAt(12)) && (dv2 == cnpj.charAt(13));
}

function popup(link, width, height) {
	var w = typeof(width) != 'undefined' ? width : 320;
	var h = typeof(height) != 'undefined' ? height : 480;
	var win = null;
	if (typeof(link) != 'undefined') {
		win = window.open(link.href, 'popup', 'scrollbars,width=' + w + ',height=' + h);
	}
	return win;
}

function redirect(file) {
	if (typeof(file) == 'string') {
		window.location = relPath + file;
	}
}

function nextField(f) {
	var field = $(f);
	if (typeof(field) != 'undefined') {
		if (field.attr('maxlength') <= field.val().length) {
			var find = false;
			var next = null;
			$(':input', field.form).each(function () {
				if ($(this).attr('name') == field.attr('name')) {
					find = true;
				} else if (find) {
					next = $(this);
					return false;
				}
			});
			if (typeof(next) != 'undefined') {
				next.focus();
			}
		}
	}
}

var fontSize = .75;
var fontInc = .05;
var fontMin = fontSize - fontInc;
var fontMax = fontSize + fontInc;
var fontMeasure = 'em';
var fontElement = '#all';

function resetFontSize() {
	if (typeof(fontElement) == 'string') {
		fontElement = $(fontElement).get(0);
	}
	var size = getCookie('fontSize');
	if (!setFontSize(size)) {
		setCookie('fontSize', fontSize, '', '/');
		fontElement.style.fontSize = fontSize + fontMeasure;
	}
}

function setFontSize(size, persist) {
	size = parseFontSize(size);
	persist = typeof(persist) == 'boolean' ? persist : false;
	if (typeof(size) == 'number' && size >= fontMin && size <= fontMax) {
		fontSize = size;
		fontElement.style.fontSize = fontSize + fontMeasure;
		if (persist) {
			setCookie('fontSize', fontSize, '', '/');
		}
		return true;
	}
	return false;
}

function parseFontSize(size) {
	var type = typeof(size);
	switch (type) {
	case 'number':
		return size;
	case 'string':
		size = size.replace(/[^0-9]/i, '');
		return size.length > 0 ? parseInt(size, 10) : 0;
	default:
		return 0;
	}
}

function increaseFontSize() {
	if (fontSize < fontMax) {
		setFontSize(fontSize + fontInc, true);
	}
}

function decreaseFontSize() {
	if (fontSize > fontMin) {
		setFontSize(fontSize - fontInc, true);
	}
}

function buscaProdutos(){
	if (document.getElementById('busca_topo').value != '' && document.getElementById('busca_topo').value.length > 2) 
		document.getElementById('frm_busca').submit(); 
	else 
		alert('Informe no mínimo 3 caracteres para a busca');
}	

// Ajax

function carrinhoCalcular() {
	var cep1 = $('#cep_1').val();
	var cep2 = $('#cep_2').val();
	$.getJSON('ajax/carrinho-calcular.php', {cep_1: cep1, cep_2: cep2},
			function (data) {
				if (typeof(data.frete.erro) == '') {
					alert(data.frete.erro);
				} else {
					if (data.frete.cep != '') {
						$('.frete_calculado').addClass('ativo');
						$('#frete_valor').html('R$' + data.frete.frete);
						var endereco = data.frete.endereco + ' - ' + data.frete.bairro + ' - ' + data.frete.cidade + ' - ' + data.frete.uf;
						$('span', '#frete_entrega').html(endereco);
					} else {
            alert("Informe um CEP válido");
          }
					$('#carrinho_total').html(data.frete.total);
					$('#carrinho_parcela').html(data.frete.parcela);										$('#carrinho_total_parcelas').html(data.frete.parcelas);
					$('#frete_entrega').css('display', $.browser.msie ? 'block' : 'table-row');          
				}
			});
}

function carrinhoRemover(id) {
	$.getJSON('ajax/carrinho-remover.php', {produto: id},
			function (data) {
				if (data.remover) {
					$('#produto_' + id).remove();
					carrinhoCalcular();
					if ($('tr.produtos').size() == 1) {
						window.location = 'carrinho.php';
					}
				}
			});
}

function carrinhoAlterar(id) {
	var q = $('#quantidade_' + id).val();
	$.getJSON('ajax/carrinho-alterar.php', {produto: id, q: q},
			function (data) {
				if (data.alterar) {
					$('#subtotal_' + id).html(data.total);
					$('#quantidade_' + id).val(data.q);
					carrinhoCalcular();
				}
			});
}

function cidadeListar(estado, tipo) {
	var combo = $('#' + (tipo == 'J' ? 'juridica' : 'fisica') + '_cidade');
	combo.empty();
	if (estado > 0) {
		combo.append($('<option>').val(0).html('Aguarde...'));
		$.getJSON('ajax/cidade-listar.php', {estado: estado}, function (data) {
			var cidades = data.cidades;
			combo.empty();
			if (cidades.length > 0) {
				combo.append($('<option>').val(0).html('Selecione uma cidade...'));
				for (var i = 0; i < cidades.length; i++) {
					var cidade = cidades[i];
					combo.append($('<option>').val(cidade.cidade).html(cidade.nome));
				}
			} else {
				combo.append($('<option>').val(0).html('Selecione um estado antes...'));
			}
		});
	} else {
		combo.append($('<option>').val(0).html('Selecione um estado antes...'));
	}
}

function togglePessoa(tipo) {
	if (typeof(tipo) == 'string') {
		$('fieldset.fisica').removeClass('ativo');
		$(':input', 'fieldset.fisica').attr('disabled', 'disabled');
		$('fieldset.juridica').removeClass('ativo');
		$(':input', 'fieldset.juridica').attr('disabled', 'disabled');
		$('fieldset.' + tipo).addClass('ativo');
		$(':input', 'fieldset.' + tipo).removeAttr('disabled');
	}
}

function showPergunta(dt) {
	$(dt).parent().next().addClass('ativo');
}

function hidePergunta(dd) {
	$(dd).parent().removeClass('ativo');
}

function showMensagens(msgs) {
	var list = $('<ul>').attr('id', 'mensagens');
	for (var i = 0; i < msgs.length; i++) {
		var item = $('<li>').html(msgs[i]);
		list.append(item);
	}
	$('#conteudo').prepend(list);
}