
/*
  file    : basket.js
  version : 2
*/

// O'reily cookie interface
<!--
// The constructor function.  Creates a cookie object for the specified
// document,  with a specified name. 
// attributes.  
// Arguments:
//   document: the Document object that the cookie is stored for.  Required.
//   name: a string that specifies a name for the cookie.  Required.
//   hours: an optional number that specifies the number of hours from now
//          that the cookie should expire.
//   path: an optional string that specifies the cookie path attribute.
//   domain: an optional string that specifies the cookie domain attribute.
//   secure: an optional boolean value that, if true, requests a secure cookie.
//
function Cookie(document, name, hours, path, domain, secure)
{
    // All the predefined properties of this object begin with '$'
    // to distinguish them from other properties which are the values to
    // be stored in the cookie.
    this.$document = document;
    this.$name = name;
    if (hours)
        this.$expiration = new Date((new Date()).getTime() + hours*3600000);
    else this.$expiration = null;
    if (path) this.$path = path; else this.$path = null;
    this.$path = '/';   // het pad keihard overschrijven met de root
    if (domain) this.$domain = domain; else this.$domain = null;
    if (secure) this.$secure = true; else this.$secure = false;
}

// This function is the store() method of the Cookie object
function _Cookie_store()
{
    // First, loop through the properties of the Cookie object and
    // put together the value of the cookie.  Since cookies use the
    // equals sign and semicolons as separators, we'll use colons
    // and ampersands for the individual state variables we store 
    // within a single cookie value.  Note that we escape the value
    // of each state variable, in case it contains punctuation or other
    // illegal characters.
    var cookieval = "";
    for(var prop in this) {
        // ignore properties with names that begin with '$' and also methods
        if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) 
            continue;
        if (cookieval != "") cookieval += '&';
        cookieval += prop + ':' + escape(this[prop]);
    }

    // Now that we have the value of the cookie, put together the 
    // complete cookie string, which includes the name, and the various
    // attributes specified when the Cookie object was created.
    var cookie = this.$name + '=' + cookieval;
    if (this.$expiration)
        cookie += '; expires=' + this.$expiration.toGMTString();
    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;
    if (this.$secure) cookie += '; secure';

    // Now store the cookie by setting the magic Document.cookie property
    this.$document.cookie = cookie;
}

// This function is the load() method of the Cookie object
function _Cookie_load()
{
    // First, get a list of all cookies that pertain to this document.
    // We do this by reading the magic Document.cookie property
    var allcookies = this.$document.cookie;
    if (allcookies == "") return false;

    // Now extract just the named cookie from that list.
    var start = allcookies.indexOf(this.$name + '=');
    if (start == -1) return false;   // cookie not defined for this page.
    start += this.$name.length + 1;  // skip name and equals sign.
    var end = allcookies.indexOf(';', start);
    if (end == -1) end = allcookies.length;
    var cookieval = allcookies.substring(start, end);

    // Now that we've extracted the value of the named cookie, we've
    // got to break that value down into individual state variable 
    // names and values.  The name/value pairs are separated from each
    // other with ampersands, and the individual names and values are
    // separated from each other with colons.  We use the split method
    // to parse everything.
    var a = cookieval.split('&');  // break it into array of name/value pairs
    for(var i=0; i < a.length; i++)  // break each pair into an array
        a[i] = a[i].split(':');

    // Now that we've parsed the cookie value, set all the names and values
    // of the state variables in this Cookie object.  Note that we unescape()
    // the property value, because we called escape() when we stored it.
    for(var i = 0; i < a.length; i++) {
        this[a[i][0]] = unescape(a[i][1]);
    }

    // We're done, so return the success code
    return true;
}

// This function is the remove() method of the Cookie object.
function _Cookie_remove()
{
    var cookie;
    cookie = this.$name + '=';
    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;
    cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';

    this.$document.cookie = cookie;
}

// extra toegevoegde functies
function _Cookie_getCookie()
{
  return this.$document.cookie;
}

function _Cookie_setCookie(sNewCookie)
{
   this.remove();
  this.$document.cookie = sNewCookie + ';path=/';
}



