//The Cookie Class 
function Cookie(path,domain,duration,secure) {
	this.defPath = path;
	this.defDomain = domain;
	this.defDuration = duration;
	this.defSecure = secure;
	return this;
}

/**
*  Send a cookie
*
*
*  @param string name;
*  @param string value;
*  @param string duration;
*  @param string path;
*  @param string domain;
*  @param bool secure;
*  @return null
*/
Cookie.prototype.setCookie = function (name, value, duration, path, domain, secure) {
	if(!duration && this.defDuration) duration = this.defDuration;
	if(!path && this.defPath) path = this.defDuration;
	if(!domain && this.defDomain) domain = this.defDomain;
	if(!secure && this.defSecure) secure = this.defSecure;
	if(duration){
     var expires = new Date();
     var curTime = new Date().getTime();
     expires.setTime(curTime + (1000 * 60 * duration));
   }
	this.setCookieDT(name, value, expires, path, domain, secure);
}

/**
*
*
*
*
*/
Cookie.prototype.setGroupCookie = function (cookiename,name, value, duration, path, domain, secure) {
	var val = this.getCookie(cookiename);	
	if(typeof val=="undefined" || val=="") {
		return this.setCookie(cookiename,name+":"+value,duration, path, domain, secure);
	}
	var arr = val.split(";;");
	var acookie;
	var found=false;
	var i;
	for(i=0;i<arr.length;i++) {
		acookie = arr[i].split(":");	
		if(acookie[0]==name) {
			arr[i] = name+":"+value;
			found = true;
		}
	}
	if(!found) {
		arr[i] = name+":"+value;
	}
	var str = arr.join(";;");
	return this.setCookie(cookiename,str,duration, path, domain, secure);
}
/**
*
*
*/
Cookie.prototype.getGroupCookie = function (cookiename,name) {
	var val = this.getCookie(cookiename);	
	if(typeof val=="undefined" || val=="") return "";
	var arr = val.split(";;");
	var acookie;
	var found=false;
	for(var i=0;i<arr.length;i++) {
		acookie = arr[i].split(":");	
		if(acookie[0]==name) {
			return acookie[1];
		}
	}
	return "";
}
/**
*
*
*/
Cookie.prototype.delGroupCookie = function (cookiename,name,duration, path, domain, secure) {
	var val = this.getCookie(cookiename);
	if(typeof val=="undefined" || val=="") return "";
	var arr = val.split(";;");
	var acookie;
	var res = new Array();
	var j=0;
	for(var i=0;i<arr.length;i++) {
		acookie = arr[i].split(":");	
		if(acookie[0]==name) continue;
		res[j++] = arr[i];
	}
	var str = res.join(";;")
	return this.setCookie(cookiename,str,duration, path, domain, secure);
}

/**
*  Set a cookie
*
*
*  @param string name;
*  @param string value;
*  @param string expires;
*  @param string path;
*  @param string domain;
*  @param bool secure;
*  @return null
*/
Cookie.prototype.setCookieDT = function (name, value, expires, path, domain, secure) {
  	document.cookie =
    name+"="+encodeURIComponent(value)+
    (expires ? "; expires="+expires.toGMTString() : "")+
    (path    ? "; path="   +path   : "")+
    (domain  ? "; domain=" +domain : "")+
    (secure  ? "; secure" : "");
}

/**
*  get cookie.
*
*  @param string name;
*  @return string
*/
Cookie.prototype.getCookie = function (name) {
  cookie = " "+document.cookie;
  offset = cookie.indexOf(" "+name+"=");
  if (offset == -1) return undefined;
  offset += name.length+2;
  end     = cookie.indexOf(";", offset)
  if (end == -1) end = cookie.length;
  return decodeURIComponent(cookie.substring(offset, end));
}
/**
*  check cookie exist or not .
*
*  @param string name;
*  @return string
*/
Cookie.prototype.existCookie = function (name) {
//	if(typeof(Cookie.prototype.getCookie(name))=='undefined') return false;
	var cookieVal = new strUtil(Cookie.prototype.getCookie(name));
//	if(!cookieVal.String) return false;
	cookieVal.trim(' ');
	if(cookieVal.length()==0) return false;
	return true;
}

/**
*  delete cookie.
*
*  @param string name;
*  @param string path;
*  @param string domain;
*  @return null
*/
Cookie.prototype.delCookie = function (name,path,domain) {
  if (this.getCookie(name)) {
    var date = new Date("January 01, 2000 00:00:01");
    this.setCookieDT(name, "", date, path, domain);
  }
}
