    function readCookie(name) {

    	if (document.cookie.length > 0) {
    		begin = document.cookie.indexOf(name + "=");
    		if (begin != -1) {
    			begin += name.length + 1;
    			end = document.cookie.indexOf(";", begin);
    			
    			if (end == -1) end = document.cookie.length;
    			
    			return unescape(document.cookie.substring(begin, end)); 
    		}
    	}
    	
    	return null;
    }

    function writeCookie(name, value, expiredays) {

    	var ExpireDate = new Date ();
    	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

    	document.cookie = name + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
    }
    
    function appendToCookie(name, value, expiredays) {
        var cookieValue = readCookie(name);
        if (cookieValue) {
            cookieValue += "&" + value;
        }
        else {
            // we create the cookie
            cookieValue = value;
        }
        
        writeCookie(name, cookieValue, expiredays);
    }
    
    function deleteCookie(name) {
    	if (readCookie(name)) {
    		document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    	}
    }
    
    
    // ----------- Session cookie methods ----------------
    
    function writeCookieInSession(name, value) {
    	document.cookie = name + "=" + escape(value);
    }
        
    function appendToCookieInSession(name, value) {
        var cookieValue = readCookie(name);
        if (cookieValue) {
            cookieValue += "&" + value;
        }
        else {
            // we create the cookie
            cookieValue = value;
        }
        
        writeCookieInSession(name, cookieValue);
    }

    function deleteCookieFromSession(name) {
    	if (readCookie(name)) {
    		document.cookie = name + "=";
    	}
    }
    
    
    // --------------------- test methods -------------------
    
    function showCookie(name) {
        var cookieValue = readCookie(name);
        if (cookieValue) {
            var arrStrings = cookieValue.split("&");
            var str = "cookieValue = " + cookieValue;
            for (i = 0; i < arrStrings.length; i++) {
                str += "\n  " + "arr[" + i + "] = " + arrStrings[i]; 
            }
            alert(str);        
        }
        else {
            alert('We found no cookie!');
        }
    }
    
    
