﻿/* AServing Common Library  */

var AServing = {}; //Create Namespace

// Function
// Include required library
AServing.include = function (file) {
  	var script  = document.createElement('script');
  	script.src  = file;
  	script.type = 'text/javascript';
  	script.defer = true;
  	document.getElementsByTagName('head').item(0).appendChild(script);
}
AServing.include('http://rs.aserving.com/lib/oHash.js');
AServing.include('http://rs.aserving.com/lib/popuplayer.js');
AServing.include('http://rs.aserving.com/lib/swfobject.js');

//  Set Adlink CSS
  	AServing.tStyleStr  = '.adlink {  border-bottom: 3px #0d7517 double; height: 0px; line-height: 16px; color: #0d7517; text-decoration: none;}\n';
  	AServing.tStyleStr += 'a.adlink	{  border-bottom: 3px #0d7517 double; height: 0px; color: #0d7517; text-decoration: none;}\n';
  	AServing.tStyleStr += 'a.adlink:visited	{  border-bottom: 3px #0d7517 double; height: 0px; color: #0d7517; text-decoration: none;}\n';
  	AServing.tStyleStr += 'a.adlink:hover		{  border-bottom: 3px #0d7517 double; height: 0px; color: #0d7517; text-decoration: none;}\n';
  	AServing.tStyleStr += 'a.adlink:active		{  border-bottom: 3px #0d7517 double; height: 0px; color: #0d7517; text-decoration: none;}\n';
		document.write('<style type="text/css">'+AServing.tStyleStr+'</style>');

// Function
// Get Timestamp
AServing.getTimestamp = function () {
	aDate = new Date();
	return aDate.getTime();
}


// Function
// URL Encode
AServing.URLEncode = function(clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}


// Function
// Get Browser Position
AServing.getWindowPos = function () {
	// Window dimensions:
	var theWidth, theHeight;
	if (window.innerWidth)
		theWidth=window.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth)
		theWidth=document.documentElement.clientWidth;
	else if (document.body)
		theWidth=document.body.clientWidth;

	if (window.innerHeight)
		theHeight=window.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight)
		theHeight=document.documentElement.clientHeight;
	else if (document.body)
		theHeight=document.body.clientHeight;

	// Window origin
	var originX, originY;
	if (window.innerHeight) {
		originX=screenX+(window.outerWidth-window.innerWidth);
		originY=screenY+(window.outerHeight-window.innerHeight);
		//originY=screenY+(window.outerHeight-window.innerHeight-25);
	}	else {
		originX=screenLeft;
		originY=screenTop;
	}
	return [originX, originY];
}

// Function
// Get Popup Layer Size
AServing.getPopupSize = function (divID,width,height,pointerX,pointerY,inScrollLeft,inScrollTop) {
	var leftPosition;
	var leftShadowPosition;
	var topPosition;
	var topShadowPosition;

	var popupWidth = width;
	var popupHeight = height;		

	var position = findPos(divID);
	var windowPos = AServing.getWindowPos();		

	leftPosition = parseInt(inScrollLeft + pointerX - windowPos[0]);
	leftShadowPosition = parseInt(inScrollLeft + pointerX - windowPos[0] + 4);
	topPosition = parseInt(inScrollTop + pointerY - windowPos[1]);
	topShadowPosition = parseInt(inScrollTop + pointerY - windowPos[1] + 4);

	// "Flip" the layer if it exceed the right edge of screen 
	if (leftPosition > parseInt(document.documentElement.clientWidth) - parseInt(popupWidth) -10) {
		leftPosition = leftPosition - parseInt(popupWidth);
		leftShadowPosition = leftShadowPosition - parseInt(popupWidth);
	}

	// Reset the layer if it exceed the left edge of screen 
	if (leftPosition < parseInt(inScrollLeft)) {
		leftPosition = parseInt(inScrollLeft);
		leftShadowPosition = parseInt(inScrollLeft + 4);
	}

	// "Flip" the layer if it exceed the bottom edge of screen 
	if (topPosition > parseInt(document.documentElement.clientHeight)  - parseInt(popupHeight) -10) {
		topPosition = topPosition - parseInt(popupHeight);
		topShadowPosition = topShadowPosition - parseInt(popupHeight);
	}

	// Reset the layer if it exceed the top edge of screen 
	if (topPosition < parseInt(inScrollTop)) {
		topPosition = parseInt(inScrollTop);
		topShadowPosition = parseInt(inScrollTop + 4);
	}

	leftPosition += 'px';
	leftShadowPosition += 'px';
	topPosition += 'px';
	topShadowPosition += 'px';

	//outAlert = "W:" + width + " H:" + height;
	//outAlert += "\nWordX: " + position[0] + " WordY:" + position[1];
	//outAlert += "\nMouseX: " + pointerX + " MouseY:" + pointerY;	
	//outAlert += "\nScrollX:" + inScrollLeft + " ScrollY:" + inScrollTop;
	//outAlert += "\nWindowLeft:" + window.screenLeft + " WindowTop:" + window.screenTop;
	//outAlert += "\nWindowX:" + window.screenX + " WindowY:" + window.screenY;
	//outAlert += "\nWindowXX:" + windowPos[0] + " WindowXY:" + windowPos[1];
	//outAlert += "\nWindowWidth:" + document.documentElement.clientWidth + " WindowHeight:" + document.documentElement.clientHeight;
	//outAlert += "\nFinalX:" + leftPosition + " FinalY:" + topPosition;
	//alert (outAlert);
	
	return [topPosition,leftPosition,topShadowPosition,leftShadowPosition];
}


