/**
Ensemble des fonctions utilisées pour l'identification
*/
var isPopupIdent; // Somme nous sur la popup
var map;
var actionAfterIdentification="";

/**
Fonction qui encode les caractères pour tapestry
*/
/*encodeTapestry=function(chaine){
chaine=chaine.replace("@","$0040");
chaine=chaine.replace(" ","$0020");
return chaine;
}*/

var VALID_T5_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890-_.:".split( "" ); 
encodeTapestry = function (uri) { 
    
    if ( uri == null || uri.length == 0 ) { 
    return "$B"; 
    } 

    var result = ""; 
    for( i = 0; i < uri.length; i++ ) { 
    var c = uri.charAt( i ); 
        if( VALID_T5_CHARS.indexOf( c ) > -1 ) { 
        result += c; 
        } 
        else { 
        result += "$00" + uri.charCodeAt( i ).toString(16); 
        } 
    } 
    
    return result; 
} 

/** 
Fonction qui fait l'identification
paramètre : popup -> O/N : Identification depuis une la popup ou pas ?
*/
valideIdentification=function(popup) {
	isPopupIdent=popup;
	var user;
	var password;
	var memo="N";
	
	if (popup=="N") {
		user=document.getElementById("username").value;
		password=document.getElementById("password").value;
	}
	
	if (popup=="O") {
		user=document.getElementById("username02").value;
		password=document.getElementById("password02").value;
		if (document.getElementById("c1").checked) memo="O";
	}
	
	var error="";
	
	// Verification remplissage des champs
	if (user==""||user=="Email") error="Merci de renseigner votre email !";
	if (password=="")
		if (error=="") error="Merci de renseigner votre mot de passe !";
		
	// Verification format email
	if (user!="") {
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   		if (reg.test(user) == false)
   			if (error=="") error="Mauvais format de l'adresse email !";	
   	}
	
	// Gestion de l'affichage de l'erreur si présente
	if (error!="") {
		if (popup=="N") {
			document.getElementById("errorIdent").innerHTML=error;
			$j("#errorIdent").show();
			return;
		}
		if (popup=="O") {
			document.getElementById("errorPopup").innerHTML=error;
			$j("#errorPopup").show();
			return;
		}
	}
	else {
		if (popup=="N") $j("#errorIdent").hide();
		if (popup=="O") $j("#errorPopup").hide();
	}
	
	if (popup=="O") jQuery(".PopupContent01").css("opacity",0.5);
	
	user=encodeTapestry(user);
	password=encodeTapestry(password);
	
	// Champs OK, on fait un appel ajax pour l'identification
	var myAjax = new Ajax.Request(
		ctx+"action/IdentificationPage/"+escape(user)+"/"+escape(password)+"/"+memo+"/"+popup+getCache(),
		{	method: "get",
			asynchronous: false,
			timeout: 5000,
			onComplete: displayReponseIdentification
		}
	)
	
	var myGlobalHandlers = {
		onLoading: function(){},
		onComplete: function(){},
		onTimeout: function (){ 
								
					}
	};
	Ajax.Responders.register(myGlobalHandlers);
}

