﻿function obj_compte()
{ 
    var m_str_urlToCreateAdr_inTunnelAchat = "/ajax/commande/livraison/ajouter.html?1=1";
    var m_str_urlToModifAdr_inTunnelAchat = "/ajax/commande/livraison/modifier.html?1=1";
    var m_str_urlToviewOtherAdr_inTunnelAchat = "/ajax/commande/livraison/changer-adresse.html?1=1";
    var m_str_urlToDetailCde = "/comptes/commande/detail.html?1=1";
    var m_str_urlToDesabonnement = "/lettre_mollat/desabonnement.aspx?1=1";
    
    //#################################################################################################
    // Vérification du formulaire de connexion au site 
    // _application/_controls/_zoneFront/comptes/form_login.ascx 
    //#################################################################################################
    this.check_form_login = function(curForm) 
    {
	    var retVal = false;
	
	    var email = curForm.form_email;
	    var pass = curForm.form_pass;
	    var hiddenDivError = curForm.form_hdn_divError;
	    
	    var divError = 	document.getElementById(hiddenDivError.value);
	    var goodEmail = (email.value.length != 0 && check_email(email.value)); // un mail est valide si il est remplis et est conforme a la RFC...
	    var goodPass = (pass.value.length != 0); // on verifie seulement la taille du mdp ici
	
	    // si le login et le mdp sont "valide" on le dit, sinon on informe l'utilisateur de ce qui n'est pâs valide 
	    if (goodEmail && goodPass) 
	    {
	        retVal = true;
	        if (divError)
			{divError.innerHTML = "";}
	    }
	    else 
	    {
		    retVal = false;		
		
		    var msg = "";
		
		    if (email.value.length == 0 && !goodPass) 
		    {msg += "Vous devez saisir votre identifiant et votre mot de passe pour votre identifier.";}
		    else if (!goodEmail) 
		    {
				if (email.value.length == 0) 
				{msg += "Veuillez saisir votre e-mail.\n";}
				else if (!check_email(email.value)) 
				{msg += "Veuillez saisir un e-mail valide.\n";}
			}
			else if (!goodPass) 
			{msg += "Veuillez saisir votre mot de passe.\n";}
			
			if (divError)
			{divError.innerHTML = msg;}
		}
		
	
	    return retVal;
	
    } // END function check_form_login()


     this.xhrLink_creationAdresse = function(inPopPup)
     {
        // si la quantité saisi n'est pas valide, on la met par défaut à 1
	    var url =  m_str_urlToCreateAdr_inTunnelAchat;
        
        if (inPopPup)
        {
            print_pseudoPopUp(url, "", true, 2, "Chargement en cours");
        }
        else
        {
            var myXHR = new httpRequest(url, "", "POST");
            myXHR.LoadContent("pseudo_popUp_1", "...", "print_pseudoPopUp.resize_popUp()");
        }
        
        return false;
     }
     
     this.xhrLink_viewOtherAdresse = function(adr_id)
     {
	    var url =  m_str_urlToviewOtherAdr_inTunnelAchat + '&id=' + adr_id;
        var myXHR = new httpRequest(url, "", "POST");
        
        print_pseudoPopUp(url, "", true, 2, "Chargement en cours");
        
        return false;
     }
     
     
     this.xhrLink_modifAdresse = function(idAdr, inPopPup, cmd, hdn_livraison, checkKdo, messKdo)
     {
	    var url =  m_str_urlToModifAdr_inTunnelAchat +'&id='+ idAdr;
	    
	    if (cmd != undefined) {
	        url += '&cmd=' + cmd;
	    }
	    if (checkKdo != undefined) {
	        url += '&chk_kdo=' + checkKdo;
	    }
	    if (messKdo != undefined) {
	        url += '&form_contact_message=' + messKdo;
	    }
	    if (hdn_livraison != undefined) {
	        url += '&hdn_livraison=' + hdn_livraison;
	    }
        
        if (inPopPup) {
            print_pseudoPopUp(url, "", true, 2, "Chargement en cours");
        } 
        else {
            var myXHR = new httpRequest(url , "", "POST");
            myXHR.LoadContent("pseudo_popUp_1", "...", "print_pseudoPopUp.resize_popUp()");
        }
        
        return false;
     }
     
     
     this.activate_retraitLibrairie  = function()
     {
        
        document.getElementById('optLivr_shop').className = "";
        document.getElementById('optLivr_shop_label').className = "optLivr_label_selected";
        
        document.getElementById('optLivr_home').className = "optLivr_disabled";
        document.getElementById('optLivr_home_label').className = "optLivr_label";
        document.getElementById('optAdr').style.visibility = "hidden";
        
        document.getElementById('hdn_livraison').value = '0';
        
     }
     
     this.activate_thisAdresse = function(idAdr)
     {
        document.getElementById('optLivr_shop').className = "optLivr_disabled";
        document.getElementById('optLivr_shop_label').className = "optLivr_label";
        
        document.getElementById('optLivr_home').className = "";
        document.getElementById('optLivr_home_label').className = "optLivr_label_selected";
        document.getElementById('optAdr').style.visibility = "visible";
             
        document.getElementById('hdn_livraison').value = idAdr;
        
     }
     
     this.activate_formPopUp_thisAdresse = function(idAdr, forLink)
     {
        this.activate_thisAdresse(idAdr, forLink);
     }
     
     /* afficher le détails d'une commande client */
     this.xhrDetailCde = function(idCde)
     { 
        return pv_xhrPop(m_str_urlToDetailCde + "&id_cde=" + idCde);
     }
      
       var pv_xhrPop= function(url) 
    { 
        print_pseudoPopUp(url, "", true, 2, "Chargement en cours");
        
        return false;
    }
    
         
    //#################################################################################################
    // Vérification du formulaire de création/modification du profil utilisateur 
    // _application/_controls/_zoneFront/comptes/form_profil.ascx 
    //#################################################################################################
    this.check_form_newAccount = function (isModif) 
    {
	    var retVal = false;
    	
	    var prenom = document.getElementById('form_prenom');
	    var nom = document.getElementById('form_nom');
	    var email = document.getElementById('form_email');
    	
	    var prenom_ok = (prenom.value != '');
	    var nom_ok = (nom.value != '');
	    var email_ok = check_email(email.value);
    	
    	var adrL1 = document.getElementById('form_adr_ligne1') , 
    	    adrCP = document.getElementById('form_adr_cp'), 
    	    adrville = document.getElementById('form_adr_ville'),
    	    adrTel = document.getElementById('form_adr_tel1');
    	
    	var adrLi_ok =  (adrL1.value != ''),
    	    adrCP_ok =  (adrCP.value != ''),
    	    adrville_ok =  (adrville.value != ''),
    	    adrTel_ok =  (adrTel.value != '') ;
    	    
    	var pass_ok, pass2_ok, pass_confirmed;
    	
	    if (!isModif) {
		    var pass = document.form_newAccount.form_pass; //document.getElementById('form_pass');
		    var pass2 = document.form_newAccount.form_pass2; //document.getElementById('form_pass2');
		    pass_ok = (pass.value != '');
		    pass2_ok = (pass2.value != '');
		    pass_confirmed = (pass.value == pass2.value);
	    }
	    else {
		    pass_ok = true;
		    pass2_ok = true;
		    pass_confirmed = true;
	    }
    	
    	
	    if (prenom_ok && nom_ok && email_ok && pass_ok && pass2_ok && pass_confirmed && adrTel_ok && adrville_ok && adrCP_ok && adrLi_ok) 
	    {
		    retVal = true;
	    }
	    else 
	    {
		    var nbErrorSaisie = 0;
		    var msg_error = ''; 
    		
		    if (!prenom_ok) {
			    nbErrorSaisie += 1;
			    msg_error += ' - le pr\351nom\n';
		    }
		    if (!nom_ok) {
			    nbErrorSaisie += 1;
			    msg_error += ' - le nom\n';
		    }
		    if (!email_ok) {
			    nbErrorSaisie += 1;
			    msg_error += ' - un e-mail correct\n';
		    }
		    if (!pass_ok) {
			    nbErrorSaisie += 1;
			    msg_error += ' - un mot de passe\n';
		    }
		    if (!pass2_ok) {
			    nbErrorSaisie += 1;
			    msg_error += ' - la confirmation du mot de passe\n';
		    }
		    
		    if (!adrTel_ok){
			    nbErrorSaisie += 1;
			    msg_error += ' - le t\351l\351phone\n';
		    }
		    
		    if (!adrCP_ok){
			    nbErrorSaisie += 1;
			    msg_error += ' - le code postal\n';
		    }
		    if (!adrLi_ok){
			    nbErrorSaisie += 1;
			    msg_error += ' - une adresse\n';
		    }
		    
		    if (!adrville_ok){
			    nbErrorSaisie += 1;
			    msg_error += ' - la ville\n';
		    }
		    
    		
		    var msg = '';			
		    if (nbErrorSaisie != 0) {
			    msg += 'Vous devez saisir :\n' + msg_error + '\n';
		    }
    		
		    if (pass_ok && pass2_ok && !pass_confirmed) {
			    msg += 'Le mot de passe et sa confirmation ne concordent pas.\nVeuillez les saisir \340 nouveau SVP.\n\n ';
			    pass.value = '';
			    pass2.value = '';
		    }
    		 
		    retVal = false;
    	    alert(msg);
	    }
	    return retVal;
    }
    
    this.desabonnement_lettreMollat = function(idUser, mailUser)
    {
        return pv_xhrPop(m_str_urlToDesabonnement + "&id=" + idUser + '&mail=' + mailUser);
    }
    
    this.verifForm = function()
    {
        if (document.getElementById('hdn_livraison').value.length > 0)
        {
            return true;
        }
        else
        {
            alert("Vous devez choisir une adresse de livraison");
            return false;
        }
    
    }
}
