﻿$.ajaxSetup({cache: false});

$.fn.serializeObject = function()
{
   var o = {};
   var a = this.serializeArray();
   $.each(a, function() {
       if (o[this.name]) {
           if (!o[this.name].push) {
               o[this.name] = [o[this.name]];
           }
           o[this.name].push(this.value || '');
       } else {
           o[this.name] = this.value || '';
       }
   });
   return o;
};

var intervalo;

function Trim(str){return str.replace(/^\s+|\s+$/g,"");}


function login() {
	document.getElementById("btnAcessar").style.display = 'none';
	document.getElementById("loading").style.display = '';
	fUnBlink();
	
	$.ajax({
		type: "POST",
		url: "Default.aspx/ValidaLogin",
		data: "{usuario: '" + $('#txtLogin').val() + "', pass: '" + $('#txtSenha').val() + "'}",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(response) {
			fLogin(response.d);
		}
	});
} 

function loginAdmin() {

	$.ajax({
		type: "POST",
		url: "Default.aspx/ValidaLoginAdmin",
		data: "{usuario: '" + $('#txtUsuario').val() + "', pass: '" + $('#txtSenha').val() + "'}",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(response) {
			fLoginAdmin(response.d);
		}
	});
} 

function logout() {
	$.ajax({
		type: "POST",
		url: "Default.aspx/Logout",
		data: "{}",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(response) {
			fLogout(response.d);
		}
	});
} 


function fLogin(response){
	if (response == "-1"){
		alert('Erro ao tentar validar o Acesso! Favor entrar em contato com o administrador');
	}
	else if (response == "") {
		$('#lbMSG').html("Usuário/Senha Inválida!");
		$('#lbMSG').show();		
		intervalo = window.setInterval(fBlink, 500);
	}
	else{
		var s = response.split("|");
		
		document.getElementById("tabelaLogin").style.display = 'none';
		document.getElementById("tabelaLogado").style.display = '';
		$('#tdNomeAssociado').html(s[1]);
		$('#conteudo').load('artigos/associados.aspx');
	}
	
	document.getElementById("btnAcessar").style.display = '';
	document.getElementById("loading").style.display = 'none';
}

function fLoginAdmin(response){
	if (response == "-1"){
		alert('Erro ao tentar validar o Acesso! Favor entrar em contato com o administrador');
	}
	else if (response == "0") {
		alert("Usuário ou senha inválida!");
	}
	else{
		location.href = 'default.aspx';
	}
}

function fLogout(response){
	if (response == "-1"){
		alert('Erro ao tentar sari da área de associados! Favor entrar em contato com o administrador');
	}
	else{
		document.getElementById("tabelaLogin").style.display = '';
		document.getElementById("tabelaLogado").style.display = 'none';
		$('#tdNomeAssociado').html("");
		document.getElementById("txtLogin").value = "";
		document.getElementById("txtSenha").value = "";
	}
	
	//document.getElementById("btnAcessar").style.display = '';
	//document.getElementById("loading").style.display = 'none';
}

function fBlink(){
	if( $('#lbMSG').is(':visible') ) {
		if ($.browser.msie)
			$("#lbMSG").fadeOut(1);
		else
			$("#lbMSG").fadeOut(300);
	}
	else {
		if ($.browser.msie)
			$("#lbMSG").fadeIn(1);
		else
			$("#lbMSG").fadeIn(300);
	}
}

function fUnBlink(){
	clearInterval(intervalo);
	$('#lbMSG').hide();
}

function sairAdmin() {

	$.ajax({
		type: "POST",
		url: "Default.aspx/sairAdmin",
		data: "",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(response) {
			fSairAdmin(response.d);
		}
	});
} 

function fSairAdmin(){
	location.href = '../default.aspx';
}

function fResize(o){
	var nome = "#" + o.id;
	
	var top = $(nome).offset().top;
	var left = $(nome).offset().left;
	
	o.style.position = 'absolute';
	o.style.top = top + 'px';
	o.style.left = left + 'px';
	
	$(nome).animate({width: "160", marginLeft: "-40px"}, 300);
}

function fUnResize(o){
	var nome = "#" + o.id;
	
	$(nome).animate({width: "80", marginLeft: "0px"}, 300, null, function() {o.style.position = '';});
	//o.style.position = 'absolute';
	//o.style.top = top + 'px';
	//o.style.left = left + 'px';
}

function carregaMaskedInputAssociar(){
	$("#txtDataNascimento").mask("99/99/9999");
	$("#txtEmissao").mask("99/99/9999");
	$("#txtCep").mask("99.999-999");
	$("#txtCpf").mask("999.999.999-99");
	$("#txtTelRes").mask("(99) 9999-9999");
	$("#txtCel").mask("(99) 9999-9999");
	$("#txtTelCon").mask("(99) 9999-9999");
	$("#txtFax").mask("(99) 9999-9999");
}