displayReponseIdentification = function (request)
{
	// display text
	if (isPopupIdent=="O") jQuery(".PopupContent01").css("opacity",1);
	
	var	resultat=request.responseText.substring(request.responseText.indexOf("</head>")+7,request.responseText.indexOf("</htm"));

	// Gestion de l'erreur
	if (resultat.indexOf("<div")<0) {
		if (isPopupIdent=="N") {
				document.getElementById("errorIdent").innerHTML=resultat;
				$j("#errorIdent").show();
				return;
		}
		if (isPopupIdent=="O") {
				document.getElementById("errorPopup").innerHTML=resultat;
				$j("#errorPopup").show();
				return;
		} 
	}
	
	// Pas d'erreur -> On actualiser le bloc identification en haut si présent

	if (document.getElementById("identification")!=undefined) {
		resultat=resultat.substring(resultat.indexOf("<!-- DEBUT"),resultat.indexOf("<!-- FIN"));
		document.getElementById("identification").innerHTML=resultat;
		if (document.getElementById("errorIdent")!=undefined) $j("#errorIdent").hide();
	}
	
	if (isPopupIdent=="O") closePopup();
	
	if (actionAfterIdentification=="compte") {
		window.location.href=ctx+"profile/WelcomeMyAccount"+getCache();
		actionAfterIdentification="";
		return;
	}
	
	if (actionAfterIdentification=="tunnelStep1" || actionAfterIdentification=="creationOrStep1") {
		window.location.href=ctx+"tunnelachat/panier_step1"+getCache();
		actionAfterIdentification="";
		return;
	}
	
	displayBasketRight(); // On réaffiche le panier à droite
	displayDeliveryRight(); // On réaffiche le bloc de livraison
	recalculListe(); // On actualise la liste
	
	if (actionAfterIdentification=="addArticleToBasket")
	{
		addArticleToBasket(saveReference);
		//saveReference="";
		return;
	}
}

/**
Fonction qui affiche le popup d'identification
*/
displayPopupIdentification=function() {
		if(initPopup())
	{
		var myAjax = new Ajax.Request(
				ctx+"action/DisplayPopupIdentification"+getCache(),
				{	method: "get",
					asynchronous: true,
					timeout: 5000,
					onComplete: displayReponsePopupIdentification
				}
			)
			
			var myGlobalHandlers = {
				onLoading: function(){},
				onComplete: function(){},
				onTimeout: function (){ 
										
							}
			};
			Ajax.Responders.register(myGlobalHandlers);
	}
			else
		{
			closePopup();	
		}
}

displayReponsePopupIdentification = function (request)
{
	// display text
	var	resultat=request.responseText.substring(request.responseText.indexOf("</head>")+7,request.responseText.indexOf("</htm"));
	var myCase = "maCase1";
	if(document.getElementById(myCase) == null)
	{
		myCase="maCase2";
	}
		
	document.getElementById(myCase).innerHTML=resultat;
	//jQuery(".Popup01").hide();
	renderPopup("#"+ myCase +"?clone=false,width=566px,height=auto,type=local");
	if (document.getElementById("popzipcode").value!="") $j("#popziperror").hide();
}




/**
Fonction qui affiche la popup de changement d'email
*/
openChangeEmail=function(action) {
	if(initPopup())
	{
		actionAfterClose=action;
		var myAjax = new Ajax.Request(
				ctx+"action/DisplayFormChangeEmail"+getCache(),
				{	method: "get",
					asynchronous: true,
					timeout: 5000,
					onComplete: displayReponseChangeEmail
				}
			)
			
			var myGlobalHandlers = {
				onLoading: function(){},
				onComplete: function(){},
				onTimeout: function (){ 
										
					}
	};
	Ajax.Responders.register(myGlobalHandlers);
	}
			else
		{
			closePopup();	
		}
}

displayReponseChangeEmail = function (request)
{
	// display text
	var	resultat=request.responseText.substring(request.responseText.indexOf("</head>")+7,request.responseText.indexOf("</htm"));

	if (actionAfterClose!="") closePopup();
	
	document.getElementById("identPopup").innerHTML=resultat;
	//jQuery(".Popup01").hide();
	renderPopup("#maChangementAddresseMail?clone=false,width=566px,height=auto,type=local");
	/*$j("#errorEmail").hide();
	$j("#errorNewEmail").hide();
	$j("#errorEmailConf").hide();
	$j("#errorPassword").hide();*/
}

