﻿
	$(function() {
	    //$(".showMore").hide();
        //$(".showLess").show();
	    //$("#edreg").hide();
	    //$("#reg_").hide();
		var username = $("#txtusername"),
		    password = $("#txtpassword"), 
		    tips = $("#validateTips"),
		    tipspass = $("#validateTipsPass"),
		    //isLogged = <%=strIsLogged %>,
		    errorMessage;		   
		    		
		function checkEmpty(o,n) {
			if ( o.val().length == 0) {
				errorMessage+="Campo " + n + " não pode ser vazio. \r\n" ;
				return false;
			} else {
				return true;
			}
		} 			
		
		function checkLength(o,n,min,max) {
			if ( o.val().length > max || o.val().length < min ) {
				return false;
			} else {
				return true;
			}
		}    
		    			    				
		function checkRegexp(o,regexp,n) {
			if ( !( regexp.test( o.val() ) ) ) {
				errorMessage+=n+"\r\n";
				return false;
			} else {
				return true;
			}
		}	
        //if(isLogged){
//        alert("Rui");
            //alert('islogged');
           // $("#login").css('display','none')
            //$("#loginMessage").css('display','block')
            //tips.text(<%=strUsername %>);
        //}

        $('#txtusername').bind('keypress', function(e) {
        if(e.keyCode==13){
            $("#OKSubmit").click(); 
            } 
        });
        
        $('#txtpassword').bind('keypress', function(e) {
        if(e.keyCode==13){
            $("#OKSubmit").click(); 
            } 
        });
        
		$("#OKSubmit").click(function(){
		    var bValid=true;		    
		    errorMessage="";
		    bValid= bValid && checkEmpty(username,"Email");		    
		    if (errorMessage==""){bValid = bValid && checkRegexp(username,/^((([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,"Email Inválido. ex.: msilva@rr.pt \r\n")};
		    bValid= bValid && checkEmpty(password,"Password");
		    if (bValid==false) {

		        //DETECTA ERROS E RETORNA MENSAGEM
		        $("#login").hide('fast',function(){
		            $("#loginMessage").show();
		            tips.text(errorMessage);
		            tipspass.text(errorMessage);
		        });			        
		    }
		    else{
		        //VAI FAZER LOGIN
		        var urlData='email=' + encodeURIComponent(username.val()) + '&password='+encodeURIComponent(password.val())+'&rememberme='+$("#login").find("input[name='rememberMe']").attr('checked');                                 
                $.ajax({
                    url: 'ajax/megavip/loginmegavip.aspx',
                    type: 'POST',
                    cache: false,                                    
                    data : urlData,                            
                    success : function(result) {
                        if (result=='-1') {
                            //ocorreram erros no login                            
                            //combinação email password incorrectas. tentar novamente lembrar password 
                            $("#login").hide('fast',function(){
		                    $("#loginMessagePass").show();
		                    //tips.text(errorMessage);
		                    //tipspass.text(errorMessage);
		                    });	                       
                        }
                        else {
                            //Login sem problemas
	                        $("#login").hide('fast',function(){
	                            document.getElementById('logged').style.visibility = "visible"; 
	                            $("#logged").show();
	                            $("#logged").html(result);
	                            
	                            $("#notloggedComment").hide();
                                $("#loggedComment").show();
                                
                                //Formulário
                                $("#loggedFormularioNow").show();
                                $("#notloggedFormulario").hide();
	                        });	                                                                                         
                        }                        
                    },
                    failure : function(result) {
                         console.error(arguments);
                    }
                });                             		        		        
		    }	    
		});
	    $("#aErrorOkPass").click(function(){
	        $("#loginMessagePass").hide('fast',function(){
	            $("#login").show();
	        }); 
	    });
	
		$("#aErrorOk").click(function(){
	        $("#loginMessage").hide('fast',function(){
	            $("#login").show();
	        });			        
		});				
    });


    /*********LogOff*********/
    function logOff(){
        $.ajax({
        type: "POST",
        url: "ajax/megavip/logoffmegavip.aspx",
                    success : function(result) {
                    
                        if (result=='-1') {
                            //ocorreram erros no login                            
                            //combinação email password incorrectas. tentar novamente lembrar password                        
                        }
                        else {
	                        $("#login").show();
	                        $("#logged").hide();
	                        document.getElementById('login').style.visibility = "visible";
                            $("#notloggedComment").show();
                            $("#loggedComment").hide();
                            //Formulário
                            $("#loggedFormularioNow").hide();
                            $("#notloggedFormulario").show();                                                         
                        }                        
                    }
     });
    return false;
}
function cleanTextUser(){
	document.getElementById('txtusername').value = "";
}
function cleanTextPass(){
    document.getElementById('txtpassword').value = "";
}

