$(function(){
	$("#authentification").submit(function (){
		connecter();
		return false;
	});

	$("#connexion").click(function(){
		connecter();
		return false;
	});
});
function connecter(){
    var param = "module=jauth&action=loginsw:in&login=" + $('#login').val() + "&password=" + $('#password').val();
        $.ajax({
            type: "POST",
            url : j_basepath + "ajax.php",
            data: param,
            dataType: 'text',
            async: false,
            success: function(resultat){
                if(resultat == 'OK'){
                    // appel de l'action user~compteFo:index
                    $.post(j_basepath + "ajax.php?module=user&action=compteFo:modifierDerniereConnexion",function(data){
                        if(data == 1) {
                            //document.location.href=j_basepath + "index.php?module=user&action=compteFo:index";
                            var url = (j_locale=='fr')?j_locale+"/votre-compte/":j_locale+"/your-account/"; 
                            document.location.href=j_basepath + "index.php/"+url;
                        }
                    });                    
                }else{
                    // revient sur la page d'authentification
                    $('.form_connexion').hide('slow',function(){
                    		 $('#echec_auth').show('slow'); 
                    	});
                    	if (typeof(iTimeoutID) != 'undefined') {
							clearTimeout(iTimeoutID);
						}
						iTimeoutID = setTimeout(function(){
						$("#echec_auth").hide('slow',function(){
								$('.form_connexion').show('slow');
								$('#login').css("border","1px solid red");
                    			$('#password').css("border","1px solid red");
							});				
						}, 4000, 'JavaScript');
                }
            }
        });
}

function trim(myString){
    return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
} 

function oubliMdp(){
        $.ajax({
            type:'POST',
            url:j_basepath+'ajax.php',
            data:'module=user&action=compteFo:motDePasseOublie&email='+trim(document.getElementById('txt_email_log').value),
            dataType:'json',
            async:false,
            success:function(result){
                    //console.log(result);
                     switch(result){
                    case 0:
                         $('#erreur_oubliForm1').css('display','block'); 
                          $('.form_connexion').hide('slow',function(){
                    		 $('#erreur_oubliForm1').show('slow'); 
                    	});
                    	if (typeof(iTimeoutID) != 'undefined') {
							clearTimeout(iTimeoutID);
						}
						iTimeoutID = setTimeout(function(){
						$("#erreur_oubliForm1").hide('slow',function(){
								$('.form_connexion').show('slow');
								$('#txt_email_log').css("border","1px solid red");
							});				
						}, 3000, 'JavaScript');
                         $('#erreur_oubliForm2').css('display','none');                          
                    break;
                    case 1:
                    	$('#erreur_oubliForm1').css('display','none');
                    	$('#txt_email_log').css("border","none");
                    	$('.form_connexion').hide('slow',function(){
                    		 $('#erreur_oubliForm2').show('slow'); 
                    	});
                    	if (typeof(iTimeoutID) != 'undefined') {
							clearTimeout(iTimeoutID);
						}
						iTimeoutID = setTimeout(function(){
						$("#erreur_oubliForm2").hide('slow',function(){
								$('.form_connexion').show('slow');
							});				
						}, 4000, 'JavaScript');
                        
                }
                
                
            }
        });
}

function oubliMdpAuthentification(){
        $.ajax({
            type:'POST',
            url:j_basepath+'ajax.php',
            data:'module=user&action=compteFo:motDePasseOublie&email='+trim(document.getElementById('txt_email_forget').value),
            dataType:'json',
            async:false,
            success:function(result){
                    //console.log(result);
                      switch(result){
                    case 0:
                         var msg= "<ul style='color:#f3af4c'><li>" + $('#erreur_bag_oubliForm1').val() + "</li></ul>";
	                    $('#txt_email_forget').addClass('invalid');
						$('#contentMsg_bagForgotPwd').empty();
						$('#contentMsg_bagForgotPwd').append(msg);
						error_action('error_bagForgotPwd');
                    break;
                    case 1:
                        $('#erreur_bag_oubliForm1').css('display','none');
                        $('#part_bag_forget').hide('slow',function(){
                    		 $('#erreur_bag_oubliForm2').show('slow'); 
                    	});
                    	if (typeof(iTimeoutID) != 'undefined') {
							clearTimeout(iTimeoutID);
						}
						iTimeoutID = setTimeout(function(){
						$("#erreur_bag_oubliForm2").hide('slow',function(){
								$('#part_bag_forget').show('slow');
							});				
						}, 5000, 'JavaScript');
                }
            }
        });
}

function deconnecter(){
    $.ajax({
        type : "POST",
        url  : j_basepath + "ajax.php",
        data : "module=user&action=compteFo:deconnecter",
        //data : "module=jauth&action=loginsw:out",
        success:function(result){
            //document.location.href=j_basepath + "index.php";
            var url = (j_locale=='fr')?j_locale+"/accueil":j_locale+"/home"; 
            document.location.href=j_basepath + "index.php/"+url;
        }
    });
}

function authentification(){
    var param = "module=jauth&action=loginsw:in&login=" + $('#txt_mail_id').val() + "&password=" + $('#txt_mail_pwd').val();
        $.ajax({
            type: "POST",
            url : j_basepath + "ajax.php",
            data: param,
            async: false,
            success: function(resultat){
                if(resultat == 'OK'){
                    // appel de l'action user~compteFo:index
                    $.post(j_basepath + "ajax.php?module=user&action=compteFo:modifierDerniereConnexion",function(data){
                        if(data == 1) {
                            //document.location.href=j_basepath + "index.php?module=transport&action=offreTransportFo:afficher";
                            var url = (j_locale=='fr')?j_locale+"/transport/":j_locale+"/transport/"; 
            				document.location.href=j_basepath + "index.php/"+url;
                        }
                    });                    
                }else{
                    // revient sur la page d'authentification
                    var msg="<ul style='color:#f3af4c'><li>" + $('#echec_bag_auth').val() + "</li></ul>";
                    $('#txt_mail_id').addClass('invalid');
                    $('#txt_mail_pwd').addClass('invalid');
					$('#contentMsg_bagAuth').empty();
					$('#contentMsg_bagAuth').append(msg);
					error_action('error_bagAuth');
                }
            }
        });
}