// maak klasse voor een cookie
Cookie.prototype.store = _Cookie_store;
Cookie.prototype.load = _Cookie_load;
Cookie.prototype.remove = _Cookie_remove;
Cookie.prototype.getCookie = _Cookie_getCookie;
Cookie.prototype.setCookie = _Cookie_setCookie;


// Winkelwagentje

/*
 klasse      : clsWinkelwagentje
 description : Winkelwagentje die met behulp van cookies producten opslaat,
                zodat deze later weer besteld kunnen worden. 
*/
/*
 function    : [constructor van clsWinkelwagentje]
 description : creeert en initialiseert het winkelwagentje
 arguments   : variabel aantal argumenten, worden opgeslagen als strings
               de argumenten zijn de namen van de velden voor de producten.
 returns     : een object van het type clsWinkelwagentje
 sample      : var winkelwagen = new clsWinkelwagentje('artikelnaam', 'stukprijs');
*/
function clsWinkelwagentje()
{
  // stel de functies in
  this.voegProductToe = clsWinkelwagentje_voegProductToe;
  this.verwijderProduct = clsWinkelwagentje_verwijderProduct;
  this.maakLeeg = clsWinkelwagentje_maakLeeg;
  this.geefProductGegevens = clsWinkelwagentje_geefProductGegevens;
  this.geefArrayVanProducten = clsWinkelwagentje_geefArrayVanProducten;
  this.geefArrayVanProductIDs = clsWinkelwagentje_geefArrayVanProductIDs;
  
  // maak een cookie voor de veldwaarden en maak een cookie voor het opslaan van de veldnamen
  this.winkelwagentje = new Cookie(document, 'winkelwagentje');
  this.winkelwagentje.load();
  
  // haal de argumenten op van deze functies. deze bevatten de veldnamen
  var asFunctionArguments = clsWinkelwagentje.arguments;
  // sla de veldnamen op in een nieuwe array
  this.asVeldNamen = new Array;
  for (var iArgumentNummer = 0; iArgumentNummer<asFunctionArguments.length; iArgumentNummer++)
  {
    this.asVeldNamen[iArgumentNummer] = asFunctionArguments[iArgumentNummer]
  }
}
// klasse functies voor clsWinkelwagentje

/*
 function    : clsWinkelwagentje_voegProductToe
 description : wordt gebruikt door de klasse clsWinkelwagentje om een product
               toe te voegen. 
 arguments   : het eerste argument is een uniek id nummer van het artikel waaronder het
               wordt opgeslagen. de overige argumenten vormen de waarden van de
               velden. deze functie heeft dus exact 1 argument meer dan de constructor.  
*/
function clsWinkelwagentje_voegProductToe(sProductID)
{
  // sla alle veldwaarden op
  var sVeldNaam, sVeldWaarde
  for (var iVeldNummer = 0; iVeldNummer < this.asVeldNamen.length; iVeldNummer++)
  {
    // veldnaam van het iVeldNummer-ste veld
     sVeldNaam = this.asVeldNamen[iVeldNummer];
    // veldwaarde ophalen uit de functie argumenten
    sVeldWaarde = clsWinkelwagentje_voegProductToe.arguments[iVeldNummer + 1];
  
    // sla de veldwaarde op in het bijbehorende veld 
    eval('this.winkelwagentje.' + sVeldNaam + sProductID + ' = \'' + escape(sVeldWaarde) + '\''); // aanpassing escape toegevoegd
  }
  
  // sla op in cookie
  this.winkelwagentje.store();
}

