var CM8Agent = navigator.userAgent.toLowerCase();
var CM8IsIE = (navigator.appVersion.toLowerCase().indexOf('msie') != -1);
var CM8IsNav = ((CM8Agent.indexOf('mozilla') != -1) &&
				(CM8Agent.indexOf('spoofer') == -1) &&
				(CM8Agent.indexOf('compatible') == -1) &&
				(CM8Agent.indexOf('opera') == -1) &&
				(CM8Agent.indexOf('webtv') == -1));

function CM8GetTime()
{
   	var theDate = new Date();	
	var YYYY = new String(theDate.getYear());
	for(i=4-YYYY.length;i>0;i--)
		YYYY = "0" + YYYY;
	var MM = new String(theDate.getMonth()+1);
	if (MM < 1 || MM > 12)
		MM = 1;
	if (MM.length < 2)
		MM = "0" + MM;
	var DD = new String(theDate.getDate());
	if (DD < 1 || DD > 31)
		DD = 1;
	if (DD.length < 2)
		DD = "0" + DD;
	var HH = new String(theDate.getHours());
	if (HH < 0 || HH > 24)
		HH = 1;
	if (HH.length < 2)
		HH = "0" + HH;
    return "DATE=" + YYYY + MM + DD + "&HOUR=" + HH;
}

function CM8IsFrames()
{
	return (CM8Agent.indexOf("win") != -1) &&
	       window.parent &&
	       window.parent.frames &&
	       (window.parent.frames.length >= 2) && 
	       (typeof(window.parent.document) != 'unknown') &&
	       (typeof(window.parent.document.body.clientWidth) != 'undefined');
}

function CM8GetSize()
{
	if (CM8IsIE)
	{
		if (typeof(window.document.body.clientWidth) != 'undefined')
			return "WIDTH=" + window.document.body.clientWidth +
			       "&HEIGHT=" + window.document.body.clientHeight;
		if (CM8IsFrames())
			return "WIDTH=" + window.parent.document.body.clientWidth +
			       "&HEIGHT=" + window.parent.document.body.clientHeight;
	}
	else if (CM8IsNav && (typeof(window.innerWidth) != 'undefined'))
		return "WIDTH=" + window.innerWidth +
			   "&HEIGHT=" + window.innerHeight;
	return "WIDTH=1024&HEIGHT=768";
}

function CM8GetFontSize()
{
	if (CM8IsIE && (CM8Agent.indexOf("win") != -1))
	{
		document.write('<span style="position:absolute;visibility:hidden;" id="CM8textSize">_</span>');
		if (typeof(document.all.CM8textSize) != 'undefined' && typeof(document.all.CM8textSize.offsetWidth) != 'undefined')
			return document.all.CM8textSize.offsetWidth;
		else
  			return 8;
	}
	else
		return 10;
}

function CM8GetDomain()
{
	var dom = "";
	if (location.href.indexOf("file") != 0)
		dom = document.domain;
	var ind = dom.indexOf(":");
	if (ind != -1)
		dom = dom.substring(0, ind);
	return escape(dom);
}

function CM8ConvertChar(source,charX)
{
	var d1=(charX.charCodeAt(0)/16)%16;
	var d2=charX.charCodeAt(0)%16
	var to="%"+
		String.fromCharCode((d1<10)?("0".charCodeAt(0)+d1):("a".charCodeAt(0)+d1-10))+
		String.fromCharCode((d2<10)?("0".charCodeAt(0)+d2):("a".charCodeAt(0)+d2-10));
	while (source.indexOf(charX)!=-1)
		source=source.slice(0,source.indexOf(charX))+to+source.slice(source.indexOf(charX)+1);
	return source;
}

function CM8GetLocation(loc)
{
	if (loc == "") loc="?";
	loc=escape(loc);
	CM8ConvertChar(loc,"?");
	CM8ConvertChar(loc,"&");
	CM8ConvertChar(loc,";");
	return loc;
}

if ((typeof(CM8Profile) == "undefined") || (CM8Profile == ""))
	var CM8Profile = "";
else
	CM8Profile += "&";

if (typeof(CM8Location) == 'function')
	CM8Profile += "LOC=" + CM8GetLocation(CM8Location(document.location.href)) + "&";
else if (typeof(CM8Location) != 'undefined')
	CM8Profile += "LOC=" + CM8GetLocation(document.location.href) + "&";

CM8Profile += 
	CM8GetSize() +
	"&" + CM8GetTime() +
	"&FONTSIZE=" + CM8GetFontSize() +
	"&SERVICEHOST=" + CM8Server +
	"&DOMAIN=" + CM8GetDomain() +
	"&ORD=" + (new Date()).getTime();

if ((typeof(CM8RichMedia) != "undefined") &&
    ((CM8RichMedia.toString().toLowerCase() == "no") ||
	 (CM8RichMedia.toString().toLowerCase() == "false")))
	CM8RichMedia = "";
else
	CM8RichMedia = "r";

