var _estat_key = 'sqills_estat';
function getCookie (name) {
         if ( document.cookie) { // Le cookie est-il valide ?
                  index = document.cookie.indexOf( name);
                  if ( index != -1) {
                           nDeb = (document.cookie.indexOf( "=", index) + 1);
                           nFin = document.cookie.indexOf( ";", index);
                           if (nFin == -1) {nFin = document.cookie.length;}
                           return unescape(document.cookie.substring(nDeb, nFin));
                  }
         }
         return null;
}

function rnd() {
        rndMx=2147483647;
        var visitDate=new Date().getTime();
        return Math.round(Math.random()*rndMx)+"."+(new Date().getTime()) + "." + visitDate;
}

function _sC(key){
        document.cookie =
          _estat_key + "=" + key +"; expires=Thu, 31 Jan 2099 00:00:00 GMT; path=/";
}

function estatvoid(){
        return;
}


function SqillsEstat(custid, quantity, value) {
	if (custid == 'undefined' || typeof(custid) == 'undefined') {
		custid="";
	}
	var transaction_quantity = 0;
	var transaction_value = 0;

        if (typeof(quantity) != 'undefined') { 
		transaction_quantity = quantity;
        }
	if (typeof(value) != 'undefined') {
		transaction_value = value;
	}				         

        transaction = '&tr=' + transaction_quantity + '&value=' + transaction_value;
        
        var r = document.referrer;
        var domain = document.domain;
        var cookie = getCookie(_estat_key);
        var val = rnd();
	var title = document.title;
        if (cookie == '' || cookie == null) {
                _sC(val);
        } else {
                try {
                var key = cookie;
                var sess_arr = cookie.split(".");
                var old_time = sess_arr[2];
                var date = new Date();
                //var time = parseInt(old_time) + (30*60*1000);
                //date.setTime(time);
                var key = sess_arr[0] + "." + sess_arr[1] + "." + date.getTime();
                val = key;
                _sC(key);
                } catch(err) {
                        alert(err);
                        _sC(rnd());
                }
        }
	var lan=navigator.language? navigator.language : navigator.userLanguage;
        var param = "cust="+custid+"&r=" + r + "&cid="+val + "&lan="+lan+"&title="+title+transaction;
        var i=new Image(1,1);
	var proto=window.location.protocol;
       	i.src=proto+"//espector.sqills.com/blank.gif?"+param;
        i.onload = function () { estatvoid(); }
}

function Espector(custid, quantity, value) {
        SqillsEstat(custid, quantity, value);
}
 

