//bou_panier.js
var Memo = new Array();

// structure de stockage du panier
function Structure(numprod,ref,fournisseur,nom,px,par,qtt,stock,type)
{
	this.numprod = numprod;
	this.ref = ref;
	this.fournisseur=fournisseur;
	this.nom = nom;
	this.px = px;
	this.par=par;
	this.qtt = qtt;
	this.stock = stock;
	this.type = type;
	return this;
}

//
function LookupMemo(id)
{
	var i;
	for(i=0;i<Memo.length;i++)
	{
		if(Memo[i].prodnum==id)
		{
			return Memo[i];
		}
	}
	
	//
	return null;
}

//
function RefreshCommand()
{
	Memo.length=0;
	
	//
	FillKdieASAP();
	LoadCommand();
}

/*
	//
*/
function LoadCommand()
{
	var __DropKdieToLU=0;
	var qtt;
	var par;
	
	//pour chaque produit référencé
	for (i=0; i< document.forms.length; i++)
	{
		var dernier = -1;
		for (j=0; j< Memo.length; j++)
		{
			if (Memo[j].numprod == document.forms[i].id.value)
			dernier=j;
		}
		
		/*
			//le produit ne figure pas dans le panier
		*/
		if (dernier == -1
			&&	document.forms[i].qtt)
		{
			__set_qte(document.forms[i],0);
			
			//
			continue;
		}
		
		//le produit est épuisé
		if(!document.forms[i].qtt)
		{
			continue
		}
		
		/*
			//le produit figure dans le panier
		*/
		qtt=Memo[dernier].qtt;
		
		//	stock modifié
		if(Memo[dernier].stock!=document.forms[i].sto.value)
		{
			//	stock insuffisant
			if(	String(document.forms[i].sto.value).length
				&& parseInt(document.forms[i].sto.value)<parseInt(qtt))
			{
				qtt=document.forms[i].sto.value;
			}
			
			//
			//__DropKdieToLU=1;
		}

		//
		if(document.forms[i].par1)
		{
			if(	(document.forms[i].par1.value=="1"
				&&	qtt<parseInt(document.forms[i].par.value))

				||
				(document.forms[i].par1.value=="0"
				&&	qtt>=parseInt(document.forms[i].par.value)))
			{
				__set_qte(document.forms[i],qtt);
			}

			else
			{
				__set_qte(document.forms[i],0);
			}
		}

		else
		{
			__set_qte(document.forms[i],qtt);
		}
		
		//
		if(__DropKdieToLU==1)
		{
			//DropKdieToLU();
		}
	}
}

/*
	//
*/
// Vérification du stock
function CheckCommand(obj)
{
	var tmpqtt = obj.qtt.value;
	var tmpsto = obj.sto.value;


	if (!tmpqtt.match(/^\d+$/))
	{
		alert("La quantité est invalide");
		return false;
	}
	else
	{
		if (parseInt(tmpsto,10) > -1000)
		{
			if (tmpqtt > parseInt(tmpsto,10))
			{
				alert("Commande impossible (" + tmpsto + " max)");
				return false;
			}
		}
	}
	return true;
}

// Ajout au panier
function Commander(obj,ifopen)
{//alert(obj.name);
	if(obj.complement)
	{
		var data=__merge_produit(obj);
		var qtt=data[0];
		var prix=data[1];
	}
	
	else
	{
		var qtt=__get_qte(obj);
		var prix=obj.px.value;
	}

	//
	var dernier = -1;
	for (i=0; i<Memo.length; i++)
	{
		if (Memo[i].numprod == obj.id.value)
			dernier=i;
	}
		
	//
	var type=obj.type.value;
	if(dernier != -1)
	{
		if(qtt==0)
			Supprimer(dernier);
		else
			Memo[dernier] = new Structure(obj.id.value,obj.ref.value,obj.fournisseur.value,obj.nom.value,prix,obj.par.value,qtt,obj.sto.value,type);
	}
	else
	{
		Memo[Memo.length] = new Structure(obj.id.value,obj.ref.value,obj.fournisseur.value,obj.nom.value,prix,obj.par.value,qtt,obj.sto.value,type);
	}
	
	//
	DropKdieToLU();
	if (ifopen)
	{
		openOrder();
	}
}

//
function __get_qte(obj)
{
	switch(obj.qtt.type)
	{
		case "select-one":
			var qtt=obj.qtt.options[obj.qtt.selectedIndex].value;
				
			//
			break;
				
		default:
			var qtt=obj.qtt.value;
		//
	}

	//
	return parseInt(qtt);
}

