sas_tmstp=Math.round(Math.random()*10000000000);sas_masterflag=1;

function SmartAdServer(sas_pageid,sas_formatid,sas_target) {
 if (sas_masterflag==1) {sas_masterflag=0;sas_master='M';} else {sas_master='S';};
 document.write('<SCR'+'IPT SRC="http://www.smartadserver.com/call/pubj/' + sas_pageid + '/' + sas_formatid + '/'+sas_master + '/' + sas_tmstp + '/' + escape(sas_target) + '?"></SCR'+'IPT>');
}



function urlRewriting(){
	$('a').each(function (){
		var u = $(this).attr('href');
		if(u != '#' && u != null){
			var id_menu = parseInt(u.substring(u.indexOf('id_menu=')+8,u.indexOf('id_menu=')+8+5));
			var id_produit = parseInt(u.substring(u.indexOf('id_produit=')+11,u.indexOf('id_produit=')+11+5));
			var id_marque = parseInt(u.substring(u.indexOf('i=')+2,u.indexOf('i=')+2+5));
			var page = parseInt(u.substring(u.indexOf('p=')+2,u.indexOf('p=')+2+5));
			var id_gabarit = parseInt(u.substring(u.indexOf('id_gabarit=')+11,u.indexOf('id_gabarit=')+11+5));
			if(!isNaN(id_menu) && id_menu > 0){
				if($(this).attr('title') == 'none'){
					$(this).attr('title','');
				}
				else{
					var fu = $(this).text();
					if($(this).attr('title') != ''){
						fu = $(this).attr('title');
					}
					new_u = formatURL(fu)+'-'+id_menu;
					if(id_gabarit == 31){
						new_u += ','+id_produit;
					}
					else if(id_gabarit == 37){
						new_u += ','+id_marque;
					}
					if(page > 0){
						new_u += ','+page;
					}
					new_u += '.html';
					$(this).attr('href',new_u);
				}
			}
		}
	});
}


function replaceTxt(entry,out,add) {
	temp = "" + entry; // temporary holder
	
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}


function formatURL(u,v,a){
	// Supprime les espaces inutiles en début et fin de la chaîne passée en paramètre.
	var txt = u.replace(/^\s+|\s+$/g,"");

	var from = new Array(' ','é','?','>','è','(',')','ê',"'");
	var to = new Array('_','e','','','e','','','e','-');
	for(var i=0;i<from.length;i++){
		txt = replaceTxt(txt,from[i],to[i]);
	}
	return txt;
}






function newsletter_unsubscribe(){
	if(!c_email($('#unsub_email').attr('value'))){
		$('#confirmation_ko').html('Vérifiez votre adresse email.');
		$('#confirmation_ko').show();
		$('#confirmation_ok').hide();
		return false;
	}
	jQuery.post('./ajax.php?action=newsletter_unsubscribe','email='+$('#unsub_email').attr('value'),function (data,status){
		if(data == 'ok'){
			$('#confirmation_ko').hide();
			$('#confirmation_ok').html('Désinscription réussie.');
			$('#confirmation_ok').show();
		}
		else{
			$('#confirmation_ko').html('Cette adresse email n\'est pas inscrite à la newsletter.');
			$('#confirmation_ko').show();
			$('#confirmation_ok').hide();
		}
	});
}

function newsletter_subscribe(obj_email,obj_groupe,obj_message_ok,obj_message_ko){
	if(!c_email($('#'+obj_email).attr('value'))){
		$('#'+obj_message_ko).html('Vérifiez votre adresse email.');
		return false;
	}
	if(document.getElementById(obj_groupe).value == ''){
		$('#'+obj_message_ko).html('Choisissez un groupe.');
		return false;
	}
	var donnees = 'email='+$('#'+obj_email).attr('value')+'&groupe='+document.getElementById(obj_groupe).value;
	jQuery.post('./ajax.php?action=newsletter_subscribe',donnees,
							function (data,status){
								if(data == 'ok'){
									$('p.display_newsletter').hide();
									$('#bouton_newsletter').hide();
									$('#'+obj_email).hide();
									$('#'+obj_groupe).hide();
									$('#'+obj_message_ko).html('');
									$('#'+obj_message_ok).html('Vous avez bien été inscrit.');
								}
								else if(data == 'ko'){
									$('#'+obj_message_ok).html('');
									$('#'+obj_message_ko).html('Vous êtes déjà inscrit.');
								}
								else{
									alert(data);
								}
							});
	
	
}

