// JavaScript Document

jQuery(document).ready(function() {
	if($("#comunicLig")){
		$("#comunicLig").css("visibility","visible");
		$("#comunicLig").hide();
	}
	
	$("#rev_cep").mask("99999-999");
	$("#rev_telefone").mask("(99)9999-9999");
});

function SolicitarRevista(form){
	erros = new Array();
	expressoes = {
		'rev_cep': /^[0-9]{5}-[0-9]{3}$/,
		'rev_telefone': /^\([0-9]{2}\)[0-9]{4}-[0-9]{4}$/,
		/*'rev_email': /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/*/
	};

	$("input, select","#formSolicitarRevista form").each(function(){
		id = $(this).attr("id");
		if(($(this).val() == "") && $(this).attr("id") != "rev_complemento"){
			erros.push($("label[for="+id+"]").html()+" é obrigatório.");
		}else if((expressoes[id] != undefined) && (!expressoes[id].test($(this).val()))){
			erros.push($("label[for="+id+"]").html()+" inválido.");
		}
	});
	
	if(erros.length) {
		alert("Alguns erros foram encontrados:\n\n"+erros.join("\n")+"\n\n por favor, corrija-os");
	} else {
		$("button",form).attr("disabled","disabled");
		$("button",form).html("Registrando...");
		dados = $(form).serialize();
		$.ajax({
			type: "GET",
			data: {id: 16},
			url: "/contaclique.php"
		});
		$.ajax({ 
		  type: "POST", 
		  url: "solicitarRevista.php", 
		  data: dados, 
		  success: function(data) {
			 alert(data);
			$("button",form).removeAttr("disabled");
			$("button",form).html("Solicitar Revista");
			$('#formSolicitarRevista').fadeOut();
		  } 
		});	
	}
}

