var ie = ((document.all) ? true : false);
var logged = false;
var pos_x = 0;
var pos_y = 0;
var id_current_rayon_n2 = 0;
var id_current_rayon_n3 = 0;
var id_niveau_1_show = '';
var id_niveau_2_show = '';
var current_url = '';
var dom = (document.getElementById)? true:false;
var ns4 = (document.layers)? true:false;
var ie4 = (document.all)? true:false;
var current_box_ = null;
var current_tips_title = null;
var nb_rayons_selected = 0;

function setNewLangOrRate(url){
    url += '&current_location='+encodeURIComponent(document.location.href);
    jQuery.post("v2/front/index.php"+url, "", function (data,status){
        document.location.href = data;
    });
}

/* promos */
function promos_ecp2009_view_form(){
    $('body').append("<div class='prettyPhotoOverlay' style='display:none;'></div>");
    $('div.prettyPhotoOverlay').css('opacity',0.8);
    $('div.prettyPhotoOverlay').css('height',$(document).height());
    $('body').append('<div id="formulaire" class="popupopenshop" style="z-index:100000;position:absolute;top:0;">'+$('#html_formulaire').html()+'</div>');
    $('#formulaire').css('left',0.5 * ($(document).width() - $('#formulaire').width()));
    $('#formulaire').css('top','20%');
    window.scrollTo(0,0);
    $('#html_formulaire').remove();
    $('div.prettyPhotoOverlay').show();
    urchinTracker("/open_shop_step0");
}

function promos_ecp2009_hide_form(){
    $('body').append('<div id="html_formulaire" style="display:none;">'+$('#formulaire').html()+'</div>');
    $('div.prettyPhotoOverlay').remove();
    $('#formulaire').remove();
}

function promos_ecp2009_valid_form(){
    var provenance = 'site';
    if(document.location.href.indexOf('ecp_2009') > -1){
        provenance = 'ecp_2009';
    }
    if($('#boutique').attr('value') == ''){
        alert($('#mandatory').html());
        return false;
    }
    if($('#boutique').attr('value').length > 12){
            alert($('#erreur_boutique_max_car').html());
            return false;
    }
    if(in_text($('#boutique').attr('value'),new Array('www.','http','.fr','.com','.net','.info','.eu','.org','.biz'))){
            alert($('#erreur_boutique_format').html());
            return false;
    }
    if(!c_url(replace_8($('#boutique').attr('value')+'.com'))){
            alert($('#erreur_boutique_format').html());
            return false;
    }
    if(!c_pw($('#pw').attr('value'))){
        alert($('#format_pw').html());
        return false;
    }
    vars = "x=lang|"+$('#lang').attr('value')+"|provenance|"+provenance+"|email|"+clean($('#email').attr('value'))+"|nom|"+clean($('#nom').attr('value'))+"|prenom|"+clean($('#prenom').attr('value'))+"|boutique|"+clean($('#boutique').attr('value'))+"|pw|"+clean($('#pw').attr('value'))+"|pw2|"+clean($('#pw2').attr('value'));
    jQuery.post('./ajax.php?action=promos_ecp2009_valid_form',vars,function (data,status){
       switch(data){
           case 'format_email':
               alert($('#format_email').html());
           break;
           case 'pw':
               alert($('#alert_pw').html());
           break;
           case 'mandatory':
               alert($('#mandatory').html());
           break;
           case 'mail_pris':
               alert($('#mail_pris').html());
           break;
           case 'boutique_prise':
               alert($('#boutique_prise').html());
           break;
           case 'valid':
               $('body').html($('#transition_open_preform').html());
               setTimeout("document.location.href = '"+$('#redir_url').html()+"';",3000);
           break;
       }
    });
}


/* fin promos */











function load_espace_client(p){
	$('#menu_espace_client_commandes').attr('class','');
	$('#menu_espace_client_identifiants').attr('class','');
	$('#menu_espace_client_adresses').attr('class','');
	$('#menu_espace_client_messages').attr('class','');
	if(p == 'espace_client_messages_cmd'){
		$('#menu_espace_client_commandes').attr('class','active');
	}
	else{
		$('#menu_'+p).attr('class','active');
	}
	loading('start');
	jQuery.post("./index.php?action="+p+"&web2=oui",
  							function (data,status){
  								$('#contenu_espace_client').html(data);
  								espace_client_update_nb_message_unread();
  								loading('stop');
  							}
  						);
}

function espace_client_save_ident(){
	var email,pseudo,pw,ddn_jour,ddn_mois,ddn_annee;
	
	email = $('#edit_email').attr('value');
	pseudo = $('#edit_login').attr('value');
	pw = $('#edit_pw').attr('value');
    ddn_jour = $('#ddn_jour').attr('value');
    ddn_mois = $('#ddn_mois').attr('value');
    ddn_annee = $('#ddn_annee').attr('value');
	
	if(pseudo == ''){
		alert($('#erreur_login').html());
		document.getElementById('edit_login').focus();
		return false;
	}
	
	if(pw != '' && pw.length < 4){
		alert($('#erreur_pw').html());
		document.getElementById('edit_pw').focus();
		return false;
	}
	
	if(pw.indexOf("'") > -1 || pw.indexOf('"') > -1){
		alert($('#alerte_carac').html());
		document.getElementById('edit_pw').focus();
		return false;
	}

    if(!c_pw(pw)){
        alert($('#format_pw').html());
        return false;
    }
	
	if(!c_email(email)){
		alert($('#erreur_email').html());
		document.getElementById('edit_email').focus();
		return false;
	}
	else{
		if(window.XMLHttpRequest) // FIREFOX
	       xhr = new XMLHttpRequest();
	  else if(window.ActiveXObject) // IE
	       xhr = new ActiveXObject("Microsoft.XMLHTTP");
	  else
	       return(false);
		xhr.open("POST", "/v2/front/ajax.php?action=espace_client_check_email", false);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
		xhr.send("email="+email);
		if(xhr.responseText == 'ko'){
			alert($('#erreur_email2').html());
			document.getElementById('edit_email').focus();
			return false;
		}
	}
	
	if(window.XMLHttpRequest) // FIREFOX
       xhr = new XMLHttpRequest();
  else if(window.ActiveXObject) // IE
       xhr = new ActiveXObject("Microsoft.XMLHTTP");
  else
       return(false);
	xhr.open("POST", "/v2/front/ajax.php?action=espace_client_check_pseudo", false);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
	xhr.send("pseudo="+pseudo);
	if(xhr.responseText == 'ko' || pseudo.length < 4){
		alert($('#erreur_login2').html());
		document.getElementById('pseudo').focus();
		return false;
	}
	
	loading('start');
	
	var donnees = "login="+pseudo+"&email="+email+"&pw="+pw+"&ddn_jour="+ddn_jour+"&ddn_mois="+ddn_mois+"&ddn_annee="+ddn_annee;
	
	jQuery.post("/v2/front/index.php?action=espace_client_save_ident&web2=oui",donnees,
  							function (data,status){
  								load_espace_client('espace_client_identifiants');
  								loading('stop');
  							}
  						);
}

function espace_client_save_news(){
	loading('start');
	var edit_news = document.getElementById('edit_news_oui').checked ? '1' : '0';
	var edit_offres = document.getElementById('edit_offres_oui').checked ? '1' : '0';
	var donnees = "edit_news="+edit_news+'&edit_offres='+edit_offres;
	jQuery.post("/v2/front/index.php?action=espace_client_save_news&web2=oui",donnees,
  							function (data,status){
  								load_espace_client('espace_client_identifiants');
  								loading('stop');
  							}
  						);
}

function espace_client_update_nb_message_unread(){
	jQuery.post("/v2/front/index.php?action=espace_client_update_nb_message_unread&web2=oui",
  							function (data,status){
  								$('#nb_message_unread').html(data);
  							}
  						);
}


