//    Copyright (C) 1998   Zhong Yang  oops@beida.com
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                

// Sources libres optimisés par Guy Capra, Créatique 1999 (www.creatique.com)
//
// Vous pouvez réutiliser ce code sous les termes de la licence GPL (Général Public Licence).
// En d'autres termes, vous pouvez l'utiliser personnellement et/ou commercialement 
// à la condition de transmettre aussi les droits d'accès et de modification aux sources,
// et de ne rien enlever ou modifier à l'entête (le présent texte).

function view_cart() {
   mycookies=new Array(20);
   var i=0;
   var startpoint=0;
      whole_len=document.cookie.length;
   while(whole_len!=startpoint && i< 20)
   {
     if ((temlen=document.cookie.indexOf(";",startpoint))!=-1)
     {
	mycookies[i]=document.cookie.substring(startpoint,temlen);
	i++;
	startpoint=temlen+1
     }
     else if (startpoint != whole_len)
     {
        mycookies[i]=document.cookie.substring(startpoint,whole_len);
        i++;
        startpoint=whole_len;
     }
   }
    printtitle();
    for (j=0;j<i;j++)
     {
//filter out cookies that are not used by shopping cart
	if((mycookies[j].substring(0,4)=="site")||(mycookies[j].substring(0,5)==" site"))
	  getCookie(mycookies[j]);

     }
	 
   document.write("</table>");
}


function getCookie (acookie) {
    clen = acookie.length;
    nlen= acookie.indexOf("=",0);
    //get rid of the "crea" at the beginning, thus start at 4
    if(acookie.substring(0,1)==" ")
       cname=acookie.substring(5,nlen);
    else
    cname= acookie.substring(4,nlen);
    bname= acookie.substring(nlen+1,clen);
    sname_len=cname.indexOf("+",0);
    store=cname.substring(0,sname_len);
    dlen=cname.indexOf("+",sname_len+1);
    descrip=cname.substring(sname_len+1,dlen)
    product=cname.substring(dlen+1,nlen);

    qlen=bname.indexOf("+",0);
    rlen=bname.indexOf("+",qlen+1);
    quant=bname.substring(0,qlen);
    if(quant==0)
	document.cookie="site"+cname+"=bb;  path=/"+"";
    refer=bname.substring(qlen+1,rlen);
    price=bname.substring(rlen+1,clen-nlen)//*quant;
	
// Remise quantitative pour articles d'une même référence, surtout pour VV	
	if (product == "BGA"){
			//if (quant < 10 ){
				FraisEx=0
		        price=price
			//}else if (quant >= 10 && quant <= 15){
				//FraisEx=0
				//price=((price*(1-0.1))*quant)+FraisEx
			//}else if (quant >= 16 && quant <= 20){
				//FraisEx=0
				//price=((price*(1-0.15))*quant)+FraisEx
			//}else if (quant >= 21 && quant <= 30){
				//FraisEx=0
				//price=((price*(1-0.20))*quant)+FraisEx
			//}else if (quant >= 31 && quant <= 100){
				//FraisEx=0
				//price=((price*(1-0.30))*quant)+FraisEx
			//}else{
				//FraisEx=0
				//price=price*quant
			//}
	}else if (product == "VVI"){
		FraisEx=0
		price=price
	}else{
		price=price*quant
		FraisEx=0
	}
    price=price_format(price);
	FraisEx=price_format(FraisEx);
    printcontent();
}

function putback(str1){
  document.cookie="site"+str1+"=bb;  path=/"+""; 
  quant=0
}

// There's no need to touch anything above this line
// You can use "store","descrip","product","quant","price"
// and "refer" in the code below

