function showElem(elem){
	var idHtml = '#'+elem;
	$(idHtml).css('display','block');
}

function hideElem(elem){
	var idHtml = '#'+elem;
	$(idHtml).css('display','none');
}

function checkStatus() {
	if($('#cgv').attr('checked')==false) {
		alert('Vous devez accepter les conditions générales de vente');	
	}
	else {
		$("#valider").attr('href','coordonnees.html');
	}	
}

function checkLivraison(nbLivraison) {
	var check = true;
	for(var i=0;i<nbLivraison;i++) {
		if($('#mode_livraison-' + i).attr('checked') == false) {
			check = false;
		}
		else {
			check = true;
			break;
		}
	}
	if(check==false) {
		alert('Vous devez choisir un mode de livraison');	
	}
	else {
		$("#livraison").attr('action','recapitulatif.html');
	}	
}

function sameLiv() {
	if($('#identique').attr('checked')==true){
		$('#nom_liv').val($('#nom').val());
		$('#prenom_liv').val($('#prenom').val());
		//$('#email_liv').val($('#email').val());
		$('#adresse_liv').val($('#adresse').val());
		$('#code_postal_liv').val($('#code_postal').val());
		$('#ville_liv').val($('#ville').val());
		$('#pays_liv').val($('#pays').val());
		$('#telephone_liv').val($('#telephone').val());
		$('#mobile_liv').val($('#mobile').val());
		$('#fax_liv').val($('#fax').val());
	}
	else {
		$('#nom_liv').val('');
		$('#prenom_liv').val('');
		$('#email_liv').val('');
		$('#adresse_liv').val('');
		$('#code_postal_liv').val('');
		$('#ville_liv').val('');
		$('#pays_liv').val('');
		$('#telephone_liv').val('');
		$('#mobile_liv').val('');
		$('#fax_liv').val('');	
	}
}

