MonetarySymbol        = '&#8364;&nbsp;'; // euro

function bestellingverwijderen(){
document.cookie = 'aantal=leeg; expires=Fri, 27 Jul 2001 02:47:11 UTC; path=/';
document.cookie = 'bestelling=leeg; expires=Fri, 27 Jul 2001 02:47:11 UTC; path=/';
}

//---------------------------------------------------------------------||
// FUNCTION:    AddToCart                                              ||
// PARAMETERS:  Form Object                                            ||
// RETURNS:     Cookie to user's browser, with prompt                  ||
// PURPOSE:     Adds a product to the user's shopping cart             ||
//---------------------------------------------------------------------||
function AddToCart(thisForm) {
   var nrzegels = 0;
   var zegelalbesteld = false;
   var notice = "";
   var dbanjo = "";

   if ( thisForm.PRICE == null )
      strPRICE     = "0.00";
   else {
      strPRICE     = thisForm.PRICE.value;
   }

   if ( thisForm.NAME == null )
      strNAME      = "";
   else {
      strNAME      = thisForm.NAME.value;
   }

   if ( thisForm.ADVNR == null )
      strADVNR      = "";
   else {
      strADVNR      = thisForm.ADVNR.value;
   }

   // is zegel al in bestelling?
   // loop over alle zegels 
   // zet hier later eventueel nog een extra for loop omheen voor meerdere cookies
//      Maximum stored cookies in a cookie file: 300
//      Maximum size for a cookie: 4Kbytes
//      Maximum number of cookies stored by the same domain: 20


   dbanjo = GetCookie("bestelling");
   if ( dbanjo == null ) dbanjo = "";
   nrzegels = GetCookie("aantal");
   if ( nrzegels == null ) nrzegels = 0;

   posplus = 0;
   posnew = 0;
   for ( i = 1; i <= nrzegels; i++ ) { //loop over aantal zegels
      posnew = dbanjo.indexOf(";", posplus);
      zegel = dbanjo.substring(posplus, posnew); 
      bla = zegel.search (strADVNR);
      if  (bla == 0) { //zegel zit al in bestelling
         zegelalbesteld = true;
         alert ("deze zegel zit al in uw bestelling");
      }
      posplus = posnew+1;
   }


   if ( !zegelalbesteld ) {

      nrzegels++;
      SetCookie("aantal", nrzegels, null, "/");
      order = GetCookie("bestelling");

      if (order == null)
         nieuweorder = strADVNR + "|" + strNAME + "|" + strPRICE  + ";";
      else
         nieuweorder =  order + strADVNR + "|" + strNAME + "|" + strPRICE + ";";

      SetCookie("bestelling", nieuweorder, null, "/");
      alert ("zegel " + strADVNR + " " + strNAME + " " + strPRICE + " is aan uw bestelling toegevoegd");
   }
}

//---------------------------------------------------------------------||
// FUNCTION:    getCookieVal                                           ||
// PARAMETERS:  offset                                                 ||
// RETURNS:     URL unescaped Cookie Value                             ||
// PURPOSE:     Get a specific value from a cookie                     ||
//---------------------------------------------------------------------||
function getCookieVal (offset) {
   var endstr = document.cookie.indexOf (";", offset);

   if ( endstr == -1 )
      endstr = document.cookie.length;
   return(unescape(document.cookie.substring(offset, endstr)));
}


//---------------------------------------------------------------------||
// FUNCTION:    FixCookieDate                                          ||
// PARAMETERS:  date                                                   ||
// RETURNS:     date                                                   ||
// PURPOSE:     Fixes cookie date, stores back in date                 ||
//---------------------------------------------------------------------||
function FixCookieDate (date) {
   var base = new Date(0);
   var skew = base.getTime();

   date.setTime (date.getTime() - skew);
}


//---------------------------------------------------------------------||
// FUNCTION:    GetCookie                                              ||
// PARAMETERS:  Name                                                   ||
// RETURNS:     Value in Cookie                                        ||
// PURPOSE:     Retrieves cookie from users browser                    ||
//---------------------------------------------------------------------||
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                                              ||
// PARAMETERS:  name, value, expiration date, path, domain, security   ||
// RETURNS:     Null                                                   ||
// PURPOSE:     Stores a cookie in the users browser                   ||
//---------------------------------------------------------------------||

