﻿
var func_to_exec = "";
var mail;

function login(module)
{
    var mail = document.getElementById("mail").value;
    var mdp = document.getElementById("mdp").value;
    mail_ax = mail;
    mdp_ax = mdp;
    module_ax = module
    
    var reg_exp = /^[a-z0-9\-_\.]+@[a-z0-9]+\.[a-z]{2,5}$/i;
    
    if (reg_exp.test(mail))
     {
                if (mdp != "")
                {
                    SoapMethod = "POST";
                    ajax.Start(Axlogin);
                }
                else
                {
                    alert("Vous devez rentrer votre mot de passe");
                }
     }
     else
     {
        alert("Adresse mail incorrecte");
        document.getElementById("mail").value = "";
     }
}

var Axlogin = {
	delay: 200,
	prepare: function() 
	{ 
	    InputVar = new Array(mail_ax, mdp_ax, module_ax);	    
	},
	
	call: proxies.AjaxFunc.login,
	finish: function (p) 
	{ 
	
	    if (p != null && p != "0")
	        {
	                        SetCookie("USAGER", p, 2);
	                        
	                        if (module_ax == 1)
	                        {
	                           GetSondages(id_sondage);
	                        }
	                        else if (module_ax == 2)
	                        {
	                            document.location.href="/community/upload_video.aspx";
	                        }
	                        else if (module_ax == 3)
	                        {
	                            document.location.href="/community/videos.aspx?p=0";
	                        }
	                        else if (module_ax == 4)
	                        {
	                           Enregistre_rep_prono();
	                        }
	                        else if (module_ax == 5)
	                        {
	                           document.location.href="/";
	                        }
	                        else if (module_ax == 6)
	                        {
	                           document.location.href="/community/edit_profil.aspx";
	                        }
	                        
	                        TB_AB_remove();
	                        
	                       
	                        
	        }
	        
	    else if (p == "0")
	        {
	                        alert("Vous avez rentré un mauvais mot de passe");
	                        document.getElementById("mdp").value="";
	        }    
	        
	    else
	        {
	                        alert("Vous n'êtes pas encore enregistré");
	                        document.getElementById("mail").value="";
	                        document.getElementById("mdp").value="";
	                        document.getElementById("champ_mdp").style.display = "block";
	                        document.getElementById("pas_membre").checked = true;
	                        document.getElementById("membre").checked = false;           
	        }        
	        
	    
	},
	onException: ajax.alertException
};


function inscription(module)
{
    var mail = document.getElementById("mail").value;
    var mdp = document.getElementById("mdp").value;
    var mdp2 = document.getElementById("mdp2").value;
    mail_ax = mail;
    mdp_ax = mdp;
    module_ax = module;
    
    var reg_exp = /^[a-z0-9\-_\.]+@[a-z0-9]+\.[a-z]{2,5}$/i;

        if (reg_exp.test(mail))
        {
                    if (mdp != "" && mdp2 != "")
                    {
                                    if (mdp == mdp2)
                                    {
                                    SoapMethod = "POST";
	                                ajax.Start(Axinscription);     
                                    }
                                    else 
                                    {
                                    alert("Mot de passe incorrect");
                                    document.getElementById("mdp").value = "";
                                    document.getElementById("mdp2").value = ""; 
                                    }   
                        
                    }
                    else
                    {
                           alert("Vous devez saisir un mot de passe et le confirmer");              
                    }
         }
         else
         {
            alert("Adresse mail incorrecte");
            document.getElementById("mail").value = "";
         }
          
         
}


var Axinscription = {
	delay: 200,
	prepare: function() { 
	InputVar = new Array(mail_ax, mdp_ax, module_ax);	    
	},
	
	call: proxies.AjaxFunc.inscription,
	finish: function (p) 
	{ 
	

	    if (p == null)
	    {
	        alert("Vous êtes déjà enregistré");
	        document.getElementById("pas_membre").checked = false;
	        document.getElementById("membre").checked = true;
	        document.getElementById("champ_mdp").style.display = "none";
	    }
	    else
	    {
	        /*if (func_to_exec)
	        setTimeout(func_to_exec,0);*/
	        SetCookie("USAGER", p, 2);
	        
	        if (module_ax == 1)
	        {
	            GetSondages(id_sondage);
	        }
	        else if (module_ax == 2)
	        {
	            document.location.href="/community/upload_video.aspx";
	        }
	        else if (module_ax == 3)
	        {
	            document.location.href="/community/videos.aspx?p=0";
	        }
	        else if (module_ax == 4)
	        {
	            GetPronostic(id_prono);
	        }
	        else if (module_ax == 5)
	        {
	            document.location.href="/";
	        }
	        else if (module_ax == 6)
            {
               document.location.href="/community/edit_profil.aspx";
            }
	        
	       
	        TB_AB_remove();
	        //document.location.href="";
	        
	       
	    }
	},
	onException: ajax.alertException
};

function logs(bouton, mod)
{
    if(bouton == 0)
    {   
        inscription(mod);
    }
    else if(bouton == 1)
    {
        login(mod);
    }
}



// GESTION DE LA RECUPERATION DU MOT DE PASSE
var AxSendPassword = {
	delay: 200,
	prepare: function() { 
	    InputVar = new Array(mail);	    
	},
	call: proxies.AjaxFunc.SendPassword,
	finish: function (p) 
	{ 
	    if (p == "ok")
	    {
	        alert("Votre mot de passe vous a été envoyé");
	        document.getElementById("Manage1").style.display = "";
            document.getElementById("Manage2").style.display = "none";
	    }
	    else
	    {
	        alert("Vous n'êtes pas inscrit sur le site ABrugby");
	    }
	},
	onException: ajax.alertException
}

function RecupPassword(s)
{
    mail = s;
	var place = mail.indexOf("@",1);
	var point = mail.indexOf(".",place+1);
	if ((place > -1)&&(mail.length >2)&&(point > 1))
	{
		// On ajoute l'adresse à la table Newsletter
		SoapMethod = "POST";
	    ajax.Start(AxSendPassword);
	}
	else
	{
		alert("Entrez un email valide");
	}
}