// Function
// Create Popup Layer
AServing.createPopup = function (popupWidth,popupHeight,divID,topPosition,leftPosition,topShadowPosition,leftShadowPosition,titleSubject,inAdID) {
	// Shadow
	var divShadow = document.createElement('div');
	divShadow.id = 'LayerCover'+inAdID;
	divShadow.style.position = 'absolute';
	divShadow.style.display = 'block';
	divShadow.style.zIndex = '10003';				
	divShadow.style.left = leftShadowPosition;
	divShadow.style.top = topShadowPosition;
	divShadow.style.width = popupWidth + 'px';
//	divShadow.style.height = parseInt(popupHeight) + 20 + 'px';
	divShadow.style.backgroundColor = '#999999';
	divShadow.style.filter = 'alpha(opacity=75)';
	divShadow.style.mozOpacity = '.75';
	divShadow.style.opacity = '.75';
	

	// Popup
	var divPopup = document.createElement('div');
	divPopup.id = 'LayerPopupID'+inAdID;
	divPopup.style.fontSize = '8pt';
	divPopup.style.border = '1px solid #000000';
	divPopup.style.backgroundColor = '#ffffe1';
	divPopup.style.color = '#000000';		
	divPopup.style.position = 'absolute';
	divPopup.style.display = 'block';
	divPopup.style.zIndex = '10004';				
	divPopup.style.left = leftPosition;
	divPopup.style.top = topPosition;	
	divPopup.style.width = parseInt(popupWidth) + 'px';
//	divPopup.style.height = parseInt(popupHeight) + 20 + 'px';	

	// Popup Title Bar
	var divTitle = document.createElement('div');
	divTitle.style.width = parseInt(popupWidth) - 4 + 'px';
	divTitle.style.height = '16px';
	divTitle.style.padding = '2px';
	divTitle.style.backgroundColor = '#ffffff';
	divTitle.style.fontWeight = '600';
	divTitle.innerHTML = titleSubject;

	document.getElementById(divID).appendChild(divShadow);
	document.getElementById(divID).appendChild(divPopup);
	divPopup.appendChild(divTitle);

	return divPopup;
}


// Function
// Create Layer AD content
AServing.createLayerAD = function (divID1,divID2,type,inKeywordID,inTagID,inAdContentID,width,height,evt) {	
// divID1 - in-text ads position
//
// divID2 - popup position
//
// type - 
// 0 : html
// 1 : text only
// 2 : image only
// 3 : flash only
// 12 : text + image
// 13 : text + flash
// 
// inAdContentID[x] -
// 0 : text
// 1 : url link
// 2 : image source
// 3 : image width
// 4 : image height
// 5 : flash source
// 6 : flash width ( no 'px' )
// 7 : flash height ( no 'px' )	
//
// width : popup width
//
// height : popup height

    //  Get the pointer position
    if (window.event) {
        var pointerX = event.screenX;
        var pointerY = event.screenY;                
    } else {
        var pointerX = evt.screenX;
        var pointerY = evt.screenY;       
    }
    var scrollLeft = document.documentElement.scrollLeft;
    var scrollTop = document.documentElement.scrollTop; 
    
    //  Get the popup position	
		var adType = type;
		var	inAdContent = eval(inTagID + 'AdLib.getItem(' + inAdContentID + ')');	
		var divContent = document.createElement('div');
	
		//  Build the popup and shadow
		AServing.createPopup(width,height,divID2,0,0,0,0,inAdContent[4],inAdContent[0]);		//content[4,0] = subject,ad id

		var divPopup = document.getElementById('LayerPopupID'+inAdContent[0]);			
		var divShadow = document.getElementById('LayerCover'+inAdContent[0]);	

		// 	HTML
		if (adType == 0) {		
			AServing.showLayerAdHTML(divContent,divPopup,inAdContent[5],inAdContent[6],inAdContent[0],inKeywordID);	//content[5,6,0] = text	/ url / ad id
    //  Text
		}	else if (adType == 1) {
			AServing.showLayerAdText(divContent,divPopup,inAdContent[5]);	//content[5] = text
 		//  Image
		}	else if (adType == 2)	{
			AServing.showLayerAdImage(divContent,divPopup,inAdContent[6],inAdContent[7]);	//content[6-7] = url / image source
		//  Flash
		}	else if (adType == 3)	{
			AServing.showLayerAdFlash(divContent,divPopup,inAdContent[10],inAdContent[11],inAdContent[12]);	//content[10-12] = flash source / width / height
		//  Text + Image
		}	else if (adType == 12) {
			AServing.showLayerAdTextImage(divContent,divPopup,inAdContent[5],inAdContent[6],inAdContent[7]);
		//  Text + Flash
		}	else if (adType == 13) {
			AServing.showLayerAdTextFlash(divContent,divPopup,inAdContent[5],inAdContent[10],inAdContent[11],inAdContent[12]);	
		}		
	
		//  Calculate and set the popup and shadow height
		var pos = findPos(divContent);		
		divPopup.style.height = (parseInt(pos[3]) + 20) + 'px';
		divShadow.style.height = (parseInt(pos[3]) + 20) + 'px';
	
		var position = AServing.getPopupSize(divID1,pos[2],pos[3],pointerX,pointerY,scrollLeft,scrollTop);
		divPopup.style.left = position[1];
		divPopup.style.top = position[0];
		divShadow.style.left = position[3];
		divShadow.style.top = position[2];
	
		for (var tmp in SelectArray)
        document.getElementById(SelectArray[tmp]).style.visibility = 'hidden';
}


