// anti-frames
if (parent.frames.length) top.location = self.location;

// menu funcion
function toggle_sub(e) {
	var elem = document.getElementById('sub' + e);
	
	if (elem == null) return;
	else {
		elem.style.display = (elem.style.display == 'block' ? 'none' : 'block');
	}
}

// functions to hide and show the password overlay
function hide_overlay() {
	var d = document.getElementById('pass_overlay');
	d.style.visibility = 'hidden';
	
	var f = document.getElementById('login_form');
	f.pass.focus();
}
function show_overlay() {
	var f = document.getElementById('login_form');
	if (!f.pass.value) {
		var d = document.getElementById('pass_overlay');
		d.style.visibility = 'visible';
	}
}

// login form validation
function do_login() {
	var f = document.getElementById('login_form');
	
	if (f.login.value == 'login') {
		alert('Por favor digite seu nome.');
		f.login.focus();
	}
	else if (!f.pass.value) {
		alert('Por favor digite seu e-mail.');
		f.pass.focus();
	}
	else {
		f.submit();
	}
	
	return false;
}

// displaying login errors
function show_error(err) {
	switch(err) {
		case 1: var erro = 'Nome de usuário incorreto.'; break;
		case 2: var erro = 'Senha incorreta'; break;
	}
	alert(erro);
	
	var f = document.getElementById('login_form');
	f.login.focus();
}

// contact form validation and submission
function contact_action() {
	var f = document.getElementById('contact_form');
	
	if (f.nome.value == "nome") {
		alert("Campo obrigatório: name");
		f.nome.focus();
	}
	else if (f.email.value == "e-mail") {
		alert("Campo obrigatório: e-mail");
		f.email.focus();
	}
	else if (f.email.value.indexOf('@') == -1 || f.email.value.indexOf('.') == -1) {
		alert("E-mail inválido.");
		f.email.select();
	}
/*
	else if (!f.para.value) {
		alert("Campo obrigatório: to");
		f.para.focus();
	}
*/
	else if (f.comentario.value == "comentário") {
		alert("Campo obrigatório: comment");
		f.comentario.focus();
	}
	else {
		f.submit();
	}
}

// tell-a-friend form validation and submission
function spread_action() {
	var f = document.getElementById('spread_form');
	
	if (f.r_nome.value == "seu nome") {
		alert("Campo obrigatório: seu nome");
		f.r_nome.focus();
	}
	else if (f.r_email.value == "seu e-mail") {
		alert("Campo obrigatório: seu e-mail");
		f.r_email.focus();
	}
	else if (f.d_nome.value == "nome do amigo(a)") {
		alert("Campo obrigatório: nome do amigo(a)");
		f.d_nome.focus();
	}
	else if (f.d_email.value == "e-mail do amigo(a)") {
		alert("Campo obrigatório: e-mail do amigo(a)");
		f.d_email.focus();
	}
	else if (f.comentario.value == "comentário") {
		alert("Campo obrigatório: comentário");
		f.comentario.focus();
	}
	else {
		f.submit();
	}
}

// updating prices at the library
function update_value(fid) {
	var p = document.getElementById('preco' + fid);
	var q = document.getElementById('qt' + fid);
	var f = document.getElementById('subtotal' + fid);
	
	if (q.value) {
		var subtotal = (q.value * p.value);
		subtotal = "R$ " + subtotal.toFixed(2);
		f.value = subtotal.replace(".", ",");
	}
	else {
		f.value = "R$ 0,00";
	}
}

// order form validation and submission
function order_action() {
	var f = document.getElementById('order_form');
	
	if (f.nome.value == 'nome') {
		alert("Campo obrigatório: nome");
		f.nome.focus();
	}
	else if (f.email.value == 'e-mail') {
		alert("Campo obrigatório: e-mail");
		f.email.focus();
	}
	else if (f.endereco.value == 'endereço') {
		alert("Campo obrigatório: endereço");
		f.endereco.focus();
	}
	else if (f.cep.value == 'cep') {
		alert("Campo obrigatório: cep");
		f.cep.focus();
	}
	else if (f.cidade.value == 'cidade') {
		alert("Campo obrigatório: cidade");
		f.cidade.focus();
	}
	else if (!f.estado.value) {
		alert("Campo obrigatório: estado");
		f.estado.focus();
	}
	else if (f.cnpj.value == 'cpf/cnpj') {
		alert("Campo obrigatório: cpf/cnpj");
		f.cnpj.focus();
	}
	else if (f.ddd.value == 'ddd') {
		alert("Campo obrigatório: ddd");
		f.ddd.focus();
	}
	else if (f.telefone.value == 'telefone') {
		alert("Campo obrigatório: telefone");
		f.telefone.focus();
	}
	else if (f.subtotal.value != 0) {
		window.open('about:blank', 'resposta', 'width=320, height=240')
		f.submit();
	}
	
}

function tellafriend(num) {
	window.open('send2friend.php?id=' + num, '', 'width=400, height=200');
}

function printnews(num) {
	window.open('printnews.php?id=' + num, '', 'width=640, height=480, scrollbars=yes');
}

function contacts_print(type) {
	window.open("address_book_print.php?t=" + type, "", "width=640, height=480, scrollbars=yes");
}


function do_signup() {
	var f = document.getElementById('signup_form');
	
	if (f.nome.value == 'nome') {
		alert('<?=output("Por favor digite seu nome.", "Por favor digite seu nome.", "Por favor digite seu nome.")?>');
		f.nome.focus();
	}
	else if (f.email.value == 'e-mail') {
		alert('<?=output("Por favor digite seu e-mail.", "Por favor digite seu e-mail.", "Por favor digite seu e-mail.")?>');
		f.email.focus();
	}
	else if (f.email.value.indexOf('@') == -1 || f.email.value.indexOf('.') == -1) {
		alert('<?=output("Por favor digite um e-mail válido.", "Por favor digite um e-mail válido.", "Por favor digite um e-mail válido.")?>');
		f.email.select();
	}
	else {
		f.submit();
	}
}