/*
Set Cookie Javascript Function 
You need to put the name and values in quotes when you call the function, like this:
Set_Cookie( '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 days. 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. 

This will set the cookie. It's the most complicated part, but actually for most purposes all you need to set are the first 3 parameters, name, value, and expires. If you want the cookie available site wide and are setting it in a folder below the site root, you will need to add '/' as a path variable, that tells the script to set the cookie for the root of the domain, not just the current folder. Generally 'domain' and 'secure' are not something you will be needing to use. 
*/

function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	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 days, to make it
	// for hours, delete * 24, for minutes, delete * 60 * 24
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	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" : "" );

}

// this function gets the cookie, if it exists

/*
This will retrieve the cookie by name, if the cookie does not exist, it will return false, so you can do things like if ( Get_Cookie( 'your_cookie' ) ) do something. 
*/

function Get_Cookie( name ) {
	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

// this deletes the cookie when called
/*
Here all you need to do is put in: Delete_Cookie('cookie name') and the cookie will be deleted. 
*/
function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
/*
If you run this, what will happen is that you will get two alerts, the first one will have the content you set with Set_Cookie, the second will say 'it is gone' since you have now deleted the cookie. That's about it. 

<script type="text/javascript">
Set_Cookie( 'test', 'it is real' );
if ( Get_Cookie( 'test' ) ) alert( Get_Cookie('test'));
Delete_Cookie('test');
( Get_Cookie( 'test' ) ) ? alert( Get_Cookie('test')) : alert( 'it is gone');
</script>
*/