﻿/*
 * Funcoes comuns a calculadora
 *
 */

function validatePaternityForm() {
	validate = $('type').value;
	validate_bloodtype = !($F('bloodtype_mother')=='' || $F('bloodtype_child')=='' || $F('bloodtype_father')=='') ? true : false;
	validate_eyecolor  = !($F('eyecolor_mother')==''  || $F('eyecolor_child')==''  || $F('eyecolor_father')=='')  ? true : false;
	validate_earlobe   = !($F('earlobe_mother')==''   || $F('earlobe_child')==''   || $F('earlobe_father')=='')   ? true : false;
	if( validate=='all' && !(validate_bloodtype || validate_eyecolor || validate_earlobe) )
		return false;
	else if( validate=='bloodtype' && !validate_bloodtype )
		return false;
	else if( validate=='eyecolor' && !validate_eyecolor )
		return false;
	else if( validate=='earlobe' && !validate_earlobe )
		return false;
	else
		return true;
}

// Alterna as abas da calculadora
function setActiveTab(type) {
	$('type').value = type;
	$('content').className='tab-'+type;
	$('validation-error').style.display='none';
}

// Busca o resultado por Ajax
function getPaternityResult() {
	if( !validatePaternityForm() ) {
		$('validation-error').innerHTML = 'Por favor, selecione os campos necessários.';
		if( $('validation-error').style.display=='none' )
			new Effect.BlindDown('validation-error');
		return false;
	} else {
		if( $('validation-error').style.display!='none' )
			new Effect.BlindUp('validation-error');
	}
	new Effect.BlindDown('loading');
	clearResults();
	new Ajax.Updater('result','resultado.php', {
		method: 'post',
		parameters: $('frm1').serialize(),
		onSuccess: function() {
			new Effect.BlindUp('loading');
			new Effect.BlindDown('result');
		}
	} );
}
// Limpa o resultado
function clearResults() {
	if( $('result').style.display != 'none' )
		new Effect.BlindUp('result');
}

// Adiciona o email ao cadastro de newsletter
function addEmailNewsletter() {
	$('send_button').src = 'img/ajax-load-big.gif';
	if( $('newsletter-response').style.display != 'none' )
		new Effect.BlindUp('newsletter-response');
	new Ajax.Updater('newsletter-response-text','newsletter.php', {
		method: 'post',
		parameters: $('frm2').serialize(),
		onSuccess: function(transport) {
			new Effect.BlindDown('newsletter-response');
			$('send_button').src = 'img/bt-send.gif';
			if( transport.responseText.match(/sucesso/) )
				$('email').value = '';
		}
	});
	return false;
}

