$(document).ready(function(){

	$('#listaAmiga').submit(function(){
	
		exibeAlerta('Enviando... Aguarde...');
	
		if (testaNome() == false) {
			exibeAlerta('O nome está errado!');
			return false;
		}
//		if (testaRG() == false) {
//			exibeAlerta('O RG está errado!');
//			return false;
//		}
		if (testaEmail() == false) {
			exibeAlerta('O e-mail está errado!');
			return false;
		}
		
		pageTracker._trackPageview("/lista-amiga");
	
		$.post(
			'lista-amiga',
			{
				evento:	$('#promoEvento').val(),
				nome:	$('#promoNome').val(),
//				rg:		$('#promoRG').val(),
				email:	$('#promoEmail').val()
			},
			function(data) {
				if (data == true) {
					exibeAlerta('Ok! O nome "' + $('#promoNome').val() + '" está na lista amiga.');
					$('#promoNome').val('nome');
//					$('#promoRG').val('rg (identidade)');
					$('#promoEmail').val('e-mail');
				} else {
					exibeAlerta('Vixi! O sistema não funcionou corretamente. =[<br />Tente novamente mais tarde.');
				}
			}
		);
		
		return false;
		
	});
	
	$('#promoNome').focus(function(){
		if ($(this).val() == 'nome completo') $(this).val('');
	}).blur(function(){
		if ($(this).val() == '') $(this).val('nome completo');
		testaNome();
	}).keyup(function(){
		testaNome();
	}).change(function(){
		testaNome();
	});
	
//	$('#promoRG').focus(function(){
//		if ($(this).val() == 'rg (identidade)') $(this).val('');
//	}).blur(function(){
//		if ($(this).val() == '') $(this).val('rg (identidade)');
//		testaRG();
//	}).keyup(function(){
//		testaRG();
//	}).change(function(){
//		testaRG();
//	});
	
	$('#promoEmail').focus(function(){
		if ($(this).val() == 'e-mail') $(this).val('');
	}).blur(function(){
		if ($(this).val() == '') $(this).val('e-mail');
		testaEmail();
	}).keyup(function(){
		testaEmail();
	}).change(function(){
		testaEmail();
	});
	
	$('#promoParticipar').click(function(){
		
		pageTracker._trackPageview("/sorteio");
	
		exibeAlerta('Enviando... Aguarde...');
	
		$.post(
			'sorteio',
			{ evento: $('#promoEvento').val() },
			function(data) {
				if (data == true) {
					exibeAlerta('Ok! Você já está inscrito na promoção.');
					$('#promoStatus').html('<p align="center"><strong>Você já está participando desta promoção. =]</strong></p>');
				} else {
					exibeAlerta('Vixi! O sistema não funcionou corretamente. =[<br />Tente novamente mais tarde.');
				}
			}
		);
		
	});
	
});

function testaNome () {

	if ($('#promoNome').val() != 'nome' && $('#promoNome').val().length >= 5) {
		$('#promoNome').removeClass('erro');
		$('#promoNome').addClass('ok');
		return true;
	} else {
		$('#promoNome').removeClass('ok');
		$('#promoNome').addClass('erro');
		return false;
	}

}

function testaRG () {

	if ($('#promoRG').val() != 'rg (identidade)' && $('#promoRG').val().length >= 5) {
		$('#promoRG').removeClass('erro');
		$('#promoRG').addClass('ok');
		return true;
	} else {
		$('#promoRG').removeClass('ok');
		$('#promoRG').addClass('erro');
		return false;
	}

}

function testaEmail () {

	if (/^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/.test($('#promoEmail').val())) {
		$('#promoEmail').removeClass('erro');
		$('#promoEmail').addClass('ok');
		return true;
	} else {
		$('#promoEmail').removeClass('ok');
		$('#promoEmail').addClass('erro');
		return false;
	}

}