if (typeof(MediaSmart) != "object") {
	MediaSmart = new Object;
	MediaSmart.ads = new Array();
	
	// Needed for randomize victorian caravan ad fix
	// Creates a randomly sorted "deck" of the numbers 1-4
	Array.prototype.shuffle = function() {
		function randOrd(){return (Math.round(Math.random())-0.5); }
		this.sort( randOrd );
	}
	//var caravanAdArray = [1,2,3,4]; 
	//caravanAdArray.shuffle();
	
	
	
//PreDefined site and area -- Customize for each site
/*
if (typeof(MediaSmart.site) == "undefined") {
		if((document.location + "").indexOf("tradingpost.com.au/Automotive/") != -1)
				MediaSmart.site = "SEN.AUTOTRADER.AUTOMOTIVE";
		else
		MediaSmart.site = "SEN.TRADINGPOST.CLASSIFIED";
}*/

/*if (typeof(MediaSmart.tags1) == "undefined") {
		if((document.location + "").indexOf("tradingpost.com.au/Automotive/") != -1)
				MediaSmart.tags1.area = "AUTOMOTIVE.AUTOTRADER";
		else
		MediaSmart.tags1.area = "CLASSIFIED.TP";
}*/

	MediaSmart.newAd = function(json){		
		if (typeof(json.polite) != "undefined" && json.polite == false) {
			document.write(MediaSmart.Utilities.generateAdCall(json));
		}
		else if(json.id.toLowerCase() == "generictower"){
			var towerpercent =0;
			var skypercent = 0;
			var sizedecider = (Math.floor(Math.random()*100));
			json.width = 160 ;
			json.height = 600 ; 
			if(sizedecider<towerpercent){								
				json.width = 120;
				json.height = 240;
				MediaSmart.ads[MediaSmart.ads.length] = json;
				document.write("<div id='zMSplacement" + MediaSmart.ads.length + "' style='width:" + json.width + "px; height:" + json.height + "px;'></div>");
				document.write("<div style='height: 20px' ></div>");
				MediaSmart.ads[MediaSmart.ads.length] = json;
				document.write("<div id='zMSplacement" + MediaSmart.ads.length + "' style='width:" + json.width + "px; height:" + json.height + "px;'></div>");
			}
			else if(sizedecider<(towerpercent+skypercent)){
				json.width = 120 ;
				json.height = 600;
				MediaSmart.ads[MediaSmart.ads.length] = json;
				document.write("<div id='zMSplacement" + MediaSmart.ads.length + "' style='width:" + json.width + "px; height:" + json.height + "px;'></div>");
			}
			else {
				MediaSmart.ads[MediaSmart.ads.length] = json;
				document.write("<div id='zMSplacement" + MediaSmart.ads.length + "' style='width:" + json.width + "px; height:" + json.height + "px;'></div>");
			}
		}
		else if(json.width == 2 && json.height ==4){
			MediaSmart.ads[MediaSmart.ads.length] = json;
			document.write("<div id='zMSplacement" + MediaSmart.ads.length + "' style='width:148px; height:120px;'></div>");
		}
		else if(json.width == 300 && json.height == 250 && page == "addetails" && tpsite == "SEN.AUTOTRADER.AUTOMOTIVE"){
			MediaSmart.newAd({id: "topIS",width: "300",height: "30",position: "1", polite: false});
			MediaSmart.ads[MediaSmart.ads.length] = json;
			document.write("<div id='zMSplacement" + MediaSmart.ads.length + "' style='width:" + json.width + "px; height:" + json.height + "px;'></div>");
		}
		else if((json.width == 300 && json.height == 250) || (json.width == 728 && json.height == 90)){
			var sizedecider = (Math.floor(Math.random()*100));
			var expandPercent = 0;
				
			if(sizedecider < expandPercent){
				json.polite = false; 
				document.write(MediaSmart.Utilities.generateAdCall(json));
			}else{
				MediaSmart.ads[MediaSmart.ads.length] = json;
				document.write("<div id='zMSplacement" + MediaSmart.ads.length + "' style='width:" + json.width + "px; height:" + json.height + "px;'></div>");
			}
		}
		else {
			MediaSmart.ads[MediaSmart.ads.length] = json;
			document.write("<div id='zMSplacement" + MediaSmart.ads.length + "' style='width:" + json.width + "px; height:" + json.height + "px;'></div>");
		}
		if (json.width == 150 && json.height == 200) {
				document.getElementById("zMSplacement" + MediaSmart.ads.length).style.display = "none";
		}
	}
}