function checkForm() {
	var error = 0;
	if($('#nom').val()=='') {
		$('#nom').css('background-color','#ffb3b1');
		$('#nom').css('display','none');
		$('#nom').fadeIn('slow', function() {
	   	});
		error = 1;	
	}
	else {
		$('#nom').css('background-color','#ffffff');
	}
	
	if($('#nom_liv').val()=='') {
		$('#nom_liv').css('background-color','#ffb3b1');
		$('#nom_liv').css('display','none');
		$('#nom_liv').fadeIn('slow', function() {
	   	});
		error = 1;	
	}
	else {
		$('#nom_liv').css('background-color','#ffffff');
	}
	
	if($('#prenom').val()=='') {
		$('#prenom').css('background-color','#ffb3b1');
		$('#prenom').css('display','none');
		$('#prenom').fadeIn('slow', function() {		
	   	});
		error = 1;
	}
	else {
		$('#prenom').css('background-color','#ffffff');	
	}
	
	if($('#prenom_liv').val()=='') {
		$('#prenom_liv').css('background-color','#ffb3b1');
		$('#prenom_liv').css('display','none');
		$('#prenom_liv').fadeIn('slow', function() {		
	   	});
		error = 1;
	}
	else {
		$('#prenom_liv').css('background-color','#ffffff');	
	}
	
	if($('#email').val()=='') {
		$('#email').css('background-color','#ffb3b1');
		$('#email').css('display','none');
		$('#email').fadeIn('slow', function() {
	   	});
		error = 1;	
	}
	else {
		$('#email').css('background-color','#ffffff');
	}
	
	if($('#email_liv').val()=='') {
		$('#email_liv').css('background-color','#ffb3b1');
		$('#email_liv').css('display','none');
		$('#email_liv').fadeIn('slow', function() {
	   	});
		error = 1;	
	}
	else {
		$('#email_liv').css('background-color','#ffffff');
	}
	
	if($('#adresse').val()=='') {
		$('#adresse').css('background-color','#ffb3b1');
		$('#adresse').css('display','none');
		$('#adresse').fadeIn('slow', function() {	
	   	});
		error = 1;
	}
	else {
		$('#adresse').css('background-color','#ffffff');
	}
	
	if($('#adresse_liv').val()=='') {
		$('#adresse_liv').css('background-color','#ffb3b1');
		$('#adresse_liv').css('display','none');
		$('#adresse_liv').fadeIn('slow', function() {	
	   	});
		error = 1;
	}
	else {
		$('#adresse_liv').css('background-color','#ffffff');
	}
	
	if($('#code_postal').val()=='') {
		$('#code_postal').css('background-color','#ffb3b1');
		$('#code_postal').css('display','none');
		$('#code_postal').fadeIn('slow', function() {	
	   	});
		error = 1;
	}
	else {
		$('#code_postal').css('background-color','#ffffff');
	}
	
	if($('#code_postal_liv').val()=='') {
		$('#code_postal_liv').css('background-color','#ffb3b1');
		$('#code_postal_liv').css('display','none');
		$('#code_postal_liv').fadeIn('slow', function() {	
	   	});
		error = 1;
	}
	else {
		$('#code_postal_liv').css('background-color','#ffffff');
	}
	
	if($('#ville').val()=='') {
		$('#ville').css('background-color','#ffb3b1');
		$('#ville').css('display','none');
		$('#ville').fadeIn('slow', function() {	
	   	});
		error = 1;
	}
	else {
		$('#ville').css('background-color','#ffffff');
	}
	
	if($('#ville_liv').val()=='') {
		$('#ville_liv').css('background-color','#ffb3b1');
		$('#ville_liv').css('display','none');
		$('#ville_liv').fadeIn('slow', function() {	
	   	});
		error = 1;
	}
	else {
		$('#ville_liv').css('background-color','#ffffff');
	}
	
	if($('#pays').val()=='') {
		$('#pays').css('background-color','#ffb3b1');
		$('#pays').css('display','none');
		$('#pays').fadeIn('slow', function() {	
	   	});
		error = 1;
	}
	else {
		$('#pays').css('background-color','#ffffff');
	}
	
	if($('#pays_liv').val()=='') {
		$('#pays_liv').css('background-color','#ffb3b1');
		$('#pays_liv').css('display','none');
		$('#pays_liv').fadeIn('slow', function() {	
	   	});
		error = 1;
	}
	else {
		$('#pays_liv').css('background-color','#ffffff');
	}
	
	if($('#telephone').val()=='') {
		$('#telephone').css('background-color','#ffb3b1');
		$('#telephone').css('display','none');
		$('#telephone').fadeIn('slow', function() {	
	   	});
		error = 1;
	}
	else {
		$('#telephone').css('background-color','#ffffff');
	}
	
	if($('#telephone_liv').val()=='') {
		$('#telephone_liv').css('background-color','#ffb3b1');
		$('#telephone_liv').css('display','none');
		$('#telephone_liv').fadeIn('slow', function() {	
	   	});
		error = 1;
	}
	else {
		$('#telephone_liv').css('background-color','#ffffff');
	}
	
	if(error == 0) { 
		return true;
    }
	else {
		return false;
	}
}

function modifPanier(code) {
	var html = $.ajax({
		type: "POST",
		url: "changeNbArticles.php",
		async: false,
		data: "code="+code}).responseText;
}

function supPanier(nbArticle) {
	var nb = 0;
	for(var i=1;i<=nbArticle;i++) {
		if($("#checkbox-"+i).attr('checked')==false) {
			nb++; 
		}
	}
	return nb;
}

function quantite(code) {
	$(".fic_produit").submit(function(){
		modifPanier(code);
	});
	$(".fic_produit").attr("action","panier.html");
}

function valide() {
	var ok = 1;
	if($('#nom').val()=='') {
		ok = 0;
		alert('Vous devez indiquer votre nom');
		$('#nom').focus();
		return false;
	}
	if($('#prenom').val()=='') {
		ok = 0;
		alert('Vous devez indiquer votre prénom');
		$('#prenom').focus();
		return false;
	}
	if($('#telephone').val()=='') {
		ok = 0;
		alert('Vous devez indiquer votre n° de téléphone');
		$('#telephone').focus();
		return false;
	}
	if($('#e_mail').val()=='') {
		ok = 0;
		alert('Vous devez indiquer votre adresse email');
		$('#e_mail').focus();
		return false;
	}
	else {
		ok = testMail();
		if(ok==0) {
			return false;
		}
	}
	if($('#produit').val()=='') {
		ok = 0;
		alert('Vous devez indiquer votre produit');
		$('#produit').focus();
		return false;
	}
}