/**
fonction qui soumet le formulaire de changement d'email
*/
submitChangeEmail=function() {
	var email=document.getElementById("emailCh").value;
	var password=document.getElementById("passwordCh").value;
	var newEmail=document.getElementById("newEmail").value;
	var newEmailConf=document.getElementById("newEmailConf").value;
	
	document.getElementById("errorEmail").innerHTML="";
	document.getElementById("errorPassword").innerHTML="";
	document.getElementById("errorNewEmail").innerHTML="";
	document.getElementById("errorEmailConf").innerHTML="";
	
	var error="N";
	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	if (email=="") {
		document.getElementById("errorEmail").innerHTML="Merci de renseigner votre adresse email";
		$j("#errorEmail").show();
		error="O";
	}
	else {
		if (reg.test(email) == false) {
	   		document.getElementById("errorEmail").innerHTML="Mauvais format de votre adresse email";
			$j("#errorEmail1").show();
			error="O";
   		}
	}
	
	if (password=="") {
		document.getElementById("errorPassword").innerHTML="Merci de renseigner votre mot de passe";
		$j("#errorPassword").show();
		error="O";
	}
	
	if (newEmail=="") {
		document.getElementById("errorNewEmail").innerHTML="Merci de renseigner votre nouvelle adresse email";
		$j("#errorNewEmail").show();
		error="O";
	}
	else {
		if (reg.test(newEmail) == false) {
	   		document.getElementById("errorNewEmail").innerHTML="Mauvais format de votre nouvelle adresse email";
			$j("#errorNewEmail").show();
			error="O";
   		}
	}
	
	if (newEmailConf=="") {
		document.getElementById("errorEmailConf").innerHTML="Merci de renseigner confirmation de la nouvelle adresse email";
		$j("#errorEmailConf").show();
		error="O";
	}
	else {
	if (newEmail!=newEmailConf) {
	   		document.getElementById("errorEmailConf").innerHTML="La confirmation  et votre nouvelle adresse email ne sont pas identique";
			$j("#errorEmailConf").show();
			error="O";
	   	}
	}
	
	if (newEmail==email) {
		document.getElementById("errorNewEmail").innerHTML="Merci de renseigner une nouvelle adresse email";
		$j("#errorNewEmail").show();
		error="O";
	}
	
	if (error=="O") return;
	
	jQuery(".PopupContent03").css("opacity",0.5);
	
   	password=encodeTapestry(password);
   	email=encodeTapestry(email);
   	newEmail=encodeTapestry(newEmail);
   	
   	if(initPopup())
   	{
	   	var myAjax = new Ajax.Request(
			ctx+"action/ChangeEmail/"+email+"/"+newEmail+"/"+password+getCache(),
			{	method: "get",
				asynchronous: true,
				timeout: 5000,
				onComplete: displayReponseSubmitChangeEmail
			}
		)
		
		var myGlobalHandlers = {
			onLoading: function(){},
			onComplete: function(){},
			onTimeout: function (){ 
									
						}
		};
		Ajax.Responders.register(myGlobalHandlers);
   	}
   			else
		{
			closePopup();	
		}
}

displayReponseSubmitChangeEmail = function (request)
{
	// display text
	var	resultat=request.responseText.substring(request.responseText.indexOf("</head>")+7,request.responseText.indexOf("</htm"));
	
	closePopup();
	
	document.getElementById("identPopup").innerHTML=resultat;
//	jQuery(".Popup01").hide();
	renderPopup("#confChangeEmail?clone=false,width=566px,height=auto,type=local");
}



/**
Fonction qui affiche la popup de d'envoi du mot de passe
*/
openSendPassword=function(action) {
	if(initPopup())
	{
		actionAfterClose=action;
		var myAjax = new Ajax.Request(
				ctx+"action/DisplayFormSendPassword"+getCache(),
				{	method: "get",
					asynchronous: true,
					timeout: 5000,
					onComplete: displayReponseSendPassword
				}
			)
			
			var myGlobalHandlers = {
				onLoading: function(){},
				onComplete: function(){},
				onTimeout: function (){ 
										
							}
			};
			Ajax.Responders.register(myGlobalHandlers);
	}
			else
		{
			closePopup();	
		}
}

displayReponseSendPassword = function (request)
{
	// display text
	var	resultat=request.responseText.substring(request.responseText.indexOf("</head>")+7,request.responseText.indexOf("</htm"));

	if (actionAfterClose!="") closePopup();
	
	document.getElementById("identPopup").innerHTML=resultat;
	//jQuery(".Popup01").hide();
	renderPopup("#maSendPassword?clone=false,width=566px,height=auto,type=local");
	$j("#errorPwd").hide();
}



