//JavaScript Document for Quelle
//Version : 0.1
//Authors : Herve FRACKOWIAK / Maxime RICHARD


/* Fonction de redirection selon choix fait dans la lightbox lb4 */
function choix_catalogue()
{	if (document.getElementById('radionon').checked==true)
{ 
	lightbox('lb7'); 
}
else if (document.getElementById('radiooui').checked==true)
{ lightbox('lb5'); }
}

/* Fonction de redirection selon saisie faite dans la lightbox lb5 */
function saisie_numclient()
{
	var num_client = document.getElementById('num_client_v');
	var nom_client = document.getElementById('nom_client_v');
	var erreur = 0;


	if(verfi_vide(nom_client.value)){
		alert('Veuillez saisir votre nom');
		document.getElementById('nom_client_v').focus();
	}else{
		if(verfi_vide(num_client.value)){
			alert('Veuillez saisir votre numero de client');
			document.getElementById('num_client_v').focus();
		}else{
			if(!verif_numerique(num_client.value)){
				alert('Le code client doit etre numérique');
				document.getElementById('num_client_v').focus();
			}else{
				Ajax_Call("/VAD/VerificationCompte?nom="+nom_client.value+"&code="+num_client.value);
			}
		}
	}
}

/* Fonction de redirection selon saisie faite dans la lightbox lb5 */
function saisie_numclient2()
{
	var num_client = document.getElementById('num_client_v2');
	var nom_client = document.getElementById('nom_client_v2');
	var erreur = 0;
	if(verfi_vide(nom_client.value)){
		alert('Veuillez saisir votre nom');
		document.getElementById('nom_client_v').focus();
	}else{
		if(verfi_vide(num_client.value)){
			alert('Veuillez saisir votre numero de client');
			document.getElementById('num_client_v').focus();
		}else{
			if(!verif_numerique(num_client.value)){
				alert('Le code client doit etre numérique');
				document.getElementById('num_client_v').focus();
			}else{
				Ajax_Call("/VAD/VerificationCompte?nom="+nom_client.value+"&code="+num_client.value);
			}
		}
	}
}
/* Fonction de r�cup�ration du param�tre 'param' en URL */
function getParamValue(param)
{
	/*
    var u = url == undefined ? document.location.href : url;
    var reg = new RegExp('(\\?|&|^)'+param+'=(.*?)(&|$)');
    var matches = u.match(reg);
    return matches[2] != undefined ? decodeURIComponent(matches[2]).replace(/\+/g,' ') : ''; 
	 */
	param = param.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+param+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];

}

/* Fonctions appel�es au chargement de la page */
$(document).ready(function() {
	$(".plus").toggle(function(){
		$(this).removeClass("plus").addClass("moins");
		$("#" + $(this).attr("req")).slideDown('slow');
	},function(){
		$(this).removeClass("moins").addClass("plus");
		$("#" + $(this).attr("req")).slideUp('slow');
	});

	if (getParamValue("page")=="accueil")
	{

		$("#contenu_espace1").hide();
		$("#contenu_espace2").hide();
		$("#contenu_espace3").hide();
		$("#contenu_espace4").hide();
		$("#contenu_espace5").hide();
		$("#contenu_espace0").show(); 

		if (getParamValue("item")!='')
		{ 	$("#contenu_espace0").hide(); 
		$("#contenu_espace" + getParamValue("item")).show();
		$("#bouton_plus" + getParamValue("item")).removeClass("bouton_plus").addClass("bouton_moins");
		}
	}
	if (getParamValue("item")!='')
	{	
		// modif
		$("#contenu_espace0").hide(); 
		$("#contenu_espace" + getParamValue("item")).show();
		//
		$("#bouton_plus" + getParamValue("item")).removeClass("bouton_plus").addClass("bouton_moins");
	}
});