function testMail() {
	var ok=1;
	if($('#e_mail').val()=='') {
		ok = 0;
		alert('Vous devez saisir votre adresse email');
		$('#e_mail').focus();
		return ok;
	}
	else {
		var m = $('#e_mail').val().split("@");
		if(m.length<2) {
			ok = 0;
		}
		else {
			var s = m[1];
			var m1 = s.split(".");
			if(m1.length<2) {
				ok = 0;
			}
			else {
				if(m[0].length<1) {
					ok = 0;
				}
				if(m1[0].length<1) {
					ok = 0;
				}
				if(m1[1].length<1) {
					ok = 0;
				}
			}
		}
		if(ok==0) {
			alert('l\'adresse email n\'est pas correcte');
			$('#e_mail').focus();
			return ok;
		}
	}
	if(ok==1) {
		return ok;
	}	
}








function afficherPanier(){
	var quantite = 0;
	var montant = 0;
	var monpanier = getCookieInfo('panier');
	articles=monpanier.split('||');
	longueur=articles.length;
	for(i=0;i<longueur;i++) {
		nbarticles=articles[i].split('<>');
		quantite = parseInt(quantite)+parseInt(nbarticles[3]);
		montant = parseFloat(montant) + (parseFloat(nbarticles[2])*parseInt(nbarticles[3]));
	}
	
	document.write('<div id="resumePanier">');
	if (!isNaN(quantite) && parseInt(quantite)!=0){
		document.write('<table border="0" cellspacing="0" cellpadding="0"><tr><td><b><a class="titrePanier" href="?r=panier">' + quantite + ' article(s)</b></td>');
		document.write('<td style="padding:0 0 0 15px;"><b>' + Math.round(montant*100) / 100 + '€</a></b></td></tr></table>');
	}else{
		document.write('<b>Votre panier est vide</b>');
	}
	document.write('</div>');
}

function panier_changeDiv50(){
	var browser = checkNavigateur();
	var quantite = 0;
	var montant = 0;
	var monpanier = getCookieInfo('panier');
	articles=monpanier.split('||');
	longueur=articles.length;
	for(i=0;i<longueur;i++) {
		nbarticles=articles[i].split('<>');
		quantite = parseInt(quantite)+parseInt(nbarticles[3]);
		montant = parseFloat(montant) + (parseFloat(nbarticles[2])*parseInt(nbarticles[3]));
	}
	
	if(window.opener){
		if(window.opener.document.getElementById('resumePanier')){
			if (!isNaN(quantite) && parseInt(quantite)!=0){
				window.opener.document.getElementById('resumePanier').innerHTML ='<table border="0" cellspacing="0" cellpadding="0"><tr><td><b><a class="titrePanier" href="?r=panier">' + quantite + ' article(s)</b></td><td style="padding:0 0 0 15px;">' + Math.round(montant*100) / 100 + '€</a></b></td></tr></table>';
			}else{
				window.opener.document.getElementById('resumePanier').innerHTML ='<b>Votre panier est vide</b>';
			}
		}
	}else{
		
		if(document.getElementById('resumePanier')){
			if (!isNaN(quantite) && parseInt(quantite)!=0){
				document.getElementById('resumePanier').innerHTML ='<table border="0" cellspacing="0" cellpadding="0"><tr><td><b><a class="titrePanier" href="?r=panier">' + quantite + ' article(s)</b></td><td style="padding:0 0 0 15px;">' + Math.round(montant*100) / 100 + '€</a></b></td></tr></table>';
			}else{
				document.getElementById('resumePanier').innerHTML ='<b>Votre panier est vide</b>';
			}
		}
	}
}

function panier_changeDiv51(){
	var browser = checkNavigateur();
	var quantite = 0;
	var montant = 0;
	var monpanier = getCookieInfo('panier');
	articles=monpanier.split('||');
	longueur=articles.length;
	for(i=0;i<longueur;i++) {
		nbarticles=articles[i].split('<>');
		quantite = parseInt(quantite)+parseInt(nbarticles[3]);
		montant = parseFloat(montant) + (parseFloat(nbarticles[2])*parseInt(nbarticles[3]));
	}
	if(document.getElementById('resumePanier')){
		if (!isNaN(quantite) && parseInt(quantite)!=0){
			document.getElementById('resumePanier').innerHTML ='<table border="0" cellspacing="0" cellpadding="0"><tr><td><b><a class="titrePanier" href="?r=panier">' + quantite + ' article(s)</b></td><td style="padding:0 0 0 15px;">' + Math.round(montant*100) / 100 + '€</a></b></td></tr></table>';
		}else{
			document.getElementById('resumePanier').innerHTML ='<b>Votre panier est vide</b>';
		}
	}
}

