function StripLocation(loc) {
   // remove everything after the last / onward but keep
   // http://<domain-name>
   var protocol_end = '://';
   var protocol_end_pos = loc.indexOf(protocol_end);
   // strange - failed to find protocol part...
   if (-1 == protocol_end_pos) {
      return "";
   }
   var domain_start = protocol_end_pos + protocol_end.length;

   // scan for a "/" from protocol onward
   var last_occ_pos = loc.lastIndexOf('/');
   // there is no trailing slah - return whole loc
   if (-1 == last_occ_pos || last_occ_pos <= domain_start) {
      return loc;
   }

   return loc.substring(0, last_occ_pos);
}

function RemoveWWW(loc) {
   // remove WWW part if present after the
   // the http://
   return loc.replace(':\/\/www\.', '://');
}

function ExtarctDomain(loc) {
   var protocol_end = '://'
   var protocol_end_pos = loc.indexOf(protocol_end);
   // strange - failed to find whete protocol ends...
   if (-1 == protocol_end_pos) {
      return "";
   }
   var domain_start = protocol_end_pos + protocol_end.length;

   // scan for a "/" from protocol onward
   var first_occ_pos = loc.indexOf('/', domain_start + 1);

   // there is not trailing slash - return to the end
   if (-1 == first_occ_pos) {
      return loc.substring(domain_start);
   }

   return loc.substring(domain_start, first_occ_pos);
}

function GetReferrer(loc, referrer) {
   if (referrer == '') {
      return ""
   }

   var stripped_loc = StripLocation(loc);
   stripped_loc = RemoveWWW(stripped_loc);
   referrer = RemoveWWW(referrer);

   // if referrer starts starts like location
   // we don't count it as referrer:
   if (0 == referrer.indexOf(stripped_loc)) {
      return "";
   }

   return ExtarctDomain(referrer);
}

function StatsGetCookie(name) {
   var dc     = document.cookie;
   var prefix = name + "=";
   var begin  = dc.indexOf( "; " + prefix );
   if (begin == -1) {
      begin = dc.indexOf(prefix);
      if (begin != 0) return '';
   } else {
      begin += 2;
   }
   var end = dc.indexOf(";", begin);
   if (end == -1) end = dc.length;
   return unescape(dc.substring(begin + prefix.length, end));
}

function StatsDeleteCookie(name) {
   document.cookie = name + "=; path=/; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function HitStats(script_url, site_id, page_id, sess_id_name, prof_info) {
   var cur_referrer = StatsGetCookie('cur_referrer');
   if (cur_referrer != '') {
      StatsDeleteCookie('cur_referrer');
   } else {
      cur_referrer = document.referrer;
   }

   var ref = GetReferrer(document.location.href, cur_referrer);
   var ref_pv = ref != '' ? '&ref=' + escape(ref) : '';
   var sess_id = StatsGetCookie(sess_id_name);
   var url = script_url +
               "?__DCP_NO_SESS__=1&site_id=" + site_id + "&page_id=" + page_id +
               "&sess_id=" + sess_id + ref_pv +
               ( prof_info ?
                  "&prof_time=" + escape(prof_info.RawTime) +
                  "&prof_text=" + escape(prof_info.Message)
                  :
                  "" );
   var hit_frm = document.createElement('IFRAME');
   hit_frm.style.visibility = 'hidden';
   hit_frm.style.display    = 'none';
   hit_frm.style.width = 1;
   hit_frm.style.height = 1;
   hit_frm.src = url;
   hit_frm.id = "hit_frame";
   document.body.insertBefore(hit_frm, document.body.lastChild);
}