/* Fonction : cr�ation LightBox */
function lightbox (id_lb)
{
	
	//V�rification de l'existence d'une lightbox.
	if($("div.modale").length==0 && $("div.modalev1").length==0 && $("div.modalebig").length==0)
	{	
		//alert("premi�re modale");
		//Insertion de la blackbox
		$("body").prepend('<div class="black_box" style="filter: alpha(opacity=40);"></div>');
		// Insertion du r�ceptacle
		$("body").prepend('<div class="receptacle"></div>');

		//Disparition de la LB
		//kill_lightbox($("div.modale").attr("id"));
		//$("#" + $("div.modale").attr("id")).fadeOut("slow");
	}

	//Appel Ajax
	var param = {"idBox": id_lb};
	if(arguments.length == 2){
		param = $.extend(param, {"arg": arguments[1]});
	}
	
	
	//ajax vers /VAD/Lightbox suivi des params
	
	//append dans receptacle
	var url = '/VAD/Lightbox';
		
		
	 $.ajax({
		   type: "GET",
		   cache:true,
		   url: url,
		   data: param,
		   success: function(msg){
		 		
		 		$(".receptacle").empty();
		 		$(".receptacle").append(msg);
		 	
		 		//Centre le contenu
				centre_lightbox($("#"+id_lb));

				//Hack Dropdownliste IE6
				$(".black_box").bgiframe();

				//Apparition
				$('.black_box')
				.css({width: $(window).width(), height: $(document).height()})
				.fadeIn("slow",function(){$("#"+id_lb).fadeIn("slow");});
		   }
		 });
	
	
	
	//$(".receptacle").load("/VAD/Lightbox", param, function(){
		//Centre le contenu
	//	centre_lightbox($("#"+id_lb));

		//Hack Dropdownliste IE6
	//	$(".black_box").bgiframe();

		//Apparition
	//	$('.black_box')
	//	.css({width: $(window).width(), height: $(document).height()})
	//	.fadeIn("slow",function(){$("#"+id_lb).fadeIn("slow");});
	//});
}

/* Fonction : cr�ation LightBox */
function lightboxbis (id_lb, functionafter)
{
	
	//V�rification de l'existence d'une lightbox.
	if($("div.modale").length==0 && $("div.modalev1").length==0 && $("div.modalebig").length==0)
	{	
		//alert("premi�re modale");
		//Insertion de la blackbox
		$("body").prepend('<div class="black_box" style="filter: alpha(opacity=40);"></div>');
		// Insertion du r�ceptacle
		$("body").prepend('<div class="receptacle"></div>');

		//Disparition de la LB
		//kill_lightbox($("div.modale").attr("id"));
		//$("#" + $("div.modale").attr("id")).fadeOut("slow");
	}

	//Appel Ajax
	var param = {"idBox": id_lb};
	if(arguments.length == 2){
		param = $.extend(param, {"arg": arguments[1]});
	}

	
	//append dans receptacle
	var url = '/VAD/Lightbox';
		
		
	 $.ajax({
		   type: "GET",
		   cache:true,
		   url: url,
		   data: param,
		   success: function(msg){
		 		
		 		$(".receptacle").empty();
		 		$(".receptacle").append(msg);
		 	
		 		//Centre le contenu
				
				centre_lightbox($("#"+id_lb));

				//Hack Dropdownliste IE6
				$(".black_box").bgiframe();

				//Apparition
				$('.black_box')
				.css({width: $(window).width(), height: $(document).height()})
				.fadeIn("slow",function(){$("#"+id_lb).fadeIn("slow");});
				
				eval(functionafter); 
		   }
		 });
	
	
//	$(".receptacle").load("/VAD/Lightbox", param, function(){
//		//Centre le contenu
//		
//		centre_lightbox($("#"+id_lb));
//
//		//Hack Dropdownliste IE6
//		$(".black_box").bgiframe();
//
//		//Apparition
//		$('.black_box')
//		.css({width: $(window).width(), height: $(document).height()})
//		.fadeIn("slow",function(){$("#"+id_lb).fadeIn("slow");});
//		
//		eval(functionafter); 
//		
//	});
}





/* /Fonction */
function lightboxError(msg){
	lightbox('lb99', msg);
}
/* Fonction : cr�ation LightBox avec paramètre */
function lightboxWithParam(id_lb, arg) {
	//alert('test3');
	if(arg!=null && arg!="") {
		//V�rification de l'existence d'une lightbox.
		if($("div.modale").length==0 && $("div.modalev1").length==0 && $("div.modalebig").length==0)
		{	
			//alert("première modale");
			//Insertion de la blackbox
			$("body").prepend('<div class="black_box" style="filter: alpha(opacity=40);"></div>');
			// Insertion du r�ceptacle
			$("body").prepend('<div class="receptacle"></div>');
		}

		//Appel Ajax
		//append dans receptacle
		//Appel Ajax
		var param = {"idBox": id_lb};
		if(arguments.length == 2){
			param = $.extend(param, {"arg": arguments[1]});
		}
		
		var url = '/VAD/Lightbox';
			
			
		 $.ajax({
			   type: "GET",
			   cache:true,
			   url: url,
			   data: param,
			   success: function(msg){
			 		
			 		$(".receptacle").empty();
			 		$(".receptacle").append(msg);
			 	
			 		//Centre le contenu
					centre_lightbox($("#"+id_lb));

					//Hack Dropdownliste IE6
					$(".black_box").bgiframe();

					//Apparition
					$('.black_box')
					.css({width: $(window).width(), height: $(document).height()})
					.fadeIn("slow",function(){$("#"+id_lb).fadeIn("slow");});
			   }
			 });
		
		
		
//		
//		$(".receptacle").load("/VAD/Lightbox?idBox="+id_lb+"&arg="+arg, function(){
//			//Centre le contenu
//			centre_lightbox($("#"+id_lb));
//
//			//Hack Dropdownliste IE6
//			$(".black_box").bgiframe();
//
//			//Apparition
//			$('.black_box')
//			.css({width: $(window).width(), height: $(document).height()})
//			.fadeIn("slow",function(){$("#"+id_lb).fadeIn("slow");});
//
//		});
	} else {
		alert("email invalide");
	}
}
/* /Fonction */

