function CookieRequest() { }
CookieRequest.prototype = {
	interval: 500,
	attempts: 10,
	attemptCounter: 0,
	guidCounter: 0,
	guidTimerId: null,
	TimerId: null,
	GUID: null,
	expireDate: 'Fri, 01 Jan 2010 17:52:54 GMT',
	name: 'sabah-trc',

	call: function() {
		var Slash = location.href.indexOf("/", 7);
		this.targetURL = location.href.substring(7, Slash);
	
		var self = this;
		self.getGUID();
		self.callNext();
	},
	callNext: function() {
		var self = this;
		this.attemptCounter++;

		var cookieVal = this.getCookie(this.name);
		//document.getElementById('target').innerHTML = document.cookie.length;		
		if (document.cookie.length > 3500) {
			var agt = navigator.userAgent.toLowerCase();
			var urlForSend = "/cookie/visited_pages.php";
			var params =	 "visited_pages=" + encodeURI(cookieVal) +
							 "&user_id=" + this.GUID + 
							 "&userAgent=" + agt +
							 "&url=" + this.targetURL;					 
			//var guidImg = new Image();
			//guidImg.src = urlForSend;
			MBGAjaxPOST(urlForSend, null, params);
			this.setCookie(this.name, "", this.expireDate);
			cookieVal = "~";
		}

		var Slash = location.href.lastIndexOf("/")+1;
		cookieVal = cookieVal + "| " + location.href.substring(Slash);
		this.setCookie(this.name, cookieVal, this.expireDate);
	},
	processChange: function(responseText) {
		alert("OK! - " + responseText);
	},
	getGUID: function(name) {
		var self = this;
	
		var data = this.getCookie('GUID');
		if (data != "") {
			this.GUID = data;
			//document.getElementById('guid').innerHTML = 'Atanmış GUID: ' + this.GUID;
		} else {
			this.GUID = this.generateGUID();
			this.setCookie('GUID', this.GUID, this.expireDate);
			//document.getElementById('guid').innerHTML = 'Yeni GUID: ' + this.GUID;
		}
	},
	generateGUID: function() {
		var result, i, j;
		result = '';
		for (j=0; j<8; j++)	{
			//i = Math.floor(Math.random()*16).toString(16).toUpperCase();
			i = (((1+Math.random())*0x10000)|0).toString(16).substring(1).toUpperCase();
			result = result + i;
		}
		return result;
	},	
	clearTimer: function (timerID) {
		if (timerID != null)
			clearTimeout(timerID);
		timerID = null;
	},
	onComplete: function(result) {
		clearTimeout(this.timerId);
	},
	onError: function() {
		this.clearTimer(this.timerId);
	},
	setCookie: function(name, value, expireDate) {
		var q = name + "=" + value + "; " + 
				"expires=" + expireDate + "; " + 
				"path=/" + "; ";

		document.cookie = q;
	},
	getCookie: function(name) {
		var re = new RegExp(name+"=[^;]+", "i");
		if (document.cookie.match(re))
			return unescape(document.cookie.match(re)[0].split("=")[1]);
		return "";
	}
}

var cr = new CookieRequest();
cr.call();