//
function __set_qte(obj,qtt)
{
	var k,l;
	switch(obj.qtt.type)
	{
		case "select-one":
			for(k=0;k<obj.qtt.options.length;k++)
			{
				if(obj.qtt.options[k].value==qtt)
				{
					l=k;
					break;
				}

				//
				if(qtt>0)
				{
					l=obj.qtt.options.length-1;
				}
			}
			
			//
			obj.qtt.selectedIndex=l;
			
			//
			break;
			
		default:
			obj.qtt.value= qtt;
		//
	}
}

//
function __merge_produit(obj)
{
	var cobj=document.forms[obj.complement.value];

	//
	if(obj.par1.value==1)
	{
		var obj1=obj;
		var objn=cobj;
	}

	else
	{
		var obj1=cobj;
		var objn=obj;
	}
	//alert("obj1=" + obj1.name + " -- objn=" + objn.name);

	/*
		//
	*/
	var qttobj=__get_qte(obj);
	var qttcobj=__get_qte(cobj);
	var qttobj1=__get_qte(obj1);
	var qttobjn=__get_qte(objn);
	var qtt=qttobj /*+ qttcobj*/;

	//
	if(qtt<parseInt(obj.par.value))
	{
		//alert(qtt + " < " + parseInt(obj.par.value));
		if(qttobjn>0)
		{
			//qttobjn=0;
			__set_qte(objn,0);
			//alert(objn.name+": qttobjn=0 (1)");
		}

		if(qttobj1!=qttobj)
		{
			//qttobj1=qtt;
			__set_qte(obj1,qtt);
			//alert(obj1.name+": qttobj1=" + qtt + " (2)");
		}

		var prix=obj1.px.value;
	}

	else
	{//alert(qtt + " >= " + parseInt(obj.par.value));
		if(qttobj1>0)
		{
			//qttobj1=0;
			__set_qte(obj1,0);
			//alert(obj1.name+": qttobj1=0 (3)");
		}

		if(qttobjn!=qttobj)
		{
			//qttobjn=qttobj;
			__set_qte(objn,qtt);
			//alert(objn.name+": oqttbjn=" + qtt + " (4)");
		}

		var prix=objn.px.value;		
	}

	//
	return new Array(qtt,prix);
}

/*
	//
*/
// Ajout au panier
function __CommanderDirect(obj,qtt)/*non utilisé*/
{
//alert('Commander');

	var dernier = -1;
	for (i=0; i<Memo.length; i++)
	{
		if (Memo[i].numprod == obj.id.value)
			dernier=i;
	}
	
	//
	var type=obj.type.value;
	if(dernier != -1)
	{
		if(qtt==0)
			Supprimer(dernier);
		else
			Memo[dernier] = new Structure(obj.id.value,obj.ref.value,obj.fournisseur.value,obj.nom.value,obj.px.value,obj.par.value,qtt,obj.sto.value,type);
	}
	else
	{
		Memo[Memo.length] = new Structure(obj.id.value,obj.ref.value,obj.fournisseur.value,obj.nom.value,obj.px.value,obj.par.value,qtt,obj.sto.value,type);
	}
	DropKdieToLU();
}

/*
	//
*/
// Suppression du panier
function Supprimer(indic)
{
	for (i=indic; i<Memo.length-1; i++)
	{
		Memo[i] = Memo[i+1];
	}
	Memo.length--;
}

/*
	//
*/
// Ouverture de la pop-up de panier
function openOrder()
{
if (Memo.length==0)
{
	//alert('Votre panier est vide');
	//return;	
}
	var option = "toolbar=no,menubar=no,location=no,directories=no,status=yes,scrollbars=yes,resizable=yes,width=734,height=500";
	var fenpanier = window.open("/boutique/commande/order.asp","Panier" + getLU("sessid"),option);
	
	try
	{
		fenpanier.focus();
	}
	
	catch(exception)
	{
		//self.location.href="/boutique/caves/caves-de-virginie/cave_lyon.asp";
	}
}
//-->
//</script>
//<script language="javascript">
//<!--
//bou_gato.js
var OKcookie = false;

var date;
expir = new Date ();
// 48 heures
expir.setTime(expir.getTime() + (2 * 86400 * 1000));

// Vérification de l'usage des cookies
function isLU()
{
	document.cookie = "isLU=true";
	if (document.cookie.indexOf("isLU=true") != -1)
	{
		OKcookie = true;
		FillKdieFromLU();
	}
	else
	{
		OKcookie = false;
		alert("Pour enregistrer votre panier, vous devez activer les cookies de votre navigateur");
	}
}