/* Fonction : affichage du panneau livraison (1er niveau) */
function show_livraison(id_mode)
{
	$("#lieu1").slideUp('fast');
	$("#lieu2").slideUp('fast'); 
	$("#lieu3").slideUp('fast'); 
	$("#choix1").slideUp('fast'); 
	$("#choix2").slideUp('fast'); 
	$("#lieu"+id_mode).slideDown('fast');
	
	if(id_mode==1){
		
	}
	
	
	
}
/* /Fonction */

/* Fonction : affichage du panneau livraison (2ème niveau) */
function show_choixlivraison(id_choix)
{
	$("#choix1").slideUp('fast'); 
	$("#choix2").slideUp('fast'); 
	$("#choix"+id_choix).slideDown('fast');
	
	
	
}
/* /Fonction */

/* Fonction : destruction de LightBox */
function kill_lightbox (id_lb)
{
	$('#'+id_lb).fadeOut("slow",function(){
		$(".black_box").fadeOut("slow",
				function(){
			$('.receptacle, .black_box').remove();
		})
	});
}
/* /Fonction */


/* Fonction : centrage de LightBox */
function centre_lightbox (el)
{
	var top = $(window).scrollTop()+($(window).height()/2)-(el.height()/2);
	var left = ($(window).width()/2)-(el.width()/2);
	el.css({top: top +"px", left: left +"px"});	
}
/* /Fonction */


/* Fonction : switch de menuitem (item, bouton) */
function toggle_item (it,bt)
{

	if ($("#" + bt).hasClass("bouton_plus")) {
		
		$(".contenu_espace:visible").slideUp('slow');
		$(".bouton_moins").removeClass("bouton_moins").addClass("bouton_plus");
		$("#" + bt).removeClass("bouton_plus").addClass("bouton_moins");
		$("#" + it).slideDown('slow');
		
	}else{
		$(".contenu_espace:visible").slideUp('slow');
		$("#contenu_espace0").slideDown('slow');
		$("#" + bt).removeClass("bouton_moins").addClass("bouton_plus");
		
	}
	
}
/* /Fonction */

/* Fonction : affichage des messages d'info dans la lightbox lb7 (création de compte) */
function toggle_info(inf)
{
	$("#infos0").slideUp('slow');
	$("#infos1").slideUp('slow');
	$("#infos2").slideUp('slow');
	$("#infos3").slideUp('slow');
	$("#infos4").slideUp('slow');
	$("#infos" + inf).slideDown('slow');
}
/* /Fonction */