// Function
// Destroy the popup content
AServing.clearPopup = function (divID) {
	document.getElementById(divID).innerHTML = '';		
	for (var tmp in SelectArray)
        document.getElementById(SelectArray[tmp]).style.visibility = 'visible';
}


// Function
// Show HTML on Layer Ad
AServing.showLayerAdHTML = function (divContent,divPopup,inHTML,inLink,inAdID,inKeywordID) {
	divContent.style.padding = '2px';
	var tDiv = document.createElement('div');
	inHTML = inHTML.replace(/\[\[Link\]\]/g,'http://ts.aserving.com/c.aspx?t='+inAdID+'&k='+inKeywordID+'&u='+AServing.URLEncode(inLink));
	inHTML = inHTML + '<img src="http://ts.aserving.com/i.aspx?t='+inAdID+'&k='+inKeywordID+'" style="display: none;"';
	tDiv.innerHTML = inHTML;
	
	divContent.appendChild(tDiv);	
	divPopup.appendChild(divContent);	
}


// Function
// Show Text on Layer Ad
AServing.showLayerAdText = function (divContent,divPopup,text) {
	divContent.style.padding = '2px';
	var label = document.createElement('label');
	label.innerHTML = text;
	
	divContent.appendChild(label);	
	divPopup.appendChild(divContent);	
}


// Function
// Show Image on Layer Ad
AServing.showLayerAdImage = function (divContent,divPopup,url,imageSrc) {
	var href = document.createElement('a');
	href.target = '_blank';
	href.href = url;

	var img = document.createElement('img');
	img.src = imageSrc;
	img.style.border = '0px';	
	
	href.appendChild(img);
	divContent.appendChild(href);
	divPopup.appendChild(divContent);
}


// Function
// Show Flash on Layer Ad
AServing.showLayerAdFlash = function (divContent,divPopup,flashSrc,width,height) {	
	divContent.id = 'LayerPopupIDFlash';
	divPopup.appendChild(divContent);
	var so = new SWFObject(flashSrc, "mymovie", width, height, "6.0.65", "#336699");
	so.write("LayerPopupIDFlash");
}


// Function
// Show Text + Image on Layer Ad
AServing.showLayerAdTextImage = function (divContent,divPopup,text,url,imageSrc) {
	var table = document.createElement('table');
	table.width = '100%';
	table.cellPadding = '0';
	table.cellSpacing = '0';
	table.border = '0';

	var tbody = document.createElement('tbody');

	var tr = document.createElement('tr');
	tr.style.verticalAlign = 'top';

	var td1 = document.createElement('td');
	td1.width = '50%';
	td1.style.padding = '2px';
	td1.innerHTML = text;

	var td2 = document.createElement('td');
	td2.width = '50%';
	var href = document.createElement('a');
	href.target = '_blank';
	href.href = url;
	var img = document.createElement('img');
	img.src = imageSrc;
	img.style.border = '0px';				
	href.appendChild(img);
	td2.appendChild(href);

	tr.appendChild(td1);
	tr.appendChild(td2);
	tbody.appendChild(tr);
	table.appendChild(tbody);
	divContent.appendChild(table);
	divPopup.appendChild(divContent);
}


// Function
// Show Text + Image on Layer Ad
AServing.showLayerAdTextFlash = function (divContent,divPopup,text,flashSrc,width,height) {
	var table = document.createElement('table');
	table.width = '100%';
	table.cellPadding = '0';
	table.cellSpacing = '0';
	table.border = '0';

	var tbody = document.createElement('tbody');

	var tr = document.createElement('tr');
	tr.style.verticalAlign = 'top';

	var td1 = document.createElement('td');
	td1.width = '50%';
	td1.style.padding = '2px';
	td1.innerHTML = text;

	var td2 = document.createElement('td');
	td2.width = '50%';
	var span = document.createElement('span');
	span.id = 'spanFlash';
	td2.appendChild(span);						

	tr.appendChild(td1);
	tr.appendChild(td2);
	tbody.appendChild(tr);
	table.appendChild(tbody);
	divContent.appendChild(table);
	divPopup.appendChild(divContent);

	var so = new SWFObject(flashSrc, "mymovie", width, height, "6.0.65", "#336699");
	so.write("spanFlash");
}


AServing.getFlashObj = function(objID) {
	if (navigator.appName.indexOf ("Microsoft") !=-1) {
		return window[objID]
	} else {
		return document[objID]
	}
}

