var Cookies = {
	expires:"",
	path:"; path=/",
	domain:"",
	secure:"",
	types:[],
	properties:{},
	validator: false,
    init: function (days, path, domain, secure) {
		this.expires = this.getExpires(days);
		this.path = (path) ? "; path=" + path : "; path=/";
		this.domain = (domain) ? "; domain=" + domain : "";
		this.secure = (secure) ? "; secure" : "";	
		
		// Check Validator object
		//if (typeof (Validator) != 'undefined' && Validator) this.validator = Validator;
		
		// Get cookies from client's browser
		this.types = this.getCookies();
    },
    getExpires: function(days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			return "; expires="+date.toGMTString();
		} else if (days == 0) {
			return "";
		} else {
			var date = new Date();
			date.setTime(date.getTime()+(365*10*24*60*60*1000));
			return "; expires="+date.toGMTString();			
		}    
    },    
    getCookies: function(dup) {
        var types = [];
        var allCookies = document.cookie.split('; ');
        for (var i=0;i<allCookies.length;i++) {
            var cookiePair = allCookies[i].split('=');
            if (cookiePair.length == 2) {
                cookiePair[0] = this.trim(cookiePair[0]);
                cookiePair[1] = this.trim(cookiePair[1]);
                if (cookiePair[0].length > 0 && cookiePair[1].length > 0) 
                    types[cookiePair[0]] = cookiePair[1];
            }
        }
        
        return types;
    },
    getAsJSON: function() {
		prop = {};
		for (var attr in this.types) {
			if (this.validator && 'filter' in this.validator) {
				if (this.validator.filter(attr)) {
					prop[attr] = unescape(this.types[attr]);
				}
			} else {
			    prop[attr] = unescape(this.types[attr]);
			}
		}
		
		if (this.validator && 'conditionalFilter' in this.validator) {
			prop = this.validator.conditionalFilter(prop);
		}
		
		return prop;
    },
    saveURL: function (url) {
        if (url.length > 0 && this.validator && 'get_url_parameter' in this.validator) {
            for (var i=0; i<this.validator.params.length; i++) {
				var pm = this.validator.get_url_parameter(this.validator.params[i], url);
				if (pm.length > 0) this.save(this.validator.params[i], pm);
            }
        }
    },
    saveCk: function (name, value, expires) {
        var cookie_str = name + "=" + escape(value);
        
        if (cookie_str.length > 1) {
            try {
                document.cookie = cookie_str + expires + this.path + this.domain + this.secure;
            } catch (e) {
                return false;
            }
        }
                
        return true;
    },
    save: function (name, value) {
		name = this.trim(name);
		
		/* Change the value in JSON */
		var type = typeof value;
		switch(type) {
			case 'undefined':
			case 'function' :
			case 'unknown'  : return false;
			case 'boolean'  : 
			case 'string'   : 
			case 'number'   : value = String(value.toString());
		}
		
		value = this.trim(value);
		
		var isSaved = false;
		if (name.length > 0 && value.length > 0) {
		    if (this.validator && 'filter' in this.validator) {
		        if (this.validator.filter(name)) {
		            isSaved = this.saveCk(name, value, this.expires);
		        }
		    } else {
		        isSaved = this.saveCk(name, value, this.expires);
		    }

			if (isSaved) this.types[name] = value;
		}
		
		return isSaved;
    },
    erase: function (name) {
		name = this.trim(name);
		var isSaved = false;
        
        if (this.types[name]) {
            isSaved = this.saveCk(name, '', this.getExpires(-1), false);
            if (isSaved) this.types[name] = undefined;
        }
        
        return isSaved;
    },
    read: function (name, defaultValue) {
		name = this.trim(name);
        if (this.types[name]) {
        	return unescape(this.types[name]);
        } else {
        	return defaultValue;
        }
    },
    exist: function (name) {
        name = this.trim(name);
        return (this.types[name]) ? true : false;
    },
	trim: function(v) {
		return v.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	}
};

Cookies.init(); 
