// JavaScript Document
function setCookie(c_name, value, expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
	
}
function getCookie(c_name)
{
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}
function querySt() {
	var qs = window.location.search.substring(1);
	gy = qs.split("&");
	for (i=0; i<gy.length; i++) {
		ft = gy[i].split("=");
		if (ft[0] == 'cg_id') {
			return ft[1];		
		}
	}
}
var cg_id = querySt();

if(cg_id != null && cg_id != "") {
	setCookie('cg_id', cg_id, 1);
}
else {
	cg_id = getCookie('cg_id');
}

if(cg_id != null && cg_id != "") {
	
	var startTime = new Date();        //Start the clock!
	window.onbeforeunload = function()        //When the user leaves the page(closes the window/tab, clicks a link)...
	{
		var endTime = new Date();        //Get the current time.
		var timeSpent = (endTime - startTime);        //Find out how long it's been.
		/*timeSpent = timeSpent/1000;
		var seconds = Math.floor(timeSpent % 60);*/
		timeSpent = Math.round(timeSpent/1000);

		var xmlhttp;        //Make a variable for a new ajax request.
		if (window.XMLHttpRequest) {       //If it's a decent browser...
			// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp = new XMLHttpRequest();        //Open a new ajax request.
		}
		else {       //If it's a bad browser...
			// code for IE6, IE5
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");        //Open a different type of ajax call.
		}
		var page_url = window.location.href;
		var url = "http://www.cliqiq.com/track/log_time.php?cg_id="+cg_id+"&time="+timeSpent+"&page_url="+page_url; //Send the time on the page to a php script of your choosing.
		xmlhttp.open("GET",url,false);        //The false at the end tells ajax to use a synchronous call which wont be severed by the user leaving.
		xmlhttp.send(null);        //Send the request and don't wait for a response.
	}
}