// A Cookie utility class
function Cookie(name){
	this.$name = name;
	
	var allCookies = document.cookie;
	
	if(allCookies == "") return;
	
	var cookies = allCookies.split(';');
	
	var cookie = null;
	
	for(var i = 0; i < cookies.length; i++) {

		var cookieTemp = trimBlankSpace(cookies[i]);
		
 		if(cookieTemp.substring(0, (name.length+1)) == (name + "=")) {
			cookie = cookieTemp;
			break;
		}
	}
	
	if(cookie == null) return;
	
	var cookieValue = cookie.substring(name.length + 1);
	
	var a = cookieValue.split('&');
	
	for(var i = 0; i < a.length; i++) {
		
		a[i] = a[i].split(':'); //a[i][i]
	}
	
	for(var i = 0; i < a.length; i++) {
	
		this[a[i][0]] = decodeURIComponent(a[i][1]);
	}
}//end cookie constructor

Cookie.prototype.store = function(daysToLive, path, domain, secure) {
	
	var cookieValue = "";
	
	for(var prop in this) {
		
		if((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function'))
			continue;
		
		if(cookieValue != "") cookieValue += '&';
		
		cookieValue += prop + ':' + encodeURIComponent(this[prop]);
	}
	
	var cookie = this.$name + '=' + cookieValue;
	
	if(daysToLive || daysToLive == 0) {
		
		cookie += "; max-age=" + (daysToLive * 24 * 60 * 60);
	}
	
	if(path) cookie += "; path=" + path;

	if(domain) cookie += "; domain=" + domain;
	
	if(secure) cookie += "; secure";  
	
	document.cookie = cookie;
}//end store function

Cookie.prototype.remove = function(path, domain, secure) {
	
	for(var prop in this) {
		
		if(prop.charAt(0) != '$' && typeof this[prop] != 'function') {
			
			delete this[prop];
		}
	}
	
	this.store(0, path, domain, secure);
}

Cookie.enabled = function() {
	
	if(navigator.cookieEnabled != undefined) return navigator.cookieEnabled;
	
	if(Cookie.enabled.cache != undefined) return Cookie.enabled.cache;
	
	document.cookie = "testcookie=test; max-age=10000";
	
	var cookies = document.cookie;
	
	if(cookies.indexOf("testcookie=test") == -1) {
		
		return Cookie.enabled.cache = false;
	}else {
		document.cookie = "testcookie=test; max-age=0";
		return Cookie.enabled.cache = true;
	}
}//end enabled function

/*use trimBlanSpace function to delete the leading and tailing blank spaces*/
function trimBlankSpace(str) {
	
	return str.replace(/^\s+|\s+$/g, '');
	
}



