﻿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 null;
}

function setCookie(name, value) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + 10000);
    document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exdate.toGMTString();
}

function isContain(containerString, containedString) {
    return containerString.indexOf(containedString) > -1;
}

function getQueryString(queryString) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i = 0; i < gy.length; i++) {
        ft = gy[i].split("=");
        if (ft[0] == queryString) {
            return ft[1];
        }
    }
    return null;
}

function trackPage() {
    var hash = getQueryString("scrlnk");
    if (hash != null) {
        setCookie("last_traqa_hash", hash);
    } else {
        hash = getCookie("last_traqa_hash");
    }
	if (hash == null)
		hash = "regularVisitor";

    var firstVisit = true;
    if (hash != null) {
        firstVisit = false;
        var visitCookie = getCookie("traqa_visited_hashes");
        if (visitCookie == null)
        {
            firstVisit = true;
            visitCookie = "";
        }
        else if (!isContain(visitCookie, "," + hash + ","))
            firstVisit = true;
        if (firstVisit)
            setCookie("traqa_visited_hashes", visitCookie + "," + hash + ",");
    }

    var dataToSend = "pt=" + escape(document.title.replace("&"," "));
    dataToSend += "&u=" + escape(window.location);
    dataToSend += "&r=" + escape(document.referrer);
    if (hash != null && hash != "regularVisitor")
        dataToSend += "&h=" + escape(hash);
    if (firstVisit)
        dataToSend += "&f=1";
    else
        dataToSend += "&f=0";
    SendRequest(dataToSend);
}

function SendRequest(dataToSend) 
{
	var XMLHttpObj;
	if (window.XMLHttpRequest)
		XMLHttpObj = new XMLHttpRequest();
	else if (window.ActiveXObject)
	    XMLHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
	XMLHttpObj.open("GET", "http://www.screama.com/traqa.aspx?" + dataToSend, true);
	XMLHttpObj.send();
}