function ajouterPanier(ref,libelle,prix,quantite){
	var chaine = ref+'<>'+libelle+'<>'+prix+'<>'+quantite;
	poserDsCookie("panier",chaine,"",'/',"");
}

function poserDsCookie( nomPanier, valeurPanier, expiration, chemin, domaine) 
{
	var strTmp = String(document.location);
	var strTmp2 = strTmp.substring(7,strTmp.length);
	var strTmp3 = strTmp2.split("/");
	if(strTmp3[0] == 'central'){
		var root = 'http://'+strTmp3[0]+'/'+strTmp3[1];
	}else{
		var root = '';
	}
	
	var ligneArt = valeurPanier.split('<>');
	var form = document.getElementById('ajouter_panier');
	
	//On recupere la reference du produit à insérer
	var refAjout = ligneArt[0] + ligneArt[1];
	var refart = '';
	var donnecookiePanier = getCookieInfo('panier');
	if(donnecookiePanier!='false'){
	
	//On verifie que le produit n'existe pas deja...
		var string = donnecookiePanier;
		if(string!='false'){
			var lignearticle = string.split('||');
			var i = 0;
			while(i<lignearticle.length && refart != refAjout){
				article = lignearticle[i];
				article = article.split('<>');
				refart = article[0]+article[1];
				i++;
			}
			if (refart == refAjout){
				valeurPanier = string;
					alert('Ce produit est dejà présent dans le panier');
					document.cookie = "refererurl=" +document.location.href+";path=/";
					url=root + '/service/panier.php';
					nom = 'fencentarticle';
					largeur = 770;
					hauteur = 450;
					options = 'toolbar=no,resizable=no,status=no,scrollbars=yes'
					var haut=(screen.height-hauteur)/2;
					var Gauche=(screen.width-largeur)/2;
					fencentarticle=window.open(url,nom,"top="+haut+",left="+Gauche+",width="+largeur+",height="+hauteur+","+options);
			}else{
				valeurPanier += '||' + string;
				document.cookie = nomPanier + "=" +valeurPanier+";path=/";
				//window.location.reload();
				alert('Ce produit a été ajouté au panier');
				document.cookie = "refererurl=" +document.location.href+";path=/";
				panier_changeDiv50();
				panier_changeDiv51();
				url=root + '/service/panier.php';
					nom = 'fencentarticle';
					largeur = 770;
					hauteur = 450;
					options = 'toolbar=no,resizable=no,status=no,scrollbars=yes'
					var haut=(screen.height-hauteur)/2;
					var Gauche=(screen.width-largeur)/2;
					fencentarticle=window.open(url,nom,"top="+haut+",left="+Gauche+",width="+largeur+",height="+hauteur+","+options);
			}	
		}else{
			document.cookie = nomPanier + "=" +valeurPanier+";path=/";
			//window.location.reload();
			alert('Ce produit a été ajouté au panier');
			document.cookie = "refererurl=" +document.location.href+";path=/";
			panier_changeDiv50();
			panier_changeDiv51();
			url=root + '/service/panier.php';
					nom = 'fencentarticle';
					largeur = 770;
					hauteur = 450;
					options = 'toolbar=no,resizable=no,status=no,scrollbars=yes'
					var haut=(screen.height-hauteur)/2;
					var Gauche=(screen.width-largeur)/2;
					fencentarticle=window.open(url,nom,"top="+haut+",left="+Gauche+",width="+largeur+",height="+hauteur+","+options);
		}
	}else{
		document.cookie = nomPanier + "=" +valeurPanier+";path=/";
			//window.location.reload();
			alert('Ce produit a été ajouté au panier');
			document.cookie = "refererurl=" +document.location.href+";path=/";
			panier_changeDiv50();
			panier_changeDiv51();
			url=root + '/service/panier.php';
					nom = 'fencentarticle';
					largeur = 770;
					hauteur = 450;
					options = 'toolbar=no,resizable=no,status=no,scrollbars=yes'
					var haut=(screen.height-hauteur)/2;
					var Gauche=(screen.width-largeur)/2;
					fencentarticle=window.open(url,nom,"top="+haut+",left="+Gauche+",width="+largeur+",height="+hauteur+","+options);
	}
}


