/* Document JavaScript */

var videoCodigo='';

$(document).ready(function(){

	$('a[href^="http"]').attr('target', '_blank');
	$("input").cleanField();

	$('input[name="cpf"], input[name="dia"], input[name="mes"], input[name="ano"], input[name="endereco_numero"], input[name="cep_a"], input[name="cep_b"], input[name="fone_ddd"], input[name="fone_numero"], input[name="celular_ddd"], input[name="celular_numero"]').keypress(function(event){
		if(event.which!=0 && event.which!=8 && (event.which<48 || event.which>57))
			event.preventDefault();
	});

	$('select[name="estado"]').change(function(){
		var estado=$(this).val();
		$('#carrega-cidades').load('./cidades/html/'+ estado);
	});

	$('#form-cadastro').submit(function(event){
		event.preventDefault();
		$('#mensagem').slideUp('slow');
		var validacao=true;

		if($('input[name="nome"]', this).val().length<2){
			validacao=false;
			$('input[name="nome"]', this).addClass('Erro');
		}else
			$('input[name="nome"]', this).removeClass('Erro');

		$('input[name="email"]', this).val($('input[name="email"]', this).val().toLowerCase());
		var ERemail=/^[a-z0-9_\.-]{2,}@([a-z0-9_-]{2,}\.)+[a-z]{2,4}$/
		if(!ERemail.exec($('input[name="email"]', this).val())){
			validacao=false;
			$('input[name="email"]', this).addClass('Erro');
		}else
			$('input[name="email"]', this).removeClass('Erro');

		if($('input[name="dia"]', this).val().length>0 || $('input[name="mes"]', this).val().length>0 || $('input[name="ano"]', this).val().length>0){
			$('input[name="dia"]', this).val(parseInt($('input[name="dia"]', this).val()));
			$('input[name="mes"]', this).val(parseInt($('input[name="mes"]', this).val()));
			$('input[name="ano"]', this).val(parseInt($('input[name="ano"]', this).val()));
			var ERdia=/^([1-9]|[1-2][0-9]|[3][0-1])$/;
			if(!ERdia.exec($('input[name="dia"]', this).val())){
				validacao=false;
				$('input[name="dia"]', this).addClass('Erro');
			}else
				$('input[name="dia"]', this).removeClass('Erro');
			var ERmes=/^([1-9]|[1][0-2])$/;
			if(!ERmes.exec($('input[name="mes"]', this).val())){
				validacao=false;
				$('input[name="mes"]', this).addClass('Erro');
			}else
				$('input[name="mes"]', this).removeClass('Erro');
			var ERano=/^([1-2][0-9][0-9][0-9])$/;
			if(!ERano.exec($('input[name="ano"]', this).val())){
				validacao=false;
				$('input[name="ano"]', this).addClass('Erro');
			}else
				$('input[name="ano"]', this).removeClass('Erro');
		}else{
			validacao=false;
			$('input[name="dia"]', this).addClass('Erro');
			$('input[name="mes"]', this).addClass('Erro');
			$('input[name="ano"]', this).addClass('Erro');
		}

		var tmp=false;
		$('input[name="sexo"]', this).each(function(){
			if($(this).is(':checked')) tmp=true;
		});
		if(!tmp){
			validacao=false;
			$('#sexo', this).slideDown('slow');
		}else
			$('#sexo', this).slideUp('slow');

		if(!validaCPF($('input[name="cpf"]', this).val())){
			validacao=false;
			$('input[name="cpf"]', this).addClass('Erro');
		}else
			$('input[name="cpf"]', this).removeClass('Erro');

		if($('input[name="endereco"]', this).val().length<2){
			validacao=false;
			$('input[name="endereco"]', this).addClass('Erro');
		}else
			$('input[name="endereco"]', this).removeClass('Erro');

		if($('input[name="endereco_numero"]', this).val().length<1){
			validacao=false;
			$('input[name="endereco_numero"]', this).addClass('Erro');
		}else
			$('input[name="endereco_numero"]', this).removeClass('Erro');

		if($('input[name="bairro"]', this).val().length<2){
			validacao=false;
			$('input[name="bairro"]', this).addClass('Erro');
		}else
			$('input[name="bairro"]', this).removeClass('Erro');

		ERcep_a=/^(\d{5})$/;
		if(!ERcep_a.exec($('input[name="cep_a"]', this).val())){
			validacao=false;
			$('input[name="bairro"]', this).addClass('Erro');
		}else
			$('input[name="bairro"]', this).removeClass('Erro');

		ERcep_a=/^(\d{5})$/;
		if(!ERcep_a.exec($('input[name="cep_a"]', this).val())){
			validacao=false;
			$('input[name="cep_a"]', this).addClass('Erro');
		}else
			$('input[name="cep_a"]', this).removeClass('Erro');

		ERcep_b=/^(\d{3})$/;
		if(!ERcep_b.exec($('input[name="cep_b"]', this).val())){
			validacao=false;
			$('input[name="cep_b"]', this).addClass('Erro');
		}else
			$('input[name="cep_b"]', this).removeClass('Erro');

		if($('select[name="estado"]', this).val().length<1){
			validacao=false;
			$('#estado', this).slideDown('slow');
		}else
			$('#estado', this).slideUp('slow');

		if($('select[name="cidade"]', this).val().length<1){
			validacao=false;
			$('#cidade', this).slideDown('slow');
		}else
			$('#cidade', this).slideUp('slow');

		ERfone_ddd=/^(\d{2})$/;
		if(!ERfone_ddd.exec($('input[name="fone_ddd"]', this).val())){
			validacao=false;
			$('input[name="fone_ddd"]', this).addClass('Erro');
		}else
			$('input[name="fone_ddd"]', this).removeClass('Erro');

		ERfone_numero=/^(\d{8})$/;
		if(!ERfone_numero.exec($('input[name="fone_numero"]', this).val())){
			validacao=false;
			$('input[name="fone_numero"]', this).addClass('Erro');
		}else
			$('input[name="fone_numero"]', this).removeClass('Erro');

		if($('input[name="celular_ddd"]', this).val().length>0 || $('input[name="celular_numero"]', this).val().length>0){
			ERcelular_ddd=/^(\d{2})$/;
			if(!ERcelular_ddd.exec($('input[name="celular_ddd"]', this).val())){
				validacao=false;
				$('input[name="celular_ddd"]', this).addClass('Erro');
			}else
				$('input[name="celular_ddd"]', this).removeClass('Erro');
			ERcelular_numero=/^(\d{8})$/;
			if(!ERcelular_numero.exec($('input[name="celular_numero"]', this).val())){
				validacao=false;
				$('input[name="celular_numero"]', this).addClass('Erro');
			}else
				$('input[name="celular_numero"]', this).removeClass('Erro');
		}

		if($('select[name="numero_pe"]', this).val().length<1){
			validacao=false;
			$('#numero_pe', this).slideDown('slow');
		}else
			$('#numero_pe', this).slideUp('slow');

		if($('input[name="video_nome"]', this).val().length<2){
			validacao=false;
			$('input[name="video_nome"]', this).addClass('Erro');
		}else
			$('input[name="video_nome"]', this).removeClass('Erro');

		if($('input[name="video_url"]', this).val().length<36){
			validacao=false;
			$('input[name="video_url"]', this).addClass('Erro');
		}else
			$('input[name="video_url"]', this).removeClass('Erro');

		if(!$('input[name="autorizacao"]').is(':checked')){
			validacao=false;
			$('input[name="autorizacao"]', this).addClass('Erro');
		}else
			$('input[name="autorizacao"]', this).removeClass('Erro');

		if(!$('input[name="regulamento"]').is(':checked')){
			validacao=false;
			$('input[name="regulamento"]', this).addClass('Erro');
		}else
			$('input[name="regulamento"]', this).removeClass('Erro');

		var mensagemErro='Favor preencha todos campos corretamente!';
		if(!validacao)
			$('#mensagem').html(mensagemErro).slideDown('slow');
		else{
			$('#mensagem').html('enviando ...').slideDown('slow');
			$(this).ajaxSubmit({
				dataType: 'html',
				success: function(data){
					switch(data){
						case 'sucesso':
								$('#mensagem').html('redirecionando ...').slideDown('slow');
								window.location.href='./cadastro/sucesso';
							break;
						case 'repetido_cadastro':
								$('#mensagem').html('Voc&ecirc; j&aacute; inseriu um video!').slideDown('slow');
							break;
						case 'repetido_video':
								$('#mensagem').html('Este video j&aacute; foi inserido!').slideDown('slow');
							break;
						default:
							$('#mensagem').html(mensagemErro).slideDown('slow');
					}
				}
			});
		}

	});

	$('#fechar').click(function(e){
		e.preventDefault();
		$('#indicador').hide('slow');
	});
	$('.Indicacao').click(function(e){
		e.preventDefault();
		var offset=$(this).offset();
		videoCodigo=$(this).attr('href');
		$('input[name="video"]').val(videoCodigo);
		$('#indicador').css({top: offset.top, left: offset.left}).show('slow');
	});

	$('#form-indique').submit(function(event){
		event.preventDefault();
		$('#mensagem').slideUp('slow');
		var validacao=true;

		if($('input[name="indicador_nome"]', this).val().length<2 || $('input[name="indicado_nome"]', this).val().length<2)
			validacao=false;

		$('input[name="indicador_email"]', this).val($('input[name="indicador_email"]', this).val().toLowerCase());
		$('input[name="indicado_email"]', this).val($('input[name="indicado_email"]', this).val().toLowerCase());
		var ERemail=/^[a-z0-9_\.-]{2,}@([a-z0-9_-]{2,}\.)+[a-z]{2,4}$/
		if(!ERemail.exec($('input[name="indicador_email"]', this).val()) || !ERemail.exec($('input[name="indicado_email"]', this).val()))
			validacao=false;

		var mensagemErro='Favor preencha todos campos corretamente!';
		if(!validacao)
			$('#mensagem').html(mensagemErro).slideDown('slow');
		else{
			$('#mensagem').html('enviando ...').slideDown('slow');
			$(this).ajaxSubmit({
				dataType: 'html',
				success: function(data){
					switch(data){
						case 'sucesso':
								$('#mensagem').html('Indica&ccedil;&atilde;o efetuada com sucesso!').slideDown('slow');
								$('#form-indique').resetForm();
							break;
						default:
							$('#mensagem').html(mensagemErro).slideDown('slow');
					}
				}
			});
		}
	});

	$('#box-confirmar').hide();
	$('.Confirmar').click(function(e){
		e.preventDefault();
		$('#box-confirmar').fadeIn('slow');
	});

	$('.Votar').click(function(e){
		e.preventDefault();
		var endereco=$(this).attr('href');
		var video=$(this).attr('rel');
		var captcha=$('input[name="captcha"]').val();
		$('#'+ video).slideUp('fast');
		$.post(endereco, {video: video, captcha: captcha}, function(retorno){
			if(retorno=='sucesso')
				$('#'+ video).html('Seu voto foi realizado com sucesso!').addClass('Sucesso').slideDown('slow');
			else if(retorno=='validacao')
				$('#'+ video).html('Falha de valida&ccedil;&atilde;o, favor tente novamente!').addClass('Erro').slideDown('slow');
			else if(retorno=='repetido')
				$('#'+ video).html('Voc&ecirc; s&oacute pode votar uma vez neste v&iacute;deo!').addClass('Erro').slideDown('slow');
		});
	});

});
