
var kd_cde="commande.php"; var kd_adresse="adresse.html"; var kd_paiement="test.php"
var kd_tva=1;  kd_txtva=0
var kd_port=0;	// 
var kd_taille=1;
var kd_couleur=1;

var pathname=location.pathname;
var myDomain=pathname.substring(0,pathname.lastIndexOf('/')) +'/';
var dateExp=new Date();
var dateOld=new Date();
dateOld.setTime(dateExp.getTime()-1);
dateExp.setTime(dateExp.getTime()+(365*24*3600*1000));
var montantF=0;var totalF=0; var totalE=0; var masseT=0; var portF=0; var caddieOK=0; var adresseOK=0; var cdePays=""; var tvaF=0;
var taux=6.55957;var lim=100;
function getCookieVal(offset) {
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) {endstr=document.cookie.length;}
	return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie(name) {
	var arg=name+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen) {
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg)
			return getCookieVal (j);
		i=document.cookie.indexOf(" ",i)+1;
        if (i==0) break;
	}
	return null;
}
function SetCookie(name,value) {
	var argv=SetCookie.arguments;
	var argc=SetCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=name+"="+escape(value)+
		((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
		((path==null) ? "" : ("; path="+path))+
		((domain==null) ? "" : ("; domain="+domain))+
		((secure==true) ? "; secure" : "");
}
// ------------------------------------------------------------------------- //
function format(valeur) {
	var decimal=2;
	var deci=Math.floor(parseFloat(valeur)*Math.pow(10,decimal)) -Math.floor(parseFloat(valeur))*Math.pow(10,decimal)
	var val=Math.floor(parseFloat(valeur))
	var val_format=val+"";
	var nb=val_format.length;
	for (var i=1;i<3;i++) {
		if (val>=Math.pow(10,(3*i))) {
			val_format=val_format.substring(0,nb-(3*i))+" "+val_format.substring(nb-(3*i));
		}
	}
	if (deci>=0) {if (deci<10){deci="0"+deci} val_format=val_format+"."+deci;}
	return val_format;
}
function GetNbItem() {
	var NbS=GetCookie("nbitem");
	var Nb=0
	if (NbS!=null) {Nb=parseInt(NbS);}
	return Nb;
}
function SetNbItem(nb) {
	SetCookie("nbitem",nb,dateExp,myDomain)
}
function AddItem(form) {
	var NoItem=parseInt(GetNbItem())+1;
	if (NoItem<lim)
	{	var numpdt=form.numpdt.value;var desi=form.desi.value;var qte=form.qte.value; var px=form.px.value;
		var ligne=numpdt+"¤"+desi+"¤"+qte+"¤"+px+"¤";SetCookie("itemno"+NoItem,ligne,dateExp,myDomain);SetNbItem(NoItem);} else {alert(textlim)}}
function SupItem(no) {
	var ligne=GetCookie("itemno"+no);
	var dateAvant=new Date();
	var nb=GetNbItem();
	dateAvant.setTime(dateAvant.getTime() - 1);
	if (ligne!=null) {
		if (confirm("Cet article va être enlevé du caddie\nEtes-vous d'accord ?")){
			for(var i=no+1;i<=nb;i++) {
				ligne=GetCookie("itemno"+i);
				SetCookie("itemno"+(i-1),ligne,dateExp,myDomain);
			}
			SetNbItem(nb-1);
		}
	}
}
function ChangeQte(no) {
	var item=new GetItem(no);
	var new_qte=prompt("Nouvelle quantité pour "+item.desi+" ?",item.qte);
	new_qte=Math.round(parseInt(new_qte));
	if (new_qte>0){
		var ligne=item.numpdt+"¤"+item.desi+"¤"+new_qte+"¤"+item.px+"¤";
		SetCookie("itemno"+no,ligne,dateExp,myDomain);
		window.location.reload();
		return false;
	} else {SupItem(no); return true}
}


function GetItem(no) {
	var l=GetCookie("itemno"+no); var i=0; var j=0
	j=l.indexOf("¤",i); this.numpdt=l.substring(i,j);i=j+1;
	j=l.indexOf("¤",i); this.desi=l.substring(i,j);i=j+1;
	j=l.indexOf("¤",i); this.qte=l.substring(i,j);i=j+1;
	j=l.indexOf("¤",i); this.px=l.substring(i,j);i=j+1;
}


var textlim="Version de démonstration limitée à 3 articles dans le caddie";
function DispCaddie(fond,trait,color) {
	var mtF="";var aff="";
	var NbItem=parseInt(GetNbItem());
	aff+='<INPUT type="hidden" name="nbitem" value="'+NbItem+'">';
	if (NbItem>0) {
		aff+="<TABLE width='98%' border=0 cellpadding=0 cellspacing=0><TR><TD width='5%' bgcolor='F8F8F2' class='titre_panier'>Réf.</TD><TD width='44%' bgcolor='F8F8F2' class='titre_panier'>Désignation</TD><TD width='5%' bgcolor='F8F8F2' class='titre_panier' align='left'>Qté</TD><TD width='11%' bgcolor='F8F8F2' class='titre_panier'>P.U. HT</TD><TD width='15%' bgcolor='F8F8F2' class='titre_panier'>Montant HT<TD width='20%' bgcolor='F8F8F2' class='titre_panier' align='center'>Actions</TD></TR>";
		for(var i=1;i<=NbItem;i++) {
			i1=0; i2=0;
			var item=new GetItem(i);
			item.px=parseFloat(item.px,2);
			mtF=parseFloat(parseInt(item.qte)*parseFloat(item.px),2);
			montantF+=mtF; masseT+=item.qte*item.masse;


			aff+="<TR>"
			aff+="<TD height='25' class='newscontent'>"+item.numpdt+"</TD>";
			aff+="<TD height='25' class='newscontent'>"+item.desi+"</TD>";
			aff+="<TD height='25' class='newscontent'>"+item.qte+"</TD>";
			aff+="<TD height='25' class='newscontent'>"+format(item.px)+"</TD>";
			aff+="<TD height='25' class='newscontent' align='right'>"+format(mtF)+"</TD>";
			aff+="<TD height='25' class='newscontent' align='right'><A href='' onClick='SupItem("+i+"); window.location.reload(); return false' class='linkcontent'>Supprimer</A>";
			aff+=" <A href='' onClick='ChangeQte("+i+");window.location.reload();return false' class='linkcontent'>Quantités</A>"
			
			var numpdt = new Array();
			var numpdt2 = new Array();
			numpdt[i]=item.numpdt;
			numpdt2[i]=item.numpdt;
			aff+='<INPUT type="hidden" name="numpdt['+i+']" value="'+item.numpdt+'">';
			aff+='<INPUT type="hidden" name="qte'+i+'" value="'+item.qte+'">';
			aff+='<INPUT type="hidden" name="idpdt'+i+'" value="'+item.numpdt+'">';

			aff+="<INPUT type='hidden' name='itemidpdt' value='"+item.numpdt+"'>";
			aff+="<INPUT type='hidden' name='itemqte' value='"+item.qte+"'>";
			
			aff+="</TR>"
		}
		aff+="<INPUT type='hidden' name='cpt' value='"+NbItem+"'>";

		totalF=montantF+portF;
		aff+="<tr><td height='20%' colspan='1'>&nbsp;</td>";
              	aff+="<td width='30%' class='titlealbum' colspan='2' align='right'>Montant total :</td>";
		aff+="<td height='20%'>&nbsp;</td>";
              	aff+="<td width='10%' class='newstitle' colspan='1' align='right'>"+format(totalF)+" €</td>";

		aff+="<INPUT type='hidden' name='total' value='"+format(totalF)+"'>"

		aff+="</tr></table><tr><td colspan=4><span class='newstitle'>Code réduction </span>";
		aff+="<input type='texte' name='reduc' size='6' class='formcd'>";
		aff+="<span class='newscontent'><i> (facultatif)</i></span>";
		aff+="</td></tr><tr><td width='3%'>&nbsp;</td>";
		aff+="<td colspan='3'><img src='images/separ01.gif' width='159' height='3' vspace='10'><br>";
		aff+="<INPUT type='submit' value='Terminer la commande' name='submit' class='formbouton'></td>";
		aff+="</tr><tr><td>&nbsp;</td>";
		aff+="<td colspan='3'><img src='images/separ01.gif' width='159' height='3' vspace='10'><br>";
		aff+="<span class='newstitle'>Les MP3</span><span class='newscontent'> command&eacute;s vous seront envoy&eacute;s par email, &agrave; raison d\'un titre par email, soit par exemple 12 emails si c\'est un CD 12 titres.</span></td>";
		aff+="</tr>";

	} else {
		aff="<p class=newscontent>Le caddie est vide !</p><BR><BR>";
	}
	document.write(aff);
}
function ViderCaddie() {
	for (var i=0;i<=20;i++){SetCookie("itemno"+i,"",dateOld,myDomain)}
	SetNbItem(0)
	SetCookie("leclient","",dateOld,myDomain)
}
function DispCommande(fond,trait,color) {

	DispCaddie(fond,trait,color);
}
function Envoyer(f) {
	if (GetNbItem()==0){alert("Le caddie est vide.\nVous ne pouvez pas commander.")}
	if (GetCookie("kd")!="OK"){alert("Vos coordonnées ne sont pas correctes.\nMerci de les compléter.")}
	if ((GetNbItem()>0)&&(GetCookie("kd")=="OK")){
		if(confirm("La commande va être envoyée.\nEtes-vous d'accord ?")){
			f.submit();
			window.location=kd_paiement;
		}
	}
}


function DispCaddiefinal(fond,trait,color) {
	var mtF="";var aff="";
	var NbItem=parseInt(GetNbItem());
	aff+='<INPUT type="hidden" name="nbitem" value="'+NbItem+'">';
	if (NbItem>0) {
		aff+="<TABLE width='98%' border=0 cellpadding=0 cellspacing=0><TR><TD width='5%' bgcolor='F8F8F2' class='titre_panier'>Réf.</TD><TD width='44%' bgcolor='F8F8F2' class='titre_panier'>Désignation</TD><TD width='5%' bgcolor='F8F8F2' class='titre_panier' align='left'>Qté</TD><TD width='11%' bgcolor='F8F8F2' class='titre_panier'>P.U. HT</TD><TD width='15%' bgcolor='F8F8F2' class='titre_panier'>Montant HT</TD></TR>";
		for(var i=1;i<=NbItem;i++) {
			i1=0; i2=0;
			var item=new GetItem(i);
			item.px=parseFloat(item.px,2);
			mtF=parseFloat(parseInt(item.qte)*parseFloat(item.px),2);
			montantF+=mtF; masseT+=item.qte*item.masse;

			aff+="<TR>"
			aff+="<TD height='25' class='newscontent'>"+item.numpdt+"</TD>";
			aff+="<TD height='25' class='newscontent'>"+item.desi+"</TD>";
			aff+="<TD height='25' class='newscontent'>"+item.qte+"</TD>";
			aff+="<TD height='25' class='newscontent'>"+format(item.px)+" €</TD>";
			aff+="<TD height='25' class='newscontent' align='right'>"+format(mtF)+" €</TD>";
			aff+='<INPUT type="hidden" name="qte'+i+'" value="'+item.qte+'">';
			aff+='<INPUT type="hidden" name="idpdt'+i+'" value="'+item.numpdt+'">';	
			aff+="<INPUT type='hidden' name='cpt' value='"+NbItem+"'>";
			aff+="</TR>"
		}
		totalF=montantF+portF;

		aff+="<tr><td height='20%' colspan='1'>&nbsp;</td>";
              	aff+="<td width='30%' class='titlealbum' colspan='2' align='right'>Montant total :</td>";
		aff+="<td height='20%'>&nbsp;</td>";
              	aff+="<td width='10%' class='newstitle' colspan='1' align='right'>"+format(totalF)+" €</td>";

		aff+="<INPUT type='hidden' name='total' value='"+format(totalF)+"'>"

	} else {
		aff="Le caddie est vide !<BR><BR>";
	}
	document.write(aff);
}