function printtitle(){
tabletitle="<TABLE WIDTH=500 BORDER=0 align=center><tr> \
<TD WIDTH=100 BGCOLOR=#000099 BORDERCOLOR=#FFFFFF><FONT SIZE=2 FACE=Arial COLOR=#FFFFFF><B>R&eacute;f&eacute;rence</B></FONT></td> \
<TD WIDTH=200 BGCOLOR=#000099 BORDERCOLOR=#FFFFFF><FONT SIZE=2 FACE=Arial COLOR=#FFFFFF><B>D&eacute;signation</B></FONT></td> \
<TD WIDTH=100 Align=CENTER BGCOLOR=#000099 BORDERCOLOR=#FFFFFF><FONT SIZE=2 FACE=Arial COLOR=#FFFFFF><B>Dur&eacute;e</B></FONT></td> \
<TD WIDTH=100 align=RIGHT BGCOLOR=#000099 BORDERCOLOR=#FFFFFF><FONT SIZE=2 FACE=Arial COLOR=#FFFFFF><B>Montant TTC</B></FONT></td> \
<td> </td></tr>";
document.write(tabletitle);
}

function durqua(){
if (product != "") {
	if (product=="BGA"){
  		dur=quant+" mois"
	}else if (product=="VVI"){
		if (quant==24){
  			dur=quant+" Heures de consultation sur Internet" 
		}else if (quant==12){
			dur=quant+" mois de consultation sur Internet" 
		}
	}else{
  		dur= quant+" ex"  
	}
}else{
	dur=""
}	
 return dur
}

function printcontent(){
  if(quant!=0){
    document.write("<tr align=LEFT><TD WIDTH=100 VALIGN=TOP><FONT SIZE=2 FACE=Arial><B>"+product+"</B></FONT></td><TD WIDTH=200 VALIGN=TOP><FONT SIZE=2 FACE=Arial><B>");
    document.write(descrip+"</B></FONT></td><TD WIDTH=100 Align=CENTER VALIGN=TOP><FONT SIZE=2 FACE=Arial><B>"+durqua()+"</B></FONT></td><TD WIDTH=100 align=RIGHT VALIGN=TOP><FONT SIZE=2 FACE=Arial><B>");
    document.write(price+" €</B></FONT></td>");
    document.write("<TD><form><input type=submit value=Enlever "); 
    document.write("onClick=\"putback(\'"+store+"+"+descrip+"+"+product);
    document.write("\')\"></form></td></tr>");
   }
}

//add a field so that shopper can change amount of product in cart
function printcontent2(){
  if(quant!=0){
    document.write("<tr align=center><td>"+product+"</td><td>");
    document.write(descrip+"</td><td><form method=get ");
    document.write("action=voirbtp.asp><input type=text size=3");
    document.write(" name=quantity value="+quant+">");
    document.write("<input type=hidden name="+price);
    document.write(" value=\""+descrip+"\">");
    document.write("<input type=hidden name=\""+product);
    document.write("\" value=\""+store+"\"></td><td>");
    document.write(price+"</td><td>"+"<a href="+refer+">"+store+"</a>");
    document.write("</td><td><input type=submit value=update onClick=");
    document.write("addtocart(this.form,'voirbtp.asp')>");
    document.write("</form></td></tr>");
   }
}

function price_format(tmpprice){
// get rid off the extra digits behind "."
   dot = tmpprice.toString().indexOf(".");
   if(dot!=-1)
      tmpprice=tmpprice.toString().substr(0,dot+4);
   tmpprice=Math.round(tmpprice*100)/100;
// add "0" if necessary
   dot = tmpprice.toString().indexOf(".");
   if(dot ==-1)
      tmpprice=tmpprice.toString()+".00";
   else if((tmpprice.toString().length-dot)==2)
      tmpprice=tmpprice.toString()+"0";
   return tmpprice;
}

function donnecookie(nom)
{
  var recherche=nom+'=';                    // pour recherche nom dans la liste
  if (document.cookie.length>0)             // si il existe des cookies
  {
    position=document.cookie.indexOf(recherche);   // donne la position du nom
    if (position!=-1)                              // si une position existe
      {
        position=position+recherche.length;        // on passe après nom=
        fin=document.cookie.indexOf(';',position); // on cherche la fin (";")
        if (fin==-1)                               // si le ";" n'existe pas
        {
          fin=document.cookie.length;       // alors le cookie va jusqu'au bout
        }
        return unescape(document.cookie.substring(position,fin));// donne texte
      }
      else {return '';}                     // retourne du 'vide'
  }
  else {return '';}                         // retourne du 'vide'
}

