/*
**	Cookiejar.js - A Javascript class for storing and accessing cookies 
**	Author: Shyam Agarwal
**  Based on CookieJar OpenSource Project
*/
function cookiejar (jarname)
{
	this.jar = "";
	this.jarname = jarname;
	this.expiration = 1 * 365 * 24 * 60 * 60 * 1000;
	this.divider = "^";
	this.divider2 = "!";
	this.path = null;
    this.domain = c_getCookieDomain();
	this.secure = false;

	this.doesExist = c_doesExist;	
	this.delCookie = c_delCookie;
	this.setCookie = c_setCookie;
	this.getCookie = c_getCookie;
	this.size = c_size;

	this.read	= c_read;
	this.write = c_write;
	this.reset = c_reset;
	this.setExpiration = c_setExpiration;
	
	this.read();
}


function c_getCookieDomain() 
{
	var ds = ''; 
	if(href.indexOf('.netzero.net') != -1) ds='netzero.net'; 
	if(href.indexOf('.juno.com') != -1) ds='juno.com'; 
	if(href.indexOf('.mybluelight.com') != -1) ds='mybluelight.com'; 
	return ds;
}


function c_doesExist(c_id)
{	if (c_id == "") return false;
	return this.jar.indexOf(escape(c_id) + this.divider2);
}

function c_delCookie(c_id)
{	
	exists = this.doesExist(c_id);
	if (exists == -1) return false;
	
	var end = this.jar.indexOf(this.divider,exists);
	if (end == -1)
		end = this.jar.length
	else
		end;

	this.jar = this.jar.substring(0,exists-1) + this.jar.substring(end,this.jar.length);
		
    if(this.jar.charAt(0)=='^'){
    this.jar=this.jar.substring(1,this.jar.length)
    }

	return true;
}
		
		
function c_setCookie(c_id, c_value,c_exp)
{	
if((c_value.length+this.size())>2048) {alert('Cookie limit exceeded.. ');}

	this.delCookie(c_id);
	this.jar += this.divider + escape(c_id) + this.divider2 + escape(c_value);

    if(this.jar.charAt(0)=='^'){
    this.jar=this.jar.substring(1,this.jar.length)
    }

	return true;
}

function c_size()
{	var datasize= this.jar.length + this.jarname.length + 1;
	return datasize;
}

function c_getCookie(c_id)
{	
	exists = this.doesExist(c_id);
	if (exists == -1) return null;
	
	tempjar = this.jar.substring(exists,this.jar.length);

	var end = tempjar.indexOf(this.divider);
	if (end == -1)
		end = tempjar.length
	
	tempjar = tempjar.substring(0,end);

	var tempArr = tempjar.split(this.divider2);

	if (unescape(tempArr[0]) != c_id)
		return null;

	return unescape(tempArr[1]);		
}

function c_read()
{
  var dc = document.cookie;
  var prefix = this.jarname + "=0:";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return "";
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  this.jar =  unescape(dc.substring(begin + prefix.length, end));

  return true;
}

function c_reset()
{
	this.jar = "";
	this.write();
}


function c_write()
{
	var dateObj = new Date();
	dateObj.setTime(dateObj.getTime() + this.expiration);

	var base = new Date(0);
    var skew = base.getTime();
    if (skew > 0)
       dateObj.setTime(dateObj.getTime() - skew);


	document.cookie = this.jarname + "=0:" + escape(this.jar) +
				"; expires=" + dateObj.toGMTString() + 
				  ((this.path == null)   ? "" : "; path=" + this.path) +
				  ((this.domain == null) ? "" : "; domain=" + this.domain) +
				  ((this.secure) ? "; secure" : "");

	return true;
}

function c_setExpiration(years, days, mins, secs, mill)
{

	 this.expiration = (((years) ? (years) : 1) * 365 * 60 * 60 * 1000) +
	  				   (((days)  ? (days)  : 365) * 60 * 60 * 1000) +
					   (((mins)  ? (mins)  : 60) * 60 * 1000 ) +
					   (((secs)  ? (secs)  : 60) * 1000) +
					   ((mill)  ? (mill)   : 1000);
}
