/**
 * 
 * Sets a cookie.
 *
 * You need to put the name and values in quotes when you call the function, like this:
 * setCookie( 'mycookie', 'visited 9 times', 30, '/', '', '' );
 * 
 * Don't forget to put in empty quotes for the unused parameters or you'll get an error when you run the code. 
 * This makes the cookie named 'mycookie', with the value of 'visited 9 times', and with a life of 30 minutes, 
 * and the cookie is set to your root folder.
 *
 * The Set_Cookie values for 'domain' and 'secure' are not utilized. Use 'domain' on the Javascript cookie if 
 * you are using it on a subdomain, like widgets.yoursite.com, where the cookie is set on the widgets subdomain, 
 * but you need it to be accessible over the whole yoursite.com domain.
 *
 * It's good practice to not assume the path to the site root will be set the way you want it by default, 
 * so do this manually as a rule, '/'. If no value is set for expires, it will only last as long as the 
 * current session of the visitor, and will be automatically deleted when they close their browser. 
 * 
 * @param name
 * @param value
 * @param expires the time in minutes until expiring. If empty, expires at end of session.
 * @param path
 * @param domain
 * @param secure
 * @return
 */
function setCookie(name, value, expires, path, domain, secure) {
	// remember current time
	var today = new Date();
	today.setTime(today.getTime());

	/*
	 * if the expires variable is set, make the correct expires time, the
	 * current script below will set it for x number of minutes
	 */
	if (expires) {
		expires = expires * 1000 * 60;
	}
	var expires_date = new Date(today.getTime() + (expires));

	document.cookie = name + "=" + escape(value)
			+ ((expires) ? ";expires=" + expires_date.toGMTString() : "")
			+ ((path) ? ";path=" + path : "")
			+ ((domain) ? ";domain=" + domain : "")
			+ ((secure) ? ";secure" : "");
}


/**
 * Gets the cookie value
 * 
 * @param check_name
 * @return the value of the cookie with name <code>check_name</code>
 */
function getCookie(check_name) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split(';');
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for (i = 0; i < a_all_cookies.length; i++) {
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split('=');

		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if (cookie_name == check_name) {
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no =
			// sign, that is):
			if (a_temp_cookie.length > 1) {
				cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g,
						''));
			}
			// note that in cases where cookie is initialized but no value, null
			// is returned
			return cookie_value;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	return null;
}

/**
 * Deletes a cookie if it exists
 * @param name
 * @param path
 * @param domain
 * @return
 */
function deleteCookie (name,path,domain) {
	  if (getCookie(name)) {
	    document.cookie = name + "=" +
	    ((path) ? "; path=" + path : "") +
	    ((domain) ? "; domain=" + domain : "") +
	    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	    }
}
	 


