function verificaDuplicado(obj) {

	$.post('/penseArgohost/verificaDuplicado/', { email: $(obj).val() }, function(msg){
		
		if(msg == 'duplicado') {
			$('#avisoDuplicado').html('E-mail j&aacute; cadastrado. Seu permalink: <br /><a class="input" href="http://www.penseargohost.net/' + $(obj).val() + '">http://www.penseargohost.net/' + $(obj).val() + '</a>');
			$('#submit').attr('disabled','disabled');
		} else {
			$('#avisoDuplicado').html('');
			$('#submit').removeAttr('disabled');
		}
		
	})

}

$(document).ready(function(){
		
		$("input[@name='resposta']").click(function(){
			if($(this).val() == '') {
				$("input[@name='outra']").focus();
			}
		});
		
		$("input[@name='outra']").click(function(){
			$("input[@name='resposta']:last").attr('checked','checked');
		});
		
		
		$("input[@name='email']").keyup( function() { verificaDuplicado(this) });
		$("input[@name='email']").blur(  function() { verificaDuplicado(this) });
		

	if($("form#cadastrar")) {
		
		var validator = $("form#cadastrar").validate({
			submitHandler: function() {
				$('#submit').attr('disabled','true');
				$("#container").html('<div style="text-align:justify;font-weight:bold">Um momento...</div>').fadeOut('slow').fadeIn('slow').fadeOut('slow').fadeIn('slow').fadeOut('slow').fadeIn('slow'); 
				
				if($("input[@name='idEu']")) {
					idEu = $("input[@name='idEu']").val();
				}
				else {
					idEu = '';
				}

				$.ajax({
					data: 'nome=' + $('#nome').val() + '&email=' + $('#email').val() + '&cidade=' + $('#cidade').val() + '&uf=' + $('#uf').val() + '&resposta=' + $("input[@name='resposta'][@checked]").val() + '&outra=' + $("#outra").val() + '&idEu=' + idEu,
					type: 'post',
					url: '/penseArgohost/cadastrar/',
					success: function(retorno) {
						$("input[@type='text']").val("");
						$("#container").html(retorno).fadeOut('slow').fadeIn('slow');
					}
				})
			},
			
			errorLabelContainer: $("#container"),
			
			rules: {
		
				nome: { required: true },
				
				email: {
					required: true,
					email: true
				},
				
				cidade: {required: true}				
				
			},
		
			messages: {
				nome: 'Ops, digite seu nome completo.<br />',
				
				email: { 
					required: 'Ops, digite seu e-mail.<br />',
					email: 'Ops, e-mail inv&aacute;lido.<br />'
				},
				
				cidade: 'Ops, digite sua cidade.'				
				
			}
		}).showErrors({
			"nome": "",
			"email": "",
			"cidade": ""
		});
	}
	
})