//The maximum image requests which can occur at a time before they start knocking each other out. Used to conserve memory
var cm_max = 50;

//The current slot in the cm_img array to load a coremetrics gif into
var cm_count = 0;

//An array to store coremetrics images in. Used to guarantee that an image load doesn't get prematurely aborted
var cm_img = new Array(cm_max);



/**
	The coremetrics class.
	The way the arguments are passed is somewhat recondite, 
	but left in place to avoid any unforseen consequences of changing them.
	
	Essentialy it expects the following params in the following order.
	
	@class _cm
	
	@param arguments[0] String this is a string parameter which appears to always be "tid", which is used solely to name a property to store the next argument
	@param arguments[1] String an ID used to detemine what kind of page view is tracked
	@param arguments[2] String this is a string parameter which appears to always be "vn2", which is used solely to name a property to store the next argument
	@param arguments[3] String this appears to be a version number of some sort, it's values usually appear to be "e3.1"
	
*/
function _cm() {
	
	//Parse out the arguments
	var a=arguments;
	for (var i=0;i < a.length; i++)
		this[a[i]]=a[++i];
	
	this.ci = cm_ClientID;
	
}


/**

	This method does most of the work in this class.
	It looks through it's properties, and builds a url passing .
		
	@method getImgSrc
	@return String a url pointing to a gif on the coremetrics server
*/
_cm.prototype.getImgSrc = function() {
	
	var n=navigator,
		d=document,
		cm_pl=location.protocol;
		
	this.vn1="e3.1";
	this.qs="tid="+this.tid;
	var dt=new Date();
	this.rnd=dt.getTime()%10000000;
	
	if (!this.rf) {
		
		if (n.appName=="Microsoft Internet Explorer" && parseFloat(n.appVersion) < 4) {
			this.rf="unavailable";
		} else if (d.referrer=="undefined") {
			this.rf="";
		} else this.rf = d.referrer;
		
	}
	
	if (this.ul==null || this.ul=="" || this.ul == "(none)")
		this.ul=window.location.href;
		
	this.ts=cmCookie("TestSess");

	if (this.ts!="Yes") {
		d.cookie="TestSess=Yes";
		this.ts=cmCookie('TestSess');
	}
	
	this.tp=cmCookie("TestPerm");
	
	if (this.tp!="Yes") {
		dt.setHours(dt.getHours()+5);
		d.cookie="TestPerm=Yes;expires="+dt.toGMTString()+"; path=/;";
		this.tp=cmCookie('TestPerm');
	}
	
	for(var cOb in this) {
		if(this[cOb]==null || this[cOb]=="" || this[cOb].toString().indexOf("function ") >= 0 || cOb == "qs" || cOb=="tid")	continue;
		this.qs += "&" + cmStrip(cOb) + "=" + cmEncode(cmStrip(this[cOb]));
	}
	
	if(this.tid=="2"||this.tid=="3")cm_pl="https:";
	
	if(cm_pl!="http:"&&cm_pl!="https:")cm_pl="http:";
	
	var request=cm_pl+"//"+cm_HOST+cmEncode(this.qs);
	
	return request;
	
}


/**
	The _cm.writeImg method	
	This function uses the coremetrics parameters to generate an HTML img tag.
		
	@method writeImg
		
	@return String the HTML img tag (this return is not currently in use)
	
	@deprecated use loadImg instead.
*/
_cm.prototype.writeImg = function() {
		
	var tag= "<img width=\"1\" height=\"1\" src=\""+this.getImgSrc()+"\">";
	document.write(tag);
	return tag;
		
}


/**
	The _cm.loadImg method	
	This function is used to pass the tracking data to coremetrics. It simulates an html img request.
	It's only negative effect is that it kills the coremetrics tagbar.
	
	@method loadImg
*/
_cm.prototype.loadImg = function() {
	
	cm_img[cm_count]=new Image();
	cm_img[cm_count].src=this.getImgSrc();
	cm_count++;
	if (cm_count==cm_max) cm_count=0;
		
}



/**
	A utility function which strips out apostrophes, double quotes, 
	carriage returns and newline characters from a string
	
	@function cmStrip
	
	@param s String
	@return String
*/
function cmStrip(s) {
	
	var z="";
	s=z+s;
	return s.split("'").join(z).split("\"").join(z).split("\r").join(z).split("\n").join(z);
	
}



/**
	Performs a trim operation and then escapes out the characters of a string.
	It then replaces all "+" with "%2B" and returns the string
	
	@function cmEncode
	
	@param s String the original string
	@param String the encoded string
*/
function cmEncode(s) {
	
	//find start and end of trim
	var i=0,j;
	while(s.charAt(i) == " "  &&  i != s.length) i++;
	j = s.length-1;
	while(s.charAt(j) == " " && j != 0) j--;
	
	//trim and escape
	s=escape(s.substring(i,j+1));
	
	//Escape +
	s=s.split("+").join("%2B");
	
	return s;
}



/**
	
	@function cmCookie
	
	@param n String
	
*/
function cmCookie(n) {
	
	var d=document.cookie,
		ar=n+"=",
		al=ar.length,
		cl=d.length,
		i=0;
	
	while(i < cl) {
		var j=i+al;
		if(d.substring(i,j)==ar){
			var en=d.indexOf(";",j);
			if(en==-1) en=d.length;
			return unescape(d.substring(j,en));
		}
		i=d.indexOf(" ",i)+1;
		if(i==0)break;
	}
	
	return null;
	
}