// Function
// Render Tear Ad
AServing.renderTearAd = function (inTag,inFile,inPos,inFullSize,inClosedSize) {

	document.write('<div id="'+inTag+'div" style="position:absolute;visibility:visible;left:'+inPos[0]+'px;top:'+inPos[1]+'px;z-index:10001;width:'+inClosedSize[0]+'px;height:'+inClosedSize[1]+'px;overflow:hidden;">');
	document.write('<div style="position:absolute;visibility:visible;left:0;top:0;z-index:10001;">');
	document.write('	<object codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width="'+inFullSize[0]+'" height="'+inFullSize[1]+'" id="'+inTag+'Flash">');
	document.write('	<param name="movie" value="'+inFile+'?tagid='+inTag+'&'+AServing.getTimestamp()+'">');
	document.write('	<param name="wmode" value="transparent">');
	document.write('	<param name="allowScriptAccess" value="always">');
	document.write('	<param name="quality" value="high">');
	document.write('	<embed src="'+inFile+'?tagid='+inTag+'&'+AServing.getTimestamp()+'" ');
	document.write('	width="'+inFullSize[0]+'" height="'+inFullSize[1]+'" quality="high" allowScriptAccess="always"');
	document.write('	type="application/x-shockwave-flash" ');
	document.write('	PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" name="'+inTag+'Flash" swLiveConnect="true" wmode="transparent"></embed>');
	document.write('	</object>');
	document.write('</div>');
	document.write('</div>');

/*	document.write('<script LANGUAGE="VBScript">');
	document.write('    Sub '+inTag+'Flash_FSCommand(ByVal command, ByVal args)');
	document.write('        call '+inTag+'Flash_DoFSCommand(command, args)');
	document.write('    end sub');
	document.write('</script>	');	


	document.write('<script>																																																\n');
	document.write('	function '+inTag+'Flash_DoFSCommand(command, args) {																									\n');
	//document.write('	  alert (command+\' \'+args);');
	document.write('    // Enlarge the flash to see the content																															\n');
	document.write('    if (args == "open") {																																								\n');
	document.write('        AServing.getFlashObj(\''+inTag+'Flash\').SetVariable(\'strVideoURL\',\''+eval(inTag+'Video')+'\');																\n');
	document.write('        parent.document.getElementById(\''+inTag+'div\').style.width = '+inTag+'FullSize[0] + \'px\';		\n');
	document.write('        parent.document.getElementById(\''+inTag+'div\').style.height = '+inTag+'FullSize[1] + \'px\';	\n');
	document.write('        for (var tmp in SelectArray)																																		\n');
	document.write('        	document.getElementById(SelectArray[tmp]).style.visibility = \'hidden\';											\n');  
	document.write('    // Reduce size of the flash																																					\n');
	document.write('    } else if (args == "close") {																																				\n');
	document.write('        parent.document.getElementById(\''+inTag+'div\').style.width = '+inTag+'ClosedSize[0] + \'px\';	\n');
	document.write('        parent.document.getElementById(\''+inTag+'div\').style.height = '+inTag+'ClosedSize[1] + \'px\';\n');
	document.write('        for (var tmp in SelectArray)																																		\n');
	document.write('        	document.getElementById(SelectArray[tmp]).style.visibility = \'visible\';											\n');
	document.write('    // Click the content when Enlarge the flash																													\n');
	document.write('    } else if (args == "click") {																																				\n');
	document.write('    		window.open('+inTag+'Link,\'_blank\');																													\n');
	document.write('    }																																																		\n');
	document.write('  }																																																			\n');
	document.write('</script>																																																\n');

	//AServing.getFlashObj(inTag+'Flash').SetVariable('strVideoURL',eval(inTag+'Video'));
*/
	document.write('<script>');
	document.write(' function '+inTag+'_expandLayer() {\n');
	document.write('        parent.document.getElementById(\''+inTag+'div\').style.width = '+inTag+'FullSize[0] + \'px\';		\n');
	document.write('        parent.document.getElementById(\''+inTag+'div\').style.height = '+inTag+'FullSize[1] + \'px\';	\n');
	document.write('        for (var tmp in SelectArray)																																		\n');
	document.write('        	document.getElementById(SelectArray[tmp]).style.visibility = \'hidden\';											\n');  
	document.write(' 				return \''+eval(inTag+'Video')+'\';\n');
	document.write(' }\n');
	document.write(' function '+inTag+'_closeLayer() {\n');
	document.write('        parent.document.getElementById(\''+inTag+'div\').style.width = '+inTag+'ClosedSize[0] + \'px\';	\n');
	document.write('        parent.document.getElementById(\''+inTag+'div\').style.height = '+inTag+'ClosedSize[1] + \'px\';\n');
	document.write('        for (var tmp in SelectArray)																																		\n');
	document.write('        	document.getElementById(SelectArray[tmp]).style.visibility = \'visible\';											\n');
	document.write(' }\n');
	document.write(' function '+inTag+'_click() {');
	document.write('    		window.open('+inTag+'Link,\'_blank\');																													\n');
	document.write(' }\n');
	document.write('</script>');

}	


