﻿
/* Init */
$(window).resize(function() {
	resizeLayerPopin();
});
$(document).ready(function(){
	
	initPopinForgot();
	checkFormIdentification();
	layerPass();	
});



//////////////////////// label pass
function layerPassGeneric(elt, inputFocus){
   elt.hide();
    var inputPass = elt.parent().find(".inputPass");
   
    if (inputFocus == false){
          inputPass[0].focus();
	 	}
    inputPass.blur(function(){
        hideLayerPass($(this));
    });   
}


function hideLayerPass(inputElt){
    if(inputElt.attr("value") == null){
        inputElt.parent().find(".layerPass").show();
    }
    else{
         inputElt.parent().find(".layerPass").hide();
    }
}
/////////////////////////////// Input texte
function layerInputGeneric(elt, inputFocus, valuedefault){
   
    var inputText = elt;
    if (inputText.attr("value") == valuedefault){
        inputText.val("");
    }
    if (inputFocus == false){
	    inputText.focus();
	}
    inputText.blur(function(){
             BlurInputPass(inputText, valuedefault);
    });   
}

function BlurInputPass(inputElt, valuedefault)
{
     if (inputElt.attr("value") == null){
        inputElt.val(valuedefault);
      }
}
////////////////////////Change

function layerChangePass(elt, inputFocus){
   
    var inputPass = elt.parent().parent().find(".MailBlock");
    if(inputPass!=null)    
    {
        if(inputPass.children(".inputEmail").attr("value") !=null)
        {
             $(".layerPass").hide();
        }
    }
    if (inputFocus == false){
	    inputPass.focus();
	}
    inputPass.blur(function(){
        hideLayerPass($(this));
    });   
}

function layerPass(){
    
     $(".inputPass").each(function(){
        hideLayerPass($(this));
     });
     
    $(".layerPass").each(function(){
            $(this).click(function() {
                layerPassGeneric($(this),false);
                return false;
		 });
	});
	
    $(".inputPass").focus(function(){
        layerPassGeneric($(this).parent().find(".layerPass"),true);
		 return false;
	});
	
	$(".inputEmail").click(function(){
	    
	     layerInputGeneric($(this),false, "Email");
		 return false;
 	});
 	
 	$(".noMarginAuthentification").click(function(){
	     layerInputGeneric($(this),false, "N° de membre");
	     //eventStopPropagation();
		 return false;
 	});
 	

    $(".inputEmail").change(function(){
        $("#layerPass").hide();
    });
    $(".layerPass").change(function(){
        $("#layerPass").hide();
   });  
   
     
}

var tant=0;
var timerID  = null;

// Ouverture de Popin //
function openPopinIdentification(popinUrl,ObjUrl, ObjService){
		$("#genericPopin").remove();
		$.ajax({
			type:"POST",
			url:popinUrl,
			data:"ObjUrl="+ ObjUrl + "&ObjService="+ ObjService  + "&ObjUrlToLoadAfterPopIn=NotSet",
			beforeSend:function() {
			$("#contenu").append('<div id="genericPopin"><div id="bgGenericPopin"></div><img id="imgLoader" src="/common/Images/ajax-loader.gif" alt="loading..." /></div>');
			backgroundPopin2();
			},
			complete:function() {
				$("#imgLoader").remove();
			},
			success:function(msg) {
				$("#bgGenericPopin").after(msg);				
				checkFormIdentification();
				layerPass();
			}
		});
}

// Ouverture de Popin //
function openPopinIdentificationWithUrlReturn(popinUrl,ObjUrl, ObjService, ObjUrlToLoadAfterPopIn){
		$("#genericPopin").remove();
		$.ajax({
			type:"POST",
			url:popinUrl,
			data:"ObjUrl="+ ObjUrl + "&ObjService="+ ObjService + "&ObjUrlToLoadAfterPopIn=" + ObjUrlToLoadAfterPopIn,
			beforeSend:function() {
			$("#corps").after('<div id="genericPopin"><div id="bgGenericPopin"></div><img id="imgLoader" src="/common/Images/ajax-loader.gif" alt="loading..." /></div>');
			backgroundPopin2();
			},
			complete:function() {
				$("#imgLoader").remove();
			},
			success:function(msg) {
				$("#bgGenericPopin").after(msg);
				//closePopinSend();
				checkFormIdentification();
				layerPass();
			}
		});
}

