var interval = null;

function bootstrap() {
	$('a[@rel=external]').attr('target', '_blank');
}

function toggleDetalhesHome(id) {
	if (typeof(id) == 'undefined') {
		var sel = $('li.ativo', 'ul.caracteristicas');
		if (sel.size() == 1) {
			id = sel.next().attr('id');
		} else {
			id = $($('li', 'ul.caracteristicas').get(0)).attr('id');
		}
	} else if (typeof(interval) != 'undefined') {
		clearInterval(interval);
		delete interval;
	}
	$('li', 'ul.caracteristicas').removeClass('ativo');
	if (typeof(id) != 'undefined') {
		$('#' + id).addClass('ativo');
		$('#conteudo_arquitetos').load('ajax/' + id + '.php');
	}
}

function autoToggleDetalhesHome() {
	toggleDetalhesHome();
	interval = setInterval('toggleDetalhesHome()', 5 * 1000);
}

function toggleResposta(link) {
	$(link).parent().next().toggleClass('ativo');
}

function toggleParcelas(tipoForma, checked) {
	if (checked == true) {
		$('input[@name=parcelas]').removeAttr('checked');
		$('button.continuar').css('display', 'none');
		$('button.finalizar').css('display', 'none');
		if (tipoForma == 1) {
			$('button.finalizar').css('display', 'inline');
			$('input[@name=parcelas]:gt(0)').attr('disabled', 'disabled');
			$('input[@name=parcelas]:eq(0)').attr('checked', 'checked');
		} else {
			$('button.continuar').css('display', 'inline');
			$('input[@name=parcelas]').removeAttr('disabled');
		}
	}
}

function toggleClienteTipo(tipo) {
	if (tipo == 'F' || tipo == 'J') {
		tipo = tipo.toLowerCase();
		var outro = tipo == 'j' ? 'f' : 'j';
		$('.p' + tipo).css('display', 'list-item');
		$('.p' + outro).css('display', 'none');
	}
}

function buscaCidades(estado) {
	$('#cidade').empty();
	if (estado > 0) {
		$('#cidade').append($('<option>').val(0).html('Aguarde...'));
		$.getJSON('ajax/lista-cidades.php', {estado: estado}, function (data) {
			var cidades = data.cidades;
			$('#cidade').empty();
			if (cidades.length > 0) {
				$('#cidade').append($('<option>').val(0).html('Selecione uma cidade...'));
				for (var i = 0; i < cidades.length; i++) {
					var cidade = cidades[i];
					$('#cidade').append($('<option>').val(cidade.cidade).html(cidade.nome));
				}
			} else {
				$('#cidade').append($('<option>').val(0).html('Selecione um estado antes...'));
			}
		});
	} else {
		$('#cidade').append($('<option>').val(0).html('Selecione um estado antes...'));
	}
}

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;
	}
	var li = $(campo).parent();
	if (typeof(li) != 'undefined') {
		li.removeClass('ok erro');
		if (check) {
			li.addClass('ok');
		} else {
			li.addClass('erro');
		}
	}
}

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);
	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 recuperarSenha() {
	var email = $('#email').val();
	window.location = 'esqueci.php?do=esqueci&email=' + email;
}

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();
			}
		}
	}
}