function Trim(str){return str.replace(/^\s+|\s+$/g,"");}
$(document).ready(function(){
		$('#post').click(function() {
			var frm = document.getElementById("login");
			frm.submit();	
		})
	$(document).bind("contextmenu",function(e){
		return false;
	});
	$("#dialog2").dialog({
			bgiframe: true,
			modal: false,
			autoOpen: false,			
			buttons: {
				Ok: function() {
					$(this).dialog('close');
				}
			}
		});

		var name = $("#name"),
			email = $("#email"),
			password = $("#password"),
			allFields = $([]).add(name).add(email).add(password),
			tips = $("#validateTips");

		function updateTips(t) {
			tips.text(t).effect("highlight",{},1500);
		}
		function checkLength(o,n,min,max) {
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips( n + " deve ter entre "+min+" a "+max+" letras.");
				return false;
			} else {
				return true;
			}
		}
		function checkRegexp(o,regexp,n) {
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}
		}
		$("#dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 300,
			modal: true,
			buttons: {
				'Cadastrar': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');
					bValid = bValid && checkLength(name,"O nome",3,200);
					bValid = bValid && checkLength(email,"O e-mail",6,100);
					bValid = bValid && checkLength(password,"A senha",5,8);
					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"ex: joao@ucb.org.br");
					bValid = bValid && checkRegexp(password,/^([0-9a-zA-Z])+$/,"Permitido apenas: a-z 0-9");
					if (bValid) {
						$.post('registro.asp',{
								nome: name.val(),
								email: email.val() ,
								senha:  password.val()
							}, function(response) {
								$("#dialog").dialog('close');
								if (Trim(response) == 'True'){
									$('#dialog2 p').html("Cadastro efetuado com sucesso!");
									$("#dialog2").dialog('open');
								 } else {
									 $('#dialog2 p').html(Trim(response));
									 $("#dialog2").dialog('open');
								 }
								
						})
					}
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
				
		$('#create-user, #create-user2').click(function() {
			$('#dialog').dialog('open');
		}).hover(
			function(){ 
				$(this).addClass("luva"); 
			},
			function(){ 
				$(this).removeClass("luva"); 
			}
		).mousedown(function(){
			$(this).addClass("luva"); 
		})
		.mouseup(function(){
				$(this).removeClass("luva");
		});
	});
function verifica(email) {
	$("button:contains('Cadastrar')").hide();
	$('#carregando').html("<img src='/img/indicator.gif' />").show();
	$.post('/admin/verifica.asp',{
		email: email
	}, function(response) {
		$('#carregando').hide();
		if (Trim(response) == 'sim'){
			//sim, pode cadastrar
			$("input[title='ev']").removeClass('ui-state-error');
			$("button:contains('Cadastrar')").show();
		  } else if (Trim(response) == 'nao') {
		  	//não, não pode cadastrar
			$("#validateTips").html("Este e-mail j&aacute; est&aacute; cadastrado").effect("highlight",{},1500);
			$("input[title='ev']").focus();
			$("input[title='ev']").addClass('ui-state-error');
		  }
	})
}