/**
fonction qui soumet le formulaire de d'envoi du mot de passe
*/
submitSendPassword=function() {
	var email=document.getElementById("emailSend").value;
	
	document.getElementById("errorPwd").innerHTML="";
	var error="N";
	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	if (email=="") {
		document.getElementById("errorPwd").innerHTML="Merci de renseigner votre adresse email";
		$j("#errorPwd").show();
		error="O";
	}
	else {
		if (reg.test(email) == false) {
	   		document.getElementById("errorPwd").innerHTML="Mauvais format de votre adresse email";
			$j("#errorPwd").show();
			error="O";
   		}
	}
	
	if (error=="O") return;
	
	jQuery(".PopupContent03").css("opacity",0.5);
	
   	email=encodeTapestry(email);
    closePopup();
   	if(initPopup())
   	{
	   	var myAjax = new Ajax.Request(
			ctx+"action/SendPassword/"+email+getCache(),
			{	method: "get",
				asynchronous: true,
				timeout: 5000,
				onComplete: displayReponseSubmitSendPassword
			}
		)
		
		var myGlobalHandlers = {
			onLoading: function(){},
			onComplete: function(){},
			onTimeout: function (){ 
									
						}
	};
	Ajax.Responders.register(myGlobalHandlers);
   	}
   			else
		{
			closePopup();	
		}
}

displayReponseSubmitSendPassword = function (request)
{
	// display text
	var	resultat=request.responseText.substring(request.responseText.indexOf("</head>")+7,request.responseText.indexOf("</htm"));	
	document.getElementById("identPopup").innerHTML=resultat;
//	jQuery(".Popup01").hide();
	renderPopup("#confSendPassword?clone=false,width=566px,height=auto,type=local");
}

/**
Methode qui fait le changement de mode de livraison
*/
var newMode;
changeDeliveryMode=function(mode,popup) {
	newMode=mode;
	isPopupIdent=popup;
	
	var myAjax = new Ajax.Request(
		ctx+"action/ChangeDeliveryMode/"+mode+getCache(),
		{	method: "get",
			asynchronous: true,
			timeout: 5000,
			onComplete: displayReponseChangeDeliveryMode
		}
	)
	
	var myGlobalHandlers = {
		onLoading: function(){},
		onComplete: function(){},
		onTimeout: function (){ 
								
					}
	};
	Ajax.Responders.register(myGlobalHandlers);
}

displayReponseChangeDeliveryMode = function (request)
{
	if (isPopupIdent=="O") {
		closePopup();
		if (newMode==0) displayPopupSlots();
		if (newMode==1) displayChoiceStore('N');
	}
	// display text
	var	resultat=request.responseText.substring(request.responseText.indexOf("</head>")+7,request.responseText.indexOf("</htm"));
	var resultat2=resultat.substring(resultat.indexOf("<!-- DEBUT"),resultat.indexOf("<!-- FIN"));
	document.getElementById("delivery").innerHTML=resultat2;
	displayBasketRight();
}

/**
Méthode qui recherche les magasins suivant le code postal
*/
getStoreByZipCode=function(popup) {
	isPopupIdent=popup;
	var reg = /^([0-9]{5})$/;

	var zipcode;
	if (popup=="O") {
		zipcode=document.getElementById("popzipcode").value;
		if (zipcode=="") {
			$j("#popziperror").show();
			return;
		}
		if (reg.test(zipcode) == false) {
			document.getElementById("popziperror").innerHTML="Mauvais format du code postal";
			$j("#popziperror").show();
			return;
		}
	}
	
	if (popup=="N") {
		zipcode=document.getElementById("zipcode").value;
		if (zipcode=="") {
			$j("#ziperror").show();
			return;
		}
		if (reg.test(zipcode) == false) {
			document.getElementById("ziperror").innerHTML="Mauvais format du code postal";
			$j("#ziperror").show();
			return;
		}
		$j("#ziperror").hide();
	}
	
	var myAjax = new Ajax.Request(
		ctx+"action/GetStoreByCodePostal/"+zipcode+getCache(),
		{	method: "get",
			asynchronous: true,
			timeout: 5000,
			onComplete: displayReponseGetStoreByZipCode
		}
	)
	
	var myGlobalHandlers = {
		onLoading: function(){},
		onComplete: function(){},
		onTimeout: function (){ 
								
					}
	};
	Ajax.Responders.register(myGlobalHandlers);
}