function espace_client_save_adresse(id){
	var f = new Array('intitule','nom','prenom','societe','tva_intra','adresse','adresse2','cp','ville','id_pays','tel','mobile','fax','email');
	var data = 'x=';
	for(var i=0;i<f.length;i++){
		if($('#adresse_edit_'+id+'_'+f[i]+'_mandate').attr('value') == 1 && $('#adresse_edit_'+id+'_'+f[i]).attr('value') == ''){
			alert($('#mandate').html());
			document.getElementById('adresse_edit_'+id+'_'+f[i]).focus();
			return false;
		}
		data += f[i]+'|'+$('#adresse_edit_'+id+'_'+f[i]).attr('value')+'|';
	}
    if(document.getElementById('adresse_edit_'+id+'_email')){
        email = $('#adresse_edit_'+id+'_email').attr('value');
        if(!c_email(email)){
            alert($('#erreur_email').html());
            document.getElementById('edit_email').focus();
            return false;
        }
        else{
            if(window.XMLHttpRequest) // FIREFOX
               xhr = new XMLHttpRequest();
          else if(window.ActiveXObject) // IE
               xhr = new ActiveXObject("Microsoft.XMLHTTP");
          else
               return(false);
            xhr.open("POST", "/v2/front/ajax.php?action=espace_client_check_email", false);
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.send("email="+email);
            if(xhr.responseText == 'ko'){
                alert($('#erreur_email2').html());
                document.getElementById('edit_email').focus();
                return false;
            }
        }
    }
	if(document.getElementById('adresse_edit_'+id+'_intitule')){
		if($('#adresse_edit_'+id+'_intitule').attr('value').length > 15){
			alert($('#err_nb_carac_int').html());
			return false;
		}
	}
	if(!c_tel($('#adresse_edit_'+id+'_tel').attr('value'))){
		document.getElementById('adresse_edit_'+id+'_tel').focus();
		alert($('#err_telephone').html());
		return false;
	}
	if(!c_tel($('#adresse_edit_'+id+'_mobile').attr('value'))){
		document.getElementById('adresse_edit_'+id+'_mobile').focus();
		alert($('#err_telephone').html());
		return false;
	}
	if(!c_tel($('#adresse_edit_'+id+'_fax').attr('value'))){
		document.getElementById('adresse_edit_'+id+'_fax').focus();
		alert($('#err_telephone').html());
		return false;
	}
	if(document.getElementById('adresse_edit_'+id+'_tva_intra')){
		if(!c_tva_intra($('#adresse_edit_'+id+'_tva_intra').attr('value')) && $('#adresse_edit_'+id+'_tva_intra').attr('value') != ''){
			document.getElementById('adresse_edit_'+id+'_tva_intra').focus();
			alert($('#err_tva').html());
			return false;
		}
	}
	var m = document.getElementById('adresse_edit_'+id+'_genre_m').checked;
	var mme = document.getElementById('adresse_edit_'+id+'_genre_mme').checked;
	var mlle = document.getElementById('adresse_edit_'+id+'_genre_mlle').checked;
	if(!m && !mme && !mlle){
		alert($('#mandate').html());
		return false;
	}
	else if(m){
		data += "titre|0|";
	}
	else if(mme){
		data += "titre|1|";
	}
	else if(mlle){
		data += "titre|2|";
	}
	data += "etat|"+espace_client_getEtat(id)+'|';
	data += "commentaires|"+$('#adresse_edit_'+id+'_commentaires').attr('value')+'|';
	data += "id_adresse|"+id;
	loading('start');
	jQuery.post("/v2/front/index.php?action=espace_client_save_adresse&web2=oui",data,
  							function (data,status){
  								// sauvegarder...
									var id_click = data;
									jQuery.post("/v2/front/index.php?action=espace_client_adresses&web2=oui",
								  							function (data,status){
								  								$('#contenu_espace_client').html(data);
								  								if(parseInt(id_click) > 0){
									  								$('.link_'+id_click).click();
									  							}
								  								loading('stop');
								  							}
								  						);
  							}
  						);
	return true;
}

function espace_client_delete_adresses(id){
	if(confirm($('#delete_adresse').html())){
		loading('start');
		jQuery.post("/v2/front/index.php?action=espace_client_delete_adresse&web2=oui","id_adresse="+id,
	  							function (data,status){
	  								// supprimer...
										var id_click = 0;
										jQuery.post("/v2/front/index.php?action=espace_client_adresses&web2=oui",
									  							function (data,status){
									  								$('#contenu_espace_client').html(data);
									  								if(parseInt(id_click) > 0){
										  								$('.link_'+id_click).click();
										  							}
									  								loading('stop');
									  							}
									  						);
	  							}
	  						);
		return true;
	}
}

function espace_client_getEtat(id){
	switch(parseInt(document.getElementById('adresse_edit_'+id+'_id_pays').value)){
		case 218:
			return $('#adresse_edit_'+id+'_etat_liste_usa').attr('value');
		break;
		case 36:
			return $('#adresse_edit_'+id+'_etat_liste_ca').attr('value');
		break;
		default:
			return $('#adresse_edit_'+id+'_etat_free').attr('value');
		break;
	}
}

function espace_client_display_state(id){
	$('#adresse_edit_'+id+'_etat_liste_usa').hide();
	$('#adresse_edit_'+id+'_etat_liste_ca').hide();
	$('#adresse_edit_'+id+'_etat_free').hide();
	switch(parseInt(document.getElementById('adresse_edit_'+id+'_id_pays').value)){
		case 218:
			$('#adresse_edit_'+id+'_etat_liste_usa').show();
		break;
		case 36:
			$('#adresse_edit_'+id+'_etat_liste_ca').show();
		break;
		default:
			$('#adresse_edit_'+id+'_etat_free').show();
		break;
	}
}

function espace_client_ajouter(){
	if(parseInt($('#nb_addr').html()) > parseInt($('#nb_max_addr').html())){
		alert($('#max_addr').html());
		return false;
	}
	$('div.livraison').hide();
	$('#adresse_view_0').hide();
	$('#adresse_edit_0_etat_free').show();
	$('#adresse_0').show();
	$('#adresse_edit_0').show();
}


function espace_client_messagerie_cocher(type){
	$('input.'+type+'_cocher').each(function(){
		this.checked = true;
	});
}

function espace_client_messagerie_decocher(type){
	$('input.'+type+'_cocher').each(function(){
		this.checked = false;
	});
}

function espace_client_messagerie_delete(type,id){
	if(confirm($('#delete').html())){
		if(id > 0){
			var ids = id;
		}
		else{
			var ids = "0";
			$('input.'+type+'_cocher').each(function(){
				if(this.checked){
					ids += ","+this.value;
				}
			});
			if(ids == '0'){
				alert($('#err_cocher').html());
				return false;
			}
		}
		loading('start');
		jQuery.post("/v2/front/index.php?action=espace_client_messagerie_delete&web2=oui","ids="+ids+"&type="+type,
										  							function (data,status){
										  								load_espace_client('espace_client_messages');
										  								espace_client_update_nb_message_unread();
										  							}
										  						);
	}
}


function espace_client_messagerie_read(type,id){
	if(type == 'none'){
		var ids = id;
	}
	else{
		var ids = "0";
		$('input.'+type+'_cocher').each(function(){
			if(this.checked){
				ids += ","+this.value;
			}
		});
		if(ids == '0'){
			alert($('#err_cocher').html());
			return false;
		}
	}
	loading('start');
	jQuery.post("/v2/front/index.php?action=espace_client_messagerie_read&web2=oui","ids="+ids+"&type="+type,
									  							function (data,status){
									  								espace_client_update_nb_message_unread();
									  								if(type != 'none'){
									  									load_espace_client('espace_client_messages');
									  								}
									  							}
									  						);
}


function espace_client_messagerie_lire(type,id){
	if(type == 'to'){
		$('#titre_message_from').hide();
		$('#messages_envoyes').hide();
	}
	else{
		$('#titre_message_to').hide();
		$('#messages_recus').hide();
	}
	$('div.lire_'+type).hide();
	espace_client_messagerie_annuler(id);
	$('#lire_'+id).show();
	jQuery.post("./index.php?action=espace_client_messagerie_read&web2=oui","ids="+id+"&type="+type,
						function (data,status){
							espace_client_update_nb_message_unread();
						});
}


function espace_client_messagerie_aff_repondre(id,id2){
	if(id == 0){ // reclam
		hide(new Array('lire_'+id2+'_txt','lire_'+id2+'_fcts','lire_'+id2+'_de','n_msg_'+id2));
		show(new Array('repondre_'+id2,'repondre_'+id2+'_fcts_reclam','lire_'+id2+'_a_reclam'));
		$('#sujet_'+id2).html($('#sujet_reclam_'+id2).html());
	}
	else{
		hide(new Array('lire_'+id+'_txt','lire_'+id+'_fcts','lire_'+id+'_de','n_msg_'+id));
		show(new Array('repondre_'+id,'repondre_'+id+'_fcts','lire_'+id+'_a'));
		$('#sujet_'+id).html($('#sujet_init_'+id).html());
	}
}

function espace_client_messagerie_annuler(id){
	if(parseInt(id) >= 0){
		hide(new Array('repondre_'+id,'repondre_'+id+'_fcts','lire_'+id+'_a','lire_'+id+'_a_reclam','repondre_'+id+'_fcts_reclam'));
		show(new Array('lire_'+id+'_txt','lire_'+id+'_fcts','lire_'+id+'_de','n_msg_'+id));
		$('#sujet_'+id).html($('#sujet_init_'+id).html());
	}
	else{
		$('tr.bouton_messagerie').show();
		$('#ecrire').hide();
	}
}

