// -- COOKIES ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

function createCookie(name, value, expiry, path) {
	if (expiry && expiry != null) {
		var date;
		if (expiry.toGMTString) {
			// The expiry param is already a date
			date = expiry;
		} else {
			date = new Date();
			date.setTime(date.getTime()+(expiry*24*60*60*1000));
		}
		var expires = "; expires="+date.toGMTString();
	} else {
		expires = "";
	}

	if(!path && (path != "")) {
		path = "path=/;";
	} else if(path != "") {
		path = "path=" + path + ";";
	}
	
	var sCookie = document.cookie;

	var domainName;
	domainName = location.hostname;
	domainName = domainName.substr(domainName.indexOf("."));
	document.cookie = name + "=" + value + expires + ";" + path + "domain=" + domainName + ";";
	
	if(sCookie != document.cookie) {
		return true;
	} else {
		return false;
	}
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function getApplicationValue(name, cookieName) {
	if (!cookieName)
		cookieName = "SSVars";
	var sCookieValue = readCookie(cookieName);

	if(sCookieValue != null) {
		var sValues = sCookieValue.split("&");
		var sItem;
		for(var i = 0; i < sValues.length; i++) {
			sItem = sValues[i].split("=");
			if(sItem[0].toUpperCase() == name.toUpperCase()) {
				return unescape(sItem[1]);
				break;
			}
		}
	}
}

function setApplicationValue(name, value, cookieName, expiry, year, month, day) {
	if (!cookieName)
		cookieName = "SSVars";

	var sCookieValue = readCookie(cookieName);
	if(sCookieValue != null) {
		var sValues = sCookieValue.split("&");
		var sItem, bFound = false;
		for(var i = 0; i < sValues.length; i++) {
			sItem = sValues[i].split("=");
			if(sItem[0].toUpperCase() == name.toUpperCase()) {
				sValues[i] = sItem[0] + "=" + escape(value);
				bFound = true;
				break;
			}
		}
		if(!bFound) {
			sCookieValue += ("&" + name + "=" + escape(value));
		} else {
			sCookieValue = "";
			for(var i = 0; i < sValues.length; i++) {
				if(sValues[i].trim() != "") {
					sCookieValue += ("&" + sValues[i]);
				}
			}
			sCookieValue = sCookieValue.substring(1);
		}
	} else {
		sCookieValue = name + "=" + escape(value);
	}
	// If year/month/day is specified, use them instead of the expiry date
	if (year && year > 0) {
		expiry = new Date(year, month-1, day);
	}
	createCookie(cookieName, sCookieValue, expiry);
}

String.prototype.trim = function() {
	return this.replace(/^\s*/,"").replace(/\s*$/,"");
}

function cookieHitCount(name, cookieName, expiry, year, month, day, cookieExpiry) {
	var sVal = getApplicationValue(name, cookieName);
	var count = 0;
	var lastExpiry = 0;
	if (sVal && sVal != "") {
		var arr = sVal.split(",");
		// Get the last hit count
		count = Number(arr[0]);
		// Get the expiry date of the last hit count
		lastExpiry = Number(arr[1]);
		if (lastExpiry > new Date().getTime()) {
			// If it still hasn't expired, increase the hit count.
			count++;
		} else {
			// Otherwise, hit count is 0
			// Also reset the expiry
			count = 0;
			lastExpiry = 0;
		}
	}
	
	var date;
	// If year/month/day is specified, use them instead of the expiry date
	if (year && year > 0) {
		expiry = new Date(year, month-1, day);
	}
	if (expiry.toGMTString) {
		// The expiry param is already a date
		date = expiry;
	} else {
		// Only set a new expiry if the old one has expired already
		// This means, if you set the expiry to 7, the counter will be reset 7 days
		// after the first viewing
		if (lastExpiry == 0) {
			date = new Date();
			date.setTime(date.getTime()+(expiry*24*60*60*1000));
		} else {
			date = new Date(lastExpiry);
		}
	}
	if (!cookieExpiry) {
		cookieExpiry = 7; // Default the cookie to expire in 7 days.
	}
	setApplicationValue(name, count + "," + date.getTime(), cookieName, cookieExpiry);
	
	return count;
}
// -- END COOKIES ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

