//Manorama Online @ 2008
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);
}
 
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}


JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}


var ipAdServer = "http://advt.manoramaonline.com/cgi-bin/mmads.dll/portal/FCv2.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();
    getme(getCookie("mm_oids_"+divName)) 
	var obj="";
	my_geo_loc=getCookie("mm_geo");
    if(my_geo_loc==""){
	 obj=new JSONscriptRequest( ipAdServer+'?weightage='+r+'&d='+thisDate.getTime()+'&divName='+layerName+'&x=y&adCat='+adCat+'&requestPage='+escape(callingPage)+'&callback=mmads&adid='+adv_id  );     
	}
	else
	{   
		 obj=new JSONscriptRequest( ipAdServer+'?weightage='+r+'&d='+thisDate.getTime()+'&divName='+layerName+'&x=y&adCat='+adCat+'&requestPage='+escape(callingPage)+'&callback=mmads&adid='+adv_id+'&mm_geo='+my_geo_loc);     
	}
	obj.buildScriptTag(); 
	obj.addScriptTag(); 
}

function mmads(ob){
if(ob==null){
  
}else{
     if(getCookie("mm_geo")=="")
	{
	setCookie("mm_geo",ob.zcode,1);	 
	}
	   setCookie("mm_oids_"+divName,  ob.oids,1);
	  	displayAd( ob.ipads );
		
		
   }
}
 function setCookie(c_name,value,expiredays)
{var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}

function displayAd( adData){
	
	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 language=JavaScript type=text/javascript>") > -1 || adOnly.indexOf("MMPopupScriptAd") > -1 )
		launchJavascript(adOnly);
	else 
    	if( displayIn ) displayIn.innerHTML = adOnly ;
	

}
  function getme(oids)
  {
	 if(oids=="")
	  {
	  
	  adv_id=1;
	  return
	  }
	  
  spt_od =new Array();
  spt_od=oids.split(',');
  for(mmm_i=0;mmm_i<spt_od.length;mmm_i++){
  get_val=setAllCookies(spt_od[mmm_i])
  if(get_val==0)
  {

  continue;
  }
  else
  {
  adv_id=get_val;
  break;
  }
  }
  }
  function setAllCookies(mo)
  { mm_id_sp=new Array();
    mm_id_sp=mo.split('#')

    var mymmDate=new Date()
	mmc_name=divName+"_mm_"+mm_id_sp[0]+mymmDate.getDate()+"_"+parseInt(mymmDate.getMonth()+1) ;
	mmval=getCookie(mmc_name);
	if(mmval=="")
	{
	setCookie(mmc_name,1,1);
	
	return mm_id_sp[0];
	}
	else
	{
	numb=parseInt(getCookie(mmc_name))+1;
	if(numb>parseInt(mm_id_sp[1]))
	{
	return 0
	}
	else
	{
	setCookie(mmc_name,numb,1);
	return mm_id_sp[0]; 
	}
  }}
function launchJavascript(responseText) {

var ScriptFragment = '(?:<script.*?>)((\n|.)*?)(?:</script>)';
if(responseText.indexOf("MMPopupScriptAd") > -1){
	responseText = responseText.substring(15);
	alert(responseText);
	try{ eval(responseText);}catch(err){ alert("err"+err.description );}
}else{
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){ 
		
		return document.getElementById(id);    
	 } else if(document.all){ 
		 
		 return document.all[id];    
	 }else if(document.layers){ 
		
		 return document.layers[id];   
	 }else{ 
		
		return null;    
	}
 }

