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

//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);
    }
}

/*
    ******* 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=G1:A=A6:C=pcode:R=region:T=town; 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 Demographic 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();

    //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)   {
                //Age
                case 'A':
                var sNodeName = 'ad';
                break;
                //Postcode
                case 'C':
                var sNodeName = 'cd';
                break;
                //gender
                case 'G':
                var sNodeName = 'gd';
                break;
                //Region
                case 'R':
                var sNodeName = 'rd';
                break;
                //Town
                case 'T':
                var sNodeName = 'td';
                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))   {
                this.dInfo[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 demographic data
        var iPosSemiColon = sCode.lastIndexOf(';')+1;
        var sStartDart = sCode.slice(0,iPosSemiColon);
        var sEndDart = sCode.slice(iPosSemiColon);
        //Build the demographic string
        var sDInfo = '';
        if (this.dInfo) {
            sDInfo = segQS+'ad='+this.dInfo.ad+';gd='+this.dInfo.gd+';cd='+this.dInfo.cd+';rd='+this.dInfo.rd+';td='+this.dInfo.ad+';';
        }
        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
        + ';tile=' + this.tile
        + ';ord=' + this.ord
        + '?" type="text/javascript">'
        + '</scr' + 'ipt>';
    this.tile++;
    document.write(dart_code);
}
dartUK.init();