/*
 function    : clsWinkelwagentje_verwijderProduct
 description : wordt gebruikt door de klasse clsWinkelwagentje om een product
               te verwijderen. 
 arguments   : sProductID is het id nummer van het artikel dat moet worden verwijderd.
*/
function clsWinkelwagentje_verwijderProduct(sProductID)
{
  var sCookieString = this.winkelwagentje.getCookie() + '&';

  var sVeldNaamMetID, sVeldWaarde
  // iBeginPositie zal de positie in de string aangeven van waaraf moet worden
  // geknipt. EindPositie het laatste karakter
  var iBeginPositie, iEindPositie;
  for (var iVeldNummer = 0; iVeldNummer < this.asVeldNamen.length; iVeldNummer++)
  {
    // veldnaam met ID van het iVeldNummer-ste veld
     sVeldNaamMetID = '' + this.asVeldNamen[iVeldNummer] + sProductID;
//    alert('sCookieString = ' + sCookieString);
    
    // bepaal de positie van de veldnaam
    iBeginPositie = sCookieString.indexOf(sVeldNaamMetID);
    // indien de veldnaam niet is gevonden, dan niets doen
    if (iBeginPositie != -1) 
    {
      // spring voorbij de veldnaam en ook voorbij het : teken
      iEindPositie = iBeginPositie + sVeldNaamMetID.length + 2;
      // zoek naar :  dit is het einde van de veldnaam/veldwaarde combinatie
      while (sCookieString.charAt(iEindPositie)!= '&') iEindPositie++;
      
      // knip het stringetje eruit
      if (iBeginPositie>0)
      sCookieString = sCookieString.substring(0, iBeginPositie) + sCookieString.substring(iEindPositie + 1, sCookieString.length);
    }
  }
  
  // haal ampersand & aan het einde weg. deze is aan het begin toegevoegd
  // er wordt gecontroleerd of deze empersand er staat, omdat de cookie ook volledig
  // leeg kan zijn in welk geval er geen empersand meer bestaat. 
  if (sCookieString.charAt(sCookieString.length-1) == '&') 
    sCookieString = sCookieString.substring(0, sCookieString.length - 1);

  // sla de cookie weer op
  this.winkelwagentje.setCookie(sCookieString);
    
}

/*
 function    : clsWinkelwagentje_maakLeeg
 description : wordt gebruikt door de klasse clsWinkelwagentje en zorgt ervoor
               dat het gehele winkelwagentje wordt geleegd. 
 arguments   : 
*/
function clsWinkelwagentje_maakLeeg()
{
  // cookie leeggooien
  this.winkelwagentje.remove();
}

/*
 function    : clsWinkelwagentje_geefProductGegevens
 description : wordt gebruikt door de klasse clsWinkelwagentje om de gegevens van
               een product terug te geven. 
 arguments   : het eerste argument is het id nummer van het artikel waaronder het
               is opgeslagen. de overige argumenten vormen de waarden van de
               velden. deze functie heeft dus exact 1 argument meer dan de constructor.  
*/
function clsWinkelwagentje_geefProductGegevens(sProductID)
{
  // haal alle veldwaarden op
  var sVeldNaam, sVeldWaarde
  for (var iVeldNummer = 0; iVeldNummer < this.asVeldNamen.length; iVeldNummer++)
  {
    // veldnaam van het iVeldNummer-ste veld
     sVeldNaam = this.asVeldNamen[iVeldNummer];
    //alert(this.asVeldNamen[iVeldNummer]);
    
    // plaats de veldwaarde in het argument
    //eval('alert(this.winkelwagentje.' + sVeldNaam + sProductID + ')');
//    clsWinkelwagentje_geefProductGegevens.arguments[iVeldNummer + 1].value = eval('this.winkelwagentje.' + sVeldNaam + sProductID); 
      var tmpVeldWaarde = eval('this.winkelwagentje.' + sVeldNaam + sProductID); 
	  clsWinkelwagentje_geefProductGegevens.arguments[iVeldNummer + 1].value = (tmpVeldWaarde) ;
  }
}

