
function show_ads(responsetext, context) {

	var tmp = responsetext;

	var index1 = tmp.indexOf("src=");
	var index2 = tmp.indexOf("&width=");
	var index3 = tmp.indexOf("&height=");
	var src    = tmp.substring(index1 + 4, index2);
	var width  = tmp.substring(index2 + 7, index3);
	var height = tmp.substring(index3 + 8);
	
	if( index1 != -1 && index2 != -1 && index3 != -1 )
	{
	    if( context && context.frameid )
	    {
		var frameid = context.frameid;
		var frm = document.getElementById(frameid);
		frm.src = src;
		frm.style.width  = width;
		frm.style.height = height;
		
		if( context.tableid )
		{
			var tableid = context.tableid;
			var tbl = document.getElementById(tableid);
			tbl.style.display = "inline";
			tbl.style.padding = '2px';
			if( tableid.charAt(0) == 'C' && tableid.charAt(1) == 'B' )
			{
				var uptableid = "CENTERBODY" + tableid.substring(2);
				var uptbl = document.getElementById(uptableid);
				uptbl.style.paddingLeft = '10px';
				uptbl.style.paddingTop = '100px';
			}
		}
	    }
	}
}



function setAdElementCss(obj, attribute, value)
{
	var href = document.location.href;
	if( href.charAt(0) == 'h' && href.charAt(1) == 't' && href.charAt(2) == 't' && href.charAt(3) == 'p' )
	{
		var url1 = "/servlet/Page?ElementId=" + obj + "&" + attribute + "=" + value;
		adajax.doCommand(url1);

		var url2 = "/servlet/GetAds?ElementId=" + obj + "&" + attribute + "=" + value;
		adajax.doCommand(url2);
  	}
	if( document.getElementById(obj) )
		document.getElementById(obj).style.setAttribute(attribute, value);
}

function setAdSessionElementCss(prefix, attribute, value)
{
	var elementid = prefix + SESSIONID;
	var href = document.location.href;
	if( href.charAt(0) == 'h' && href.charAt(1) == 't' && href.charAt(2) == 't' && href.charAt(3) == 'p' )
	{
		var url1 = "/servlet/Page?ElementId=" + elementid + "&" + attribute + "=" + value;
		adajax.doCommand(url1);

		var url2 = "/servlet/GetAds?ElementId=" + elementid + "&" + attribute + "=" + value;
		adajax.doCommand(url2);
  	}
	if( document.getElementById(elementid) )
		document.getElementById(elementid).style.setAttribute(attribute, value);
}



var adajax = {

	doCommand: function(url) {			//this function just send out a request, does not expect any response and do not intend to handle any response
		this._doCommand(url);
	},

	_doCommand: function(url) {
		var req;
		if( window.XMLHttpRequest ) 
		{
			req = new XMLHttpRequest();
		} else 
		if( window.ActiveXObject ) 
		{
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		req.onreadystatechange = function() {;}	//do nothing
		req.open("GET", url, true);
		req.send(null);
		return;
	},



	doGet: function(url, XMLOrText, callbackfunction, passthrough) {
		this._doGet(url, XMLOrText, callbackfunction, passthrough);
	},

	_doGet: function(url, XMLOrText, callbackfunction, passthrough) {
		var req;
		if( window.XMLHttpRequest ) 
		{
			req = new XMLHttpRequest();
		} else 
		if( window.ActiveXObject ) 
		{
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}


		var ctx = {req: req,
			   url: url,
			   XMLOrText: XMLOrText,
			   callbackfunction: callbackfunction,
			   passthrough: passthrough};

		req.onreadystatechange = function() {
			adajax._processResponse(ctx);
		}

		req.open("GET", url, true);
		req.send(null);
		return;
	},

	_processResponse: function(ctx) {
		var req = ctx.req;
		if( req.readyState == 4 )
		{
			if( req.status == 200 )
			{
				var callbackfunction = ctx.callbackfunction;
				if( !callbackfunction )							//the callbackfunction might not be available yet, suc as loading from an external .js file
				{
					setTimeout(function() {adajax._processResponse(ctx);}, 100);	//the script will keep looping until a callbackfunction is available, it might run into a infinite looping if can not get the callbackfunction for whatever the reasons
				}

				if( callbackfunction )
				{
					if( ctx.XMLOrText )
					{
						callbackfunction(req.responseXML,  ctx.passthrough);
					} else
					{
						callbackfunction(req.responseText, ctx.passthrough);
					}
				}
			} else
			{
				//alert("There was a problem retrieving the XML data: " + req.statusText);
			}
		}
		return;
	}
};