/*
function SetCookie (name,value,expires,path,domain,secure) {
   document.cookie = name + "=" + escape (value) +
                     ((expires) ? "; expires=" + expires.toGMTString() : "") +
                     ((path) ? "; path=" + path : "") +
                     ((domain) ? "; domain=" + domain : "") +
                     ((secure) ? "; secure" : "");
}
*/

function SetCookie (name,value,expires,path,domain,secure) {
// anjo cookie blijft standaard drie dagen geldig
		var date = new Date();
		date.setTime(date.getTime()+(3*24*60*60*1000));
		var extra = "; expires="+date.toGMTString();
   document.cookie = name + "=" + escape (value) +
                     ((expires) ? extra : extra) +
                     ((path) ? "; path=" + path : "") +
                     ((domain) ? "; domain=" + domain : "") +
                     ((secure) ? "; secure" : "");
}

/*
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
*/


//---------------------------------------------------------------------||
// FUNCTION:    DeleteCookie                                           ||
// PARAMETERS:  Cookie name, path, domain                              ||
// RETURNS:     null                                                   ||
// PURPOSE:     Removes a cookie from users browser.                   ||
//---------------------------------------------------------------------||
function DeleteCookie (name,path,domain) {
   if ( GetCookie(name) ) {
      document.cookie = name + "=" +
                        ((path) ? "; path=" + path : "") +
                        ((domain) ? "; domain=" + domain : "") +
                        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
   }
}


//---------------------------------------------------------------------||
// FUNCTION:    MoneyFormat                                            ||
// PARAMETERS:  Number to be formatted                                 ||
// RETURNS:     Formatted Number                                       ||
// PURPOSE:     Reformats Dollar Amount to #.## format                 ||
//---------------------------------------------------------------------||
function moneyFormat(input) {
   var dollars = Math.floor(input);
   var tmp = new String(input);

   for ( var decimalAt = 0; decimalAt < tmp.length; decimalAt++ ) {
      if ( tmp.charAt(decimalAt)=="." )
         break;
   }

   var cents  = "" + Math.round(input * 100);
   cents = cents.substring(cents.length-2, cents.length)
           dollars += ((tmp.charAt(decimalAt+2)=="9")&&(cents=="00"))? 1 : 0;

   if ( cents == "0" )
      cents = "00";
   if ( cents == "1" ) cents = "01"; //anjo
   if ( cents == "2" ) cents = "02"; //anjo
   if ( cents == "3" ) cents = "03"; //anjo
   if ( cents == "4" ) cents = "04"; //anjo
   if ( cents == "5" ) cents = "05"; //anjo
   if ( cents == "6" ) cents = "06"; //anjo
   if ( cents == "7" ) cents = "07"; //anjo
   if ( cents == "8" ) cents = "08"; //anjo
   if ( cents == "9" ) cents = "09"; //anjo

   return(dollars + "." + cents);
}

//---------------------------------------------------------------------||
// FUNCTION:    RemoveFromCart                                         ||
// PARAMETERS:  Order Number to Remove                                 ||
// RETURNS:     Null                                                   ||
// PURPOSE:     Removes an item from a users shopping cart             ||
//---------------------------------------------------------------------||

