onload=function() 
      { 
//Configurable Variables. Changes here will reflect in all places for a tenant. Not using separate config file to reduce file i/o overhead   
     	 //var tbsNetworkId="nw=42448"; //test
		 var tbsNetworkId="nw=48804"; //prod
     	 var amp="&";
     	 var semi=";";
     	 var ptgt="ptgt=s";
     	 var slid="slid=ads_frame_js";
     	 var width="w=300";
     	 var height="h=50";
     	 var envp="envp=g_js";
     	 var randomnumber="pvrn="+Math.floor(Math.random()*100000) + 10000;
     	 var resp="resp=ad";
    	 var customerId = "csid=" + document.getElementById("hiddenparam").value; 
var proxyurl="/primary/tbs_freewheel_ad_proxy?";
    	 var url=proxyurl+tbsNetworkId+amp+customerId+amp+randomnumber+amp+resp+semi+semi+ptgt+amp+envp+amp+slid+amp+width+amp+height;
//Ajax Call to Freewheel Ads server to get the script in response
		 var xmlhttp;
		 var resp;

		 if (window.XMLHttpRequest)
	 		 {// code for IE7+, Firefox, Chrome, Opera, Safari
			 	 xmlhttp=new XMLHttpRequest();
			 }
		 else
		 	 {// code for IE6, IE5
			 	 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			 }

	 xmlhttp.onreadystatechange=function(){
		 if (xmlhttp.readyState==4 && xmlhttp.status==200)
		 {
		 		 resp=xmlhttp.responseText;
				 var myResp = resp ;
				 var mydiv = document.getElementById("freewheel_ads");
 //Gotcha: Doesn't look like the best way to do this isn't it? But if you put script element dynamically inside the html DOM it doesn't get evaluated as script elements are evaluated at the time of DOM parsing!!!
				 myResp = myResp.substring(myResp.indexOf("document.write(\""), myResp.lastIndexOf("</div></body></html>"));
				 
				 myResp = myResp.substring("document.write(\".".length-1);
				 myResp = myResp.substring(0,myResp.lastIndexOf("\");"));
				 myResp = myResp.replace(/\\n/g,"");
				 myResp = myResp.replace(/\\t/g,"");
				 myResp = myResp.replace(/\\'/g,"\'");
				 myResp = myResp.replace(/\\&/g,"\&");
				 myResp = myResp.replace(/&lt;/g,"<");
				 myResp = myResp.replace(/scr"[\s*]\+[\s*]"ipt/g,"script");
				
				 var myScriptResp = myResp.match(/<script\s.*<\/script>/g);
				 myResp = myResp.replace(/<script\s.*<\/script>/g,"");
				 myResp = myResp.replace(/\\"/g,"\"");

				 var myScript = myScriptResp[0];
				 myScript = myScript.substring("<script type=\\\"text/javascript\\\">".length);
				 myScript = myScript.substring(0,myScript.lastIndexOf("</script>"));
				 myScript = myScript.replace(/\\\"/g,"\"");
				 myScript = myScript.replace(/}}}/g,"}}};");

				 eval(myScript);
				 mydiv.innerHTML = myResp;
		}
	}
	xmlhttp.open("GET",url,false);
	xmlhttp.send(null);
}

