/* Finds value of a given cookie by searching for its name in the cookie string */
function getCookie( cookieName ) {
	var startPos = document.cookie.indexOf( cookieName + "=" );
	
	var cookieLength = startPos + cookieName.length + 1;
	
	if ( ( !startPos ) && ( cookieName != document.cookie.substring( 0, cookieName.length ) ) ) {
		return null;
	}
	
	if( startPos == -1 ) {
		return null;
	}
	
	var endPos = document.cookie.indexOf( ';', cookieLength );
	
	if( endPos == -1 ) {
		endPos = document.cookie.length;
	}
	
	return unescape( document.cookie.substring( cookieLength, endPos ) );
}

/* Sets a certain cookie by giving it a value and expiration date, path, and/or domain */
function setCookie( cookieName, cookieValue, cookieExpires, cookiePath, cookieDomain ) {
	var dateToday = new Date();
	dateToday.setTime( dateToday.getTime() );
	
	if ( cookieExpires ) {
		cookieExpires = cookieExpires * 1000 * 60 * 60 * 24;
	}
	
	var cookieExpireDate = new Date( dateToday.getTime() + ( cookieExpires ) );
	
	document.cookie = cookieName + '=' + escape( cookieValue ) +
		( ( cookieExpires ) ? ';expires=' + cookieExpireDate.toGMTString() : '' ) +
		( ( cookiePath ) ? ';path=' + cookiePath : '' ) +
		( ( cookieDomain ) ? ';domain=' + cookieDomain : '' );
}

/* Deletes a cookie by finding it and setting its expiration to way back in the day */
function deleteCookie( cookieName, cookiePath, cookieDomain ) {
	if( getCookie( cookieName ) ) {
		document.cookie = cookieName + '=' +
			( ( cookiePath ) ? ';path=' + cookiePath : '') +
			( ( cookieDomain ) ? ';domain=' + cookieDomain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
	}
}