function loadAdvert(loader, ad) {

	if (loader.contentDocument) {
		var nodes = loader.contentDocument.body.childNodes;
	} else {
		 // IE has a contentWindow instead of a contentDocument so use that.
		var nodes = loader.contentWindow.document.body.childNodes;
	}
	
	if (document.importNode) {
		for (var i = 0; i < nodes.length; i++) {
			var item = document.importNode(nodes.item(i), true);
            if(item.nodeName != "SCRIPT") {
				ad.appendChild(item);
			}
		}
	} else {
		var adContent = "";
		for (var i = 0; i < nodes.length; i++) {
            // This works in IE, which doesn't support importNode, unfortunately cloning and appending nodes doesn't!
			var item = nodes.item(i).cloneNode(true);
			if (item.nodeName != "SCRIPT" && item.outerHTML) {
				adContent += item.outerHTML;
			}
		}
		ad.innerHTML = adContent;
	}
}

function topSlotAd(loader, ad) {
	var height = getAdProperty(ad, "height");
	
	if( height <= 60 ) {
		loadAdvert(loader, ad);
	} else if( height <= 90 ) {
		loadAdvert(loader, ad);
		ad.style.height = '90px';
		ad.style.width = '728px';
	} else {
		var comment = document.createComment("Ad too tall for top slot");
		loader.appendChild(comment);
	}
}

function rightSlot1Ad(loader, ad) {
	var width = getAdProperty(ad, "width");

	if(width <= 300) {
		loadAdvert(loader, ad);
	} else {
		var comment = document.createComment("Ad too wide for right slot 1");
		loader.appendChild(comment);
	}
}


function getAdProperty(element, property) {
	var objectList = element.getElementsByTagName("OBJECT");
	if (objectList.length > 0) return parseInt(objectList[0][property]);
	
	var imageList = element.getElementsByTagName("IMG");
	if (imageList.length > 0) return parseInt(imageList[0][property]);
	
	// Don't know what the height is so just return a value such that the ad will show
	return 0;	
}

function buildIntrusiveAd(adHost, geoCountry, geoRegion, geoCity, geoBandwidth, randString, commercialFolder, keywords, pageUrl, site) {
 	        var theseCookies = document.cookie;
 	        var pos = theseCookies.indexOf('GUDHTMLAds=');
 	        if      (pos == -1) {
 	                var seconds = 180;
 	                var expireTime = new Date();
 	                var currenttimeinmills = expireTime.getTime();
 	                expireTime.setTime(currenttimeinmills + seconds * 1000 );
 	                document.cookie = 'GUDHTMLAds=Dummy; expires=' + expireTime.toGMTString() + ' ; path=/ ; domain=guardian.co.uk';
	               
 	        var     intrusad =
 	            '<' + 'script type="text/javascript" src="' + adHost +
 	            '/js.ng/spacedesc=01&amp;comfolder=' + commercialFolder +
 	            '&amp;keywords=' + keywords +
 	            '&amp;country=' + geoCountry +
 	            '&amp;region=' + geoRegion +
 	            '&amp;city=' + geoCity +
 	            '&amp;bandwidth=' + geoBandwidth +
 	            '&amp;rand=' + randString +
 	            '&amp;site=' + site +
 	            '&amp;url=' + pageUrl +  '"></' + 'script>';
 	           
 	                document.write(intrusad);
 	                document.close();
 	        }
 	       
 	}