/*
' function    : geefArrayVanProducten
' description : geeft een array terug met alle waarden van een specifiek veld in het winkelwagentje
' arguments   : [in] sFieldname, het veld waarvan de waarden teruggegeven dienen te worden
' returns     : array
*/
function clsWinkelwagentje_geefArrayVanProducten(sFieldname)
{
    // First, get a list of all cookies that pertain to this document.
    // We do this by reading the magic Document.cookie property
    var allcookies = document.cookie;
    if (allcookies == '') 
    {
      var emptyarray = new Array(0);
      return emptyarray;
    }

    // Now extract just the named cookie from that list.
    var start = allcookies.indexOf('winkelwagentje=');
    if (start == -1) return;   // cookie not defined for this page.
    start += 15;  // skip name 'winkelwagentje' and equals sign.
    var end = allcookies.indexOf(';', start);
    if (end == -1) end = allcookies.length;
    var cookieval = allcookies.substring(start, end);
    // Now that we've extracted the value of the named cookie, we've
    // got to break that value down into individual state variable 
    // names and values.  The name/value pairs are separated from each
    // other with ampersands, and the individual names and values are
    // separated from each other with colons.  We use the split method
    // to parse everything.

    var a = cookieval.split('&');  // break it into array of name/value pairs
    for(var i=0; i < a.length; i++)  // break each pair into an array
    {
        a[i] = a[i].split(':');
//        alert(a[i][1]);
    }

    // nu alle opgevraagde veldwaarden aan de nieuwe array toevoegen
    var aFieldValues, sFieldValues;
    sFieldValues = '';
    for(var i = 0; i < a.length; i++) 
    {
      if (a[i][0].indexOf(sFieldname) == 0)
      {
        sFieldValues += ((a[i][1]) + '~');
      }
    }
    if (sFieldValues.substring(sFieldValues.length-1, sFieldValues.length) == '~')
    {
      sFieldValues = sFieldValues.substring(0, sFieldValues.length - 1);
    }
      
    aFieldValues = sFieldValues.split('~');

    // return de array van veldwaarden van het opgevraagde veld 
//alert(aFieldValues.length);    
    return aFieldValues;
}

/*
' function    : geefArrayVanProductIDs
' description : geeft een array terug met productIDs (geen productnummers dus, maar de unieke IDs)
' arguments   : [in] sFieldname, een veldnaam. wat tussen dit veld en = staat wordt als ID genomen
' returns     : array
*/
function clsWinkelwagentje_geefArrayVanProductIDs(sFieldname)
{
    // First, get a list of all cookies that pertain to this document.
    // We do this by reading the magic Document.cookie property
    var allcookies = document.cookie;
    if (allcookies == '') 
    {
      var emptyarray = new Array(0);
      return emptyarray;
    }

    // Now extract just the named cookie from that list.
    var start = allcookies.indexOf('winkelwagentje=');
    if (start == -1) 
    {
      // cookie not defined for this page, return an empty array.
      var emptyarray = new Array(0);
      return emptyarray;   
    }
    start += 15;  // skip name 'winkelwagentje' and equals sign.
    var end = allcookies.indexOf(';', start);
    if (end == -1) end = allcookies.length;
    var cookieval = allcookies.substring(start, end);
    // Now that we've extracted the value of the named cookie, we've
    // got to break that value down into individual state variable 
    // names and values.  The name/value pairs are separated from each
    // other with ampersands, and the individual names and values are
    // separated from each other with colons.  We use the split method
    // to parse everything.

    var a = cookieval.split('&');  // break it into array of name/value pairs
    for(var i=0; i < a.length; i++)  // break each pair into an array
    {
        a[i] = a[i].split(':');
    }

    // nu alle opgevraagde veldwaarden aan de nieuwe array toevoegen
    var aFieldValues, sFieldValues;
    sFieldValues = '';
    for(var i = 0; i < a.length; i++) 
    {
      // Als het huidige veld het te vinden veld is dan...
      if (a[i][0].indexOf(sFieldname) == 0)
      {
        // het nummer toevoegen
        //sFieldValues += ((a[i][1]) + '~');
        sFieldValues += a[i][0].substring(sFieldname.length, a[i][0].length) + '~';
      }
    }

    if (sFieldValues.substring(sFieldValues.length-1, sFieldValues.length) == '~')
    {
      sFieldValues = sFieldValues.substring(0, sFieldValues.length - 1);
    }
      
    aFieldValues = sFieldValues.split('~');

    // return de array van veldwaarden van het opgevraagde veld 
//alert(aFieldValues.length);    
    return aFieldValues;
}


// klasse voor objecten die als byref variabele gebruikt kunnen worden
function byrefVariable()
{
  this.value = '';
}






//-->



