//<![CDATA[

$(function() {

	$("#login_usuario_topo").focus(function(){
		if ( $(this).val() == "Login" ) {
			$(this).val("");
		}
	});
	
	$("#login_usuario_topo").blur(function(){
		if ( $(this).val() == "" ) {
			$(this).val("Login");
		}
	});
	
	$("#login_senha_label_topo").focus(function(){
		$(this).hide();
		$("#login_senha_topo").show();
		$("#login_senha_topo").focus();
	});
	
	// login topo
	$("#form_login_topo").submit(function(event) {
		event.preventDefault();

		$.post("/user/login", $(this).serialize(), function(data){
			try {
				if ( data.done == true ) {
					//alert("Login efetuado com sucesso");
					// Recarregar tudo
					//location.reload();
					window.location="/servicos";
				}
				else {
					alert(data.msg);
				}
			}
			catch (err) {
				alert("Erro de comunicação com o servidor");
			}
		}, "json");
		
	});

	// login
	$("#form_login").submit(function(event) {
		event.preventDefault();

		$.post("/user/login", $(this).serialize(), function(data){
			try {
				if ( data.done == true ) {
					//alert("Login efetuado com sucesso");
					// Recarregar tudo
					location.reload();
				}
				else {
					alert(data.msg);
				}
			}
			catch (err) {
				alert("Erro de comunicação com o servidor");
			}
		}, "json");
		
	});
	
	// cadastro
	$("#form_register").submit(function(event) {
		event.preventDefault();

		$(".field_error").html("");
		$(this).disabled = true;
		$.post("/user/register", $(this).serialize(), function(data){
			try {
				if ( data.done == true ) {
					alert("Verifique seu email para fazer a confirmação do cadastro.");
				}
				else {
					$.each(data, function(index, value) { 
						$("#"+index).html(value);
					});
				}
			}
			catch (err) {
				alert("Erro de comunicação com o servidor");
			}
		}, "json");
		$(this).disabled = false;
	});
	
	$("#reset_password").submit(function(event) {
		event.preventDefault();
		
		return false;
		$.post("/user/reset_password", $("#reset_password").serialize(), function(data){
			try {
				if ( data.done == true ) {
					$.each(data, function(index, value) { 
						console.log(index + ": " + value);
					});
				}
				else {
					$.each(data, function(index, value) { 
						console.log(index + ": " + value);
					});
				}
			}
			catch (err) {
				//console.log("Erro de comunicação com o servidor");
			}
		}, "json");
	});	

	$("#change_password").submit(function(event) {
		event.preventDefault();
		
		$.post("/user/reset_new_password", $("#change_password").serialize(), function(data){
			try {
				if ( data.done == true ) {
					$.each(data, function(index, value) { 
						console.log(index + ": " + value);
					});
				}
				else {
					$.each(data, function(index, value) { 
						console.log(index + ": " + value);
					});
				}
			}
			catch (err) {
				//console.log("Erro de comunicação com o servidor");
			}
		}, "json");

	});
	
});

// Confirmar saída se há operação em andamento
var idle = true;
window.onbeforeunload = confirmExit;

function confirmExit() {
	if ( idle != true ) {
		return "Há uma operação em andamento. Tem certeza que deseja sair da página?";
	}
}

//]]>