// Function
// Render Banner
AServing.renderBanner = function (inLayer, inList, inLib, inAdPerLine) {
	    for (var tmp in inList) {
	    	if (inList[tmp] != null) {
	        var tmpHref = document.createElement('a')
	        tmpHref.href = 'http://ts.aserving.com/t.aspx?a=c&t=' + inLib[inList[tmp]][0] + '&u=' + inLib[inList[tmp]][2] + '&b=' + inLib[inList[tmp]][5] + '&c=' + inLib[inList[tmp]][6];		// Tag ID / Link / Banner ID / Campaign ID
	        //tmpHref.href = 'http://192.168.38.231:9004/t.aspx?a=c&t=' + inLib[inList[tmp]][0] + '&u=' + inLib[inList[tmp]][2] + '&b=' + inLib[inList[tmp]][5] + '&c=' + inLib[inList[tmp]][6];		// Tag ID / Link / Banner ID / Campaign ID
	        //tmpHref.href = inLib[inList[tmp]][2];		// Banner ID / Link
	        tmpHref.target = "_blank";
	    
	        var tmpImg = document.createElement('img');
	        //tmpImg.className = "thumbnail";
	        tmpImg.id = "ABnrImg" + inLib[inList[tmp]][0];				// Banner ID
	        tmpImg.style.margin = "0px 0px 0px 0px";
	        tmpImg.setAttribute("width", inLib[inList[tmp]][3]);	// Width
	        tmpImg.setAttribute("height", inLib[inList[tmp]][4]);	// Height
	        tmpImg.setAttribute("border", "0");
	        tmpImg.setAttribute("src", inLib[inList[tmp]][1]);		// Image Source
	        tmpImg.setAttribute("imgsrc", 'http://ts.aserving.com/t.aspx?a=i&t=' + inLib[inList[tmp]][0] + '&u=' + inLib[inList[tmp]][1] + '&b=' + inLib[inList[tmp]][5] + '&c=' + inLib[inList[tmp]][6] );	// Tag ID / Image Source / Banner ID / Campaign ID
	        //tmpImg.setAttribute("imgsrc", 'http://192.168.38.231:9004/t.aspx?a=i&t=' + inLib[inList[tmp]][0] + '&u=' + inLib[inList[tmp]][1] + '&b=' + inLib[inList[tmp]][5] + '&c=' + inLib[inList[tmp]][6] );	// Tag ID / Image Source / Banner ID / Campaign ID
	        //tmpImg.setAttribute("imgsrc", inLib[inList[tmp]][1]);	// Banner ID / Image Source
	        
	        tmpHref.appendChild(tmpImg);
	        inLayer.appendChild(tmpHref);
	        
	        var tChildCount = inLayer.childNodes.length;
	        var tLineCount = 1;
	        var tI = (tLineCount * inAdPerLine) + (tLineCount - 1);
	        while (tChildCount >= tI) {
	        	if (tChildCount == tI) {
	        		var tmpBr = document.createElement('br');
	        		inLayer.appendChild(tmpBr);
	        	} 
	        	tLineCount++;
	        	tI = (tLineCount * inAdPerLine) + (tLineCount - 1);
	      	}	
	        //alert (tChildCount + ' ' + (tChildCount - (Math.ceil(tChildCount / inAdPerLine))) % inAdPerLine);
	      }  
	    }	
}

// Function
// Render Keyword Search Banner
AServing.renderKeywordSearchBanner = function (inLayer, inList, inLib, inAdPerLine, inKeywordID) {
	    for (var tmp in inList) {
	    	if (inList[tmp] != null) {
	        var tmpHref = document.createElement('a')
	        tmpHref.href = 'http://ts.aserving.com/t.aspx?a=c&t=' + inLib[inList[tmp]][0] + '&u=' + inLib[inList[tmp]][2] + '&b=' + inLib[inList[tmp]][5] + '&c=' + inLib[inList[tmp]][6];		// Tag ID / Link / Banner ID / Campaign ID
	        //tmpHref.href = 'http://192.168.38.231:9004/t.aspx?a=c&t=' + inLib[inList[tmp]][0] + '&u=' + inLib[inList[tmp]][2] + '&b=' + inLib[inList[tmp]][5] + '&c=' + inLib[inList[tmp]][6] + '&k=' + inKeywordID ;		// Tag ID / Link / Banner ID / Campaign ID / KeywordID
	        //tmpHref.href = inLib[inList[tmp]][2];		// Banner ID / Link
	        tmpHref.target = "_blank";
	    
	        var tmpImg = document.createElement('img');
	        tmpImg.className = "thumbnail";
	        tmpImg.id = "ABnrImg" + inLib[inList[tmp]][0];				// Banner ID
	        tmpImg.style.margin = "2px 2px 5px 2px";
	        tmpImg.setAttribute("width", inLib[inList[tmp]][3]);	// Width
	        tmpImg.setAttribute("height", inLib[inList[tmp]][4]);	// Height
	        tmpImg.setAttribute("border", "0");
	        tmpImg.setAttribute("src", inLib[inList[tmp]][1]);		// Image Source
	        tmpImg.setAttribute("imgsrc", 'http://ts.aserving.com/t.aspx?a=i&t=' + inLib[inList[tmp]][0] + '&u=' + inLib[inList[tmp]][1] + '&b=' + inLib[inList[tmp]][5] + '&c=' + inLib[inList[tmp]][6] );	// Tag ID / Image Source / Banner ID / Campaign ID
	        //tmpImg.setAttribute("imgsrc", 'http://192.168.38.231:9004/t.aspx?a=i&t=' + inLib[inList[tmp]][0] + '&u=' + inLib[inList[tmp]][1] + '&b=' + inLib[inList[tmp]][5] + '&c=' + inLib[inList[tmp]][6] + '&k=' + inKeywordID  );	// Tag ID / Image Source / Banner ID / Campaign ID / KeywordID
	        //tmpImg.setAttribute("imgsrc", inLib[inList[tmp]][1]);	// Banner ID / Image Source
	        
	        tmpHref.appendChild(tmpImg);
	        inLayer.appendChild(tmpHref);
	        
	        var tChildCount = inLayer.childNodes.length;
	        var tLineCount = 1;
	        var tI = (tLineCount * inAdPerLine) + (tLineCount - 1);
	        while (tChildCount >= tI) {
	        	if (tChildCount == tI) {
	        		var tmpBr = document.createElement('br');
	        		inLayer.appendChild(tmpBr);
	        	} 
	        	tLineCount++;
	        	tI = (tLineCount * inAdPerLine) + (tLineCount - 1);
	      	}	
	        //alert (tChildCount + ' ' + (tChildCount - (Math.ceil(tChildCount / inAdPerLine))) % inAdPerLine);
	      }  
	    }	
}


