//-----------------------------------------------------------------------------
// Cookie.js
// 2007.05.15 - customized by rk7138 for AT&T Communications
//-----------------------------------------------------------------------------
// METHODS
//   getCookies(name);
//   set(name, value, days, path, domain, secure);
//   remove(name);
//   isEnabled(alertMessage);
//   stringify(object);
//   parse(object);
//
// USAGE
//   enabled = Cookie.isEnabled("Please enable cookies!");
//   Cookie.set("test_cookie", "123456", "/", "mydomain.com", true );
//   value = Cookie.get("test_cookie");
//   Cookie.remove("test_cookie");
//

var Cookie = {
	VERY_OLD_DATE: "Thu, 01-Jan-1970 00:00:01 GMT",
	MILLISECS_PER_DAY: 24*60*60*1000,	
	TEMP_NAME: 'SOMETHING_VERY_UNUSUAL',
	TEMP_VALUE: '1234567890',
		
	get: function(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 this.parse( unescape( document.cookie.substring( len, end ) ) );
	},
		
	set: function ( name, value, days, path, domain, secure ) {
		var stringified = this.stringify(value);
        var expires = -1;
        
        if ( typeof days == 'number' && days >= 0 ) {
			var today = new Date();
			
			today.setTime(today.getTime() + ( days * this.MILLISECS_PER_DAY) );
			expires = today.toGMTString();
        }
        
        value = escape(value);
        
        document.cookie = name + "=" + stringified + ";" +
		(( expires != -1) ? " expires=" + expires + ";" : "") + 
		(( path         ) ? "; path=" + path : "") + 
		(( domain       ) ? "; domain=" + domain : "") + 
		(( secure       ) ? "; secure" : "");
	},
	
	
	remove: function ( name, path, domain ) {		
		if ( this.get( name ) ) {
			document.cookie = name + "=" +
		   	( ( path   ) ? ";path=" + path : "") +
		   	( ( domain ) ? ";domain=" + domain : "" ) +
			               ";expires=" + Cookie.VERY_OLD_DATE;
		}
	},
	
	isEnabled: function (alertMessage){		
		this.set(this.TEMP_NAME, this.TEMP_VALUE);
		
		if (this.TEMP_VALUE == this.get(this.TEMP_NAME)){ return true; }
		if (alertMessage){ alert(alertMessage); }
		
		return false;
	},
	
	stringify: function (hashObj){
		if (typeof hashObj == 'undefined'){ return hashObj; }
		if (typeof hashObj == 'string'   ){ return hashObj; }
		if (typeof hashObj == 'number'   ){ return hashObj; }
		if (typeof hashObj == 'function' ){ return hashObj; }
		if (typeof hashObj == 'boolean'  ){ return hashObj == true ? 'true' : 'false'; }
	
		var outputStr = '';
		
		if (hashObj.constructor == Array){
			outputStr += '[';
			
			for (var i = 0; i< hashObj.length; i++){
				var value = hashObj[i];
				
				if (typeof value != 'string'){ value = this.stringify(value); }
				else { value = "\'" + value + "\'"; }
				
				outputStr += value;
				
				if (i < hashObj.length - 1){ outputStr += ","; }
			}
			
			outputStr += ']';
		}
		else {
			outputStr += '{';
			
			for (var i in hashObj){
				var key = i;
				var value = hashObj[i];
				
				if (typeof value != 'string'){ value = this.stringify(value); }
				else { value = "\'" + value + "\'"; }
				
				outputStr += "\"" + key + "\":" + value + ",";				
			}

			outputStr = outputStr.substr(0, outputStr.length - 1);
			outputStr += '}';
		}
		
		return outputStr;
	},
		
	parse: function (givenStr){
		return eval('(' + givenStr + ')');	
	}
}
