// JavaScript Document
/* Function to updateCookies after each event is triggered */
function addValueToCookie(cookieName, cookieValue){
    // if cookie doesnt exist, create it, and add the value
    if(!$.cookie(cookieName)){
        $.cookie(cookieName, cookieValue);
    // otherwise, get it, split it, and add the value
    } else {
        var cookieVals = $.cookie(cookieName).split(",");
        log("Number of values:\t" + eval(cookieVals.length + 1));
        if(cookieVals.length < 4)
            cookieVals.push(cookieValue);
        $.cookie(cookieName, cookieVals);
    }
    log("Cookie is: "+$.cookie(cookieName));
}

/* Function to updateCookies after each event is triggered */
function removeValueFromCookie(cookieName, cookieValue){
    if($.cookie(cookieName) != null){
        var cookieVals = $.cookie(cookieName).split(",");
        log("Removing "+cookieValue);
        for(i = 0; i < cookieVals.length; i++){
            if( cookieVals[i] == cookieValue ){
                cookieVals.splice(i, 1);
            }
        }
        log("Number of values:\t" + cookieVals.length);
        $.cookie(cookieName, cookieVals);
    }
    log("Cookie is: "+$.cookie(cookieName));
}

function deleteCookie(cookieName){
    $.cookie(cookieName, null);
    log("Cookie "+cookieName+" deleted.");
}

String.prototype.cookieValueExists = function(value) {
    return ($.cookie(this) != null && $.cookie(this).indexOf(value) > -1)
}