var FIRST_VISIT = 'fv';
var UID = 'uid';
var REFERRER = 'rrr';

function init() {
    var uid = readProperty(UID);
    var firstVisit = readProperty(FIRST_VISIT);
    if( firstVisit && firstVisit.length != 0 ) {
        notify(uid,'hit',document.location.href);
    }
    else {
        firstVisit = ''+new Date().getTime();
        storeProperty(FIRST_VISIT,firstVisit);
        uid = firstVisit;
        storeProperty(UID,uid);
        notify(uid,'init',document.referrer);
    }
}

function notify(uid,name,data) {
	var ts = ''+new Date().getTime();
    var url = "http://bostonmathematics.com/tracking/track.gif?uid="+uid+"&time="+ts+"&name="+name+"&data="+encodeURI(data);
	var elem = document.getElementById('track');
	if( elem ) {
		elem.src = url;		
	}
	return;

    var ajaxRequest;
    try {
        ajaxRequest = new XMLHttpRequest();
    }
    catch(e) {
        try {
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e) {
            try {
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                return false;
			}
		}
	}
    //ajaxRequest.onreadystatechange = function() {
    //    if(ajaxRequest.readyState == 4){
    //        // ajaxRequest.responseText;
    //    }
    //}

    ajaxRequest.open("GET",url,true);
	ajaxRequest.send(null);
}

function reset() {
    storeProperty(FIRST_VISIT,'');
    storeProperty(UID,'');
    alert('cleaned');
}

function readProperty(name)
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++)
    {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function storeProperty(name,value)
{
    var date = new Date();
    date.setTime(date.getTime()+(365*24*60*60*1000));
    document.cookie = name+"="+value+"; expires="+date.toGMTString()+"; path=/";
}

