var JSTracker = {
    trackingData : {
    appName : window.navigator.appName,
	requestUrl : window.location.href,
    referrer : escape(document.referrer),
    width : window.screen.width,
    height : window.screen.height,
	time : new Date().getTime(),
    timeZone : new Date().getTimezoneOffset() / 60
    },
	setTrackServerAdress : function (trackserveraddress) {
		this.trackserveraddress = trackserveraddress;
	},    
	handleSessionCookie : function () {
        var cookie;
        if(!(cookie = Cookie.read("SessionCookie"))) {
           cookie = this.setSessionCookie();
        }
        this.trackingData.sessionCookie = cookie;
       
    },
    setSessionCookie : function () {
        var cookie;
        cookie = parseInt(new Date().getTime()).toString(32)+Math.round(Math.random()*100);
        Cookie.write("SessionCookie",cookie);
        return cookie;
    },
	setSiteId : function (trackSite) {
		this.trackingData.trackSite = trackSite;
	},
    setPageType : function (value) {
        this.trackingData.pageType = value;
    },
	setUserId : function (value) {
		this.trackingData.userId = value;
	},
    setVariable : function (jsonObject) {
        this.trackingData.jsonObject = escape(jsonObject);
    },
	buildQueryString : function (data) {
		var queryString = '?';
		var delimit = '';
		for (var propertyName in data) {
			queryString += delimit + propertyName + '=' + data[propertyName];
			delimit = '&';
		
		}
		return encodeURI(queryString);
	},
	startTracking : function ()	{
        this.handleSessionCookie();
		this.sendData();
	},
	setWWS : function (varW,varWS) {
		this.trackingData.varW = varW;
		this.trackingData.varWS = varWS;
	
	},
	sendAJAXData : function (data) {
		this.trackingData.AJAXData = escape(data);
		this.sendData();
	},
	sendData : function () {
		var requestAddress = this.trackserveraddress + this.buildQueryString(this.trackingData);
	    var xhr = new XMLHttpRequest();
		if ("withCredentials" in xhr){
		// user Cors specified XMLHttpRequest
			xhr.open('GET',requestAddress,true);
			xhr.withCredentials = "true";
			xhr.send();
		} else if(typeof XDomainRequest != "undefined") {
		// Try using XDomainRequest IE
			var xdr = new XDomainRequest(); 
			xdr.open('GET' , requestAddress);
			xdr.send();
		} else {
		// fall back to script tag injection
		    var scriptElement = document.createElement("script");
            scriptElement.setAttribute("src", requestAddress);
            document.head.appendChild(scriptElement);

		}
	}	
}