function c_email(email){
	var verif = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]{1,}[.][a-zA-Z]{2,5}$/
	if (verif.exec(email) == null){
		return false;
	}
	else{
		return true;
	}
}

function switchTxt(obj,action,txt){
	if(action == 'hide' && obj.value == txt){
		obj.value = '';
	}
	if(action == 'show' && obj.value == ''){
		obj.value = txt;
	}
}

function addToCart(id_produit,obj){
	var donnees = 'id_produit='+id_produit;
	jQuery.post('./ajax.php?action=addToCart',donnees,
							function (data,status){
								$('#qte_cart').html(data);
								if(obj != null){
									$(obj).hide();
									$('#addToCartOk_'+id_produit).show();
								}
							});
}

function removeFromCart(id_produit,obj){
	var donnees = 'id_produit='+id_produit;
	jQuery.post('./ajax.php?action=removeFromCart',donnees,
							function (data,status){
								$('#qte_cart').html(data);
								if(obj != null){
									$(obj).hide();
									$('#removeFromCartOk_'+id_produit).show();
								}
								if(document.location.href.indexOf('id_menu=160&id_gabarit=38') > -1){
									document.location.href = '/front/index.php?id_menu=160&id_gabarit=38';
								}
							});
}

function setValueSelect(obj,val){
	var o = document.getElementById(obj).options;
	for(var i=0;i<o.length;i++){
		if(o[i].value == val){
			o[i].selected = true;
			return;
		}
	}
}

function updateSEForm(){
	jQuery.post('./ajax.php?action=updateSEForm',
							function (data,status){
								data = data.split('|');
								setValueSelect('SEid_structure',data[1]);
								updateSEFamille();
								setValueSelect('SEid_utilisateur',data[0]);
								setValueSelect('SEiam',data[2]);
								if(data[3] == ''){
									data[3] = 'Tapez un mot clé...';
								}
								$('#SEkeyword').attr('value',data[3]);
							});
}

function updateSEFamille(){
	jQuery.post('./ajax.php?action=updateSEFamille',"id_structure="+document.getElementById('SEid_structure').value,
							function (data,status){
								$('#SEfamille').html(data);
							});
}

function SEselect(k,obj){
	var donnees = '';
	var kw = $('#SEkeyword').val();
	if(kw.indexOf('Tapez un mot clé...') > -1){
		kw = '';
	}
	if(k == 'keyword'){
		donnees = 'key='+k+'&val='+kw;
	}
	else{
		donnees = 'key='+k+'&val='+obj.value;
	}
	jQuery.post('./ajax.php?action=SEsetSelection',donnees,
							function (data,status){
								if(k == 'keyword'){
									document.location.href = '/front/index.php?id_menu=160&id_gabarit=32&fig_id_menu=141';
								}
							});
}

var has_clic = false;

function SEsetMarque(id_utilisateur){
	if(!has_clic){
		has_clic = true;
		var	donnees = 'key=id_utilisateur&val='+id_utilisateur;
		jQuery.post('./ajax.php?action=SEsetMarque',donnees,
								function (data,status){
									document.location.href = '/front/index.php?id_menu=160&id_gabarit=32&fig_id_menu=141';
								});
	}
	else{
		alert("Merci de patienter pendant le chargement de la page.");
	}
}

function SEsetTarget(t){
	if(!has_clic){
		has_clic = true;
		var	donnees = 'key=iam&val='+t;
		jQuery.post('./ajax.php?action=SEsetSelection',donnees,
								function (data,status){
									document.location.href = '/front/index.php?id_menu=160&id_gabarit=32&fig_id_menu=141';
								});
	}
	else{
		alert("Merci de patienter pendant le chargement de la page.");
	}
}

