function dartUK(){};
dartUK.svr = 'http://ad.uk.doubleclick.net';
dartUK.tile=0;
dartUK.checktile=0;
dartUK.ord=0;
dartUK.dInfo = new Object();
dartUK.dInfo.sFullData = '';

//Initialisation
dartUK.init = function()	{
	//Initiate a tile value and ord value for this page load.
	this.generatetile();
	this.generateOrd();
}

/*
	******* generateOrd *******
	This function generates an ORD value for the current page load.
	This value is used by all ads in a page to ensure they are not cached.
	Each subsequent page load should generate a new value.
	Accepts: no arguments
	Returns: a populate ord value
*/
dartUK.generateOrd = function()	{
	if (this.ord==0)
	{
		this.ord = (''+new Date().getTime()).substring(4)+ (''+Math.random(1)).substring(2,6)*1000000;
	}
}

/*
	******* generatetile *******
	Works out the tile value
	this is used to sort the tile DART parameter value which is an
	incremental indicate of the ad position in a page commencing at 1 for the 1st ad in a page.
	Accepts: no arguments
	Returns: value of tile
*/
dartUK.generatetile = function()	{
		this.tile = ((typeof dartCount != 'undefined'))?dartCount:1;
		this.checktile=1;
}

/*
	******* createStubCookie *******

	Stub function to to create a dummy cookie data value whilst we wait for the real thing
*/
dartUK.createStubCookie = function()	{
	var cookie_date = new Date (2008,01,31);  // current date & time
	document.cookie = "dinfo= G%3DG3%3AA%3DA7%3AC%3DC1%3AR%3D78%3AT%3D1358; expires=" + cookie_date.toGMTString() + ";domain=orange.co.uk" ;
}

/*
	******* readCookie *******

	Generic cookie reading function.
	Accepts: one argument of the cookie name.
	Returns: an array of name/value pairs
*/
dartUK.readCookie = function(sCookie)	{
	var dc = document.cookie;
	var prefix = sCookie + "=";
	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));
	return unescape(dc.substring(begin + prefix.length, end));
}

/*
	******* processDInfo *******

	The function that takes the array return by readCookie
	and populates the targeted data name/value pairs.
	Accepts: no arguments - readCookie function
	Returns: values to the corresponding nodes in the dartUK.dInfo onject

*/
dartUK.processDInfo = function()	{
	//Generate Stub cookie code
	//this.createStubCookie();

	this.dInfo.sFullData = '';

	//Read values out of the dinfo cookie
	var sCookieContents = this.readCookie('dinfo');
	//Chuck the string into an array
	if (sCookieContents != null)	{
		var aCookieElements = sCookieContents.split(':');
		//Now take the contents of sCookieContents and place the values in the corresponding nodes of dartUK.dInfo
		//Loop over the array elements and try to populate the dInfo object nodes
		for (i=0;i<aCookieElements.length;i++)	{
			var sCaseStr = aCookieElements[i].substring(0,1);
			var aCaseNameValuePair = aCookieElements[i].split('=');
			var sCaseVal = aCaseNameValuePair[1];
			switch (sCaseStr)	{
				case 'A':
				var sNodeName = 'ad';
				break;
				case 'C':
				var sNodeName = 'cd';
				break;
				case 'G':
				var sNodeName = 'gd';
				break;
				case 'R':
				var sNodeName = 'rd';
				break;
				case 'T':
				var sNodeName = 'td';
				break;
				// Additional
				default:
				var sNodeName = aCaseNameValuePair[0];
				break;
			}
			//Only update a node if the switch statement triggers and if the value for this name/value pair has some content
			if (typeof sNodeName != 'undefined' && (sCaseVal != '' || sCaseVal.length != -1 || sCaseVal != null))	{
				if (this.dInfo.sFullData != '') {
					this.dInfo.sFullData = this.dInfo.sFullData + ';';
				}
				this.dInfo.sFullData = this.dInfo.sFullData + sNodeName + '=' + sCaseVal;
			}
		}
	}
}

/*
	******* buildDART *******
	This function works out whether to build target DART string or not.
	Accepts: vanilla DART CODE
	Returns: a validly targeted/non-targeted DART code.

*/
dartUK.buildDART = function(sCode,bPartner)	{

	//Initialise variable for rebuilt DART code
	var sDART = '';
	//Catch any issues with Revenue Science code failing.
	/*if (typeof segQS == 'undefined')
	{
		var segQS = '';
	}*/

	//Code to work out whether to serve a targeted ad or not. Read in the contents of the dInfo cookie.
	//this.processDInfo
	if(bPartner==0) { //This IF statement will change as it indicates a targeted DART code for the Portal...
		this.processDInfo();
		//Split the sCode value so that we can insert targeted data
		var iPosSemiColon = sCode.lastIndexOf(';sz')+1;
		var sStartDart = sCode.slice(0,iPosSemiColon);
		var sEndDart = sCode.slice(iPosSemiColon);
		//alert(iPosSemiColon + '\n' + sStartDart + '\n' + sEndDart);
		//Build the targeting string
		var sDInfo = '';
		if (this.dInfo.sFullData != '')	{
			sDInfo = segQS+this.dInfo.sFullData + ';';
		}
		else	{
			sDInfo = segQS;
		}
		sDART = sStartDart + sDInfo + sEndDart;
		return sDART;
	}
	//code is for a partner site and there is to be no attempt at serving a DART code with DInfo name/value pairs.
	else	{
		sDART = sCode;
		return sDART;
	}
}



/*
	******* render *******

	The function constructs the DART callto Doubleclick
	Accepts:	base DART string in parmameter named sCode
				boolean as to whether called from a partner or not bPartner - optional
	Returns:	document.write of the js include to show the DART ad

*/
dartUK.render = function (sCode,bPartner)
{
	//bPartner is optional so let's default it to 0 if not from a partner
	if (typeof bPartner == 'undefined')
	{
		var bPartner = 0;
	}

	//Call the DART generation function.
	var sDartCode = this.buildDART(sCode, bPartner);


	var dart_code = '<scri' + 'pt language="JavaScript" '
		+ ' src="' + this.svr + '/adj/'+ sDartCode;
		//Optionally insert a semi-colon if one isn't present to maintain correct DART syntax.
		if (dart_code.charAt(dart_code.length-1) != ';')	{
			dart_code += ';';
		}
		dart_code += 'tile=' + this.tile
		+ ';ord=' + this.ord
		+ '?" type="text/javascript">'
		+ '</scr' + 'ipt>';
	this.tile++;
	document.write(dart_code);
}
dartUK.init();