function openPopinIdentificationWithClosePopUp(popinUrl,ObjUrl, ObjService, Email){
		
		$("#genericPopin").remove();
		$.ajax({
			type:"POST",
			url:popinUrl,
			data:"ObjUrl="+ ObjUrl + "&ObjService="+ ObjService +"&Email="+ Email,
			beforeSend:function() {
			// Loader 
			$("#corps").after('<div id="genericPopin"><div id="bgGenericPopin"></div><img id="imgLoader" src="/common/Images/ajax-loader.gif" alt="loading..." /></div>')
			backgroundPopin2();
			},
			complete:function() {
				$("#imgLoader").remove();
			},
			success:function(msg) {
				$("#bgGenericPopin").after(msg);
				closePopinMkr();
				checkFormIdentification();
			}
		});
		
}


function clearMkrUserSuggestedAccount(){
		$.ajax({
			type:"POST",
			url:"/Authentification/Popin/CleartMkrUserSuggestedAccount.aspx",	
			data:"ObjUrl=fdsfds",		
			beforeSend:function() {			
			    void(0);
			},
			complete:function() {				
			    void(0);
			},
			success:function(msg) {			
			    void(0);
			}
		});

}


//ouverture popins password et nb de membre oublie
function initPopinForgot() {
	$("p[@class*=forgot] a").click(function() {
		openPopinForgot($(this).attr("href"));
		return false;
	});
};

// Ouverture de Popin //
function openPopinForgot(popinUrl){
		$("#genericPopin").remove();
		$.ajax({
			type:"GET",
			url:popinUrl,
			beforeSend:function() {
			// Loader 
			$("#corps").after('<div id="genericPopin"><div id="bgGenericPopin"></div><img id="imgLoader" src="/common/Images/ajax-loader.gif" alt="loading..." /></div>')
			backgroundPopin2();
			},
			complete:function() {
				$("#imgLoader").remove();
			},
			success:function(msg) {
				$("#bgGenericPopin").after(msg);
				closePopinSend();
				checkFormIdentification();
			}
		});
}

//afficher mot de passe oublie
function showPassword() {
    
    var linkForgot = document.getElementById("forgotPassLink");
    if(linkForgot){
     
        linkForgot.getElementsByTagName("a")[0].onclick = function(){
            document.getElementById("blocPassword").style.display = "block";
            return false;
        }
    }
    
    
	$(".forgotPassLink a").click(function() {
		$("#blocPassword").show();	
		return false;
	});
	
}

// Fermeture de popin //
function closePopinSend(){
	$(".bt_close a").click(function() {	
		$("#genericPopin").remove();
		if (typeof document.body.style.maxHeight === "undefined") { 
			$("select").show();
		}
		return false; 
	});
}


// Fermeture de popin de numero d'abonne//
function closePopinMkr(){
	$(".bt_close a").click(function() {	
		$.ajax({
			type:"POST",
			url:"/Authentification/Popin/CleartMkrUserSuggestedAccount.aspx",	
			data:"ObjUrl=1",		
			beforeSend:function() {			
			    void(0);
			},
			complete:function() {				
			    $("#genericPopin").remove();
		        if (typeof document.body.style.maxHeight === "undefined") { 
			        $("select").show();
		        }
			},
			success:function(msg) {	
				
			  
			},
			error:function(){			    
			}
		});
		
		
		
		//clearMkrUserSuggestedAccount();
		return false; 
	});
}

// Fermeture de popin //
function closePopinIdentification(){
    var layerPopinIdentification = $("#genericPopin");
    var popinReduction = $("#reduction");
    var nbReductionImprimer = $("#nbReductionImprimer");
    $("#genericPopin").remove();
        if (typeof document.body.style.maxHeight === "undefined") { 
	        $("select").show();
        }
        if(popinReduction.length && nbReductionImprimer.length<1){          
            window.print();
            LoadBR();
         }
}

function LoadBR()
{
   tant = tant + 1; 
   if (tant < 5 ) {                          
           timerID = setTimeout("LoadBR()",1000);
   } 
   else 
   {
      tant = 0;
      redirectionUrl('/coin_perso/Avantages/ListeBR');
   }
}


