﻿function checkDate( aDateString )
{
	var theDate = new Date( aDateString ); 

	if( isNaN( theDate ) )
		if( aDateString == "" )
			return null;
		else
			throw Error( "The date is not valid. \'" + aDateString + "\' is not a Date" );
	else
		return theDate;
}

function addAction( name, value, expire, path, domain, secure )
{
	try
	{
		new Cookie( document, name ).set( value, checkDate( expire ), path, domain, secure );
		//document.location.reload();
	}
	catch( error )
	{
		if( error instanceof Error )
			alert( error.name + ": " + error.message );
		else
			alert( error.toString() );
	}
}

function deleteAction( name )
{
	try
	{
		new Cookie( document, name ).remove();
		document.location.reload();
	}
	catch( error )
	{
		if( error instanceof Error )
			alert( error.name + ": " + error.message );
		else
			alert( error.toString() );
	}
}

function modifyAction( name, value, expire, path, domain, secure )
{
	try
	{
		new Cookie( document, name ).set( value, checkDate( expire ), path, domain, secure );
		document.location.reload();
	}
	catch( error )
	{
		if( error instanceof Error )
			alert( error.name + ": " + error.message );
		else
			alert( error.toString() );
	}
}



// cookies.js
// Javascript 1.2 or later
// Author: Yushin Hozumi (Studio RAIN)
// URL:http://www.studio-rain.biz/


function Cookie( document, name )
{
	this.document = document;
	this.name = name;
}

new Cookie();

Cookie.prototype.set = function( value, expires, path, domain, secure )
{
	this.document.cookie = this.name + "=" + escape( value ) +
    ( ( expires )? "; expires=" + expires.toGMTString() : "" ) +
    ( ( path )? "; path=" + path : "" ) +
    ( ( domain )? "; domain=" + domain : "" ) +
    ( ( secure )? "; secure" : "" );

	return this;
}

Cookie.prototype.get = function()
{
	var theCookies = this.document.cookie.split("; " );
	
	for( var i in theCookies )
	{
		var aCookie = theCookies[i];
		
		if( aCookie.split( "=" )[0] == this.name )
			return unescape( aCookie.split( "=" )[1] )
	}

	return null;
}

Cookie.prototype.remove = function()
{
	if( this.get() )
	{
		var expire = new Date;
		expire.setTime( expire.getTime() - ( 365 * 24 * 60 * 60 * 1000) );
		
		this.document.cookie = this.name + "=" +
//		( ( path )? "; path=" + path : "" ) +
//		( ( domain )? "; domain=" + domain : "" ) +
		"; expires=" + expire.toGMTString();
	}

	return this;
}

Cookie.parse  = function( document )
{
	if( !document || !document.cookie || document.cookie == "" )
		return null;
		
	var theCookieStrings = document.cookie.split("; " );
	var theCookies = new Object();
	
	//alert(theCookieStrings.length);
	
	var contador = 1;
	
	for( var i in theCookieStrings )
	{
		if(contador <= theCookieStrings.length) {
			aCookie = theCookieStrings[i];
			contador ++;
			var theName = aCookie.split( "=" )[0];
			
			if( !theCookies[theName] )
				theCookies[theName] = new Cookie( document, theName );
		}
	}	

	return theCookies;
}