// Function
// Render InTextAd
AServing.renderInTextAd =	function (inTagID, inLayerID, inReplaceLib, inContentLib) {

		var inTextAdText = document.getElementById(inLayerID).innerHTML;
		var inTextAdOrder = 0;
		for (var I=0; I<inReplaceLib.length; I++) {
    	var tempStr = inTextAdText;
    	var tempStr2 = "";
    	var intIndexOfMatch = tempStr.indexOf(inReplaceLib.getItem(I)[2]);    
    
    	while (intIndexOfMatch != -1) {
        tempStr2 += tempStr.substring(0,intIndexOfMatch);        
        var tAdContentID = inReplaceLib.getItem(I)[1];
        var replaceString = '<span id="span' + inTextAdOrder + '" onmouseout="if (checkMouseLeave (this, event)) AServing.clearPopup(\'ad' + inTextAdOrder + '\');"><a href="javascript:void(0);" id="link' + inTextAdOrder + '" onmouseover="AServing.clearPopup(\'ad' + inTextAdOrder + '\');AServing.createLayerAD(this,\'ad' + inTextAdOrder + '\',\'' + inContentLib.getItem(tAdContentID)[1] + '\',\''+  inReplaceLib.getItem(I)[0] + '\',\''+  inTagID + '\',\'' + inReplaceLib.getItem(I)[1] + '\',\'' + inContentLib.getItem(tAdContentID)[2] + '\',\'' + inContentLib.getItem(tAdContentID)[3] + '\',event);"  class="adlink">' + inReplaceLib.getItem(I)[2] + '</a><span id="ad' + inTextAdOrder + '"><img src="http://http://ts.aserving.com/l.aspx?k='+inReplaceLib.getItem(I)[0]+'&t='+inContentLib.getItem(tAdContentID)[0]+'" style="display: none;"></span></span>';
        tempStr2 += replaceString;
        tempStr = tempStr.substring(intIndexOfMatch+inReplaceLib.getItem(I)[2].length,tempStr.length);
        intIndexOfMatch = tempStr.indexOf(inReplaceLib.getItem(I)[2]);
        inTextAdOrder++;
    	}
    
    	tempStr2 = tempStr2 + tempStr;   
    	inTextAdText = tempStr2;   
		}

		document.getElementById(inLayerID).innerHTML = inTextAdText;
}

