/* 
 	. Created by Lis/2k1. rev# 22.8.2 : 15:0:32
	------------------------------------
	. (c) Reservation Art, 2002
*/

// CVSinfo
// $Id: cCookies.js,v 1.3 2002/11/24 02:23:21 lis Exp $
// $Revision: 1.3 $
// $Name:  $

/**	This class manipulate and unify accees to browser cookies 
	@param iTimeout - Optionally. Default expiration period. 
		0 (Zero)- no expiration period needed.
*/
function cCookies( iTimeout ) {
	this.isDisable = true;
	this.varPairs = new Array();
	this.maxPairs = 20;
	this.defaultExpPeriod = 0;	
	this.objS = new cSerialize();
	this.activeDomain = ""; //"; domain=lis"; // ; domain=lis
 	this.activePath = "; path=/"
	
	this.Init = function() {
		this.isDisable = false;
	
		if ( typeof(document.cookie) == "undefined" ) {
			this.ShowWarning();
			this.isDisable = true;
			return(this);
		}
		
		var vName = "sdy4jd783j8de3";
		var vValue = "v" + (new Date()).getTime();
		
		this.SetVariable( vName, vValue );
		if ( this.GetValue( vName ) != vValue ) {
			this.ShowWarning();
			this.isDisable = true;
			return( this );
		} else {
			this.RemoveVariable( vName );
		}
			
		if ( typeof(iTimeout) !== "undefined" )
			this.SetExpirationPeriod( iTimeout ); 
		this.isDisable = false;
		return( this );
	}
	
	this.ShowWarning = function() {
		var msg = "Warning! This page for correct appear using cookies, "
				+ "but now, in your browser this option is turned off or not enabled!";
		alert( msg );
	}
	
	
	this.SetVariable = function( iName, iValue, iExpPeriodSec ) {
		if ( this.isDisable )
			return;
		
		if ( document.cookie.indexOf( iName + "=" ) == -1 ) {
			var c = document.cookie.match( /.+?=.*?(; |$)/g );
			if ( c != null && c.length >= this.maxPairs )
				alert("Too many vars stored in cookies!");
		}
			
		var expDate = "";
		if ( typeof(iExpPeriodSec) != "undefined" 
			|| this.defaultExpPeriod != 0 ) {
			var it = iExpPeriodSec || this.defaultExpPeriod;
			expDate = "; expires=" 
				+ new Date( new Date().getTime() + it * 1000 ).toGMTString();
		} else {
			expDate = "; expires=" 
				+ new Date( new Date().getTime() + 60*60*24*31*12*1000 ).toGMTString();
		}
		
		var dstr = iName + "=" 
			+ escape( this.objS.Serialize(iValue) )
			+ expDate
			+ this.activeDomain 
			+ this.activePath;
		document.cookie = dstr;
		return( this );
	}
	
	this.RemoveVariable = function ( iName ) {
		if ( this.isDisable )
			return;
		document.cookie = iName 
			+ "=" + null + "; expires=Fri, 31 Dec 1999 23:59:59 GMT;"
			+ this.activeDomain 
			+ this.activePath;
		return( this );
	}

	this.GetValue = function( iName ) {
		if ( this.isDisable ) 
			return( null );
		var re = new RegExp( iName + "=(.*?)(; |$)" );
		if ( re.test( document.cookie ) ) {
			var value = RegExp.$1;
			if ( value != "")
				var result = this.objS.Unserialize( unescape(value) );
			else
				result = null;
			return( result ); 
		}		
		return( null );
	}
	
	/** iTimeout - in sec */
	this.SetExpirationPeriod = function( iTimeout ) {
		if ( this.isDisable ) 
			return;
		this.defaultExpPeriod = iTimeout;
		return( this );		
	}
	
	this.GetAllData = function () {
		if ( this.isDisable ) return( null );
		return( document.cookie );
	}
	
	this.RemoveAll = function() {
		re = /(.+?)=.*?(; |$)/
		while(re.test( document.cookie) ) {
			this.RemoveVariable( RegExp.$1 );
		}
		return( this );
	}

	this.Init( iTimeout );	
	return(this);
}