// ***************************************************************************** //
// cookie_write(p) = sets a cookie value
// p = {cookie:"X",value:"Y"} = required to pass
// p.cookie = the cookie name to save
// p.value = the value of the name
// p.path = add "/folder_name/" this will set the cookie for the just that path, not for the whole domain.
// p.domain = set for a subdomain, it will be available to the whole domain
// p.secure = the cookie can only be sent to a Secure Sockets Layer (SSL) server
// p.constant = default is a session cookie
// ***************************************************************************** //
function cookie_write(p){
	if(typeof(p) != "object"){return}
	if(p.constant){
		var lDate = new Date();
		lDate.setTime(lDate.getTime());
		var new_date = gCookie.props.days * 1000 * 60 * 60 * 24;
		p.expires = "; expires=" + new Date(lDate.getTime()+(new_date));
	}else{
		// default is a session cookie //
		p.expires = "";
	}

	p.path = p.path || "/";
	p.domain = (p.domain)? "; domain=" + p.domain : "";
	p.secure = p.secure || false;
	document.cookie = p.cookie + "=" + escape(p.value) + p.expires + "; path=" + p.path + p.domain + ((p.secure)? "; secure" : "");
}

// ***************************************************************************** //
// cookie_read(p) returns a value in a cookie
// p = {cookie:"X"} = required to pass
// ***************************************************************************** //
function cookie_read(p){
	// Return null if no cookies set //
	if(document.cookie == ""){return null;}
	// assign the cookie to an array //
	var lCookie = document.cookie.split(";");

	// loop through values //
	var i = 0;
	for (var i = 0; i < lCookie.length; i++){
		// check for space //
		if (lCookie[i].charAt (0) == " "){
			lCookie[i] = lCookie[i].substr(1,lCookie[i].length - 1);
		}
		// Find the cookie name//
		var lName = (lCookie[i].indexOf("=") > 0)? lCookie[i].split("=")[0] : lCookie[i];
		// find the cookie value //
		var lValue = (lCookie[i].indexOf("=") > 0)? lCookie[i].split("=")[1] : "";
		// return cookie value //
		if (lName == p.cookie){
			return unescape(lValue);
		}
	}

	// cookie was not found //
	return null;
}

// ***************************************************************************** //
// cookieJar_write(p) allows multiple values to a single cookie
// p = {cookie:"X",name:"Y",value:"Z"} = required to pass
// ***************************************************************************** //
function cookieJar_write(p){
	var lFound = false;
	var lString = "";
	var lCookie = cookie_read(p);
	
	if(lCookie != null && lCookie != ""){
		var lArray = lCookie.split("~");
		for(var i= 0; i < lArray.length; i++) {
			var lName = lArray[i].split("=")[0];
			var lValue = lArray[i].split("=")[1];
			// cookie name found //
			if(lName == p.name){
				lFound = true;
				lValue = p.value;
			}
			
			if(lString.length > 0){
				lString += "~";
			}
			lString += lName + "=" + lValue;
		}
	}

	if(!lFound){
		if (lString.length > 0){
			lString += "~";
		}
		lString += p.name + "=" + p.value;
	}
	
	// reassign the cookie properties //
	p.value = lString;
	cookie_write(p)
}

// ***************************************************************************** //
// cookieJar_read(p) returns single value in a multiple cookie
// p = {cookie:"X",name:"Y"} = required to pass
// ***************************************************************************** //
function cookieJar_read(p){
	var lCookie = cookie_read(p);
	if(lCookie != null && lCookie != ""){
		var lArray = lCookie.split("~");
		for(var i = 0; i < lArray.length; i++){
			var lName = lArray[i].split("=")[0];
			var lValue = lArray[i].split("=")[1];
			if (lName == p.name){
				return unescape(lValue);
			}
		}
	}
	
	// subcookie was not found return null //
	return null;
}

// ***************************************************************************** //
// cookie_delete(p)
// p = {cookie:"X"} = required to pass
// ***************************************************************************** //
function cookie_delete(p){
	if(typeof(p) != "object"){return}
	var lCookie = cookie_read(p);
	if(lCookie != null){
		p.path = p.path || "/";
		p.domain = (p.domain)? "; domain=" + p.domain : "";
		document.cookie = p.cookie + "=" + ";path=" + p.path + p.domain + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}

