// Counter.js
// calls Counter.php to save stats counter on host server.
// The function of Counter.js is to privide a cleaner html interface 
// all the parmeteraistion to counter.php is encapsulated the this file
// plus additional data regarding the user 
// ( no of previous visits / browser type / referred ) can be captured and forwarded as data
//
// configurable paramaters for Counter.php
// C - Counter ID - a unique ID for rech client's counter
// F - foreground Optional set  colour Defaukts to black. #000000 
//		Forground is ALWAYS RED if counter ID in invalid / File not found on logging host
// B - background Optional, defauts to white #FFFFFF
// H - Hide no display at all
// V - Visible Show count figure, Default, show RJB Logo
// R - Read Only Display but do not bump counter
// D - Data Data to be stored as a sequential log in counter file
//
// Usage: <script type='text/javascript'> counter(); </script>


//
// Configuration for THIS application of counter.js
// 
ImgClass = 'rjb';	// Style for Image
ID		='MJDeShop';	// Name of logfile
B		='transparent'; // background colour _border
F		='000000'; // Foreground Colour
V		=0;		// Don't show counter
H		=0; 	// Don't hide logo
LogHost 	= 'http://www.rjbarnett.co.uk'

// end of configuration

expDate = new Date()
fixDate(expDate)
Now = 
Yr = expDate.getUTCFullYear()+10
expDate.setUTCFullYear(Yr)
fixDate(expDate)
UTCExp = expDate.toUTCString()

var Browser = getCookie('Browser')
Browser = ((Browser) ? Browser : "Unknown")
if (Browser =="Unknown"){
	N=(navigator.appName.indexOf('Netscape')!=-1&&parseFloat(navigator.appVersion)<5)
	if (N) Browser = "NN4" 
		S=(navigator.appName.indexOf('Netscape')!=-1&&parseFloat(navigator.appVersion)>4.9)
	if (S) Browser = "Compliant" 
	M=(navigator.appName.indexOf('Microsoft')!=-1)
	if (M) Browser = "IE Newer" 
	M4=(navigator.appName.indexOf('Microsoft')!=-1&&parseFloat(navigator.appVersion.substr(navigator.appVersion.indexOf('MSIE')+5,3))<5.5)
	if (M4) Browser = "IE Older" 
}
TheBrowser = 'Moz%20'+navigator.appVersion

setCookie('Browser',Browser)
//
var Uid = getCookie('Uid')
Uid = ((Uid) ? Uid : "Unknown")
if (Uid =="Unknown"){
	u1 = Math.random()
	u32 = ((((u1*8)*8)*8)*8)*8	// 32k
	u32r = Math.round(u32)
	Uid =  u32r.toString()
}
setCookie('Uid',Uid,expDate)
// IF we are revisiting in the same session, don't bump the visit counter
var Visits = getCookie('Visits')
Visits = ((Visits) ? Visits : "0")
var deJaVue = getCookie('deJaVue')
deJaVue = ((deJaVue) ? deJaVue : "Unknown")
if (deJaVue =="Unknown"){
	nVisits = parseInt(Visits)+1
	Visits = nVisits.toString()
	deJaVue = "BeenThereDoneThat"
}
setCookie('deJaVue',deJaVue)
setCookie('Visits',Visits,expDate)
//	
function Count(){

Browser_Type = ';Browser='+TheBrowser
User_ID  = ';Uid='+Uid
Prev_Visits  = ';Visits='+Visits
Referrer  = ';Referrer='+this.document.referrer
data = Browser_Type+User_ID+Prev_Visits+Referrer

string = "<IMG class='"+ImgClass+"'";
string += " SRC='"+LogHost+"/counter.php?C="+ID;
string += "&amp;D="+data;
if (B) string += "&amp;B="+B;
if (F) string += "&amp;F="+F;
if (V) string += "&amp;V="+V;
if (H) string += "&amp;H="+H;
string += "'alt='IT Support for Business'>"
document.write(string);
}

Count();

// cookies.js
// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid 
//		   (defaults to path of calling document)
// [domain] - domain for which the cookie is valid 
//			 (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission 
//            requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toUTCString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist

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

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 UTC";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

