// Constructor -- pass a REST request URL to the constructor
//
function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
//
JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");
    
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
// removeScriptTag method
// 
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}

// addScriptTag method
//
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}


var ipAdServer = "http://advt.manoramaonline.com/cgi-bin/mmads.dll/portal/AdGenerator.jsp";
var callingPage = window.location.href;
var div2Display = "";
function advertiseScript(layerName){
	var r = 0 ;
	try { if(rotate) r=1; }catch(err){r=0;}
	var thisDate = new Date();
	var obj=new JSONscriptRequest( ipAdServer+'?weightage='+r+'&d='+thisDate.getTime()+'&divName='+layerName+'&x=y&adCat='+adCat+'&requestPage='+escape(callingPage)+'&callback=mmads'  );     
	//alert("Calling Page = "+obj.fullUrl );
	//alert(layerName);
	obj.buildScriptTag(); 
	obj.addScriptTag(); 
}

function mmads(ob){
if(ob==null){
   //do something
}else{
      //alert("ipads="+ob.ipads);
		displayAd( ob.ipads );
   }
}

function displayAd( adData){
	//document.write("<div id=\""+divName+"\" name=\""+divName+"\" >-----------------------</div>");
	//document.write("--//////////////////////////////////------");
	var fullAdData = adData;
	var adOnly = "";
	if(fullAdData!=null && fullAdData.length > 0 ){
		var divObjectIdx = fullAdData.indexOf("DIVOBJECT=");
		if(divObjectIdx > -1 ){
			adOnly  = fullAdData.substring(0,divObjectIdx);
			div2Display= fullAdData.substring(divObjectIdx+10);
			//alert("div2Display="+div2Display);
			divObjectIdx = div2Display.indexOf("&testIp=");
			if(divObjectIdx > -1 ){
				div2Display= div2Display.substring(0,divObjectIdx);
			}
			//alert(div2Display+":"+adOnly);
		}
	}
	var displayIn = getElement(div2Display);
	//if(adOnly.indexOf("<script ") > -1 ) // 18Feb08
	if(adOnly.indexOf("<SCRIPT language=JavaScript type=text/javascript>") > -1 )
		launchJavascript(adOnly);
	else 
    	if( displayIn ) displayIn.innerHTML = adOnly ;
	//alert("After wriring "+divName)
	//alert(adData);

}
/*
	var divName = "divIp";
	try {
		var index=adCat.lastIndexOf("/");
		var appendStr=adCat.substring(index+1);
		divName+="_"+appendStr;

	}catch(error) {
		divName="divIp";
	}
*/
/*
This Function is used only for Script Ads 
*/
function launchJavascript(responseText) {
// RegExp from prototype.sonio.net
var ScriptFragment = '(?:<script.*?>)((\n|.)*?)(?:</script>)';
var match = new RegExp(ScriptFragment, 'img');
var scripts = responseText.match(match);
if(scripts) {
	var js = '';
	for(var s = 0; s < scripts.length; s++) {
		var match = new RegExp(ScriptFragment, 'im');
		js += scripts[s].match(match)[1];
	}
	eval(js);
   }
}

function getElement(id){ 
	if(document.getElementById){ 
		// test the most common method first. Most browsers won't get past this test       
		return document.getElementById(id);    
	 } else if(document.all){ 
		 // test older versions of IE       
		 return document.all[id];    
	 }else if(document.layers){ 
		// test older versions of Netscape       
		 return document.layers[id];   
	 }else{ 
		 // not sure what to do...return null       
		return null;    
	}
 }

//call the function
//advertiseScript();