// Gestion de l'appui sur la touche entrée
function keyPressInput(inputPress, validInput){
	$(inputPress).keypress(function(e){
		var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
		if(key == 13){
			$(validInput).click();
			return false;
		}
		
	});
}



// Test du formulaire
function checkFormIdentification(){
	
	
	var layerShow = $("#blocPassword");
	if(layerShow.length){
	    showPassword();
	}
	
	var re= /^[a-z\d]+((\.|-|_)[a-z\d]+)*@((?![-\d])[a-z\d-]{0,62}[a-z\d]\.){1,4}[a-z]{2,6}$/gi;
	var re2 =/^[^<">]+$/gi;
	var re3 = /^[0-9]{5}$/gi;
	
	$(".errorMsg").hide();
	
	$("#submitIdentificationforget").click(function(){
	    $(".errorMsg").hide();
		if($("#mailSenderforget").attr("value") == null || $("#mailSenderforget").attr("value").match(re) == null || $("#m_passwordforget").attr("value") == null){
			$(this).parent().next(".errorMsg").show();
		}
		else{
			confirmIdentificationforget();	
		}
		return false;
	});
	keyPressInput("#mailSenderforget","#submitIdentificationforget");
	keyPressInput("#m_passwordforget","#submitIdentificationforget");
	
	
	$("#submitIdentification").click(function(){
	    $(".errorMsg").hide();
	  
		if($("#mailSender").attr("value") == null || $("#mailSender").attr("value").match(re) == null || $("#m_password").attr("value") == null){
			$(this).parent().next(".errorMsg").show();
		}
		else{
			confirmIdentification();	
		}
		return false;
	});
	keyPressInput("#mailSender","#submitIdentification");
	keyPressInput("#m_password","#submitIdentification");
	
	//////////////// la partie pour gérerer l'authetification aprés la saisie de nom , prenom,...
	$("#submitForgotPass").click(function(){
		$(".errorMsg").hide();
		if($("#forgotMail").attr("value") == null || $("#forgotMail").attr("value").match(re) == null){
			$(this).parent().next(".errorMsg").show();
		}
		else{
			confirmForgotPass();	
		}
		return false;
	});
	keyPressInput("#forgotMail","#submitForgotPass");
	
	$("#submitForgotNum").click(function(){
		$(".errorMsg").hide();
		if($("#lastNameNum").attr("value") == null || $("#firstNameNum").attr("value") == null || $("#zipCodeNum").attr("value") == null || $("#birthDateNum1")[0].options[$("#birthDateNum1")[0].selectedIndex].value == "JJ" || $("#birthDateNum2")[0].options[$("#birthDateNum2")[0].selectedIndex].value == "MM" || $("#birthDateNum3")[0].options[$("#birthDateNum3")[0].selectedIndex].value == "AAAA"){
			$(this).parent().next(".errorMsg").show();
		}
		else{
			if($("#zipCodeNum").attr("value").match(re3) == null){
				$(this).parent().next(".errorMsg").show();
			}
			else{
				confirmForgotNum();	
			}
		}
		return false;
	});
	keyPressInput("#lastNameNum","#submitForgotNum");
	keyPressInput("#firstNameNum","#submitForgotNum");
	keyPressInput("#zipCodeNum","#submitForgotNum");
	keyPressInput("#birthDateNum1","#submitForgotNum");
	keyPressInput("#birthDateNum2","#submitForgotNum");
	keyPressInput("#birthDateNum3","#submitForgotNum");
	
	recapLayer();
}

//Gestion des layers de recap
function recapLayer(){
	$("#m_ibnConfirmIdentity").click(function(){
		$("#m_divConfirmName").css("display","none");
		$("#m_divCreateNewPass").css("display","block");
		return false;
	});
	$("#submitNewPass").click(function(){
		if($("#newPass").attr("value") == null || $("#confirmNewPass").attr("value") == null){
			$(this).parent().next(".errorMsg").show();
		}
		else{
		    if($("#newPass").attr("value") != $("#confirmNewPass").attr("value")){
		        $(this).parent().next(".errorMsg").show();
		    }
		    else{
			    confirmChangePasse();
            }
		}
		return false;
   });
}

// Confirmation d'authentification
function confirmIdentification(){

    var mailSender = encodeURIComponent($("#mailSender").attr("value"));
    var passwordSender = encodeURIComponent($("#m_password").attr("value"));
    var urlToRedirect = $("#urlToload").attr("value");   
     
      $.ajax({
			type:"POST",
			url:'/Authentification/Popin/PopinCheckeValidate.aspx',
			data:"mailSender="+ mailSender + "&passwordSender="+ passwordSender,
			beforeSend:function() {
			// Loader 
			$("#popinIdentification").after('<img id="imgLoader" src="/common/Images/ajax-loader.gif" alt="loading..." />')
			},
			complete:function() {
				$("#imgLoader").remove();
			},
			success:function(msg) {
				//$("#popinIdentification").after(msg);
			 
			 if (!(urlToRedirect==="NotSet" || urlToRedirect===undefined))
			    redirectionUrl(urlToRedirect);
		 
			 switch(msg){
				    case "False":
				           errorIdentification();
				           break;
				    case "mon_compte":
				          redirectionUrl("/coin_perso/mon_compte/");  
				           break;  				           				               
				    default :
				            {
				              var layerPopinIdentification = $("#popinIdentification");
				           

			                  if(layerPopinIdentification.length){   
			                        closePopinIdentification();
			                  }
			                  else
			                  {
			                      redirectionUrl(msg);
			                  }  
			                break;
				            }
				}
			}
		});
	
}





// Confirmation d'authentificationforget
function confirmIdentificationforget(){

    var mailSenderforget = encodeURIComponent($("#mailSenderforget").attr("value"));
    var passwordSenderforget = encodeURIComponent($("#m_passwordforget").attr("value"));

      $.ajax({
			type:"POST",
			url:'/Authentification/Popin/PopinCheckeValidate.aspx',
			data:"mailSenderforget="+ mailSenderforget + "&passwordSenderforget="+ passwordSenderforget,
			beforeSend:function() {
			// Loader 
			$("#popinIdentification").after('<img id="imgLoader" src="/common/Images/ajax-loader.gif" alt="loading..." />')
			},
			complete:function() {
				$("#imgLoader").remove();
			},
			success:function(msg) {
				//$("#popinIdentification").after(msg); 
				$("#submitIdentificationforget").hide();
			 switch(msg){
			     
				    
				   case "0":
				          {
				              $(".errorMsg").hide();    
	                          $("#m_authentificationEmail .errorLog").show();
	                          break;
				           }

				    case "1":
				          {
				              $(".errorMsg").hide();    
	                          $("#m_authentificationEmail .sendEmail").show();
			                  break;
				           }
 
				    default :
				            {  
				              $(".errorMsg").hide();    
	                          $("#m_authentificationEmail .Souscription").show();
				               break;
				            }
				}
			}
		});
	
}


// erreur d'authentification
function errorIdentification(){
  
    $(".errorMsg").hide();    
	$("#popinIdentification .errorLog").show();
	$(".club_noLog .errorLog").show();
}

// erreur d'email

function errorEmail()
{
    $(".errorMsg").hide();    
	$("#popinIdentification .errorLog").show();

}
 

///////////////////////////////////// Confirmation de perte de mot de passe

function confirmForgotPass(){
	var forgotMail = encodeURIComponent($("#forgotMail").attr("value"));
  
      $.ajax({
			type:"POST",
			url:'/Authentification/Popin/PopinCheckeValidate.aspx',
			data:"forgotMail="+ forgotMail,
			beforeSend:function() {
			// Loader 
			$("#popinIdentification").after('<img id="imgLoader" src="/common/Images/ajax-loader.gif" alt="loading..." />')
			},
			complete:function() {
				$("#imgLoader").remove();
			},
			success:function(msg) {
				//$("#popinIdentification").after(msg);
				switch(msg){
				    case "1":
				           { 
				                $(".errorMsg").hide();
	                            $("#popinIdentification .sendEmail").show();
				                //closePopinIdentification()
				                break;
				            }
				    case "0":
				            {
				                $(".errorMsg").hide();
	                            $("#popinIdentification .errorLogForgot").show();
	                        }
				            
				            break;
				    case "2":
				           {
				                $(".errorMsg").hide();  
				                $("#popinIdentification .errorLogEmail").show();
	                        }
				            break;    
				}
			}
		});
}



// Confirmation de perte de numero
function confirmForgotNum(){
    var lastname = encodeURIComponent($("#lastNameNum").attr("value"));
    var firstName = encodeURIComponent($("#firstNameNum").attr("value"));
    var zipCode = encodeURIComponent($("#zipCodeNum").attr("value")); 
    var yearBirth= $("#birthDateNum3")[0].options[$("#birthDateNum3")[0].selectedIndex].value; 
    var monthBirth = $("#birthDateNum2")[0].options[$("#birthDateNum2")[0].selectedIndex].value;
    var dayBirth =  $("#birthDateNum1")[0].options[$("#birthDateNum1")[0].selectedIndex].value;
      $.ajax({
			type:"POST",
			url:'/Authentification/Popin/PopinCheckeValidate.aspx',
			data:"lastname="+ lastname + "&firstName="+ firstName + "&zipCode="+ zipCode + "&yearBirth="+ yearBirth +  "&monthBirth="+ monthBirth + "&dayBirth="+ dayBirth,
			beforeSend:function() {
			// Loader 
			$("#popinIdentification").after('<img id="imgLoader" src="/common/Images/ajax-loader.gif" alt="loading..." />')
			},
			complete:function() {
				$("#imgLoader").remove();
			},
			success:function(msg) {
				//$("#popinIdentification").after(msg);
			
				switch(msg){
				    case "Erreur":
				        {
				            $(".errorMsg").hide();
				            $("#popinIdentification .errorLog").show();
	                         break;
				        }
				     case "Multiple_User":
				        {
	   		                $(".errorMsg").hide();
				            $("#popinIdentification .MultipleUser").show();
				            break;
				        }   
				    default :
				        {
				           $("#m_Autenticationfirstname").css("display","none");
		                   $("#m_authentificationEmail").css("display","block");
				           break;
				        }
				}
			}
		});
}

// redirection à une page

function redirectionUrl(url)
{
   document.location.href = url; 
}


// Confirmation changement passes
function confirmChangePasse(){
	
	 var newpass = encodeURIComponent($("#newPass").attr("value"));
     var confirmNewPass = encodeURIComponent($("#confirmNewPass").attr("value"));
 
      $.ajax({
			type:"POST",
			url:'/Authentification/Popin/PopinCheckeValidate.aspx',
			data:"newpass="+ newpass + "&confirmNewPass="+ confirmNewPass,
			beforeSend:function() {
			// Loader 
			$("#popinIdentification").after('<img id="imgLoader" src="/common/Images/ajax-loader.gif" alt="loading..." />')
			},
			complete:function() {
				$("#imgLoader").remove();
			},
			success:function(msg) {
				//$("#popinIdentification").after(msg);
				switch(msg){
				    case "Erreur":
				            errorIdentification();
				            break;
				    default :
				        redirectionUrl(msg)
				        break;
				}
			}
		});
}

// gestion taille background Popin //
function backgroundPopin2(){
	var bodyHeight = $("body").height();
	$("#bgGenericPopin").height(bodyHeight);
	var bodyWidth = $("body").width();
	if(bodyWidth >= 954){
		$("#genericPopin").width(bodyWidth);
		$("#bgGenericPopin").width(bodyWidth);
	}
	else{
		$("#genericPopin").width(954);
		$("#bgGenericPopin").width(954);	
	}
	if (typeof document.body.style.maxHeight === "undefined") { // IE6
		$("select").hide();
		$("#genericPopin select").show();
	}
}

// resize de la popin //
function resizeLayerPopin(){
	var bodyWidth = $("body").width();
	if(bodyWidth >= 954){
		$("#genericPopin").width(bodyWidth);
		$("#bgGenericPopin").width(bodyWidth);
	}
	else{
		$("#genericPopin").width(954);
		$("#bgGenericPopin").width(954);	
	}
}
	   
/*
ne jamais utiliser des addEvents ! date de mathusalem=> utilise document.ready	   
addEvent(window,'load',layerPass,false);
addEvent(window,'load',initPopinForgot,false);
addEvent(window,'load',checkFormIdentification,false);
*/