displayReponseGetStoreByZipCode = function (request)
{
	// display text
	if (request.responseText.indexOf("idpourrepererlapagebemaxindex")>0) {
		window.location.href=ctx;
		return;
	}
	if (isPopupIdent=="N") 
	{
		displayPopupIdentification();
	}
	else {
		closePopup();
		displayPopupIdentification();
	}
	recalculListe();
}



/**
Fonction qui renseigne le mode de livraison choisi par l'utilisateur
*/
var mode;
setDeliveryMode=function() {
	if (document.getElementById("r1").checked==false&&document.getElementById("r2").checked==false) {
		document.getElementById("storerror").innerHTML="Merci de choisir un mode de livraison";
		$j("#storerror").show();
		return;
	}
	
	mode="0";
	if (document.getElementById("r1").checked) mode="0";
	if (document.getElementById("r2").checked) mode="1";
	

	// On a choisi le mode livraison à domicile
	// On met a jour en session les données car on a toutes les infos : mode de livraison + magasin
	var myAjax = new Ajax.Request(
		ctx+"action/SetDeliveryMode/"+mode+getCache(),
		{	method: "get",
			asynchronous: true,
			timeout: 5000,
			onComplete: displayReponseSetDeliveryMode
		}
	)
		
	var myGlobalHandlers = {
		onLoading: function(){},
		onComplete: function(){},
		onTimeout: function (){}
	};
	Ajax.Responders.register(myGlobalHandlers);

}

//Choix du mode livraison
chooseDeliveryMode=function() {
mode="0";
var myAjax = new Ajax.Request(
	ctx+"action/SetDeliveryMode/0"+getCache(),
	{	method: "get",
		asynchronous: true,
		timeout: 5000,
		onComplete: displayReponseSetDeliveryMode
	}
)
	
var myGlobalHandlers = {
	onLoading: function(){},
	onComplete: function(){},
	onTimeout: function (){}
};
Ajax.Responders.register(myGlobalHandlers);
}	

displayReponseSetDeliveryMode = function (request)
{
	if (request.responseText.indexOf("idpourrepererlapagebemaxindex")>0) {
		window.location.href=ctx;
		return;
	}
	displayDeliveryRight();
	closePopup();
	actionAfterClose="";
	
	
	if (mode=="1") {
		// On a choisi le mode retrait en magasin
		// On affiche la fenetre du choix du magasin
		closePopup();
		displayChoiceStore("O");
		recalculListe();
		return;
	}
	
	//Si on vient de choisir son mode de livraison suite à un clic sur "j'achète"
	if(actionAfterIdentification=="addArticleToBasket")
	{
		actionAfterIdentification="";
		addArticleToBasket(saveReference);
		return;
	}
	// display text
	if (actionAfterIdentification=="creationCompte" || actionAfterIdentification=="creationOrStep1") {
		window.location.href=ctx+"profile/CreateProfile"+getCache();
		actionAfterIdentification="";
		return;
	}
	
	if (mode=="0") displayPopupSlots();
	recalculListe();
	// On affiche la popup du choix de la date et heure de livraison

}