function enviarAdesao() {
	//document.getElementById("btnAcessar").style.display = 'none';
	//document.getElementById("loading").style.display = '';
	//fUnBlink();
	
	if(!validaFormAdesao()){
		return false;
	}
	
	var form = $('#formulario').serializeObject();
	//alert(JSON.stringify(form));
	$.ajax({
		type: "POST",
		url: "Default.aspx/EnviaPropostaAdesao",
		data: "{param: " + JSON.stringify(form) + "}", 
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(response) {
			var vet = response.d.split("|");
			
			if (vet[0] == "0"){
				alert(vet[1]);
				$.fancybox.close();
			}
			else{
				alert(vet[1]);
			}
		}
	});
} 


function validaFormAdesao(){
	
	if(Trim($('#txtNome').val()) == ''){
		alert('Digite o Nome!');
		$('#txtNome').focus();
		return false;
	}

	if($("input[@name='rdQualif']:checked").val() == undefined){
		alert('Informe a Qualificação!');
		return false;
	}
	if(Trim($('#txtDataNascimento').val()) == ''){
		alert('Digite a Data de Nascimento!');
		$('#txtDataNascimento').focus();
		return false;
	}
	if(Trim($('#txtEndereco').val()) == ''){
		alert('Digite o Endereço!');
		$('#txtEndereco').focus();
		return false;
	}
	if(Trim($('#txtBairro').val()) == ''){
		alert('Digite o Bairro!');
		$('#txtBairro').focus();
		return false;
	}
	if(Trim($('#txtCidade').val()) == ''){
		alert('Digite a Cidade!');
		$('#txtCidade').focus();
		return false;
	}
	if(Trim($('#cmbUF').val()) == ''){
		alert('Digite o Estado!');
		$('#cmbUF').focus();
		return false;
	}
	if(Trim($('#txtCep').val()) == ''){
		alert('Digite o CEP!');
		$('#txtCep').focus();
		return false;
	}
	if(Trim($('#txtCro').val()) == ''){
		alert('Digite o CRO!');
		$('#txtCro').focus();
		return false;
	}
	if(Trim($('#cmbUF_cro').val()) == ''){
		alert('Digite o Estado de Emissão do CRO!');
		$('#cmbUF_cro').focus();
		return false;
	}
	if(Trim($('#txtCpf').val()) == ''){
		alert('Digite o CPF!');
		$('#txtCpf').focus();
		return false;
	}
	if(Trim($('#txtEmail').val()) == ''){
		alert('Digite o email!');
		$('#txtEmail').focus();
		return false;
	}
	
	var regmail = /^[\w!#$%&amp;'*+\/=?^`{|}~-]+(\.[\w!#$%&amp;'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
	if (!regmail.test($("#txtEmail").val())) {
		alert('Email Inválido!');
		$('#txtEmail').focus();
		return false;
	}
//	if(Trim($('#txtUsuario').val()) == ''){
//		alert('Digite o Usuário!');
//		$('#txtUsuario').focus();
//		return false;
//	}
	if(Trim($('#txtSenhaAssociar').val()) == ''){
		alert('Digite uma Senha!');
		$('#txtSenhaAssociar').focus();
		return false;
	}
	if($('#txtSenhaAssociar').val() != $('#txtSenhaAssociar2').val()){
		alert('A senhas digitada não conferem!');
		$('#txtSenhaAssociar2').focus();
		return false;
	}
	
	return true;
}

function carregaEventos(){
	$("a#curso_evento1").fancybox({'titleShow' : false, 'showCloseButton' : false, 'padding' : 0});
	$("a#curso_evento2").fancybox({'titleShow' : false, 'showCloseButton' : false, 'padding' : 0});
	$("a#curso_evento3").fancybox({'titleShow' : false, 'showCloseButton' : true, 'padding' : 0});
	//$("a#curso_evento4").fancybox({'titleShow' : false, 'showCloseButton' : true, 'padding' : 0});
	//$("a#curso_evento5").fancybox({'titleShow' : false, 'showCloseButton' : true, 'padding' : 0});
	//$("a#curso_evento6").fancybox({'titleShow' : false, 'showCloseButton' : false, 'padding' : 0});
	$("a#curso_evento7").fancybox({'titleShow' : false, 'showCloseButton' : false, 'padding' : 0, 'autoDimensions' : false, 'width' : 740, 'height' : 'auto'});
}
