function alphaNumericalOnly(inputStr, replaceOthersWith) {
	var output = inputStr.toLowerCase();

	// Whitelist character verification
	var allowedCharacters = "a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9";
	var outputWithOnlyAllowedChars = "";

	for (var i=0; i<output.length; i++) {
		if (allowedCharacters.indexOf(output.charAt(i)) != -1) {
			outputWithOnlyAllowedChars += output.charAt(i);
		} else {
			outputWithOnlyAllowedChars += replaceOthersWith;
		}
	}

	return outputWithOnlyAllowedChars;
}

function numericalOnly(inputStr, replaceOthersWith) {
	var output = inputStr.toLowerCase();

	// Whitelist character verification
	var allowedCharacters = "0123456789";
	var outputWithOnlyAllowedChars = "";

	for (var i=0; i<output.length; i++) {
		if (allowedCharacters.indexOf(output.charAt(i)) != -1) {
			outputWithOnlyAllowedChars += output.charAt(i);
		} else {
			outputWithOnlyAllowedChars += replaceOthersWith;
		}
	}

	return outputWithOnlyAllowedChars;
}

// ***************************************************** Ad tag generation script *****************************************************
var BASE_AD_TAG = "http://ad.doubleclick.net/adj/sri.";
var SUBDOMAINS_TO_IGNORE = "preprod, wc1, wc2, noc, dev, www, concours";
var pageTile=0;
var ord = "ord=" + Math.round(Math.random()*100000000000) + "?";

function printAdTag(adSize, position) {

	// Init vars
	// var pageURL = self.location.href; Is this for iframes?
	var pageUrl = document.location + "";
	var zonename = "";
	var keyvaluepair1 = "";
	var keyvaluepair2 = "";

	// Increment tiles
	pageTile++;

	// Dcopt for use with tile 1
	var dcopt = "";
	if (pageTile == 1) {
		dcopt = "dcopt=ist;";
	}

	// Adjust position for usage (valid values : top, bottom / Defaults to top)
	if (position == null) {
		var position = "top";
	}
	var pos = "pos=" + position + ";";

	// Valid sizes : 728x90, 300x250, 160x600
	if (adSize == undefined || adSize == '') return 0;

	// Remove http://subdomain.
	var pageUrlTransit = pageUrl.substring(pageUrl.indexOf(".")+1);

	// Split url on /
	var urlSplitArray = pageUrlTransit.split("/");

	// Add site to base ad tag
	var baseAdTagWithSite = BASE_AD_TAG + urlSplitArray[0].substring(0, (urlSplitArray[0].length-4)) + "/";

	// Add subdomain to array if it's not blacklisted
	var subdomain = (document.domain + "").substring(0, (document.domain + "").indexOf("."));
	if (SUBDOMAINS_TO_IGNORE.indexOf(subdomain) == -1) {
		urlSplitArray.splice(1, 0, subdomain);
	}

	// Build zonename
	if (urlSplitArray.length >= 2) {
		if (urlSplitArray[1] != "") {
			zonename = alphaNumericalOnly(urlSplitArray[1], "").toLowerCase();
		} else {
			// This is homepage
			zonename = "homepage";
		}
	}

	// Build keyvaluepair1
	if (urlSplitArray.length >= 3) {
		if (urlSplitArray[2] != "") {
			keyvaluepair1 = "ss=" + alphaNumericalOnly(urlSplitArray[2], "").toLowerCase() + ";";
		} else if (zonename != "homepage" && zonename != "") {
			// This is a section's homepage
			keyvaluepair1 = "ss=sectionhome;";
		}
	}

	// Build keyvaluepair2
	if (urlSplitArray.length >= 4) {
		if (urlSplitArray[3] != "") {
			// Don't use value #3, use all the remaining url
			var restOfUrl = pageUrlTransit.substring(pageUrlTransit.indexOf(urlSplitArray[3]));
			keyvaluepair2 = "title=" + numericalOnly(restOfUrl, "").toLowerCase() + ";";
		}
	}

	// Build recipeCategoriesKeyValue from specified recipe categories
	var recipeCategoriesKeyValue = "";
	if (typeof(recipeCategories) == "string") {
		recipeCategoriesKeyValue = recipeCategories;
	}

	// Build recipeshowname key value from specified recipe program title
	var recipeShowNameKeyValue = "";
	if (typeof(recipeshowname) == "string") {
		recipeShowNameKeyValue = "recipeshowname=" + alphaNumericalOnly(recipeshowname, "") + ";";
	}

	// Build ad tag src
	var adTagSrc = baseAdTagWithSite + zonename + ";" + keyvaluepair1 + keyvaluepair2 + recipeCategoriesKeyValue + recipeShowNameKeyValue + dcopt + pos + "tile=" + pageTile + ";sz=" + adSize + ";" + ord;

	// Build ad tag to print
	var adTag = "<sc"+"ript type='text/jav"+"ascript' src='" + adTagSrc + "'></scr"+"ipt>";

	// Finally, print it
	document.write(adTag.toLowerCase());

}