/**
Methode qui affiche la page du choix du magasin
*/
displayChoiceStore=function(popup)
{
	if(initPopup())
	{
		isPopup=popup;
		if (popup=="O") actionAfterClose="Identification";
		 
		var myAjax = new Ajax.Request(
			ctx+"action/DisplayChoiceStore"+getCache(),
			{	method: "get",
				asynchronous: true,
				timeout: 5000,
				onComplete: displayReponseChoiceStore
			}
		)
			
		var myGlobalHandlers = {
			onLoading: function(){},
			onComplete: function(){},
			onTimeout: function (){}
		};
		Ajax.Responders.register(myGlobalHandlers);
	}
			else
		{
			closePopup();	
		}
}

displayReponseChoiceStore = function (request)
{
	// display text
	if (request.responseText.indexOf("idpourrepererlapagebemaxindex")>0) {
		window.location.href=ctx;
		return;
	}
	var	resultat=request.responseText.substring(request.responseText.indexOf("</head>")+7,request.responseText.indexOf("</htm"));

	//if (isPopup=="O") closePopup();
	document.getElementById("identPopup").innerHTML=resultat;
	//jQuery(".Popup01").hide();
	renderPopup("#maRetrait?clone=false,width=566px,height=auto,type=local");
	jQuery(".PlaceList").jScrollPane();
	$j("#storerror").hide();
	map=undefined;
	showTheMap();
}	





/**
Fonction de validation du choix du magasin pour le mode retriat en magasin
*/
setStore=function() {
	var list=document.getElementsByName("group1");
	var store="";
	var i;
	for (i=0;i<list.length;i++) {
		if (list[i].checked) store=list[i].id;
	}
		
	if (store=="") {
		document.getElementById("storerror").innerHTML="Merci de choisir un magasin de retrait";
		$j("#storerror").show();
		
		return;
	}
	var myAjax = new Ajax.Request(
		ctx+"action/SetStore/"+store+getCache(),
		{	method: "get",
			asynchronous: true,
			timeout: 5000,
			onComplete: displayReponseSetStore
		}
	)
		
	var myGlobalHandlers = {
		onLoading: function(){},
		onComplete: function(){},
		onTimeout: function (){}
	};
	Ajax.Responders.register(myGlobalHandlers);
}

displayReponseSetStore = function (request)
{
	// display text
	if (request.responseText.indexOf("idpourrepererlapagebemaxindex")>0) {
		window.location.href=ctx;
		return;
	}
	
	//Si on vient de choisir son mode de livraison suite à un clic sur "j'achète"
	if(actionAfterIdentification=="addArticleToBasket")
	{
		actionAfterIdentification="";
		addArticleToBasket(saveReference);
		return;
	}
	
	if (actionAfterIdentification=="creationCompte" || actionAfterIdentification=="creationOrStep1") {
		window.location.href=ctx+"profile/CreateProfile"+getCache();
		actionAfterIdentification="";
		return;
	}
	closePopup();
	displayDeliveryRight();
	displayBasketRight();
	displayPopupSlots();
	recalculListe();
}	


/**
Methode qui enregistre l'email pour etre averti de l'ouverture d'un magasin dans une commune
*/
setInfoEmail=function() {
	var email=document.getElementById("emailalerte").value;
		$j("#nocpok").hide();
	
	if (email==""||email=="Email") {
		$j("#noemail").show();
		return;
	}
	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   	if (reg.test(email) == false) {
		$j("#noemail").hide();
		$j("#bademail").show();
		return;
   	}
   	
   	$j("#noemail").hide();
	$j("#bademail").hide();
	
	email=encodeTapestry(email);
	
	var myAjax = new Ajax.Request(
		ctx+"action/SetEmailWarning/"+email+getCache(),
		{	method: "get",
			asynchronous: true,
			timeout: 5000,
			onComplete: displayReponseSetInfoEmail
		}
	)
		
	var myGlobalHandlers = {
		onLoading: function(){},
		onComplete: function(){},
		onTimeout: function (){}
	};
	Ajax.Responders.register(myGlobalHandlers);
}

displayReponseSetInfoEmail = function (request)
{
	if (request.responseText.indexOf("idpourrepererlapagebemaxindex")>0) {
		window.location.href=ctx;
		return;
	}
	$j("#nocpok").show();
}	