doBankWestFix = false;

MediaSmart.Utilities = new Object;
MediaSmart.Utilities.initialise = function(){
	MediaSmart.pageId = MediaSmart.Utilities.random();
	if ((document.location + "").indexOf("https://") != -1) {
		MediaSmart.domain = "https://media.sensis.com.au";
		MediaSmart.cookieDomain = "https://medrx.sensis.com.au";
		MediaSmart.adSecure = 1;
	}
	else { 
		MediaSmart.domain = "http://media.sensis.com.au";
		MediaSmart.cookieDomain = "http://medrx.sensis.com.au";
		MediaSmart.adSecure = 0;
	}
	
	if( Math.floor(Math.random()*100) < 80 )
		doBankWestFix = true;
	else
		doBankWestFix = false;
		
}
MediaSmart.Utilities.random = function(){
	return Math.round(Math.abs(Math.sin((new Date()).getTime())) * 1000000000000);
}

MediaSmart.Utilities.generateAdCall = function(ad){
	// Determine whether we're in polite mode
	if (typeof(ad.cmd) == "function")
		ad.cmd();
	if (typeof(ad.tags) == "undefined")
		ad.tags = new Array();
	var polite = true;
	if (typeof(ad.polite) != "undefined" && ad.polite == false)
		polite = false;
		
	// 206x200 fix
	if( ad.width == 206 && ad.height == 200 ) {
		ad.width = 1;
		ad.height = 7;
	}
	
	if( ad.width == "206" && ad.height == "125" && ad.id.indexOf("seller_tile")!=-1 ) {
		MediaSmart.generateSellerTile(ad, polite);
		return "";
	}

	// Generate the start of the ad call
	if (polite) {
		adCallString = "<iframe gen='1' scrolling='no' allowtransparency='yes' frameborder='0' marginheight='0' marginwidth='0' vspace='0' ";
		
		if (ad.iframeHeight != "" && typeof(ad.iframeHeight) != "undefined") 
			adCallString += "height='" + ad.iframeHeight + "' ";
		else 
			adCallString += "height='" + ad.height + "' ";
		if (ad.iframeWidth != "" && typeof(ad.iframeWidth) != "undefined") 
			adCallString += "width='" + ad.iframeWidth + "' ";
		else 
			adCallString += "width='" + ad.width + "' ";
		// redirect to cookiefix for island and leaderboard
		if ((ad.width == 300 && ad.height == 250) || (ad.width == 728 && ad.height == 90) || (ad.width == 160 && ad.height == 600))
			adCallString += "src='" + MediaSmart.cookieDomain + "/images/sensis/cookieFix.html?acc_random=" + MediaSmart.Utilities.random();
		else
			adCallString += "src='" + MediaSmart.domain + "/hserver/acc_random=" + MediaSmart.Utilities.random();
			
	}
	else {
		adCallString = "<scr" +"ipt src='"+ MediaSmart.domain + "/jserver/acc_random=" + MediaSmart.Utilities.random();
	}
	
	// Add the SITE variable
	adCallString = adCallString + "/SITE=" + MediaSmart.getSiteValue(ad);
	
	// Add the AREA variable
	adCallString = adCallString + "/AREA=" + MediaSmart.getAreaValue(ad);
	
	// Add the CATEG variable
	var categVal = MediaSmart.getCategValue(ad);
	if( categVal != "" )
		adCallString = adCallString + "/CATEG=" + categVal;
		
	// Add the MCSB (State) variable
	adCallString = adCallString + "/MCSB=TP." + MediaSmart.getStateValue(ad);
	
	// Add the AAMSZ variable
	adCallString = adCallString + "/AAMSZ=" + MediaSmart.getAAMSZValue(ad);
	
	// Add the POSITION variable
	adCallString = adCallString + "/POSITION=" + MediaSmart.getPositionValue(ad, polite);
		
	//below is for Vibe
	/*	 if(MediaSmart.tags1.section.toLowerCase() == "automotive"){
				var msAge = "USED";
				if(typeof(MediaSmart.tags1.yearfrom) != "undefined" && MediaSmart.tags1.yearfrom != "") {
				if(parseInt(MediaSmart.tags1.yearfrom) >= 2006)
						msAge= "NEARNEW";
				}
				var msValue = "null";
				if(typeof(MediaSmart.tags1.pricefrom) != "undefined" && MediaSmart.tags1.pricefrom != "" && typeof(MediaSmart.tags1.priceto) != "undefined" && MediaSmart.tags1.priceto != "")
						msValue = (parseInt(MediaSmart.tags1.pricefrom) + parseInt(MediaSmart.tags1.priceto)) /2;
				if(typeof(MediaSmart.tags1.pricefrom) != "undefined" && MediaSmart.tags1.pricefrom != "" && (typeof(MediaSmart.tags1.priceto) == "undefined" || MediaSmart.tags1.priceto == ""))
						msValue = parseInt(MediaSmart.tags1.pricefrom);
				if((typeof(MediaSmart.tags1.pricefrom) == "undefined" || MediaSmart.tags1.pricefrom == "") && typeof(MediaSmart.tags1.priceto) != "undefined" && MediaSmart.tags1.priceto != "")
						msValue = parseInt(MediaSmart.tags1.priceto);
				if(msValue < 10000)
						msValue = "LOW";
				else if (msValue >= 10000 && msValue <= 20000)
						msValue = "MEDIUM";
				else if (msValue>20000 && msValue < 30000)
						msValue = "HIGH";
				else 
						msValue = "VHIGH";
				adCallString += "/VIBE=VB."
				if(MediaSmart.tags1.subsection != "")
						adCallString += MediaSmart.tags1.subsection.toUpperCase() + ".";
				adCallString += msAge;
				if(typeof(MediaSmart.tags.carclass) != "undefined" && MediaSmart.tags.carclass != "")
						adCallString += MediaSmart.tags.carclass.toUpperCase() + ".";
	}		
	*/

	// Add the PRICEMIN and PRICEMAX variables
	var priceMin = MediaSmart.getPriceMinValue(ad);
	if( priceMin != "" )
		adCallString = adCallString + "/MINPRICE=" + priceMin;
	var priceMax = MediaSmart.getPriceMaxValue(ad);
	if( priceMax != "" )
		adCallString = adCallString + "/MAXPRICE=" + priceMax;
		
	// Add the KEYWORD variable
	var keywordVal = MediaSmart.getKeywordValue(ad, polite);
	if( keywordVal != "" )
		adCallString = adCallString + "/KEYWORD=" + keywordVal;
	
	
	
	// Add all the extra tags
	for (key in MediaSmart.tags) {
		if (typeof(MediaSmart.tags[key]) != "undefined" && MediaSmart.tags[key] != "" && MediaSmart.tags[key].toLowerCase() != "null") 
			adCallString += "/" + key.toUpperCase() + "=" + MediaSmart.tags[key].toUpperCase().replace(/\s+|\//gi,"_").replace(/-/g, "_").replace(/\([^)]*\)/g,"").replace(/\_$/g,"");
	}
	if (typeof(ad.tags) != "undefined" && ad.tags != "") {
		for (key in ad.tags) {
			adCallString += "/" + key.toUpperCase() + "=" + ad.tags[key].toUpperCase();
		}
	}
	
	if((document.location + "").indexOf("MSTOOTHPASTE") != -1){
		adCallString += (document.location + "").slice((document.location + "").lastIndexOf("MSTOOTHPASTE")+12);
	}	

	adCallString += "/pageid=" + MediaSmart.pageId;
	
	if(polite)
		adCallString += "'></iframe>\n";
	else
		adCallString += "'></script>\n";

	MediaSmart.logValues( adCallString );
	return adCallString;
}

MediaSmart.insertAd = function(ad, elementId){
	var element = "";
	if (typeof(ad) == "string") {
		ad = MediaSmart.Utilities.getAdById(ad);
		if (ad) {
			if (elementId == "" || typeof(elementId) == "undefined") 
				element = document.getElementById("zMSplacement" + MediaSmart.Utilities.adIndex);
			else {
				if (document.getElementById(elementId)) 
					element = document.getElementById(elementId);
			}
			
			element.innerHTML = MediaSmart.Utilities.generateAdCall(ad);
		}
	}
	else {
		if (typeof(ad) == "object") {
			if (typeof(ad.length) === 'number' && !(ad.propertyIsEnumerable('length')) && typeof ad.splice === 'function') {
				for (i = 0; i < ad.length; i++) {
					if (document.getElementById(ad[i].containerId)) 
						document.getElementById(ad[i].containerId).innerHTML = MediaSmart.Utilities.generateAdCall(ad[i]);
				}
			}
			else 
				document.getElementById(ad.containerId).innerHTML = MediaSmart.Utilities.generateAdCall(ad);
		}
	}
}

MediaSmart.Utilities.getAdById = function(id){
	for (i = 0; i < MediaSmart.ads.length; i++) {
		if (MediaSmart.ads[i].id == id) {
			MediaSmart.Utilities.adIndex = i;
			return MediaSmart.ads[i];
		}
	}
	return false;
}

MediaSmart.insertAllAds = function(){
if ( ((document.location + "").indexOf("sdmtestid") != -1)) {var fileref=document.createElement('script');fileref.setAttribute("type","text/javascript");fileref.setAttribute("src", "https://media.sensis.com.au/images/houseads_sensis/sdm_testsite.js");document.getElementsByTagName("head")[0].appendChild(fileref);}else{
	for (i = 0; i < MediaSmart.ads.length; i++) {
		element = document.getElementById("zMSplacement" + (i + 1));
		MediaSmart.ads[i].top = element.offsetTop;
		while(element=element.offsetParent){MediaSmart.ads[i].top += element.offsetTop;}
		document.getElementById("zMSplacement" + (i + 1)).innerHTML = MediaSmart.Utilities.generateAdCall(MediaSmart.ads[i]);
	}
}
}

MediaSmart.Utilities.makeURLSafe = function(text){
	text = text.replace(/ |-|<|>|\/|\?|\+/g, "_");
	text = text.replace(/\&AMP\;/g, "AND");
	text = text.replace(/&/g, "AND");
	text = text.replace(/_+/g, "_");
	text = text.replace(/^_|_$/g, "");
	//This is to replace inch (") with nothing
	text = text.replace("ANDQUOT;","");
	text = text.replace("'","");
	return text;
}


MediaSmart.generateSellerTile = function(ad, polite) {
	if( ad.position == 1 ) {
		// Batch call 4 ads
		var batchAdCall = "http://media.sensis.com.au/bserver/AAMALL";
		batchAdCall = batchAdCall + "/SITE=" + MediaSmart.getSiteValue(ad);
		batchAdCall = batchAdCall + "/AREA=" + MediaSmart.getAreaValue(ad);
		
		var categVal = MediaSmart.getCategValue(ad);
		if( categVal != "" )
			batchAdCall = batchAdCall + "/CATEG=" + categVal;
			
		batchAdCall = batchAdCall + "/MCSB=TP." + MediaSmart.getStateValue(ad);
		batchAdCall = batchAdCall + "/AAMSZ=" + MediaSmart.getAAMSZValue(ad);
		
		// Add the PRICEMIN and PRICEMAX variables
		var priceMin = MediaSmart.getPriceMinValue(ad);
		if( priceMin != "" )
			batchAdCall = batchAdCall + "/MINPRICE=" + priceMin;
		var priceMax = MediaSmart.getPriceMaxValue(ad);
		if( priceMax != "" )
			batchAdCall = batchAdCall + "/MAXPRICE=" + priceMax;
		
		// Add the KEYWORD variable
		var keywordVal = MediaSmart.getKeywordValue(ad, polite);
		if( keywordVal != "" )
			batchAdCall = batchAdCall + "/KEYWORD=" + keywordVal;
		
		// Add all the extra tags
		for (key in MediaSmart.tags) {
			if (typeof(MediaSmart.tags[key]) != "undefined" && MediaSmart.tags[key] != "" && MediaSmart.tags[key].toLowerCase() != "null") 
				batchAdCall += "/" + key.toUpperCase() + "=" + MediaSmart.tags[key].toUpperCase().replace(/\s+|\//gi,"_").replace(/-/g, "_").replace(/\([^)]*\)/g,"").replace(/\_$/g,"");
		}
		if (typeof(ad.tags) != "undefined" && ad.tags != "") {
			for (key in ad.tags) {
				batchAdCall += "/" + key.toUpperCase() + "=" + ad.tags[key].toUpperCase();
			}
		}
		
		batchAdCall += "/pageid=" + MediaSmart.pageId;
		batchAdCall += "/acc_random=" + MediaSmart.Utilities.random();		
		batchAdCall += "/AAMB1/POSITION=1/AAMB2/POSITION=2/AAMB3/POSITION=3/AAMB4/POSITION=4";
		
		document.write( '<scr' + 'ipt src="' + batchAdCall + '">' );
		document.write( '</scr' + 'ipt>' );
		
		// This doesn't work in IE or Chrome (loaded too late for the ad calls to work)
		/*var adScript = document.createElement('script');
		adScript.setAttribute("type","text/javascript");
		adScript.setAttribute("src", batchAdCall);
		document.getElementsByTagName("head")[0].appendChild(adScript);*/
	}
	
	document.write( '<div id="tpsellertile_' + ad.position + '"></div>' );
	
	if(ad.position == "4"){
		var tileArray = new Array();
		
		if( AAMB1.indexOf("default.gif")==-1 && AAMB1.indexOf("trans.gif")==-1 )
			tileArray.push( AAMB1 );
		if( AAMB2.indexOf("default.gif")==-1 && AAMB2.indexOf("trans.gif")==-1 )
			tileArray.push( AAMB2 );
		if( AAMB3.indexOf("default.gif")==-1 && AAMB3.indexOf("trans.gif")==-1 )
			tileArray.push( AAMB3 );
		if( AAMB4.indexOf("default.gif")==-1 && AAMB4.indexOf("trans.gif")==-1 )
			tileArray.push( AAMB4 );
			
		tileArray.shuffle();
	
		for( var tileId=1; tileId<=4; tileId++ ) {
			if( tileArray.length > 0 )
				document.getElementById( "tpsellertile_" + tileId ).innerHTML = tileArray.pop();		
		}
	}
}


MediaSmart.getSiteValue = function(ad) {
	var siteValue;

	if (ad.site != "" && typeof(ad.site) != "undefined") {
		return ad.site;
	}/*
	else if( tags[0] == "SEN.TRADINGPOST.FX.CLASSIFIED" ) {
		return "SEN.TRADINGPOST.FX.CLASSIFIED";
	}*/
	else {
		if(MediaSmart.site.indexOf("autotrader") != -1 || MediaSmart.site == "SEN.AUTOTRADER.AUTOMOTIVE")
			MediaSmart.site = "SEN.AUTOTRADER.AUTOMOTIVE";
		else
			MediaSmart.site = "SEN.TRADINGPOST.CLASSIFIED";
		return MediaSmart.site;			
	}
}

MediaSmart.getAreaValue = function(ad) {
	var areaValue;

	if (ad.area != "" && typeof(ad.area) != "undefined") 
		areaValue = ad.area;
	else if(MediaSmart.tags1.area != "" && typeof(MediaSmart.tags1.area) != "undefined")
		areaValue = MediaSmart.tags1.area;
	else{
		if((document.location + "").indexOf("automotive") != -1)
			areaValue = "AUTOMOTIVE.AUTOTRADER";
		else
			areaValue = "CLASSIFIED.TP";
		areaValue = areaValue.toUpperCase().replace(/\s+|\//gi,"_").replace(/-/g, "_").replace(/\([^)]*\)/g,"").replace(/\_$/g,"");
	}
	
	// Append the SECTION to the AREA value if applicable
	if (ad.section != "" && typeof(ad.section) != "undefined" ) 
		areaValue += "." + ad.section;
	else if(MediaSmart.tags1.section != "" && typeof(MediaSmart.tags1.section) != "undefined" && MediaSmart.tags1.section.toUpperCase() != "MAIN" && MediaSmart.tags1.section != "null" && MediaSmart.tags1.section.toUpperCase() != "AUTOMOTIVE")
		areaValue += "." + MediaSmart.tags1.section.toUpperCase().replace(/\s+|\//gi,"_").replace(/-/g, "_").replace(/\([^)]*\)/g,"").replace(/\_$/g,"");
	
	// Append the SUBSECTION to the AREA value if applicable
	if (ad.subsection != "" && typeof(ad.subsection) != "undefined") 
		areaValue += "." + ad.subsection.toUpperCase().replace(/\s+|\//gi,"_").replace(/-/g, "_").replace(/\([^)]*\)/g,"").replace(/\_$|/g,"").replace("'","");
	else if(MediaSmart.tags1.subsection != "" && typeof(MediaSmart.tags1.subsection) != "undefined" && MediaSmart.tags1.subsection != "null" && MediaSmart.tags1.subsection.toUpperCase() != MediaSmart.tags1.page.toUpperCase())
		areaValue += "." + MediaSmart.tags1.subsection.toUpperCase().replace(/\s+|\//gi,"_").replace(/-/g, "_").replace(/\([^)]*\)/g,"").replace(/\_$/g,"").replace("'","");
	
	// Append the PAGE to the AREA value if applicable
	if(MediaSmart.tags1.page != "" && typeof(MediaSmart.tags1.page) != "undefined"){
		if((MediaSmart.tags1.subsection == "" || MediaSmart.tags1.subsection == "null") && MediaSmart.tags1.section.toLowerCase() == "automotive" ){
			MediaSmart.tags1.page ="Home";
		}
		areaValue += "." + MediaSmart.tags1.page.toUpperCase();
	}
	
	areaValue = areaValue.replace( /-/g, "_" );

	return areaValue;
}

MediaSmart.getCategValue = function(ad) {
	if(MediaSmart.tags1.category != "" && typeof(MediaSmart.tags1.category) != "undefined" && MediaSmart.tags1.category.toLowerCase() != "null"){
		return MediaSmart.tags1.category.toUpperCase().replace(/\s+|\//gi,"_").replace(/-/g, "_").replace(/\([^)]*\)/g,"").replace(/\_$/g,"").replace(/:/,"").replace(/'/,"");
	}
	else
		return "";
}

MediaSmart.getStateValue = function(ad) {
	if (ad.state != "" && typeof(ad.state) != "undefined") 
		return ad.state;
	else if(MediaSmart.tags1.state != "" && typeof(MediaSmart.tags1.state) != "undefined"){
			if(MediaSmart.tags1.state == "ALL_STATES")
				MediaSmart.tags1.state = "NAT";
			return MediaSmart.tags1.state.toUpperCase();
		}
	else
		return "NAT";
}

MediaSmart.getAAMSZValue = function(ad) {
	return ad.width + "x" + ad.height;
}

MediaSmart.getPositionValue = function(ad, polite) {
	var positionVal = "";

	// Find height of window
	if(typeof(window.innerWidth) == 'number') {
		MediaSmart.innerHeight = window.innerHeight;
	}else if(document.documentElement && document.documentElement.clientHeight) {
		MediaSmart.innerHeight = document.documentElement.clientHeight;
	}else if(document.body && document.body.clientHeight) {
		MediaSmart.innerHeight = document.body.clientHeight;
	}
	
	if(polite){
			if(MediaSmart.innerHeight-ad.top-90>0||typeof(ad.top)=="undefined")
				positionVal += "ABV";
			else
				positionVal += "BLW";
			if (ad.position != "" && typeof(ad.position) != "undefined")	 
				positionVal += ad.position;
	}
	else if (ad.position != "" && typeof(ad.position) != "undefined") {	
		// Victorian caravan park ad randomization fix
		/*
		if( ad.width == "206" && ad.height == "125" && ad.id.indexOf("seller_tile")!=-1 ) {
			if( caravanAdArray.length > 0 ) {
				var currentAdNumber = caravanAdArray.pop();
				ad.position = currentAdNumber;
				ad.id = "seller_tile_" + currentAdNumber;
			}
			else {
				// More than 4 caravan ads, THIS SHOULD NEVER EXECUTE!
			}
		}*/
		positionVal = ad.position;
	}
	return positionVal;
}

MediaSmart.getPriceMinValue = function(ad) {
	if( typeof(MediaSmart.tags1.pricefrom)!="undefined" && MediaSmart.tags1.pricefrom!="null" && MediaSmart.tags1.pricefrom!="" ) {
		if( MediaSmart.tags1.pricefrom.indexOf( "." ) != -1 ) {
			MediaSmart.tags1.pricefrom = MediaSmart.tags1.pricefrom.substr( 0, MediaSmart.tags1.pricefrom.indexOf( "." ) );
		}
		
		var returnValue = "";
		var leadingZeros = 10 - MediaSmart.tags1.pricefrom.length;
		var indexCounter = 0;
		for( indexCounter = 0; indexCounter<leadingZeros; indexCounter++ )
			returnValue += "0";
		returnValue += MediaSmart.tags1.pricefrom;

		return returnValue;
	}
	return "";
}

MediaSmart.getPriceMaxValue = function(ad) {
	if( typeof(MediaSmart.tags1.priceto)!="undefined" && MediaSmart.tags1.priceto!="null" && MediaSmart.tags1.priceto!="" ) {
		if( MediaSmart.tags1.priceto.indexOf( "." ) != -1 ) {
			MediaSmart.tags1.priceto = MediaSmart.tags1.priceto.substr( 0, MediaSmart.tags1.priceto.indexOf( "." ) );
		}
		
		var returnValue = "";
		var leadingZeros = 10 - MediaSmart.tags1.priceto.length;
		var indexCounter = 0;
		for( indexCounter = 0; indexCounter<leadingZeros; indexCounter++ )
			returnValue += "0";
		returnValue += MediaSmart.tags1.priceto;

		return returnValue;		
	}
	return "";
}

MediaSmart.getKeywordValue = function(ad, polite) {
	var keywordVal = "";

	if (ad.keyword != "" && typeof(ad.keyword) != "undefined" && ad.keyword != "null") 
		keywordVal += ad.keyword.toUpperCase();
	else if((document.location + "").indexOf("?mstoothpaste") != -1)
		keywordVal += "MSTOOTHPASTE";
	else if (MediaSmart.keyword != "" && typeof(MediaSmart.keyword) != "undefined" && MediaSmart.keyword != "null") 
		keywordVal += MediaSmart.Utilities.makeURLSafe(MediaSmart.keyword.toUpperCase());
		
	if( typeof(MediaSmart.tags1.seller) != "undefined" && MediaSmart.tags1.seller != "" && MediaSmart.tags1.seller != "null"){
		if( keywordVal != "" )
			keywordVal += "+" + MediaSmart.Utilities.makeURLSafe(MediaSmart.tags1.seller.toUpperCase());
		else
			keywordVal += MediaSmart.Utilities.makeURLSafe(MediaSmart.tags1.seller.toUpperCase());
	}
	
	if(!polite){
		if( keywordVal != "" )
			keywordVal += "+EXPANDABLE";
		else
			keywordVal += "EXPANDABLE";
	}
	
	//temp fix for secure ads
	if(MediaSmart.adSecure) {
		if( keywordVal != "" )
			keywordVal += "+SECURE";
		else
			keywordVal += "SECURE";
	}	
	
	//temp fix Department sponsorship
	var department;
	if	((document.location + "").indexOf("tradingpost.com.au/Garden-Outdoor-Living/Browse") != -1
		|| (document.location + "").indexOf("tradingpost.com.au/Backyard/Summer-Gardens/LandingPage") != -1
		|| (document.location + "").indexOf("tradingpost.com.au/Backyard/Great-Aussie-BBQ/LandingPage") != -1
		|| (document.location + "").indexOf("tradingpost.com.au/Backyard/Sheds-And-Tool-Storage/LandingPage") != -1
		|| (document.location + "").indexOf("tradingpost.com.au/Backyard/Rainwater-Tanks/LandingPage") != -1
		|| (document.location + "").indexOf("tradingpost.com.au/Backyard/") != -1
		){
			department = "BACKYARD";
	}
	if	((document.location + "").indexOf("tradingpost.com.au/Automotive/Caravans-Mobile-Homes-Trailers/Browse") != -1
		|| (document.location + "").indexOf("tradingpost.com.au/Caravans/Most-Popular-Caravan/LandingPage") != -1
		|| (document.location + "").indexOf("tradingpost.com.au/Caravans/Off-Road-Caravans-And-Rvs/LandingPage") != -1
		|| (document.location + "").indexOf("http://www.tradingpost.com.au/Caravans/Best-Motorhomes/LandingPage") != -1
		|| (document.location + "").indexOf("tradingpost.com.au/Caravans/Jayco-Expandas/LandingPage") != -1
		|| (document.location + "").indexOf("tradingpost.com.au/Caravans/") != -1
		){
			department = "CARAVAN";
	}
	if	((document.location + "").indexOf("tradingpost.com.au/Rural-Machinery/Browse") != -1
		|| (document.location + "").indexOf("tradingpost.com.au/Automotive/Features/WorkingWheels2009/atvs-farmbikes/LandingPage") != -1
		|| (document.location + "").indexOf("tradingpost.com.au/Farm/Work-Trailers-A-Buyers-Guide/LandingPage") != -1
		|| (document.location + "").indexOf("tradingpost.com.au/Automotive/Features/WorkingWheels2009/lifting-loading/LandingPage") != -1
		|| (document.location + "").indexOf("tradingpost.com.au/Farm/Work-Tough-Tyres/LandingPage") != -1
		|| (document.location + "").indexOf("tradingpost.com.au/Farm/") != -1
		){
			department = "FARM";
	}
	if	((document.location + "").indexOf("tradingpost.com.au/Business-Office/Browse") != -1
		|| (document.location + "").indexOf("tradingpost.com.au/Commercial/Desk-Job-Fitness/LandingPage") != -1
		|| (document.location + "").indexOf("tradingpost.com.au/Commercial/Buying-Your-First-Forklift/LandingPage") != -1
		|| (document.location + "").indexOf("tradingpost.com.au/Commercial/How-To-Set-Up-A-Commercial-Kitchen/LandingPage") != -1
		|| (document.location + "").indexOf("tradingpost.com.au/Commercial/Vans/LandingPage") != -1
		|| (document.location + "").indexOf("tradingpost.com.au/Commercial/") != -1
		){
			department = "COMMERCIAL";
	}
	if (typeof(department) != "undefined" && department != ""){
			if( keywordVal != "" )
				keywordVal += "+" + department;
			else
				keywordVal += department;
	}
	
	
	
	// Bankwest fix
	insertBankWest = false;
	containsIsland = false;
	containsLeader = false;
	for (index = 0; index < MediaSmart.ads.length; index++) {
			if( MediaSmart.ads[index].width==300 && MediaSmart.ads[index].height==250 && MediaSmart.ads[index].position==1 )
				containsIsland = true;
			else if( MediaSmart.ads[index].width==728 && MediaSmart.ads[index].height==90 ) {
				if( MediaSmart.ads[index].position==3 || MediaSmart.ads[index].position==4 || MediaSmart.ads[index].position==5 ) 
					containsLeader = true;
			}
	}
	
	if( ( (ad.width==728 && ad.height==90) || (ad.width==300 && ad.height==250) ) && containsLeader == true && containsIsland == true	) {
		if( doBankWestFix && ( (ad.width==300 && ad.height==250 && ad.position==1 ) || (ad.width==728 && ad.height==90 && ad.position>=3 && ad.position <=5 ) ) ){
			if( keywordVal != "" )
				keywordVal += "+115306_BANKWEST";
			else
				keywordVal += "115306_BANKWEST";
		}
	}
	
	
	
	
	return keywordVal;
}


function MSdebugging() {
	if( document.location.indexOf( "MSTOOTHPASTE" ) != -1 )
		return true;
	return false;
}

MediaSmart.bigpondPromotions = new Object();
MediaSmart.bigpondPromotions.newAd = function(json){
	if (MediaSmart.site == "SEN.AUTOTRADER.AUTOMOTIVE") {
		json.site = "SEN.BPPROMO.AUTOTRADER.AUTOMOTIVE";
	} else {
		json.site = "SEN.BPPROMO.TRADINGPOST.CLASSIFIED";
	}
	json.abf = false;
	if ((typeof(json.polite) != "undefined" && json.polite == false) || MediaSmart.Utilities.polite == false) {
		document.write(MediaSmart.Utilities.generateAdCall(json));
	}
	else {
		MediaSmart.ads[MediaSmart.ads.length] = json;
		document.write("<div id='zMSplacement" + MediaSmart.ads.length + "' style='width:" + json.width + "px; height:" + json.height + "px;'></div>");
	}
}


MediaSmart.logValues = function( adCall ) {
	if( Math.floor(Math.random()*1000) != 0 )
		return;
	
	var sdmLogTags = adCall.match( /src='.*'/ ) + "";
	startTagsIndex = sdmLogTags.indexOf( "acc_random=" );
	if( startTagsIndex == -1 )
		return;
	sdmLogTags = sdmLogTags.substring( startTagsIndex, sdmLogTags.length - 1 );
	sdmLogTags = sdmLogTags.replace( /\//g, "&" );
	sdmLogTags = sdmLogTags.toUpperCase();
	
	// Pass tags to tag logger
	var tagLogger = new Image();
	tagLogger.src = "http://mediasmart-apps.com/taglogger/logtags.php?" + sdmLogTags;
}


MediaSmart.Utilities.initialise();

















