
var currentDomain;
var currentLocation = window.location.host;

if ( currentLocation.indexOf( 'localhost' ) != -1 || document.layers )
{
currentDomain = '';
}
else
{
tmpColon = currentLocation.split( ':' );
var domain = tmpColon[0];

var domainpieces = domain.split('.');
if (domainpieces.length == 4 && domainpieces[ 3] == parseInt( domainpieces[ 3]) )
{
currentDomain = '; Domain=' + domain;
}
else if (domainpieces.length > 1)
{
while (domainpieces.length > 2)
{
domainpieces.shift();
}
currentDomain = '; Domain=' + '.' + domainpieces.join('.'); 
}
else 
{
currentDomain = '';
}
}

function getCookie( name )
{

var nvl = document.cookie.split( ';' );

for (var n=0; n < nvl.length; n++)
{

var nv = nvl[ n ];
if (nv.charAt( 0 ) == ' ')
nv = nv.substring( 1 );
var nvp = nv.split( '=' );
if (nvp[0] == name)
{
if ( typeof( nvp[1] ) == 'undefined' )
return '';
else
return nvp[1];
}
}
return '';
}

function setCookie( name, value, durationSeconds )
{
value += '; Path=/' + currentDomain;

if (typeof(durationSeconds) != 'undefined')
value += '; Max-age=' + durationSeconds;

document.cookie = name + '=' + value;
}

function setOneMonthCookie( name, value )
{
var oneMonthFromNow = new Date();
oneMonthFromNow.setMonth(oneMonthFromNow.getMonth() + 1);

value += '; Path=/' + currentDomain + '; expires=' + oneMonthFromNow.toGMTString();
document.cookie = name + '=' + value;
}

function setExpiringCookie( name, value, expireDate )
{
value += '; Path=/' + currentDomain + '; expires=' + expireDate.toGMTString();
document.cookie = name + '=' + value;
}

function deleteCookie( name )
{
if ( getCookie( name ) != '' )
document.cookie = name + "=; Path=/" + currentDomain + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function openCookieWindow(loc,title,properties)
{
var sessionID = getCookie("JSESSIONID");
var splitLoc = loc.split("?");
var i=0;
loc = splitLoc[i] + ";jsessionid=" + sessionID;
i++;
while(typeof(splitLoc[i]) != 'undefined'){
loc += "?" + splitLoc[i];
i++;
}
return window.open( loc ,title,properties);
}

function ComplexCookie(name)
{
this.name = name;
this.data = [];

this.expiresAt = new Date();
this.expiresAt.setMonth(this.expiresAt.getMonth() + 1);

this.retrieve();
}

ComplexCookie.prototype.retrieve = function()
{
var data = getCookie(this.name);
if (typeof(data) == "undefined" || !data || data == "")
{
this.data = [];
return;
}

var parts = data.split(",");
var result = [];
for (var i in parts)
{
var part = parts[i].split(":");
result[part[0]+""] = part[1] + "";
}

this.data = result;
}

ComplexCookie.prototype.setExpiresAt = function(expiresAt)
{
this.expiresAt = expiresAt;
this.store();
}

ComplexCookie.prototype.store = function(time)
{
var cookieData = "";
for (var i in this.data)
{
if (this.data[i] != null)
cookieData += i + ":" + this.data[i] + ",";
}

setExpiringCookie(this.name, cookieData, this.expiresAt);
}

ComplexCookie.prototype.set = function(name, value)
{
this.data[name] = value;
this.store();
}

ComplexCookie.prototype.get = function(name)
{
return this.data[name];
}

ComplexCookie.prototype.getNumber = function(name, defaultValue)
{
var value = this.get(name);
if (isNaN(value))
{
if (typeof(defaultValue) == "undefined" || isNaN(defaultValue))
return 0;
return defaultValue;
}

return parseFloat(value);
}

ComplexCookie.prototype.remove = function(name)
{
this.data[name] = null;
}

ComplexCookie.prototype.contains = function(name)
{
var data = this.get(name);

return typeof(data) != "undefined" && data;
}