// Récupération d'un cookie
function getLU(name)
{
	var offset;
	var end;
	offset = document.cookie.indexOf(name + "=") 

	if (offset != -1)
	{
		offset += name.length + 1;
		end = document.cookie.indexOf(";", offset) ;
		if (end == -1)
			end = document.cookie.length-1;
		return unescape(document.cookie.substring(offset, end)); 
	}
	else
	    return "";
}

// Récupération d'un cookie comme panier
function FillKdieFromLU()
{
//alert('FillKdieFromLU');
	var taball;
	allkdie = getLU(kdiname);
	if (allkdie.length > 0)
	{
		//if (confirm("Vous disposez d'un panier en mémoire.\nSouhaitez vous en conserver le contenu ?"))
		//{
			//taball = allkdie.split("&");
			//for (i=0; i<taball.length-1; i=i+9)
			//{
				//Memo[Memo.length] = new Structure(taball[0+i],taball[1+i],taball[2+i],taball[3+i],taball[4+i],taball[5+i],taball[6+i],taball[7+i],taball[8+i]);
			//}
		//openOrder();
		//}
		//else
		//{
			//document.cookie = kdiname + "=;expires=" + expir.toGMTString() +  "; path=/";
			document.cookie = kdiname + "; path=/";
		//}
	}
}

// Récupération d'un cookie comme panier sans demande.
function FillKdieASAP()
{
//alert('FillKdieASAP');
	var taball;
	allkdie = getLU(kdiname);
	if (allkdie.length > 0)
	{
		taball = allkdie.split("&");
		for (i=0; i<taball.length-1; i=i+9)
			Memo[Memo.length] = new Structure(taball[0+i],taball[1+i],taball[2+i],taball[3+i],taball[4+i],taball[5+i],taball[6+i],taball[7+i],taball[8+i]);
	}
}

// mise à jour du cookie avec le panier
function DropKdieToLU()
{
//alert('DropKdieToLU');
	var todrop = "";
	for (i=0; i<Memo.length; i++)
	{
		todrop = todrop + escape(Memo[i].numprod) + "&" + escape(Memo[i].ref) + "&" + escape(Memo[i].fournisseur) + "&" + escape(Memo[i].nom) + "&" + escape(Memo[i].px) + "&" + escape(Memo[i].par) + "&" + escape(Memo[i].qtt) + "&" + escape(Memo[i].stock) + "&" + escape(Memo[i].type) + "&";
	}	
	
	//document.cookie = kdiname + "=" + todrop + ";expires=" + expir.toGMTString() +  "; path=/";
	document.cookie = kdiname + "=" + todrop + "; path=/";
}
//-->
//</script>
//<script language="javascript">
//<!--
//bou_check.js
function toVirg(s)
{
	var strtmp = "" + s;
	var tab = strtmp.split(".");
	strtmp = tab.join(",");
	return strtmp;
}

function toPoint(s)
{
	var strtmp = "" + s;
	var tab = strtmp.split(",");
	strtmp = tab.join(".");
	return strtmp;
}

function myFloat(s)
{
	var strtmp = "" + s;
	var tab = strtmp.split(",");
	strtmp = tab.join(".");

	var tmp = ((strtmp.toString().charAt(0)=='.')? "0" + Math.round(strtmp*100)/100 : Math.round(strtmp*100)/100 );
	if (tmp.toString().match(/^\d*\.\d{1}$/))
		tmp = tmp + "0";
	if (tmp.toString().match(/^\d*$/))
		tmp = tmp + ".00";
	return tmp;
}