// Function
// Render Flash Banner
AServing.renderFlashBanner = function (inLayer, inList, inLib, inAdPerLine, inKeywordID) {
        
        var webaddr = "http://ts.aserving.com";
        //webaddr = "http://192.168.38.231:9004";
        
        var theDate = new Date();
        var theTime = theDate.getTime();
        
	    for (var tmp in inList) {
	    
	    	if (inList[tmp] != null) {
	            var bannerSrc = inLib[inList[tmp]][1];
            
                if ( bannerSrc.substring(bannerSrc.length-3,bannerSrc.length) == "swf" ) {
                
                    var advImg = document.createElement('img');
	                var advBr = document.createElement('br');
	                advImg.setAttribute("border", "0");
                    if ( inLib[inList[tmp]][8] == 7 || inLib[inList[tmp]][8] == 9 )  //Ad Format
                    {
                        if ( inLib[inList[tmp]][9] ==  2 )
	                        advImg.setAttribute("src", "http://rs.aserving.com/images/2/adv7.gif");	
	                    else if ( inLib[inList[tmp]][9] ==  1 )
	                        advImg.setAttribute("src", "http://rs.aserving.com/images/1/adv7.gif");	
	                    else
	                        advImg.setAttribute("src", "http://rs.aserving.com/images/0/adv7.gif");
	                    inLayer.appendChild(advImg);
	                    inLayer.appendChild(advBr);	
                    }
	                
                    var FlashDiv = document.createElement('div');
                    
	                var flashContent = '<div id="flash'+inLib[inList[tmp]][0]+'div" style="position:relative;visibility:visible;z-index:99;width:'+inLib[inList[tmp]][3]+'px;height:'+inLib[inList[tmp]][4]+'px;overflow:hidden; ">';

                    flashContent += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'+inLib[inList[tmp]][3]+'" height="'+inLib[inList[tmp]][4]+'" id="adtag_'+inLib[inList[tmp]][0]+'" name="adtag_'+inLib[inList[tmp]][0]+'">';
                    flashContent += '<param name="movie" value="'+webaddr+'/t.aspx?a=i&t='+inLib[inList[tmp]][0]+'&b=' + inLib[inList[tmp]][5] + '&c=' + inLib[inList[tmp]][6] + '&u='+inLib[inList[tmp]][1]+ '&v=' + theTime+'">';
                    
                    var FlashVars = ""; 
                    var y=1;
                    var theUrlList = new Array();
                    theUrlList = inLib[inList[tmp]][2].split(",");
                    for ( var i=1; i<= theUrlList.length; i++ ) {
//                        FlashVars += FlashVars + "c"+i+"="+webaddr+"/t.aspx?a=c%26t="+inLib[inList[tmp]][0]+"%26b=" + inLib[inList[tmp]][5] + "%26c=" + inLib[inList[tmp]][6] + "%26u="+escape(theUrlList[i-1]) + "%26k=" + inLib[inList[tmp]][7] +"&";
                        
                        if ( inKeywordID > 0 )
                            FlashVars += FlashVars + "c"+i+"="+webaddr+"/t.aspx?a=c%26t="+inLib[inList[tmp]][0]+"%26b=" + inLib[inList[tmp]][5] + "%26c=" + inLib[inList[tmp]][6] + "%26u="+escape(theUrlList[i-1]) + "%26k=" + inKeywordID +"&";
                        else
                            FlashVars += FlashVars + "c"+i+"="+webaddr+"/t.aspx?a=c%26t="+inLib[inList[tmp]][0]+"%26b=" + inLib[inList[tmp]][5] + "%26c=" + inLib[inList[tmp]][6] + "%26u="+escape(theUrlList[i-1]) + "&";
                    }
                                                                    
                    flashContent += '<param name="FlashVars" value="'+FlashVars+'">';
                    flashContent += '<param name="quality" value="high">';
                    flashContent += '<param name="WMODE" value="transparent">';
                    flashContent += '<embed src="'+webaddr+'/t.aspx?a=i&t='+inLib[inList[tmp]][0]+'&b=' + inLib[inList[tmp]][5] + '&c=' + inLib[inList[tmp]][6] + '&u='+inLib[inList[tmp]][1]+ '&v=' + theTime+'" quality="high" WMODE="transparent" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+inLib[inList[tmp]][3]+'" height="'+inLib[inList[tmp]][4]+'" name="adtag_'+inLib[inList[tmp]][0]+'" FlashVars="'+FlashVars+'"></embed>';
                    flashContent += '</object></div>';
                    
                    FlashDiv.innerHTML = flashContent;
                    inLayer.appendChild(FlashDiv);	
                    
                    if ( inLib[inList[tmp]][8] == 5 || inLib[inList[tmp]][8] == 6 )  //Ad Format
                    {
                        if ( inLib[inList[tmp]][9] ==  2 )
	                        advImg.setAttribute("src", "http://rs.aserving.com/images/2/adv5.jpg");	
	                    else if ( inLib[inList[tmp]][9] ==  1 )
	                        advImg.setAttribute("src", "http://rs.aserving.com/images/1/adv5.jpg");	
	                    else
	                        advImg.setAttribute("src", "http://rs.aserving.com/images/0/adv5.jpg");
	                    inLayer.appendChild(advImg);
	                    advImg.style.margin = "0px 0px 5px 0px";
                    }
                    else
	                    inLayer.appendChild(advBr);	
 
                }
                else
                {   
                    var tmpHref = document.createElement('a')
	                
	                if ( inKeywordID > 0)
                           tmpHref.href = webaddr+'/t.aspx?a=c&t=' + inLib[inList[tmp]][0] + '&u=' + inLib[inList[tmp]][2] + '&b=' + inLib[inList[tmp]][5] + '&c=' + inLib[inList[tmp]][6] + '&k=' + inKeywordID ;		// Tag ID / Link / Banner ID / Campaign ID / KeywordID
                        else
                            tmpHref.href = webaddr+'/t.aspx?a=c&t=' + inLib[inList[tmp]][0] + '&u=' + inLib[inList[tmp]][2] + '&b=' + inLib[inList[tmp]][5] + '&c=' + inLib[inList[tmp]][6] ;		// Tag ID / Link / Banner ID / Campaign ID 
                               
	                tmpHref.target = "_blank";
	                var tmpImg = document.createElement('img');
//	                tmpImg.className = "thumbnail";
	                tmpImg.id = "ABnrImg" + inLib[inList[tmp]][0];				// Banner ID
	                if ( inList.length > 1 )
    	                tmpImg.style.margin = "0px 0px 5px 0px";
    	            else
    	                tmpImg.style.margin = "0px 0px 0px 0px";
    	                
    	            var advImg = document.createElement('img');
	                var advBr = document.createElement('br');
	                advImg.setAttribute("border", "0");
                    if ( inLib[inList[tmp]][8] == 7  || inLib[inList[tmp]][8] == 9)  //Ad Format
                    {
                        if ( inLib[inList[tmp]][9] ==  2 )
	                        advImg.setAttribute("src", "http://rs.aserving.com/images/2/adv7.gif");	
	                    else if ( inLib[inList[tmp]][9] ==  1 )
	                        advImg.setAttribute("src", "http://rs.aserving.com/images/1/adv7.gif");	
	                    else
	                        advImg.setAttribute("src", "http://rs.aserving.com/images/0/adv7.gif");
	                    inLayer.appendChild(advImg);
	                    inLayer.appendChild(advBr);	
                    }
                    
	                tmpImg.setAttribute("width", inLib[inList[tmp]][3]);	// Width
	                tmpImg.setAttribute("height", inLib[inList[tmp]][4]);	// Height
	                tmpImg.setAttribute("border", "0");
	                tmpImg.setAttribute("src", inLib[inList[tmp]][1]);		// Image Source
	                tmpImg.setAttribute("imgsrc", webaddr+'/t.aspx?a=i&t=' + inLib[inList[tmp]][0] + '&u=' + inLib[inList[tmp]][1] + '&b=' + inLib[inList[tmp]][5] + '&c=' + inLib[inList[tmp]][6] + '&v=' + theTime );	// Tag ID / Image Source / Banner ID / Campaign ID
	                tmpHref.appendChild(tmpImg);
	                inLayer.appendChild(tmpHref);
	                
	                if ( inLib[inList[tmp]][8] == 5 || inLib[inList[tmp]][8] == 6 || inLib[inList[tmp]][8] == 8)  //Ad Format
                    {
                        if ( inLib[inList[tmp]][9] ==  2 )
	                        advImg.setAttribute("src", "http://rs.aserving.com/images/2/adv5.jpg");	
	                    else if ( inLib[inList[tmp]][9] ==  1 )
	                        advImg.setAttribute("src", "http://rs.aserving.com/images/1/adv5.jpg");	
	                    else
	                        advImg.setAttribute("src", "http://rs.aserving.com/images/0/adv5.jpg");
	                    inLayer.appendChild(advBr);	
	                    inLayer.appendChild(advImg);
	                    tmpImg.style.margin = "0px 0px 0px 0px";
	                    advImg.style.margin = "0px 0px 5px 0px";
                    }
                    
                }
	        
	        var tChildCount = inLayer.childNodes.length;
	        var tLineCount = 1;
	        var tI = (tLineCount * inAdPerLine) + (tLineCount - 1);
	        while (tChildCount >= tI) {
	        	if (tChildCount == tI) {
	        		var tmpBr = document.createElement('br');
	        		inLayer.appendChild(tmpBr);
	        	} 
	        	tLineCount++;
	        	tI = (tLineCount * inAdPerLine) + (tLineCount - 1);
	      	}	
	      }   
	    }	
}