function espace_client_messagerie_envoyer(id,id2){
	if(parseInt(id) < 0){
		var txt = document.getElementById('repondre').value;
		if($('#ecrire_a').html() != $('#ecrire_a_reclam').html() || parseInt(id) < -1){
			var data = "x=message|"+clean(txt)+"|id|"+id+"|sujet|"+$('#sujet').html();
		}
		else{
			var data = "x=message|"+clean(txt)+"|id|-1|sujet|"+clean($('#sujet').html());
		}
	}
	else if(id == 0){
		var txt = document.getElementById('repondre_'+id2).value;
		var data = "x=message|"+clean(txt)+"|id|-1|sujet|"+clean($('#sujet_'+id2).html());
	}
	else{
		var txt = document.getElementById('repondre_'+id).value;
		var data = "x=message|"+clean(txt)+"|id|"+id;
	}
    
	if(txt == ''){
		alert($('#err_msg').html());
		return false;
	}
	loading('start');
	jQuery.post("./index.php?action=espace_client_messagerie_envoyer&web2=oui",data,
									  							function (data,status){
									  								if(parseInt(id) < 0){
									  									document.location.href = './index.php?action=espace_client&g=messages';
									  								}
									  								else{
								  										load_espace_client('espace_client_messages');
								  									}
									  							}
									  						);
}

function espace_client_messagerie_question_cmd(type,id){
	var ids = "0";
	$('input.chk').each(function (){
		if(this.checked){
			ids += ","+this.value;
		}
	});
	if(ids == '0'){
		alert($('#err_chk').html());
		return false;
	}
	if(type == 'question'){
		var l = './index.php?action=espace_client&id='+id+'&g=messages_cmd&ids='+ids;
	}
	else{
		var l = './index.php?action=espace_client&id='+id+'&g=messages_cmd&t=reclam&ids='+ids;
	}
	document.location.href = l;
	return;
	$('#sujet').html($('#sujet_cmd_'+type).html());
	$('#ecrire_a').html($('#ecrire_a_'+type).html());
	$('#ecrire').show();
	$('tr.bouton_messagerie').hide();
}


function update_panier_top(){
	jQuery.post("./ajax.php?action=get_nb_panier",
									  							function (data,status){
									  								if(parseInt(data) == 0){
									  									$('#panier_top').attr('class','panier_off');
									  									$('#nb_panier_top').html('(0)');
									  								}
									  								else{
									  									$('#panier_top').attr('class','panier_on');
									  									$('#nb_panier_top').html('('+data+')');
									  								}
									  								$('#panier_top').show();
									  							}
									  						);
}




















function over_tips(obj){
	if($(obj).attr('title') != ''){
		current_tips_title = $(obj).attr('title');
		$(obj).attr('title','');
		var tmp = current_tips_title.split('|');
		var t = tmp[1];
		var w = tmp[0];
		if(w == '' || w == 0){
			w = 150;
		}
		$('body').append('<div class="box_info_bulle">'+t+'</div>');
		$('div.box_info_bulle').css('width',w);
		$('div.box_info_bulle').html(t);
		$('div.box_info_bulle').show();
	}
	var largeur = pos_x + parseInt(w) > $(document).width() + 100 ? pos_x - parseInt(w) : pos_x;
	$('div.box_info_bulle').css('top',pos_y+20);
	$('div.box_info_bulle').css('left',largeur);
}

function out_tips(obj){
	$('div.box_info_bulle').remove();
	$(obj).attr('title',current_tips_title);
}

function load_tips(){
	return false;
	$("img[rel^='tips']").each(function(){
			$(this).hover(
			function(){
				over_tips(this);
			},
			function (){
				out_tips(this);
			});
	});
}




function mercaway_start(o) {			//Init des variables,des Divs-Layers, et du onmousedown
	current_box_ = o;
 	if (ie4) {
		current_box_.onmousedown=mercaway_beginDrag;
	}else if (dom) {
		document.getElementById(current_box_.id).addEventListener("mousedown",mercaway_beginDrag, false);
	}
}

function mercaway_doDrag(e) {			// Déplacement des Divs-Layers
	if (ie4) {
		var difX=event.clientX-window.lastX;
	   var difY=event.clientY-window.lastY;
		var newX1 = parseInt(current_box_.style.left)+difX;
	   var newY1 = parseInt(current_box_.style.top)+difY;
	   current_box_.style.left=newX1+"px";
	   current_box_.style.top=newY1+"px";
	   window.lastX=event.clientX;
	   window.lastY=event.clientY;
	}else if (dom) {
		var difX=e.clientX-window.lastX;
	   var difY=e.clientY-window.lastY;
		var newX1 = parseInt(document.getElementById(current_box_.id).style.left)+difX;
	   var newY1 = parseInt(document.getElementById(current_box_.id).style.top)+difY;
	   document.getElementById(current_box_.id).style.left=newX1+"px";
	   document.getElementById(current_box_.id).style.top=newY1+"px";
	   window.lastX=e.clientX;
	   window.lastY=e.clientY;
	}
}

function mercaway_beginDrag(e) {		// Paramétrage du déplacement des Divs-Layers et des onmousemove & onmouseup
	if (ie4) {
		window.lastX=event.clientX;
	   window.lastY=event.clientY;
	   document.onmousemove=mercaway_doDrag;
	   document.onmousedown=mercaway_doDrag;
	   document.onmouseup=mercaway_endDrag;
//		if (event.srcElement.id=="wind"){
//			Récup de l'Id d'un élément sous IE
//		}
	}
	else if (dom) {
		window.lastX=e.clientX;
		window.lastY=e.clientY;
		window.onmousemove=mercaway_doDrag;
		document.onmousedown=mercaway_doDrag;
		window.onmouseup=mercaway_endDrag;
		myattr=e.target.getAttribute("ID");
//		if(myattr=="wind") {
//			Récup de l'ID d'un élément sous NS6
//		}
	}
}

function mercaway_endDrag(e) {		// Stabilisation des Divs-Layers dans la fenetre
	current_box_ = null;
	 if (ie4 || ns4) {
	 	document.onmousemove=null;
	 }else if (dom) {
		window.onmousemove=null;
	}
}














function load_inner_ajax_cart(){
	update_panier_top();
	jQuery.post("./index.php?action=inner_ajax_cart",
							function (data,status){
								var whatison = false;
								if(document.getElementById('inner_ajax_cart_on')){
									var whatison = $('#inner_ajax_cart_on').is(':hidden') ? '_off' : '_on';
								}
								$('#inner_ajax_cart_on').remove();
								$('#inner_ajax_cart_off').remove();
								$('body').append(data);
								if(!whatison){
									switch_ajax_cart("inner_ajax_cart");
								}
								else{
									switch_ajax_cart("inner_ajax_cart",whatison);
								}
								loading('stop');
							}
						);
}

function ajax_cart_prec(pos){
	$('#groupe_'+pos).hide();
	$('#groupe_'+(pos-1)).show();
}

function ajax_cart_next(pos){
	$('#groupe_'+pos).hide();
	$('#groupe_'+(pos+1)).show();
}

function switch_ajax_cart(id){
	positionner_ajax_cart(id);
	if(arguments[1] != '' && arguments[1] != null){
		$('#'+id+arguments[1]).show();
		for(var i=1;i<6;i++){
			if(!document.getElementById('groupe_'+i)){
				break;
			}
			$("#groupe_"+i).hide();
		}
		$("#groupe_"+(i-1)).show();
	}
	else{
		if($('#'+id+'_off').is(':hidden') && $('#'+id+'_on').is(':hidden')){
			$('#'+id+'_off').show();
			//$('#'+id+'_off').slideDown('slow',function (){positionner_ajax_cart(id);});
		}
		else{
			if($('#'+id+'_off').is(':hidden')){
				$('#'+id+'_off').show();
				$('#'+id+'_on').hide();
				//$('#'+id+'_on').slideUp('slow',function (){$('#'+id+'_off').show();positionner_ajax_cart(id);});
			}
			else{
				$('#'+id+'_on').show();
				$('#'+id+'_off').hide();
				/*$('#'+id+'_off').hide();
				$('#'+id+'_on').slideDown('slow',function(){positionner_ajax_cart(id);});*/
			}
		}
	}
	/*if($('#'+id+'_off').is(':hidden')){
		$('#margin_footer').css('height',214);
	}
	if($('#'+id+'_on').is(':hidden')){
		$('#margin_footer').css('height',74);
	}*/
}

function positionner_ajax_cart(id){
	if(document.getElementById(id+'_on')){
		var screen_y = 0;
		var wh = 0;
	  if (window.pageYOffset) {
	    screen_y = window.pageYOffset;
	  } else if (document.body && document.documentElement.scrollTop) {
	    screen_y = document.documentElement.scrollTop;
	  }
	  
	  if(!ie){
	  	wh = window.innerHeight;
	  }
	  else{
	  	wh = document.body.clientHeight;
	  }
	  	var h = 74;
		$('#'+id+"_off").css('left',0);
		$('#'+id+"_off").css('top',screen_y + wh - h);
		
			var h = 214;
	  $('#'+id+"_on").css('left',0);
		$('#'+id+"_on").css('top',screen_y + wh - h);
	}
}