// ************** Vérification de la saisie des coordonnés
function CheckMan(obj)
{
	var d = new Date();
	var annee = d.getYear();
	var testcertif = 0;
	var tab;
	var strtmp = "";
	var j=-1;
	with(obj)
	{
		
		
		
// ------------------------------------------------------------------------------NOM
		if (!Nom.value.match(/^[\D]+$/))
		{
			alert("Nom manquant ou invalide");
			return false;
		}
// ------------------------------------------------------------------------------PRENOM
		if (!Prenom.value.match(/^[\D]+$/))
		{
			alert("Prenom manquant ou invalide");
			return false;
		}
// ------------------------------------------------------------------------------ADRESSE 1,2,3
		if (Adresse.value.length==0)
		{
			alert("Veuillez remplir l'adresse");
			return false;
		}
// ------------------------------------------------------------------------------CODE
		if (!Cp.value.match(/^.{1,8}$/))
		{
			alert("Code postal obligatoire (8 max)");
			return false;
		}
// ------------------------------------------------------------------------------VILLE
		if (!Ville.value.match(/^\D+$/))
		{
			alert("Ville manquante ou invalide");
			return false;
		}
// ------------------------------------------------------------------------------PAYS
		if (!Pays.value.match(/^\D+$/))
		{
			alert("Pays manquant ou invalide");
			return false;
		}
// ------------------------------------------------------------------------------TELEPHONE 1
		if (!Tel.value.match(/^[\d| |-|.|(|)]{8,20}$/))
		{
			alert("Téléphone manquant ou invalide");
			return false;
		}
// ------------------------------------------------------------------------------MAIL
		if (!Email.value.match(/^.+@.+[.].+$/))
		{
			alert("E-mail invalide");
			return false;
		}
// ------------------------------------------------------------------------------COMMENT
		if (Pseudo.value.length < 5)
		{
			alert("Un pseudo est nécessaire\n(5 caractères minimum)");
			return false;
		}
// ------------------------------------------------------------------------------PASS
		if (Pass.value.length < 5)
		{
			alert("Un mot de passe est nécessaire\n(5 caractères minimum)");
			return false;
		}
// ------------------------------------------------------------------------------PASS2
		if (ConfirmPass.value != Pass.value)
		{
			alert("Confirmation du mot de passe invalide");
			return false;
		}
// ------------------------------------------------------------------------------Licences
		for (i=0; i< obj.length; i++)
		{
			if (!obj[i].name.indexOf('Lic'))
			{
				if (!obj[i].name.indexOf('LicType'))
					j++;
				
				switch (obj[i].name)
				{
					case 'LicDiscipline'+j :
						if (!obj[i].selectedIndex > 0)
							strtmp="Discipline manquante";
						break;
					case 'LicNom'+j :
						if (!obj[i].value.match(/^[\D]+$/))
							strtmp="Nom manquant";
						break;
					case 'LicPrenom'+j :
						if (!obj[i].value.match(/^[\D]+$/))
							strtmp="Prénom manquant";
						break;
					case 'LicAdresse'+j :
						if (!obj[i].value.length>0)
							strtmp="Adresse manquante";
						break;
					case 'LicCp'+j :
						if (!obj[i].value.match(/^\d{2,8}$/))
							strtmp="Code postal obligatoire (8 max)";
						break;
					case 'LicVille'+j :
						if (!obj[i].value.match(/^[\D]+$/))
							strtmp="Ville manquante";
						break;
					case 'LicPays'+j :
						if (!obj[i].value.match(/^[\D]+$/))
							strtmp="Pays manquant";
						break;
					case 'LicNaissance'+j :
						if (!obj[i].value.match(/^\d{2}\/\d{2}\/\d{4}$/))
						{
							strtmp="Date de naissance manquante ou invalide : jj/mm/aaaa";
							break;
						}
						if (!obj['LicType'+j].value.indexOf('Cyclosportive'))
						{
							tab = obj[i].value.split("/");
							if (!(tab[2] <= (annee-18)))
							{
								strtmp="L'obtention de la Licence Cyclosportive nécessite 18 ans ou plus.";
								break;
							}
						}
						if (!obj['LicType'+j].value.indexOf("Pass'Sport Nature"))
						{
							tab = obj[i].value.split("/");
							if (!(tab[2] <= (annee-4)))
							{
								strtmp="L'obtention de la Licence Pass'Sport Nature nécessite 4 ans ou plus.";
								break;
							}
						}
						break;
					case 'LicLicence'+j :
						if ((!obj[i].value.match(/^\d{10}$/)) && (obj[i].value.length>0))
							strtmp="Ancienne licence invalide (10 chiffres)";
						break;
					case 'LicNat'+j :
						if (!obj[i].value.match(/^[\D]+$/))
							strtmp="Nationalité manquante";
						break;
					case 'LicCertif'+j :
						if (!obj[i].value.length>0)
							strtmp="Indiquez votre choix concernant le certificat médical.";
						break;
				}
				
				if (strtmp.length>0)
				{
					alert("Vous devez remplir toutes les informations de prise de licence.\n" + strtmp);
					return false;
				}
			}
		}
	}
	return true;
}    



// ************** Vérification de la saisie des Pseudo et pass
function CheckPP(obj)
{
	with(obj)
	{

// ------------------------------------------------------------------------------COMMENT
		if (OldPseudo.value.length < 5)
		{
			alert("Un pseudo est nécessaire\n(5 caractères minimum)");
			return false;
		}
// ------------------------------------------------------------------------------PASS
		if (OldPass.value.length < 5)
		{
			alert("Un mot de passe est nécessaire\n(5 caractères minimum)");
			return false;
		}
	}
	return true;
}