/**
Fonction qui affiche la popup du choix de creneau de retrait ou de livraison
*/
displayPopupSlots=function() {
	if(initPopup())
	{
		actionAfterClose="";
		var myAjax = new Ajax.Request(
			ctx+"action/DisplaySlots"+getCache(),
			{	method: "get",
				asynchronous: true,
				timeout: 5000,
				onComplete: displayReponsePopupSlots
			}
		)
		
		var myGlobalHandlers = {
			onLoading: function(){},
			onComplete: function(){},
			onTimeout: function (){ 
									
						}
		};
		Ajax.Responders.register(myGlobalHandlers);
	}
			else
		{
			closePopup();	
		}
}

displayReponsePopupSlots = function (request)
{
	// display text
	if (request.responseText.indexOf("idpourrepererlapagebemaxindex")>0) {
		window.location.href=ctx;
		return;
	}
	var	resultat=request.responseText.substring(request.responseText.indexOf("</head>")+7,request.responseText.indexOf("</htm"));

	document.getElementById("identPopup").innerHTML=resultat;
//	jQuery(".Popup01").hide();
	renderPopup("#maCreneau?clone=false,width=566px,height=auto,type=local");
}

changePopupSlots=function(order) {
	if(initPopup()){
		actionAfterClose="";
		var myAjax = new Ajax.Request(
			ctx+"action/DisplaySlots/"+order+getCache(),
			{	method: "get",
				asynchronous: true,
				timeout: 5000,
				onComplete: changeReponsePopupSlots
			}
		)
		var myGlobalHandlers = {
			onLoading: function(){},
			onComplete: function(){},
			onTimeout: function (){ 
			}
		};
		Ajax.Responders.register(myGlobalHandlers);
	}
	else
	{
		closePopup();	
	}
}

changeReponsePopupSlots = function (request)
{
	// display text
	if (request.responseText.indexOf("idpourrepererlapagebemaxindex")>0) {
		window.location.href=ctx;
		return;
	}
	var	resultat=request.responseText.substring(request.responseText.indexOf("</head>")+7,request.responseText.indexOf("</htm"));
	document.getElementById("identPopup").innerHTML=resultat;
//	jQuery(".Popup01").hide();
	renderPopup("#maCreneau?clone=false,width=566px,height=auto,type=local");
}

/**
Fonction qui valide le choix du créneau de livraison
*/
valideCreneau=function(popup) {
	
	if (popup=="S") {
		window.location.href=ctx+"tunnelAchat/Payment"+getCache();
		return;
	}
	isPopupIdent=popup;
	var list=document.getElementsByName("radioButtonSlotName");
	var id="";
	var i=0;

	for (i=0;i<list.length;i++)
		if (list[i].checked) id=list[i].value;
		
	if (id=="") {
		if (popup=="O") {
			$j("#errorcreneau").show();
			return;
		} else {
			renderPopup("#errorLiv?clone=false,width=566px,height=auto,type=local");
			return;
		}
	}
	var myAjax = new Ajax.Request(
		ctx+"action/CreateSlots/"+id+getCache(),
		{	method: "get",
			asynchronous: true,
			timeout: 5000,
			onComplete: displayReponseValideCreneau
		}
	)
	
	var myGlobalHandlers = {
		onLoading: function(){},
		onComplete: function(){},
		onTimeout: function (){ 
								
					}
	};
	Ajax.Responders.register(myGlobalHandlers);
}

displayReponseValideCreneau = function (request)
{
	if (request.responseText.indexOf("idpourrepererlapagebemaxindex")>0) {
		window.location.href=ctx;
		return;
	}
	if (isPopupIdent=="O") {
		closePopup();
		displayDeliveryRight();
		if(actionAfterSlot=="addArticleToBasket")
		{
			actionAfterSlot="";
			addArticleToBasket(saveReference);
		}
	} else window.location.href=ctx+"tunnelAchat/Delivery"+getCache();
}