function RemoveFromCartAnjo(RemOrder) {

   strremove = "kies 'OK' om deze zegel uit uw mandje te verwijderen";

  if ( confirm( strRemove ) ) {

      // loop over alle zegels en haal de gewenste zegel er uit
      // haal de zegel info uit cookie en zet ze in een string voor op het scherm
      // zet hier later eventueel nog een extra for loop omheen voor meerdere cookies
      bestellingnew = "";
      dbanjo = "";
      dbanjo = GetCookie("bestelling");
      if ( dbanjo == null )
         dbanjo = "";
      nrzegels = GetCookie("aantal");
      if ( nrzegels == null )
         nrzegels = 0;

      posplus = 0;
      posnew = 0;

      nrzegelsorg = nrzegels;
      for ( i = 1; i <= nrzegelsorg; i++ ) { //loop over aantal zegels
         posnew = dbanjo.indexOf(";", posplus);
         zegel = dbanjo.substring(posplus, posnew); 
         if (i!= RemOrder) {
            bestellingnew += zegel + ";";
         }
         else {
            nrzegels -= 1;
         }
         posplus = posnew+1;
      }
      SetCookie("aantal", nrzegels, null, "/");
      SetCookie("bestelling", bestellingnew, null, "/");
      location.href=location.href;
   }
}

//---------------------------------------------------------------------||
// FUNCTION:    ManageCart                                             ||
// PARAMETERS:  Null                                                   ||
// RETURNS:     Product Table Written to Document                      ||
// PURPOSE:     Draws current cart product table on HTML page          ||
//---------------------------------------------------------------------||
function ManageCart(inclverw) {
   var strbestel = "";
   var nrzegels = 0;
   var subtotaal = 0; 		//totale kosten van bestelling zonder verzending
   var totaal = 0; 		//totale kosten van bestelling incl verzending
   var verzendkosten = 0.50; 	//totale kosten van verzenden
   var strtotaal = "";		//totaal als string(money)
   var dbanjo = "";

   nrzegels = GetCookie("aantal");
   if ( nrzegels == null ) nrzegels = 0;


   if ( nrzegels != 0 ) {
      strbestel = "<fieldset><legend>inhoud</legend><table><tr><td class=\"mandje\">advnr</td><td class=\"mandje\">naam</td><td class=\"mandje\">&nbsp;&nbsp; prijs</td></tr>";
   } else {
      strbestel += "<br /><B>Het mandje is leeg.</B> <br />&nbsp;<br />";
   }

   // haal de zegel info uit cookie en zet ze in een string voor op het scherm
   // zet hier later eventueel nog een extra for loop omheen voor meerdere cookies
   dbanjo = GetCookie("bestelling");
   if (dbanjo == null ) dbanjo = "";
   strlenbestelling = dbanjo.length;
   posplus = 0;
   posnew = 0;
   for ( i = 1; i <= nrzegels; i++ ) { //loop over aantal zegels
      posnew = dbanjo.indexOf(";", posplus);
      zegel = dbanjo.substring(posplus, posnew); 

      // haal info uit zegel en zet in bestelstring
      pospipe1 = zegel.indexOf("|", 0);
      pospipe2 = zegel.indexOf("|", pospipe1+1);
      advnr = zegel.substring(0, pospipe1); 
      naam = zegel.substring(pospipe1+1, pospipe2); 
      prijs = zegel.substring(pospipe2+1, zegel.length);  
      strbestel += "<tr><td valign=\"top\">" + advnr + "</td><td>" + naam + "</td><td>" + MonetarySymbol + prijs;
      if(inclverw){
         strbestel += "<td><input class=\"button\" type=button value=\"verwijder\"  onClick=\"RemoveFromCartAnjo("+i+")\"></td></tr>";
      }

      // tel de prijs van deze zegel op bij het totaal
      subtotaal += parseFloat(prijs);

      posplus = posnew+1;
   }

   if ( nrzegels != 0 ) {
      totaal = subtotaal + verzendkosten;
      strbestel += "<tr><td> &nbsp; </td><td class=\"mandje\">subtotaal</td><td class=\"mandje\">" + MonetarySymbol + moneyFormat(subtotaal) + "</td></tr>";
      strbestel += "<tr><td> &nbsp; </td><td class=\"mandje\">verzendkosten</td><td class=\"mandje\">" + MonetarySymbol + moneyFormat(verzendkosten) + "</td></tr>";
      strbestel += "<tr><td> &nbsp; </td><td class=\"mandje\">totaal</td><td class=\"mandje\">" + MonetarySymbol + moneyFormat(totaal) + "</td></tr></table></fieldset>";
   }


   document.write(strbestel);
   document.close();
}


