/**
 * removes whitespace from a string
 */
String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g,"");
}

/**
 * creates a cookie and writes it to the client
 *
 * @param string $name of the cookie
 * @param string $value of the cookie
 * @param int $days after which cookie expires
 */
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=/";

}//createCookie

/**
 * counts the number of cookies
 *
 * @param string $name of the cookies to count
 */
function countCookies(name) {

    var num_cookies = 0;
    var the_cookie = '';

    // create an array containing all cookies
    var cookies = document.cookie.split(';');

    for (var i = 0; i < cookies.length; i++) {

        the_cookie = cookies[i];
        the_cookie = the_cookie.trim();

        // check if name matches name of the cookie and increase counter if it does
        if (the_cookie.substring(0, name.length) == name ) {

            num_cookies++;

        }

    }

    // we only care about the number of products.
    // since each product is made up of two cookies, we devide the number of
    // cookies by 2.
    return Math.round(num_cookies / 2);

}//countCookies

/**
 * erases cookie
 *
 * @param string $name of the cookie to erase
 */
function eraseCookie(name) {

        createCookie(name,"",-1);

}//eraseCookie

/**
 * adds an item and it's price to the shoppingkart
 *
 * @param string $item to add to the shoppingkart
 * @param string $price of the item
 */
function addToShoppingKart(item, price) {

    // get next available key for our shoppingkart array
    var nextItem = countCookies('ehKart');

    // add the product and it's price
    createCookie(
                    'ehKart[' + nextItem + '][item]',
                    item,
                    null);

    createCookie(
                    'ehKart[' + nextItem + '][price]',
                    price,
                    null);

    // inform user item was added to shoppingkart
    alert('Item is toegevoegd aan de bestellijst.');

}//addToShoppingKart

/**

 * removes all cookies with an alert and a reload so that it is visible to the user

 */

function removeAllCookies()
{
    var cookies = document.cookie.split(";");

    for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i];
        var eqPos = cookie.indexOf("=");
        var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
    }
    alert('Uw bestellijst is leeg!');
    window.location.reload();
}//removeAllCookies

/**

 * just removes cookies

 */
 
function removeAllCookiesAuto()
{
    var cookies = document.cookie.split(";");

    for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i];
        var eqPos = cookie.indexOf("=");
        var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
    }
}//removeAllCookiesAuto