/**
Fonction qui valide le choix d'un autre créneau de livraison
*/
valideMajCreneau=function(popup,order) {
	
	isPopupIdent=popup;
	var list=document.getElementsByName("radioButtonSlotName");
	var id="";
	var i=0;

	for (i=0;i<list.length;i++)
		if (list[i].checked) id=list[i].value;
		
	if (id=="") {
		if (popup=="O") {
			$j("#errorcreneau").show();
			return;
		} else {
			closePopup();			
			initPopup();
			renderPopup("#errorLiv?clone=false,width=566px,height=auto,type=local");
			return;
		}
	}
	var myAjax = new Ajax.Request(
		ctx+"action/CreateSlots/"+id+"/"+order+getCache(),
		{	method: "get",
			asynchronous: true,
			timeout: 5000,
			onComplete: displayReponseValideMajCreneau
		}
	)

	var myGlobalHandlers = {
		onLoading: function(){},
		onComplete: function(){},
		onTimeout: function (){ 
								
					}
	};
	Ajax.Responders.register(myGlobalHandlers);
}

displayReponseValideMajCreneau = function (request)
{
	if (request.responseText.indexOf("idpourrepererlapagebemaxindex")>0) {
		window.location.href=ctx;
		return;
	}
	//window.location.href=ctx+"profile/welcomemyaccount"+getCache();
	location.reload();
}

/**
Fonction qui soumet le changement d'adresse de livraison
*/
submitChangeAdrliv=function() {
	if (document.getElementById("Textbox08").checked==true) document.getElementsByName("interphone")[0].value="O";
	else document.getElementsByName("interphone")[0].value="N";
	
	if (document.getElementById("Textbox10").checked==true) document.getElementsByName("ascenceur")[0].value="O";
	else document.getElementsByName("ascenceur")[0].value="N";
	
	if (document.getElementById("Option12").checked==true) document.getElementsByName("porte")[0].value="Droite";
	if (document.getElementById("Option13").checked==true) document.getElementsByName("porte")[0].value="Gauche";
	if (document.getElementById("Option14").checked==true) document.getElementsByName("porte")[0].value="En face";

	
	document.getElementsByName("precision")[0].value=document.getElementById("Textbox16").value;

		
	document.getElementById("adrLiv").submit();
}


/**
Fonction qui affiche le popup d'identification
*/
displayPopupError=function(code,retour) {
	if(initPopup())
	{
		
		if (retour!="") actionAfterClose=retour;
		var myAjax = new Ajax.Request(
				ctx+"action/DisplayError/"+code+getCache(),
				{	method: "get",
					asynchronous: true,
					timeout: 5000,
					onComplete: displayReponsePopupError
				}
			)
			
			var myGlobalHandlers = {
				onLoading: function(){},
				onComplete: function(){},
				onTimeout: function (){ 
										
							}
			};
			Ajax.Responders.register(myGlobalHandlers);
	}
			else
		{
			closePopup();	
		}
}

displayReponsePopupError = function (request)
{
	// display text
	/*if (request.responseText.indexOf("idpourrepererlapagebemaxindex")>0) {
		window.location.href=ctx;
		return;
	}*/
	var	resultat=request.responseText.substring(request.responseText.indexOf("</head>")+7,request.responseText.indexOf("</htm"));

	document.getElementById("identPopup").innerHTML=resultat;
	
	closePopup();
	if (initPopup()) renderPopup("#identPopup?clone=false,width=566px,height=auto,type=local");
}

/**
Fonction qui fait l'identification avant l'accès au compte
*/
goToMyCompte=function() {
	actionAfterIdentification="compte";
	displayPopupIdentification();
}

/**
Fonction qui affiche le choix du code postal avant la création du compte
*/
goToCreationCompte=function() {
	actionAfterIdentification="creationCompte";
	displayPopupIdentification();
}

goToStep1=function()
{
	actionAfterIdentification="tunnelStep1";
	displayPopupIdentification();
}

goToCreationOrStep1=function()
{
	actionAfterIdentification="creationOrStep1";
	displayPopupIdentification();
}

var geocoder = new GClientGeocoder();
	

		