var CM8UseDefer = false;
if ((typeof(CM8Defer) != "undefined") &&
    ((CM8Defer.toString().toLowerCase() == "yes") ||
	 (CM8Defer.toString().toLowerCase() == "true"))) {
	if (typeof(document.getElementsByTagName)!='undefined') {
		var tag=document.getElementsByTagName('BODY').item(0);
		if (typeof(tag.insertAdjacentHTML) != 'undefined')
			CM8UseDefer = true;
		else if ((typeof(tag.insertBefore) != 'undefined') && (typeof(document.createRange) != 'undefined')) {
			var r = document.createRange();
			if ((typeof(r.setStartAfter) != 'undefined') &&
				(typeof(r.createContextualFragment) != 'undefined'))
				CM8UseDefer = true;
		}
	}
}

if (! CM8UseDefer) {
	var CM8ShowAd = function(F, I)
	{
		if (typeof(CM8MultiShow) != "undefined")
			CM8MultiShow(F, I);
	};
}
else {
	var CM8ErrorPopup='';
	var CM8PHM=null;
	var CM8PHLI=0;
	var CM8ShowAd = function(F, I)
	{
		var C='CM8ShowAd('+F+((typeof(I)=='number')?(','+I):'')+')';
		if ((typeof(I)=="number")&&(I<1))
			CM8ErrorPopup+='\n'+C+' - Index '+I+' must be positive (1 and above)';

		F=escape(F);

		var FO;
		for (FO=CM8PHM;(FO!=null)&&(FO[0]!=F);FO=FO[1]);
		if (FO==null) {
			FO=new Array(3);
			FO[0]=F;
			FO[1]=CM8PHM;
			FO[2]=null;
			CM8PHM=FO;
		}

		var IO;
		var J=(typeof(I)=='number')?(I-1):0;
		for (;;J++) {
			for (IO=FO[2];(IO!=null)&&(IO[0]!=J);IO=IO[2]);
			if (IO==null) {
				CM8PHLI++;
				document.write('<DIV ID=CM8PH_'+CM8PHLI+'></DIV>');
				IO=new Array(3);
				IO[0]=J;
				IO[1]=document.getElementById('CM8PH_'+CM8PHLI);
				IO[2]=FO[2];
				FO[2]=IO;
				window.setTimeout('if (typeof(CM8MultiDoAll)=="function") CM8MultiDoAll();', 0);
				return;
			}
			if (typeof(I)=='number') {
				CM8ErrorPopup+='\n'+C+' - Index '+I+' is called twice';
				return;
			}
		}
	};

	var CM8FillTag = function(F,I)
	{
		F=escape(F);

		var FO;
		for (FO=CM8PHM;(FO!=null)&&(FO[0]!=F);FO=FO[1]);
		if (FO==null) return null;
		var IO;
		for (IO=FO[2];(IO!=null)&&(IO[0]!=I);IO=IO[2]);
		if (IO==null) return null;

		var D;
		for (D=0;(typeof(CM8Titles[D])!='undefined')&&(CM8Titles[D][0]!=F);D++);
		if (typeof(CM8Titles[D])=='undefined') return null;
		D=CM8Titles[D];
		if ((typeof(D[2][I])!='undefined')&&(typeof(D[2][I][0])=='function')) {
			D[2][I][0]='';
			var tag=IO[1];
			IO[1]=null;
			return tag;
		}
		return null;
	};

	var CM8Transplant = function(tag,code)
	{
		if (typeof(tag.insertAdjacentHTML)!='undefined')
			tag.insertAdjacentHTML('afterBegin', '<table>'+code+'</table>');
		else {
			var r=document.createRange();
			r.setStartAfter(tag);
			tag.insertBefore(r.createContextualFragment(code),tag.node);
		}
	};

	var CM8TransplantJSFile = function(file)
	{
		var code='<SCR'+'IPT LANGUAGE="JAVASCRIPT" DEFER SRC="'+file+'">';
		var bodyTag=document.getElementsByTagName('BODY').item(0);
		if (typeof(bodyTag.insertAdjacentElement)!='undefined') {
			var e=document.createElement(code);
			bodyTag.insertAdjacentElement('afterBegin', e);
		}
		else {
			var r=document.createRange();
			r.setStartAfter(bodyTag);
			bodyTag.insertBefore(r.createContextualFragment(code+'</SCR'+'IPT>'),bodyTag.node);
		}
	};
}

var url= ((document.location.protocol=="https:")?"https:":"http:") +
	"//" + CM8Server + "/adam/detect?" +
	"req=f" + CM8RichMedia +
	(CM8UseDefer ? "&tag=den" : "") +
	"&cat=" + CM8Cat + "&" + CM8Profile;
if (CM8UseDefer)
	window.setTimeout("CM8TransplantJSFile('"+url+"')", 0); 
else
	document.write("<SCR" + "IPT LANGUAGE='JAVASCRIPT' SRC='" + url + "'></SCR" + "IPT>"); 