// Function
// Reset Image of Banner for proper onmouse event in IE
AServing.resetImage = function (inLayer) {
	for(i=0; i<inLayer.childNodes.length; i++) {//loop through the tables
		var tImg = inLayer.childNodes[i].getElementsByTagName("img");//grab the rows in the specific table
	  for (j=0; j<tImg.length; j++) {
	  	tImg[j].src = tImg[j].getAttribute('imgsrc');
	  	/*try {
	    	tImg[j].src = tImg[j].getAttribute('imgsrc');
	    } catch (err) {
	    
	  	}*/
	  }
	}  
}


// Function
// Generate Random List for Banner
AServing.getRandomList = function (inMax, inList) {
		var tMax = inMax;
		var tList = inList;
		var outList = new Array();
		var tListUnique = new Hash();
		var usedList = new Hash();
		
		for (var I=0; I<inList.length; I++) {
		  tListUnique.setItem(inList[I],inList[I]);
		}
	
		while (tMax > 0) {
			var tRand = Math.ceil(Math.random() * inList.length) - 1;
			//var tListItemStr = 't'+tList[tRand];
			//alert (inMax + ' ' + outList.length + ' ' + tListUnique.length + ' ' + usedList.length);
			//alert (tList[tRand] + ' ' + tListItemStr + ' ' + (inMax - outList.length >= tListUnique.length - usedList.length) + ' ' + !(usedList.hasItem(tListItemStr)));
			if ((inMax - outList.length >= tListUnique.length - usedList.length) || !(usedList.hasItem(tList[tRand]))) {
				outList.push(tList[tRand]);
				usedList.setItem(tList[tRand],tList[tRand]);
				tList.splice(tRand,1);
				tMax--;
			}
		}
		return outList;
}			