function catch_buy_button(o){

	if($(o).attr('href') != null){ // c'est une balise <a/>
        if($(o).attr('class') == 'ajoutpan_active' || $(o).attr('class') == 'ajoutpan ajoutpan_active'){
            return false;
        }
        $(o).attr('class','ajoutpan ajoutpan_active');

		jQuery.post($(o).attr('href')+'&do=noth',
  							function (data,status){
  								load_inner_ajax_cart();
  							}
  						);
	}
	if($(o).attr('action') != null){ // c'est une balise <form/>
		var data = 'action=buy&id='+$('#id_produit').attr('value')+'&qte='+document.getElementById('qte').value+'&do=noth';
		jQuery.get($(o).attr('action'),data,
  							function (data,status){
  								load_inner_ajax_cart();
  							}
  						);
	}
	return false;
}

function logout(){
    
	if(document.getElementById('panier_totalTTC') && document.getElementById('panier_totalTTC').innerHTML > 0 && confirm(document.getElementById('alerte_deconnexion').innerHTML) == false){
		return false;
	}
	if(window.XMLHttpRequest) // FIREFOX
               xhr = new XMLHttpRequest();
          else if(window.ActiveXObject) // IE
               xhr = new ActiveXObject("Microsoft.XMLHTTP");
          else
               return(false);

  var data = "";
	xhr.open("POST", "./ajax.php?action=logout", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
	xhr.send(data);
	xhr.onreadystatechange = function(){
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
			
			// SEULEMENT DANS LE CAS DE L'ESPACE CLIENT OU DE LA PAGE D'INSCRIPTION
			if(document.location.href.indexOf('/compte/') > -1){
				document.location.href = $('#redir_logout_url').html();
			}
            if(document.location.href.indexOf('/account/') > -1){
				document.location.href = $('#redir_logout_url').html();
			}
            if(document.location.href.indexOf('/cuenta/') > -1){
				document.location.href = $('#redir_logout_url').html();
			}
            if(document.location.href.indexOf('/panier/') > -1){
				document.location.href = '/panier/';
			}
            if(document.location.href.indexOf('/cart/') > -1){
				document.location.href = '/cart/';
			}
            if(document.location.href.indexOf('/cesta/') > -1){
				document.location.href = '/cesta/';
			}
            if(document.location.href.indexOf('/panier/') > -1){
				document.location.href = '/panier/';
			}
            if(document.location.href.indexOf('/warenkorb/') > -1){
				document.location.href = '/warenkorb/';
			}
            if(document.location.href.indexOf('/carrello/') > -1){
				document.location.href = '/carrello/';
			}
            if(document.location.href.indexOf('/config/') > -1){
				document.location.href = $('#redir_logout_url').html();
			}
			check_if_logged();
			// SEULEMENT DANS LE CAS DE LA PAGE PANIER
			if(document.location.href.indexOf('/panier/') > -1){
				var u = "/panier/";
				if(window.XMLHttpRequest) // FIREFOX
			       xhr = new XMLHttpRequest();
			  else if(window.ActiveXObject) // IE
			       xhr = new ActiveXObject("Microsoft.XMLHTTP");
			  else
			       return(false);


				xhr.open("POST", u+"?web2=oui", true);
				xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
				xhr.send("");
				xhr.onreadystatechange = function(){
					// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
					if(xhr.readyState == 4 && xhr.status == 200){
						document.getElementById('wrapper').innerHTML = xhr.responseText;
					}
				}
			}
			// FIN CAS PANIER
		}
	
}

function photo_viewer(){
	$('#photo_viewer').show();
	$('#opaque').show();
	window.scrollTo(0,0);
}

function display_adulte(u){
	$('#box_alerte_adulte').show();
	$('#opaque').show();
	window.scrollTo(0,0);
	current_url = u;
}

function set_adult(is_adult){
	if(is_adult){
		if(window.XMLHttpRequest) // FIREFOX
	       xhr = new XMLHttpRequest();
	  else if(window.ActiveXObject) // IE
	       xhr = new ActiveXObject("Microsoft.XMLHTTP");
	  else
	       return(false);
		xhr.open("POST", "./ajax.php?action=set_adult", false);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
		xhr.send("");
		load_rayons(current_url);
	}
	$('#box_alerte_adulte').hide();
	$('#opaque').hide();
}

function load_rayons(u){
	var execute = true;
	if(u.indexOf('id=') > -1){
		var id_rayon = parseInt(u.substr(u.indexOf('id=')+3,4));
		if(window.XMLHttpRequest) // FIREFOX
	       xhr = new XMLHttpRequest();
	  else if(window.ActiveXObject) // IE
	       xhr = new ActiveXObject("Microsoft.XMLHTTP");
	  else
	       return(false);
		xhr.open("POST", "./ajax.php?action=is_adult&id="+id_rayon, false);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
		xhr.send("");
		if(xhr.responseText == 'ko'){
			display_adulte(u);
			execute = false;
		}
	}
	if(execute){
		if(u.indexOf('main_id') > -1){
			document.location.href = u;
			return false;
		}
		loading('start');
		if(window.XMLHttpRequest) // FIREFOX
	       xhr = new XMLHttpRequest();
	  else if(window.ActiveXObject) // IE
	       xhr = new ActiveXObject("Microsoft.XMLHTTP");
	  else
	       return(false);
	  
	  
		xhr.open("POST", u+"&web2=oui", true);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
		xhr.send("");
		
		xhr.onreadystatechange = function(){
			// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
			if(xhr.readyState == 4 && xhr.status == 200){
				document.getElementById('wrapper').innerHTML = xhr.responseText;
				$('#niv2_'+id_current_rayon_n2).show();
				$('#niv3_'+id_current_rayon_n3).show();
				$('#filtrer_niveau_1__1').hide();
				$('#filtrer_niveau_1__'+id_niveau_1_show).show();
				$('#filtrer_niveau_2_'+id_current_rayon_n2+'_1').hide();
				$('#filtrer_niveau_2_'+id_current_rayon_n2+'_'+id_niveau_2_show).show();
				load_inner_ajax_cart();
				clean_titre_niveau();
				if(document.getElementById('no_products')){
					//$('#filtre_prix').hide();
				}
			}
		}
	}
}

function clean_titre_niveau(){
	if($('#titre_niveau2').html() == ''){
		$('#col_niveau_2').hide();
	}
	if($('#titre_niveau3').html() == ''){
		$('#col_niveau_3').hide();
	}
}

function getTitreRayon(obj,level){
	//$('#titre_niveau'+level).html(obj.innerHTML);
}

function select_niv2(pos,a){
	if(document.getElementById('niv2_'+pos)){
		//$('#niv2_'+id_current_rayon_n2).hide();
		//$('#niv3_'+id_current_rayon_n3).hide();
		id_current_rayon_n2 = pos;
		//$('#niv2_'+id_current_rayon_n2).show();
		id_niveau_1_show = a;
		load_rayons('./index.php?action=item_listing&id='+pos+'&n2='+id_current_rayon_n2+'&s=i&num2='+a);
	}
}

function select_niv3(pos,a){
	if(document.getElementById('niv3_'+pos)){
		//$('#niv3_'+id_current_rayon_n3).hide();
		id_current_rayon_n3 = pos;
		//$('#niv3_'+id_current_rayon_n3).show();
		id_niveau_2_show = a;
		load_rayons('./index.php?action=item_listing&id='+pos+'&n2='+id_current_rayon_n2+'&s=i&n3='+id_current_rayon_n3+'&num3='+a+'&num2='+id_niveau_1_show);
	}
}

function select_niv4(pos){
	load_rayons('./index.php?action=item_listing&id='+pos+'&n2='+id_current_rayon_n2+'&s=i&n3='+id_current_rayon_n3);
}

function select_rayon_filtre(dis,nb_prod){
	if(dis == 'liste'){
		if(nb_prod == 0){
			alert($('#alert_pas_de_filtre_dispo').html());
			return false;
		}
		$('#liste_filtrer').show();
		$('#liste_rayons').hide();
		$('#filtre_prix').show();
		$('#lien_filtre_1').attr('class','item1 active');
		$('#lien_filtre_2').attr('class','item2');
	}
	else{
		$('#liste_filtrer').hide();
		$('#liste_rayons').show();
		$('#filtre_prix').hide();
		$('#lien_filtre_1').attr('class','item1');
		$('#lien_filtre_2').attr('class','item2 active');
	}
	if(window.XMLHttpRequest) // FIREFOX
       xhr = new XMLHttpRequest();
  else if(window.ActiveXObject) // IE
       xhr = new ActiveXObject("Microsoft.XMLHTTP");
  else
       return(false);
	xhr.open("POST", "./ajax.php?action=save_display_filtre_rayon&style="+dis, true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
	xhr.send("");
}

function view_details(n){
	if(pos_x < 513){
		$('#product_details_'+n).css('top',pos_y - 210 - 100);
		$('#product_details_'+n).css('left',pos_x - 159 + 40);
	}
	else{
		$('#product_details_'+n).css('top',pos_y - 210 - 100);
		$('#product_details_'+n).css('left',pos_x - 159 - 40 - 385);
	}
	$('#product_details_'+n).toggle();
}

function updateFilterPrix(from,to,send,goto){
	document.getElementById('prix_from_txt').innerHTML = from;
	document.getElementById('prix_to_txt').innerHTML = to;
	if(send){
		load_rayons(goto + document.getElementById('FilterPriceValue').value + '&id='+$('#current_id').attr('value'));
	}
}

function rayon_set_display(dis,u){
	if(window.XMLHttpRequest) // FIREFOX
       xhr = new XMLHttpRequest();
  else if(window.ActiveXObject) // IE
       xhr = new ActiveXObject("Microsoft.XMLHTTP");
  else
       return(false);
       
	xhr.open("POST", "./ajax.php?action=save_display_rayon&style="+dis, true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
	xhr.send("");
	if(dis == 'liste'){
		$('#liste').show();
		$('#mosaique').hide();
		$('#titre_mosa').hide();
		$('#link_liste').css('text-decoration','underline');
		$('#link_mosa').css('text-decoration','none');
	}
	else{
		$('#liste').hide();
		$('#mosaique').show();
		$('#titre_mosa').show();
		$('#link_liste').css('text-decoration','none');
		$('#link_mosa').css('text-decoration','underline');
	}
}

function rayon_move(obj,sens,pos,nb){
	if(!nb){
		nb = 1;
	}
	$('#filtrer_'+obj+'_'+pos).hide();
	if(sens == '-'){
		pos = pos - nb;
	}
	else{
		pos = pos + nb;
	}
	$('#filtrer_'+obj+'_'+pos).show();
	var u = "./ajax.php?action=save_pos_rayon&obj="+obj+"&pos="+pos;
	if(window.XMLHttpRequest) // FIREFOX
       xhr = new XMLHttpRequest();
  else if(window.ActiveXObject) // IE
       xhr = new ActiveXObject("Microsoft.XMLHTTP");
  else
       return(false);
	xhr.open("POST", u, true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
	xhr.send("");
}

function update_qte_panier(btn,o_qte,id){
	qte = parseInt(document.getElementById(o_qte).value);
	if(isNaN(qte) || qte < 0){
		alert(document.getElementById('error_format_qte').innerHTML);
		return false;
	}
	document.getElementById(o_qte).value = qte;
	if(qte == 0){
		if(!confirm(document.getElementById('question_delete').innerHTML)){
			return false;
		}
	}
	btn.disabled = true;
	btn.value = $('#en_cours').html();
	var u = "./index.php?action=edit_qte&r=no&qte="+qte+"&id="+id;
	
	jQuery.post(u+"&web2=oui",function (data,status){
		if(data.indexOf('class="vide"')>-1){
			document.location.href = './index.php?action=panier&r=no';
		}
		else{
			update_panier_top();
			$('#wrapper').html(data);
		}
	});
	
}

function loading(o){
	if(o == 'start'){
        if(document.getElementById('attente')){
            $('body').append("<div class='prettyPhotoOverlay' style='display:none;'></div>");
            $('div.prettyPhotoOverlay').css('opacity',0.2);
            $('div.prettyPhotoOverlay').css('height',$(document).height());
            $('body').append('<div id="attente_tmp" style="z-index:100000;position:absolute;top:0;">'+$('#attente').html()+'</div>');
            $('#attente_tmp').css('left',0.5 * ($(document).width() - $('#attente_tmp').width()));
            $('#attente_tmp').css('top','30%');
            /*
            $('#attente').css('top',$('div.filtreblock_Right').css('margin-top'));
            //$('#attente').css('height',$('#liste').css('height'));
            */
           window.scrollTo(0,0);
            $('div.prettyPhotoOverlay').show();
        }
        else{
            $('#img_loading').css('top',pos_y - 8);
            $('#img_loading').css('left',pos_x - 8);
            $('#img_loading').show();
        }
	}
	else{
		$('#img_loading').hide();
        $('div.prettyPhotoOverlay').remove();
        $('#attente_tmp').remove();
	}
}

function search_prod(goto,txt_init){
	if(txt_init != document.getElementById('str_id').value && document.getElementById('str_id').value != ''){
		load_rayons(goto+document.getElementById('str_id').value);
	}
	else{
		document.location.href = goto;
	}
}

function check_if_logged(data){
	if(window.XMLHttpRequest) // FIREFOX
       xhr = new XMLHttpRequest();
  else if(window.ActiveXObject) // IE
       xhr = new ActiveXObject("Microsoft.XMLHTTP");
  else
       return(false);
  
	xhr.open("POST", "./ajax.php?action=login&ajax=1", false);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
	xhr.send(data);


    if(xhr.responseText == 'refused'){
        alert($('#shop_refused_error_login').html());
        logged = false;
        return false;
    }

	if(xhr.responseText == 'prospect'){
		document.location.href = $('#redir_secure_url').html()+"/config/";
	}
        else if(xhr.responseText == 'shop'){
		document.location.href = $('#redir_secure_url').html()+"/admin/index.php?action=bord";
	}
	else if(xhr.responseText == 'false'){
		alert(document.getElementById('erreur_pw_login').innerHTML);
	}
	else if(xhr.responseText != '' && xhr.responseText != null){
		$('#nologged').hide();
		$('#logged_').show();
		$('#connection').show();
		$('#connection2').show();
		$('#logged').show();
		$('#logged').html(xhr.responseText);
		logged = true;
		if(data != ''){
			document.location.href = document.location.href.indexOf('/login/') > -1 ? '/account/' : '/compte/';
		}
	}
	else if(xhr.responseText == '' || xhr.responseText == null){
		$('#nologged').show();
		$('#logged_').hide();
		$('#connection').hide();
		$('#connection2').hide();
		$('#logged').hide();
		$('#logged').html('');
		logged = false;
	}
}

var open_shop_step1 = new Array('nom_boutique','mot_de_passe','description','site','pan_moyen','pan_id_devise','CA_mensuel','CA_mensuel_id_devise','devise_insertion_prix','delai_livraison','pays_clients','num_suivi','transporteurs');
var open_shop_step1_values = new Array();
var open_shop_step1_mandatory = new Array('nom_boutique','mot_de_passe','description','pan_moyen','pan_id_devise','CA_mensuel','CA_mensuel_id_devise','devise_insertion_prix','delai_livraison','pays_clients','num_suivi');
var open_shop_number = new Array('pan_moyen','CA_mensuel');
var open_shop_step2 = new Array('type','societe','num_tva','capital','siret','naf','type_soc','prenom','nom','adresse','adresse2','cp','ville','etat','id_pays','telephone','fax','email_boutique');
var open_shop_step2_values = new Array();
var open_shop_step2_mandatory = new Array('prenom','nom','adresse','ville','telephone','email_boutique');
var is_step_1_valid = false;
var is_step_2_valid = false;
var current_cb = 0;

function update_display_open_rayon(type){
	switch(type){
		case '0':
			$('.rayon_type_2').each(function (){
																	$(this).show();
																});
			$('.rayon_type_3').each(function (){
																	$(this).show();
																});
			$('.rayon_type_4').each(function (){
																	$(this).show();
																});
			$('.rayon_type_text_2').css('text-decoration','none');
			$('.rayon_type_text_3').css('text-decoration','none');
			$('.rayon_type_text_4').css('text-decoration','none');
			$('.rayon_type_tips_2').each(function (){
																	$(this).hide();
																});
			$('.rayon_type_tips_3').each(function (){
																	$(this).hide();
																});
			$('.rayon_type_tips_4').each(function (){
																	$(this).hide();
																});
			$('#info_produits').hide();
			$('#recap_info_produits').hide();
                        $('#delai_livraison').html($('#delai_livraison_tangibles').html());
		break;
		case '2':
			$('.rayon_type_2').each(function (){
																	$(this).show();
																});
			$('.rayon_type_3').each(function (){
																	$(this).hide();
																});
			$('.rayon_type_4').each(function (){
																	$(this).hide();
																});
			$('.rayon_type_text_2').css('text-decoration','none');
			$('.rayon_type_text_3').css('text-decoration','line-through');
			$('.rayon_type_text_4').css('text-decoration','line-through');
			$('.rayon_type_tips_2').each(function (){
																	$(this).hide();
																});
			$('.rayon_type_tips_3').each(function (){
																	$(this).show();
																});
			$('.rayon_type_tips_4').each(function (){
																	$(this).show();
																});
			$('#info_produits').show();
			$('#recap_info_produits').show();
                        $('#delai_livraison').html($('#delai_livraison_tangibles').html());
		break;
		case '3':
			$('.rayon_type_2').each(function (){
																	$(this).hide();
																});
			$('.rayon_type_3').each(function (){
																	$(this).show();
																});
			$('.rayon_type_4').each(function (){
																	$(this).hide();
																});
			$('.rayon_type_text_2').css('text-decoration','line-through');
			$('.rayon_type_text_3').css('text-decoration','none');
			$('.rayon_type_text_4').css('text-decoration','line-through');
			$('.rayon_type_tips_2').each(function (){
																	$(this).show();
																});
			$('.rayon_type_tips_3').each(function (){
																	$(this).hide();
																});
			$('.rayon_type_tips_4').each(function (){
																	$(this).show();
																});
			$('#info_produits').hide();
			$('#recap_info_produits').hide();
                        $('#delai_livraison').html($('#delai_livraison_intangibles').html());
		break;
		case '4':
			$('.rayon_type_2').each(function (){
																	$(this).hide();
																});
			$('.rayon_type_3').each(function (){
																	$(this).hide();
																});
			$('.rayon_type_4').each(function (){
																	$(this).show();
																});
			$('.rayon_type_text_2').css('text-decoration','line-through');
			$('.rayon_type_text_3').css('text-decoration','line-through');
			$('.rayon_type_text_4').css('text-decoration','none');
			$('.rayon_type_tips_2').each(function (){
																	$(this).show();
																});
			$('.rayon_type_tips_3').each(function (){
																	$(this).show();
																});
			$('.rayon_type_tips_4').each(function (){
																	$(this).hide();
																});
			$('#info_produits').hide();
			$('#recap_info_produits').hide();
                        $('#delai_livraison').html($('#delai_livraison_intangibles').html());
		break;
	}
}

function choose_sub_rayon(id_main,id_rayon,type,obj){
	var data = '';
	data = 'id_main='+id_main+'&id='+id_rayon+'&type='+type;
	if(obj.checked){
		data += '&action=add';
		nb_rayons_selected++;
	}
	else{
		data += '&action=delete';
		nb_rayons_selected--;
	}
	jQuery.post('./ajax.php?action=open_shop_set_rayon',data,
							function (text,status){
								var data = text.split('|');
								update_display_open_rayon(data[0]);
								$('#recap_rayons').html(data[1]);
								$('.f4 > p').each(function (){
										//document.getElementById($('#'+this.id+' > input').attr('id')).checked = false;
										$('#'+this.id+' > input').show();
										$('#'+this.id+' > img').hide();
									});
								var tmp = data[2].split(',');
								for(var i=0;i<tmp.length;i++){
									//document.getElementById($('#p_rayon_'+tmp[i]+' > input').attr('id')).checked = true;
									$('#p_rayon_'+tmp[i]+' > input').hide();
									$('#p_rayon_'+tmp[i]+' > img').show();
									if(ie){
										$('#p_rayon_'+tmp[i]+' > img').css('margin-left',5);
										$('#p_rayon_'+tmp[i]+' > img').css('margin-right',11);
									}
								}
							});
}

function select_sub_rayon(id_a,id_b){
	$('#form_ajouter_cat > fieldset.f1 > ul > li').each(function (){
																							$(this).attr('class','');
																							$('#'+this.id+' > a').attr('class','');
																						});
	$('#form_ajouter_cat > fieldset.f3').each(function (){
																							$(this).hide();
																						});
	$('#form_ajouter_cat > fieldset.f2').each(function (){
																							$(this).hide();
																						});
	$('#lien_'+id_a+'_'+id_b).attr('class','cat_selected');
	$('#lien_'+id_a+'_'+id_b+' > a').attr('class','cat_selected');
	$('#niveau_'+id_a+'_'+id_b).show();
}

function select_ssub_rayon(id_a,id_b,id_c){
	$('#form_ajouter_cat > fieldset.f3').each(function (){
																							$(this).hide();
																						});
	$('#form_ajouter_cat > fieldset.f2 > ul > li').each(function (){
																							$(this).attr('class','');
																							$('#'+this.id+' > a').attr('class','');
																						});
	$('#lien_'+id_a+'_'+id_b+'_'+id_c).attr('class','cat_selected');
	$('#lien_'+id_a+'_'+id_b+'_'+id_c+' > a').attr('class','cat_selected');
	$('#niveau_'+id_a+'_'+id_b+'_'+id_c).show();
}

////////////////////////////////////////////////////
		var ids_rayons_open_rayon = null;
		
		function update_liste_ids_rayons(){
			jQuery.post("./ajax.php?action=update_liste_ids_rayons",
				function (data,status){
					var tmp = data.split('|');
					ids_rayons_open_rayon = tmp[1];
					open_rayon_check();
					update_display_open_rayon(tmp[0]);
				}
			);
		}
		
		function open_rayon_check(){
			var data = ids_rayons_open_rayon.split(';');
			for(var i=0;i<data.length;i++){
				if(document.getElementById('cb_'+data[i])){
					document.getElementById('cb_'+data[i]).checked = true;
				}
			}
			var l = Math.round(($(document).width() - 780) / 2);
			$('#open_shop_rayon').css('left',l);
			$('div.prettyPhotoOverlay').show();
			$('#open_shop_rayon').show();
			window.scrollTo(0,0);
		}
		
		function open_rayon(id_structure_parente,id_structure_main,name,cb){
			/*
			if($('#open_shop_rayon').html() == null){
				alert('merci de patienter pendant le chargement complet de la page...');
				return false;
			}
			*/
			jQuery.post("./ajax.php?action=get_open_shop_rayons&id="+id_structure_parente,
				function (data,status){
					$('body').append("<div class='prettyPhotoOverlay' style='display:none;'></div>");
					$('div.prettyPhotoOverlay').css('opacity',0.9);
					$('div.prettyPhotoOverlay').css('height',$(document).height());
					$('body').append(data);
					
					$('#form_ajouter_cat > fieldset.f1 > ul > li').each(function (){
																											$(this).attr('class','');
																											$('#'+this.id+' > a').attr('class','');
																										});
					$('#form_ajouter_cat > fieldset.f3').each(function (){
																											$(this).hide();
																										});
					$('#form_ajouter_cat > fieldset.f2').each(function (){
																											$(this).hide();
																										});
					$('#form_ajouter_cat > fieldset.f1').each(function (){
																											$(this).hide();
																										});
					update_liste_ids_rayons();
					$('#titre_main_cat').html($('#titre_main_cat_'+id_structure_parente).html());
					$('#niveau_'+id_structure_parente).show();
				}
			);
			return false;
		}
		
		function close_open_rayon(){
			$('div.prettyPhotoOverlay').remove();
			$('#open_shop_rayon').remove();
		}
//////////////////////////////////////////////////////////

function is_numeric(s){
    if(s.indexOf('.') > -1 || s.indexOf(',') > -1){
        return false;
    }
	if(s == ''){
		return false;
	}
	if(isNaN(s)){
		return false;
	}
	if(s < 0){
		return false;
	}
	return true;
}

function numbersonly(myfield, e, nb_char, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

if($(myfield).attr('value').length >= nb_char){
    return false;
}

// control keys
if ((key==null) || (key==0) || (key==8) ||
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}

function viewStep1(){
	$('#step1').click().show();
	$('#step2').click().hide();
	$('#step3').click().hide();
	document.getElementById('lien_droite_step1').setAttribute('class','active');
	document.getElementById('lien_droite_step2').setAttribute('class','noactive');
	document.getElementById('lien_droite_step3').setAttribute('class','noactive');
}

function viewStep2(){
	if(is_step_1_valid){
		$('#step1').click().hide();
		$('#step2').click().show();
		$('#step3').click().hide();
		document.getElementById('lien_droite_step1').setAttribute('class','noactive');
		document.getElementById('lien_droite_step2').setAttribute('class','active');
		document.getElementById('lien_droite_step3').setAttribute('class','noactive');
	}
}

function viewStep3(){
	if(is_step_2_valid){
		$('#step1').click().hide();
		$('#step2').click().hide();
		$('#step3').click().show();
		document.getElementById('lien_droite_step1').setAttribute('class','noactive');
		document.getElementById('lien_droite_step2').setAttribute('class','noactive');
		document.getElementById('lien_droite_step3').setAttribute('class','active');
	}
	for(var i=0;i<open_shop_step1.length;i++){
		if(clean(open_shop_step1_values[i]) == ''){
			document.getElementById('recap_'+open_shop_step1[i]).innerHTML = document.getElementById('empty').innerHTML;
		}
	}
	for(var i=0;i<open_shop_step2.length;i++){
		if(clean(open_shop_step2_values[i]) == ''){
			document.getElementById('recap_'+open_shop_step2[i]).innerHTML = document.getElementById('empty').innerHTML;
		}
	}
}

function copie_open_shop(from,to){
	if(!document.getElementById(from)){
		alert(from);
		return;
	}
	if(!document.getElementById(to)){
		alert(to);
		return;
	}
	var select = new Array('pan_id_devise','CA_mensuel_id_devise','delai_livraison','devise_insertion_prix','pays_clients','num_suivi','id_pays');
	if(!in_array(from,select)){
		document.getElementById(to).innerHTML = document.getElementById(from).value;
	}
	else{
		var o = document.getElementById(from);
		var txt = o.options[o.selectedIndex].text;
		document.getElementById(to).innerHTML = txt;
	}
}

function translate_text(id){
	var spliter = new Array('[b]','[/b]','[br]','[i]','[/i]','[u]','[/u]');
	var equivalents = new Array('<b>','</b>','<br/>','<i>','</i>','<u>','</u>');
	for(var x=0;x<spliter.length;x++){
		var data = $('#'+id).html().split(spliter[x]);
		var ret = '';
		for(var i=0;i<data.length;i++){
			ret += data[i] + equivalents[x];
		}
		$('#'+id).html(ret);
	}
}

var is_nom_boutique_valid = false;
var is_email_boutique_valid = false;

function open_shop_finalize(){
	var o;
	if(nb_rayons_selected == 0){
		alert(document.getElementById('erreur_rayons').innerHTML);
		viewStep1();
		return false;
	}
	
	
	if(document.getElementById('nom_boutique').value != ''){
		is_nom_boutique_valid = false;
		
		
		if(document.getElementById('nom_boutique').value.length > 12){
			alert(document.getElementById('erreur_boutique_max_car').innerHTML);
			viewStep1();
			document.getElementById('nom_boutique').focus();
			return false;
		}
		if(in_text(document.getElementById('nom_boutique').value,new Array('www.','http','.fr','.com','.net','.info','.eu','.org','.biz'))){
			alert(document.getElementById('erreur_boutique_format').innerHTML);
			viewStep1();
			document.getElementById('nom_boutique').focus();
			return false;
		}
		if(!c_url(replace_8(document.getElementById('nom_boutique').value+'.com'))){
			alert(document.getElementById('erreur_boutique_format').innerHTML);
			viewStep1();
			document.getElementById('nom_boutique').focus();
			return false;
		}
		
		if(window.XMLHttpRequest) // FIREFOX
	       xhr = new XMLHttpRequest();
	  else if(window.ActiveXObject) // IE
	       xhr = new ActiveXObject("Microsoft.XMLHTTP");
	  else
	       return(false);
	  
	  xhr.open("POST", "./ajax.php?action=open_shop_check_nom_boutique", false);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
		xhr.send("x=nom_boutique|"+clean(document.getElementById('nom_boutique').value));
		is_nom_boutique_valid = xhr.responseText == 'ok';

		
		if(!is_nom_boutique_valid){
			alert(document.getElementById('erreur_boutique_prise').innerHTML);
			viewStep1();
			document.getElementById('nom_boutique').focus();
			return false;
		}
if(document.getElementById('site').value != ''){
                xhr.open("POST", "./ajax.php?action=open_shop_check_url", false);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xhr.send("x=url|"+clean(document.getElementById('site').value));
		is_open_shop_check_url = xhr.responseText == 'ok';
}
else{
    is_open_shop_check_url = true;
}

		if(!is_open_shop_check_url){
			alert(document.getElementById('open_shop_check_url').innerHTML);
			viewStep1();
			document.getElementById('site').focus();
			return false;
		}


	}
	
	
	if(document.getElementById('email_boutique').value != ''){
		is_email_boutique_valid = false;
		
		if(window.XMLHttpRequest) // FIREFOX
	       xhr = new XMLHttpRequest();
	  else if(window.ActiveXObject) // IE
	       xhr = new ActiveXObject("Microsoft.XMLHTTP");
	  else
	       return(false);
	  
	  
		xhr.open("POST", "./ajax.php?action=open_shop_check_email_boutique", false);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
		xhr.send("x=email_boutique|"+clean(document.getElementById('email_boutique').value));
		is_email_boutique_valid = xhr.responseText == 'ok';
		
		if(!is_email_boutique_valid){
			alert(document.getElementById('erreur_email_prise').innerHTML);
			viewStep2();
			document.getElementById('email_boutique').focus();
			return false;
		}
	}
	
	return true;
}

function validStep1(){
	for(var i = 0;i<open_shop_step1.length;i++){
		if(in_array(open_shop_step1[i],new Array('num_suivi'))){
			if(document.getElementById('info_produits').style.display != 'none' && document.getElementById(open_shop_step1[i]).value == ''){
				alert(document.getElementById('mandatory').innerHTML);
				document.getElementById(open_shop_step1[i]).focus();
				return false;
			}
		}
		else{
			if(in_array(open_shop_step1[i],open_shop_step1_mandatory) && (document.getElementById(open_shop_step1[i]).value == '' || document.getElementById(open_shop_step1[i]).value == document.getElementById('txt_input_boutique').innerHTML)){
				alert(document.getElementById('mandatory').innerHTML);
				document.getElementById(open_shop_step1[i]).focus();
				return false;
			}
		}
		if($('#description').attr('value') == ''){
			alert(document.getElementById('mandatory').innerHTML);
			document.getElementById('description').focus();
			return false;
		}
		if(open_shop_step1[i] != '' && in_array(open_shop_step1[i],open_shop_number)){
            //document.getElementById(open_shop_step1[i]).value = str_replace(',','.',document.getElementById(open_shop_step1[i]).value);
            if(!is_numeric(document.getElementById(open_shop_step1[i]).value)){
                alert(document.getElementById('erreur_number').innerHTML);
                document.getElementById(open_shop_step1[i]).focus();
                return false;
            }
		}
		if(open_shop_step1[i] == 'description'){
			open_shop_step1_values[i] = $('#description').attr('value');
			$('#recap_'+open_shop_step1[i]).html(open_shop_step1_values[i]);
		}
		else{
                    open_shop_step1_values[i] = document.getElementById(open_shop_step1[i]).value;
                    copie_open_shop(open_shop_step1[i],'recap_'+open_shop_step1[i]);
		}
	}
	
	if(document.getElementById('site').value != ''){
			if(!c_url(document.getElementById('site').value)){
				alert(document.getElementById('erreur_url').innerHTML);
				document.getElementById('site').focus();
				return false;
			}
			$('#recap_site').html('http://'+$('#recap_site').html());
		}
	
	is_step_1_valid = open_shop_finalize();
	viewStep2();
	window.scrollTo(0,335);
	urchinTracker("/open_shop_step2");
}

function openshop_setDisplayByType(){
    if(document.getElementById('type_soc1').checked){
        $('.soc').each(function (){
           $(this).hide();
        });
        $('.socfr').each(function (){
           $(this).hide();
        });
        return;
    }
    $('.soc').each(function (){
       $(this).show();
    });
    if(document.getElementById('id_pays').value == 71 || document.getElementById('id_pays').value == 234){
        $('.socfr').each(function (){
           $(this).show();
        });
        return;
    }
    $('.socfr').each(function (){
       $(this).hide();
    });

}

function validStep2(){
	copieEtatOpenShop();
	for(var i = 0;i<open_shop_step2.length;i++){
		if(in_array(open_shop_step2[i],open_shop_step2_mandatory) && document.getElementById(open_shop_step2[i]).value == ''){
			alert(document.getElementById('mandatory').innerHTML);
			document.getElementById(open_shop_step2[i]).focus();
			return false;
		}
		if(open_shop_step2[i] == 'email_boutique'){
			if(!c_email(document.getElementById(open_shop_step2[i]).value)){
				alert(document.getElementById('erreur_email').innerHTML);
				document.getElementById(open_shop_step2[i]).focus();
				return false;
			}
		}
		if(in_array(open_shop_step2[i],new Array('telephone','fax')) && !c_tel(document.getElementById(open_shop_step2[i]).value)){
				alert(document.getElementById('erreur_format_tel').innerHTML);
				document.getElementById(open_shop_step2[i]).focus();
				return false;
		}
		if(open_shop_step2[i] == 'num_tva' && c_tva_intra(document.getElementById(open_shop_step2[i]).value) == false){
			alert(document.getElementById('erreur_num_tva').innerHTML);
			document.getElementById(open_shop_step2[i]).focus();
			return false;
		}
		if(open_shop_step2[i] == 'type'){
			if(document.getElementById('societe').style.display != 'none' && ($('#id_pays').attr('value') == 71 || $('#id_pays').attr('value') == 234) && (document.getElementById('societe').value == '' || document.getElementById('naf').value == '' || document.getElementById('siret').value == '' || document.getElementById('capital').value == '')){
                                alert(document.getElementById('mandatory').innerHTML);
                                if(document.getElementById('societe').value == '') document.getElementById('societe').focus();
                                if(document.getElementById('capital').value == '') document.getElementById('capital').focus();
                                if(document.getElementById('siret').value == '') document.getElementById('siret').focus();
                                if(document.getElementById('naf').value == '') document.getElementById('naf').focus();
				return false;
			}
                        if(document.getElementById('societe').style.display != 'none' && !($('#id_pays').attr('value') == 71 || $('#id_pays').attr('value') == 234) && (document.getElementById('societe').value == '')){
                                alert(document.getElementById('mandatory').innerHTML);
                                if(document.getElementById('societe').value == '') document.getElementById('societe').focus();
				return false;
			}
			if(document.getElementById('type_soc1').checked){
				open_shop_step2_values[i] = 'par';
				//copie_open_shop('type_soc1','recap_'+open_shop_step2[i]);
				//$('#reacap_div_societe').hide();
			}
			else{
				open_shop_step2_values[i] = 'soc';
				//copie_open_shop('type_soc2','recap_'+open_shop_step2[i]);
				//$('#reacap_div_societe').show();
			}
		}
		else{
			open_shop_step2_values[i] = document.getElementById(open_shop_step2[i]).value;
			copie_open_shop(open_shop_step2[i],'recap_'+open_shop_step2[i]);
		}
	}
	is_step_2_valid = open_shop_finalize();
	viewStep3();
	window.scrollTo(0,335);
	urchinTracker("/open_shop_step3");
}

function validStep3(){
	if(!document.getElementById('cga1').checked ||  !document.getElementById('cga2').checked){
		alert(document.getElementById('erreur_cga').innerHTML);
		return false;
}
	var data = "x=";
	if(!open_shop_finalize()){
		return false;
	}
	for(var i=0;i<open_shop_step1.length;i++){
		data += open_shop_step1[i]+"|"+clean(open_shop_step1_values[i])+"|";
	}
	for(var i=0;i<open_shop_step2.length;i++){
		data += open_shop_step2[i]+"|"+clean(open_shop_step2_values[i])+"|";
	}
	
	$('#conteneur').hide();
	$('#transition_open_shop').show();
	window.scrollTo(0,0);
	
	if(window.XMLHttpRequest) // FIREFOX
       xhr = new XMLHttpRequest();
  else if(window.ActiveXObject) // IE
       xhr = new ActiveXObject("Microsoft.XMLHTTP");
  else
       return(false);
  
  
	xhr.open("POST", "./ajax.php?action=open_shop_subscribe", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
	xhr.send(data);
	xhr.onreadystatechange = function(){
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200){
			if(xhr.responseText.indexOf('http://') == -1){
				alert('Une erreur est survenue lors de la création du compte boutique, merci de contacter l\'administrateur de Mercaway.'+xhr.responseText);
				return false;
			}
			else{
                urchinTracker("/open_shop_step4");
				document.location.href = xhr.responseText;
			}
		}
	}
	
	return true;
}

function login(){
	var email,pw;
	email = pw = '';
	email = document.getElementById('_email').value;
	pw = document.getElementById('_pw').value;
	if(email == '' || pw == ''){
		alert("Vous devez renseigner les deux champs");
		return false;
	}
	
	var data = "x=email|"+clean(email)+"|pw|"+clean(pw);
	check_if_logged(data);
	return false;
}

function hide(a){
	for(var i = 0;i < a.length;i++){
		if(document.getElementById(a[i])){
			$('#'+a[i]).hide();
		}
	}
}

function show(a){
	for(var i = 0;i < a.length;i++){
		if(document.getElementById(a[i])){
			$('#'+a[i]).show();
		}
	}
}

function copy(a,b){
	if(document.getElementById(a) && document.getElementById(b)){
		$('#'+b).html($('#'+a).html());
	}
	else{
		if(document.getElementById(b)){
			$('#'+b).html(a);
		}
	}
}

function popup(page,largeur,hauteur){
	var top=(screen.height-hauteur)/2;
	var left=(screen.width-largeur)/2;
	var options = "menubar=no,scrollbars=no,statusbar=no";
	window.open(page,"","top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options);
}

function switcher(id){
	if(document.getElementById(id).style.display == 'none'){
		show(new Array(id));
	}
	else{
		hide(new Array(id));
	}
}

function switcher2(id_a,id_b){
	show(new Array(id_b));
	hide(new Array(id_a));
}



function switch_pays(){
	if(document.getElementById('blocpays').style.display == 'none'){
		show(new Array('blocpays'));
		hide(new Array('blocrechercheproduit'));
	}
	else{
		hide(new Array('blocpays'));
		show(new Array('blocrechercheproduit'));
	}
}

function selectPays(id_pays){
	alert("ID du pays sélectionné: "+id_pays);
}

function ouvrirFAQ(id_liste,lien){
	if($('#allows_all_open').html() == 'non'){
		if($(lien).attr('class') == 'active'){
			return false;
		}
		for(var i = 1;i<21;i++){
			$('#toogler_'+i).next().hide('fast');
			$('#toogler_'+i+" > a").attr('class','');
		}
	}
	$("#"+id_liste).next().toggle("fast");
	if($('#allows_all_open').html() == 'non'){
		$('#'+id_liste+"_1 > a").click();
	}
	if($(lien).attr('class') == null || $(lien).attr('class') == '' ){
		$(lien).attr('class','active');
	}
	else{
		$(lien).attr('class','');
	}
}

function get_pw(){
	var email;
	if(arguments.length > 0){
		email = arguments[0];
	}
	else{
		email = document.getElementById('_email2').value;
	}
	if(window.XMLHttpRequest) // FIREFOX
       xhr = new XMLHttpRequest();
  else if(window.ActiveXObject) // IE
       xhr = new ActiveXObject("Microsoft.XMLHTTP");
  else
       return(false);
  
  var data = "email="+email;
	xhr.open("POST", "./ajax.php?action=forgot_pw", false);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
	xhr.send(data);
	if(arguments.length > 0){
		$('#c_forgot').click().hide();$('#c_login').click().show();
		$('#b_forgot').click().hide();$('#b_login').click().show();
	}
	else{
		forgot_pw_back();
	}
	
}

function forgot_pw(){
	hide(new Array('nologged'));
	show(new Array('nologged2'));
}

function forgot_pw_back(){
	show(new Array('nologged'));
	hide(new Array('nologged2'));
}

var last_titre_selected = false;

function lireFAQ(id,titre,avec_formulaire){
	if(id.indexOf('reponse_a') > -1){
		if($('#allows_all_open').html() == 'non'){
			for(var i = 1;i<21;i++){
				$('#toogler_'+i).next().hide('fast');
				$('#toogler_'+i+" > a").attr('class','');
			}
		}
	}

	if(avec_formulaire == 'oui'){
		show(new Array('questioncontact'));
	}
	else{
		hide(new Array('questioncontact'));
	}
	hide(new Array('formquestion','faqvide'));
	
	if(last_titre_selected != false){
		$(last_titre_selected).attr('class','');
	}
	$(titre).attr('class','active2');
	
	last_titre_selected = titre;
	
	copy(id,'reponse_content');
	copy(titre.innerHTML,'reponse_titre');
}

function switchTxt(obj,action,txt){
	if(action == 'hide' && obj.value == txt){
		obj.value = '';
	}
	if(action == 'show' && obj.value == ''){
		obj.value = txt;
	}
}

function validate_create_account(){
    if(!c_pw($('#pw').attr('value'))){
        alert($('#format_pw').html());
        return false;
    }
	return true;
}

function sendMsgFaq(){
	var nom,email,message,cc;
	nom = email = message = '';
	nom = document.getElementById('nom').value;
	email = document.getElementById('email').value;
	message = document.getElementById('message').value;
	cc = document.getElementById('cc').checked ? 'oui' : 'non';
	if(nom == '' || email == '' || message == ''){
		alert(document.getElementById('form_erreur').innerHTML);
		return false;
	}
	if(!c_email(email)){
		alert(document.getElementById('form_erreur_email').innerHTML);
		return false;
	}
	var data = "x=nom|"+clean(nom)+"|email|"+email+"|cc|"+cc+"|message|"+clean(message);

	if(window.XMLHttpRequest) // FIREFOX
       xhr = new XMLHttpRequest();
  else if(window.ActiveXObject) // IE
       xhr = new ActiveXObject("Microsoft.XMLHTTP");
  else
       return(false);
  
	xhr.open("POST", "./ajax.php?action=sendMsgFaq", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
	xhr.send(data);
	xhr.onreadystatechange = function(){
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200){
			if(xhr.responseText == 'ok'){
				alert(document.getElementById('form_envoye').innerHTML);
			}
			else{
				alert(document.getElementById('form_pas_envoye').innerHTML);
			}
		}
	}
}



















var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
is_safari = BrowserDetect.browser == 'Safari';