/* MAJ */
var Box = {
	el: null,		// box actuel
	// Config par defaut
	init:{
		idBox: null,	// id de la lightbox
		arg:  null,		// parametre envoyé a la box
		show: null, 	// fonction a executer avant affichage
		close: null, 	// fonction a executer a la fermeture de box
		top: 0,			// top css de la box
		left: 0,		// left css de la box
		url: "/VAD/Lightbox",		// Url de chargement du contenu
		urldata: null,	// Paramètre de Url du contenu
		killTime: null
	},


	open: function(p){
		//Verification d'une occurence / Si oui suppression
		if(this.el != null && this.el.length != 0){Box.kill(p); return false;}
		$.extend(this, this.init, p);
	
		// Raccourci clavier (Echap pour quitter)
		$(document).one("keydown", function (e) {
			if(e.which == 27) {Box.kill(); return false;}
		});
	
		//	Insertion de la blackbox
		if($(".black_box").length == 0){
			$('<div class="black_box" style="filter: alpha(opacity=40);"></div>').prependTo("body")
			.css({width: $(window).width(), height: $(document).height()});
		}
		//	Insertion du réceptacle
		$("body").prepend('<div class="receptacle"></div>');
		
		
		var param = {"idBox": Box.idBox};
		if(Box.arg != null){
			param = $.extend(param, {"arg": Box.arg});
		}
		
		
		 $.ajax({
			   type: "GET",
			   cache:false,
			   url: Box.url,
			   data: param,
			   success: function(msg){
			 		
			 		$(".receptacle").empty();
			 		$(".receptacle").append(msg);
			 	
			 		Box.el = $("#"+ Box.idBox);
			 		
					// Positionne la box au centre si non top, ou left non definit
					if(Box.top == 0 || Box.left == 0) {
						Box.top = $(window).scrollTop() + ($(window).height()/2) - (Box.el.height()/2);
						Box.left = ($(window).width()/2) - (Box.el.width()/2);
					}
					Box.el.css({top: Box.top +"px", left: Box.left +"px"});
			
					Box.el.bgiframe();
			
					// Definit le click de fermeture
					$(".close", Box.el).click(function(){Box.kill(); return false;});
			
					//Apparition de la blackbox
					$(".black_box").fadeIn("slow", function(){
						Box.el.fadeIn("slow", function(){
							if(Box.show){Box.show();}
							//Box.killTime = setTimeout("Box.kill()",5000);
						});
					});
			   }
			 });
		
		
//		$(".receptacle").load(Box.url, param, function(){
//			Box.el = $("#"+ Box.idBox);
//	
//			// Positionne la box au centre si non top, ou left non definit
//			if(Box.top == 0 || Box.left == 0) {
//				Box.top = $(window).scrollTop() + ($(window).height()/2) - (Box.el.height()/2);
//				Box.left = ($(window).width()/2) - (Box.el.width()/2);
//			}
//			Box.el.css({top: Box.top +"px", left: Box.left +"px"});
//	
//			Box.el.bgiframe();
//	
//			// Definit le click de fermeture
//			$(".close", Box.el).click(function(){Box.kill(); return false;});
//	
//			//Apparition de la blackbox
//			$(".black_box").fadeIn("slow", function(){
//				Box.el.fadeIn("slow", function(){
//					if(Box.show){Box.show();}
//					//Box.killTime = setTimeout("Box.kill()",5000);
//				});
//			});
//		});
	},
	openParam: function(p){
		//Verification d'une occurence / Si oui suppression
		if(this.el != null && this.el.length != 0){Box.kill(p); return false;}
		$.extend(this, this.init, p);
	
		// Raccourci clavier (Echap pour quitter)
		$(document).one("keydown", function (e) {
			if(e.which == 27) {Box.kill(); return false;}
		});
	
		//	Insertion de la blackbox
		if($(".black_box").length == 0){
			$('<div class="black_box" style="filter: alpha(opacity=40);"></div>').prependTo("body")
			.css({width: $(window).width(), height: $(document).height()});
		}
		//	Insertion du réceptacle
		$("body").prepend('<div class="receptacle"></div>');
		
		
		var param = {"idBox": Box.idBox};
		
		if(Box.arg != null){
			param = $.extend(param, {"arg": Box.arg});
		}
		
		
		 $.ajax({
			   type: "GET",
			   cache:true,
			   url: Box.url,
			   data: Box.urldata,
			   success: function(msg){
			 		
			 		$(".receptacle").empty();
			 		$(".receptacle").append(msg);
			 	
			 		Box.el = $("#"+ Box.idBox);
			 		
					// Positionne la box au centre si non top, ou left non definit
					if(Box.top == 0 || Box.left == 0) {
						Box.top = $(window).scrollTop() + ($(window).height()/2) - (Box.el.height()/2);
						Box.left = ($(window).width()/2) - (Box.el.width()/2);
					}
					Box.el.css({top: Box.top +"px", left: Box.left +"px"});
			
					Box.el.bgiframe();
			
					// Definit le click de fermeture
					$(".close", Box.el).click(function(){Box.kill(); return false;});
			
					//Apparition de la blackbox
					$(".black_box").fadeIn("slow", function(){
						Box.el.fadeIn("slow", function(){
							if(Box.show){Box.show();}
							//Box.killTime = setTimeout("Box.kill()",5000);
						});
					});
			   }
			 });
	},

	kill: function(reload){
		if(this.el == null){ return;}
		
		if(this.killTime !=  null){
			clearTimeout(this.killTime);
			this.killTime = null;
		}

		
		this.el.fadeOut("slow", function(){
			$('.receptacle').remove(); Box.el = null;
			if(Box.close){Box.close();}

			if(reload != null){
				Box.open(reload);
			}else{
				$(".black_box").fadeOut("slow",	function(){
					$('.black_box').remove();
				});
			}
		});	
	}
}

function omnitureOnclick(libelleRubrique){
	s.channel="Espace Client";
	s.pageName="Espace Client:" + libelleRubrique;
	s.tl();
}

function f_show_detail(id, urlParam) {
	
	loaded = $('#fic_' + id).is(':visible');
	if (!loaded) {
		$('#fic_' + id).show();
		$('#fic_' + id).load('DetailProduitLight?' + urlParam);
    }
}