function filtrerProduits(){
	var donnees = '';
	donnees += "conso="+(document.getElementById('conso').checked ? 'oui' : 'non');
	donnees += "&distri="+(document.getElementById('distri').checked ? 'oui' : 'non');
	donnees += "&coll="+(document.getElementById('coll').checked ? 'oui' : 'non');
	donnees += "&bio="+(document.getElementById('bio').checked ? 'oui' : 'non');
	donnees += "&recycle="+(document.getElementById('recycle').checked ? 'oui' : 'non');
	donnees += "&certif_qualite="+document.getElementById('certif_qualite').value;
	jQuery.post('./ajax.php?action=filtrerProduits',donnees,
							function (data,status){
								document.location.href = document.location.href;
							});
}

function filtrerChecker(){
	if($('#toCheck').html() != null && $('#toCheck').html() != ''){
		var ids = $('#toCheck').html().split('|');
		for(var i=0;i<ids.length;i++){
			$('#'+ids[i]).attr('checked',true);
		}
	}
}

function filtrerPDV(){
	var donnees = '';
	donnees += "localite="+(document.getElementById('filtre_localite').value);
	donnees += "&marque="+(document.getElementById('filtre_marque').value);
	donnees += "&univers="+(document.getElementById('filtre_univers').value);
	donnees += "&type="+(document.getElementById('filtre_type').value);
	jQuery.post('./ajax.php?action=filtrerPDV',donnees,
							function (data,status){
								document.location.href = document.location.href;
							});
}

function setMarquePDV(id){
	jQuery.post('./ajax.php?action=filtrerPDV','marque='+id,
							function (data,status){
								document.location.href = '/front/index.php?id_menu=143&id_gabarit=33';
							});
}

function filtrerPDVChecker(){
	if($('#toCheck').html() != null && $('#toCheck').html() != ''){
		var ids = $('#toCheck').html().split('|');
		setValueSelect('filtre_localite',ids[0]);
		setValueSelect('filtre_marque',ids[1]);
		setValueSelect('filtre_univers',ids[2]);
		setValueSelect('filtre_type',ids[3]);
	}
}

function login(){
	var email = $('#client_login').attr('value');
	var pw = $('#client_pw').attr('value');
	if(email == ''){
		$('#err_client_login').show();
	}
	else{
		$('#err_client_login').hide();
	}
	if(pw == ''){
		$('#err_client_pw').show();
	}
	else{
		$('#err_client_pw').hide();
	}
	if(email == '' || pw == ''){
		return false;
	}
	jQuery.post('./ajax.php?action=login','email='+email+'&pw='+pw,function (data,status){
		if(data == 'true'){
			document.location.href = document.location.href;
		}
		else{
			$('#err_client_ident').show();
		}
	});
}


function send_forgot_pw(){
	var email = $('#forgot_login').attr('value');
	if(email == ''){
		$('#err_forgot_login').show();
		return false;
	}
	else{
		$('#err_forgot_login').hide();
	}
	jQuery.post('./ajax.php?action=send_forgot_pw','email='+email,function (data,status){
		$('#err_forgot_ident').show();
	});
}

function show_forgot_pw(){
	$('#div_login').hide();
	$('#div_forgotpw').show();
}

function show_login(){
	$('#div_forgotpw').hide();
	$('#div_login').show();
}

function client_subscribe(){
	var email = $('#subscribe_email').attr('value');
	var pw = $('#subscribe_pw').attr('value');
	var pw2 = $('#subscribe_pw2').attr('value');
	var profile = document.getElementById('subscribe_profile').value;
	
	if(email == '' || pw == '' || pw2 == '' || profile == ''){
		$('#err_subscribe').show();
		return false;
	}
	else{
		$('#err_subscribe').hide();
	}
	
	if(!c_email(email)){
		$('#err_subscribe_email').show();
		return false;
	}
	else{
		$('#err_subscribe_email').hide();
	}
	
	if(pw != pw2){
		$('#err_subscribe_pw').show();
		return false;
	}
	else{
		$('#err_subscribe_pw').hide();
	}
	
	jQuery.post('./ajax.php?action=email_client_exists','email='+email,function (data,status){
		if(data == 'ko'){
			$('#err_subscribe_exists').show();
		}
		else{
			jQuery.post('./ajax.php?action=client_subscribe','email='+email+'&pw='+pw+'&type='+profile,function (data,status){
				document.location.href = document.location.href;
			});
		}
	});
}