

// create an instance of the Date object

var now = new Date();
var exp = new Date(now.getFullYear(), now.getMonth(), now.getDate()+1, 00, 00, 00);
var exp6mth = new Date(now.getYear(), now.getMonth()+3, now.getDate(), 00, 00, 00);

//alert(exp);

//alert(document.cookie);


/*
cookie visit expires in one year (actually, 365 days)
365 days in a year
24 hours in a day
60 minutes in an hour
60 seconds in a minute
1000 milliseconds in a second
cookie connect_time expires in a day
*/

//now.setTime(now.getTime() + 24 * 60 * 60 * 1000);
//exp1.setTime(exp1.getTime() + 24*60*60*1000);

var visits = getCookie("counter");
var mm = getCookie("connect_time");

var tt;
tt = now.getHours()*3600+now.getMinutes()*60+now.getSeconds();

// if the cookie wasn't found, this is your first visit
if (!visits) {
  visits = 1; // the value for the new cookie
} else {
  // increment the counter
  visits = parseInt(visits) + 1;
}

// set the new cookie
 setCookie("counter", visits, exp);

 setCookie("last_connect_time", tt, exp, "/");


//  window.onUnload="resetcookie();"


//alert(document.cookie);

//setCookie("connect_time", nn , exp);

/*


   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
*/

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
//alert(curCookie);
//alert(document.cookie);
}


function resetcookie()
{

 var now = new Date();
 var exp = new Date(now.getYear(), now.getMonth(), now.getDate()+1, 00, 00, 00);
 var exp6mth = new Date(now.getYear(), now.getMonth()+3, now.getDate(), 00, 00, 00);


var mm = getCookie("connect_time");
var tt = getCookie("last_connect_time");
var visits = getCookie("counter");
var vv = getCookie("visit_data");


if (!mm)
{
	mm = 0;
}

mm = parseInt(mm) + (now.getHours()*3600+now.getMinutes()*60+now.getSeconds() - tt) ;

setCookie("connect_time", mm, exp, "/");

		

var tday = (now.getMonth()+1) + "/" + now.getDate() + "/" + now.getYear() ;



if (!vv)
{ 
	vv = tday + "-" + visits + "-"  + mm ; 
}
else
{

	var pos = vv.indexOf(",");

	if (pos >= 1)
	{
	
  	a_data = vv.split(",") ;

	if (a_data[0].split("-")[0] == tday)
	{ 
	//replace today's connect time and count
	    a_data[0] = tday + "-" + visits + "-"  + mm;	
	    vv = a_data.toString();
	}
	else
	{ 
		//append a new date record to the detail cookie
	 	vv = tday + "-" + visits + "-"  + mm + "," + vv ; 
	}

	}
	else
	{
		if (vv.split("-")[0] == tday)
		{ 
		//replace today's connect time and count
	
		vv = tday + "-" + visits + "-"  + mm ; 
		}
		else
		{ 

		//append a new date record to the detail cookie
	 	vv = tday + "-" + visits + "-"  + mm + "," + vv ; 
		}
		
	}

}

	setCookie("visit_data", vv, exp6mth, "/");

}


/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

