// JavaScript Document
var globalBanner; // this variable is useful for doSlideBanner function, please dont remove otherwise the function will not work
var theDivObjectName; // this variable is useful for doSlideBanner function, please dont remove otherwise the function will not work

/**

 */
function outputBanner(bannerArr, tableAttributes, tdAttributes, tdSpaceAttributes)
{
	var str = "";
	
	var bannerArrLength = bannerArr.length;
	
	if (bannerArrLength > 0)
	{
		str += "<table " + tableAttributes + ">";
	
		var i = 1;
		
		for (indexKey in bannerArr)
		{
			switch (bannerArr[indexKey][0])
			{
				case "1":
					// hinh gif
					str += "<tr>";
					str += "		<td " + tdAttributes + ">";
					
					if (bannerArr[indexKey][4] != "")
					{
						str += "			<a href='" + bannerArr[indexKey][4] + "' target='" + bannerArr[indexKey][5] + "'><img src='" + bannerArr[indexKey][3] + "' border='0' alt='" + bannerArr[indexKey][6] + "'></a>";
					}
					else
					{
						str += "			<img src='" + bannerArr[indexKey][3] + "' border='0' alt='" + bannerArr[indexKey][6] + "'>";
					}
					
					str += "		</td>";
					str += "</tr>";
					
					break;
				case "2":
					// hinh flash
					str += "<tr>";
					str += "		<td " + tdAttributes + ">";
					str += '			<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0"  width="' + bannerArr[indexKey][1] + '" height="' + bannerArr[indexKey][2] + '"><param name="movie" value="' + bannerArr[indexKey][3] + '?linkf=' + bannerArr[indexKey][4] + '"><param name="quality" value="high"><embed src="' + bannerArr[indexKey][3] + '?linkf=' + bannerArr[indexKey][4] + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + bannerArr[indexKey][1] + '" height="' + bannerArr[indexKey][2] + '"></embed></object>';
					str += "		</td>";
					str += "</tr>";
					
					break;
			}
			
			if ((tdSpaceAttributes != "") && (i < bannerArrLength))
			{
				str += "<tr>";
				str += "		<td " + tdSpaceAttributes + ">";
				str += "		</td>";
				str += "</tr>";
			}
			
			i++;
		}
		
		str += "</table>";
	}
	
	return str;
}

/**

 */
function makeADivObject(divName)
{
	document.write('<div id="' + divName + '"></div>');
}

/**

 */
function doSlideRandomBanner(bannerArr, tableAttributes, tdAttributes)
{
	globalBanner = bannerArr;
	
	theDivObjectName = "slide";
	
	makeADivObject(theDivObjectName);
	
	slideBanner(globalBanner, theDivObjectName, null, tableAttributes, tdAttributes);
	
	var timer = window.setInterval('slideBanner(globalBanner, theDivObjectName, null, \'' + tableAttributes + '\', \'' + tdAttributes + '\')', 10000);
}

/**

 */
function test(str)
{
	// this line will set timeout the test function with theDivObjectName variables. theDivObjectName should be string only
	// var timer = window.setInterval("test('" + theDivObjectName + "')",10000);
	
	// this line will set timeout the test function with theDivObjectName object
	// var timer = window.setInterval("test(globalBanner)",10000);
}

var randomBannerIndex = null; // this variables useful for the

/**

 */
function doSlideBanner(bannerArr, tableAttributes, tdAttributes)
{
	
	globalBanner = bannerArr;
	
	theDivObjectName = "slide";
	
	makeADivObject(theDivObjectName);
	
	doSlideBannerA(globalBanner, theDivObjectName, randomBannerIndex, tableAttributes, tdAttributes);
	
	var timerSlide = window.setInterval('doSlideBannerA(globalBanner, theDivObjectName, randomBannerIndex, \'' + tableAttributes + '\', \'' + tdAttributes + '\')', 10000);
	
}

/**

 */
function doSlideBannerA(bannerArr, theDivObjectName, bannerIndex, tableAttributes, tdAttributes)
{
	
	if (!bannerIndex)
	{
		var bannerArrLength = bannerArr.length;
	
		randomBannerIndex = getRandom(bannerArrLength - 1);
	}
	else
	{
		bannerIndex = bannerIndex*1 + 1;
		
		if (bannerIndex == bannerArr.length)
		{
			bannerIndex = "0";
		}
		
		randomBannerIndex = bannerIndex;
	}
	
	slideBanner(bannerArr, theDivObjectName, randomBannerIndex, tableAttributes, tdAttributes);
	
}

/**

 */
function slideBanner(bannerArr, theDivObjectName, bannerIndex, tableAttributes, tdAttributes)
{	
	var randomIndex = bannerIndex;
	
	if (!bannerIndex)
	{
		// in case we dont have bannerIndex, we will get a random index from array		
		var bannerArrLength = bannerArr.length;
	
		var randomIndex = getRandom(bannerArrLength - 1);
	}
	
	
	var theOutputString = "";
	
	theOutputString += "<table " + tableAttributes + ">";	
	theOutputString += "		<tr>";
	theOutputString += "			<td " + tdAttributes + ">";
	//alert(bannerArr[randomIndex][0]);
	//alert('Banner hien ra');
	switch (bannerArr[randomIndex][0])
	{
		case "99":
			// hinh gif
			//alert(bannerArr[0][4]);
			if (bannerArr[randomIndex][4] != "")
			{
				theOutputString += "<a href='" + bannerArr[randomIndex][4] + "' target='" + bannerArr[randomIndex][5] + "'><img src='" + bannerArr[randomIndex][3] + "' border='0' alt='" + bannerArr[randomIndex][6] + "'></a>";
			}//end if
			else
			{
				theOutputString += "<img src='" + bannerArr[randomIndex][3] + "' border='0' alt='" + bannerArr[randomIndex][6] + "'>";
			}//end else
			
			break;
		case "103":
			// hinh flash
			theOutputString += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="' + bannerArr[randomIndex][1] + '" height="' + bannerArr[randomIndex][2] + '"><param name="movie" value="' + bannerArr[randomIndex][3] + '?clicktag=' + bannerArr[randomIndex][4] + '"><param name="quality" value="high"><embed src="' + bannerArr[randomIndex][3] + '?clicktag=' + bannerArr[randomIndex][4] + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + bannerArr[randomIndex][1] + '" height="' + bannerArr[randomIndex][2] + '"></embed></object>';
		
			break;
	}//end switch
	
	theOutputString += "			</td>";
	theOutputString += "		</tr>";
	theOutputString += "</table>";
	
	var theDivObject = document.getElementById(theDivObjectName);
	//alert(theOutputString);
	
	theDivObject.innerHTML = theOutputString;
}


function Random(N) { return Math.floor(N*(Math.random()%1)) }
function Deal(N) { var J, K, Q = new Array(N)
  for (J=0; J<N; J++) { K = Random(J+1) ; Q[J] = Q[K] ; Q[K] = J }
  return Q }

//=================Banner QC khac====================
function BannerAD(bannerArr,tableAttributes, tdAttributes, popup)
{	
	var bannerArrLength = bannerArr.length-1;
	
	var ranIn = new Array();
	ranIn = Deal(bannerArr.length);
	//alert("ran=" + ranIn);


	var theOutputString = "";
	
	if(bannerArr[0][7]==100)//TH random
	{		
			theOutputString += "<table cellpadding='0' cellspacing='0'>";
			for(var i=0; i<=bannerArrLength; i++)
			{					
				theOutputString += "	<tr>";
				theOutputString += "		<td>";
				theOutputString += "<table " + tableAttributes + ">";	
				theOutputString += "	<tr>";
				theOutputString += "		<td class='" + tdAttributes + "'>";

				switch (bannerArr[ranIn[i]][0])
				{
					case "99":
						//hinh gif
						if (bannerArr[ranIn[i]][4] != "")
						{
							theOutputString += "<a href='" + bannerArr[ranIn[i]][4] + "' target='" + bannerArr[ranIn[i]][5] + "'><img src='" + bannerArr[ranIn[i]][3] + "' border='0' alt='" + bannerArr[ranIn[i]][6] + "'></a>";
						}
						else
						{
							theOutputString += "<img src='" + bannerArr[ranIn[i]][3] + "' border='0' alt='" + bannerArr[ranIn[i]][6] + "'>";	
						}
						break;
					case "103":
						// hinh flash
						theOutputString += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="' + bannerArr[ranIn[i]][1] + '" height="' + bannerArr[ranIn[i]][2] + '"><param name="movie" value="' + bannerArr[ranIn[i]][3] + '?clicktag=' + bannerArr[ranIn[i]][4] + '"><param name="quality" value="high"><embed src="' + bannerArr[ranIn[i]][3] + '?clicktag=' + bannerArr[ranIn[i]][4] + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + bannerArr[ranIn[i]][1] + '" height="' + bannerArr[ranIn[i]][2] + '"></embed></object>';
			
						break;
				}//end switch
				theOutputString += "		</td>";
				theOutputString += "	</tr>";
				theOutputString += "</table>";
				theOutputString += "		</td>";
				theOutputString += "	</tr>";
			}//end for
			theOutputString += "</table>";
			//alert(theOutputString);
				
	}//end if(bannerArr[0][7]==1)//random
	else//norandom
	{		
		theOutputString += "<table cellpadding='0' cellspacing='0'>";
		for(var i=0; i<=bannerArrLength; i++)
		{					
			theOutputString += "	<tr>";
			theOutputString += "		<td>";
			theOutputString += "<table " + tableAttributes + ">";	
			theOutputString += "	<tr>";
			theOutputString += "		<td class='" + tdAttributes + "'>";
			switch (bannerArr[i][0])
			{
				case "99":
				//hinh gif				
 					if (popup) {
						theOutputString += "<img style='cursor:pointer;' src='" + bannerArr[i][3] + "' border='0' alt='" + bannerArr[i][6] + "' onclick='open_win(\"" + bannerArr[i][4] + "\")'>";
					}
					else {									
						if (bannerArr[i][4] != "")
						{
							theOutputString += "<a href='" + bannerArr[i][4] + "' target='" + bannerArr[i][5] + "'><img src='" + bannerArr[i][3] + "' border='0' alt='" + bannerArr[i][6] + "'></a>";
						}
						else
						{
							theOutputString += "<img src='" + bannerArr[i][3] + "' border='0' alt='" + bannerArr[i][6] + "'>";	
						}
					}
					break;
				case "103":
					// hinh flash
					theOutputString += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="' + bannerArr[i][1] + '" height="' + bannerArr[i][2] + '"><param name="movie" value="' + bannerArr[i][3] + '?clicktag=' + bannerArr[i][4] + '"><param name="quality" value="high"><embed src="' + bannerArr[i][3] + '?clicktag=' + bannerArr[i][4] + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + bannerArr[i][1] + '" height="' + bannerArr[i][2] + '"></embed></object>';
		
					break;
			}//end switch
			theOutputString += "		</td>";
			theOutputString += "	</tr>";
			theOutputString += "</table>";
			theOutputString += "		</td>";
			theOutputString += "	</tr>";
		}//end for
		theOutputString += "</table>";
		//alert(theOutputString);	
			
	}//else//norandom
	

	document.write(theOutputString);
}


function BannerAD1(bannerArr1,tableAttributes1, tdAttributes1)
{
	var theOutputString = "";
	var bannerArrLength = bannerArr1.length;
	//alert(bannerArrLength);
	var ranInd = new Array();
	ranInd = Deal(bannerArr1.length);
	//alert("ran=" + ranInd);
	for(var i=0; i<bannerArrLength; i++)
	{				
		theOutputString += "<table " + tableAttributes1 + ">";	
		theOutputString += "	<tr>";
		theOutputString += "		<td class='" + tdAttributes1 + "'>";
		theOutputString += "<a href='" + bannerArr1[ranInd[i]][4] + "' target='" + bannerArr1[ranInd[i]][5] + "'><img src='" + bannerArr1[ranInd[i]][3] + "' border='0' alt='" + bannerArr1[ranInd[i]][6] + "' ></a>";		
		theOutputString += "		</td>";
		theOutputString += "	</tr>";
		theOutputString += "</table>";
		theOutputString += "<div style='height:"+ (parseInt(bannerArr1[ranInd[i]][2]) + 9) + "px;'></div>";
	}//end for
	
	document.write(theOutputString);
}


/**

 */
function displayRandomBanner(bannerArr, tableAttributes, tdAttributes, tdSpaceAttributes)
{	
	bannerArr = randomArray(bannerArr);
	
	var outputedBanner = outputBanner(bannerArr, tableAttributes, tdAttributes, tdSpaceAttributes);
	
	document.write(outputedBanner);
}

/**

 */
function displayBanner(bannerArr, tableAttributes, tdAttributes, tdSpaceAttributes)
{	
	var outputedBanner = outputBanner(bannerArr, tableAttributes, tdAttributes, tdSpaceAttributes);
	
	document.write(outputedBanner);
}

/**

 */
function displayFloatBanner(floatBannerArr, handSide)
{
	str = "";
	
	if (floatBannerArr.length > 0)
	{
		switch (handSide)
		{
			case "left":
				str += '<div id="divAdLeft" style="left: -110px; width: 108px; position: absolute; top: 103px" align="right">';
				
				for (keyIndex in floatBannerArr)
				{
					if (floatBannerArr[keyIndex][4] != "")
					{
						str += "<a href='" + floatBannerArr[keyIndex][4] + "' target='" + floatBannerArr[keyIndex][5] + "'><img src='" + floatBannerArr[keyIndex][3] + "' border='0' alt='" + floatBannerArr[keyIndex][6] + "'></a><br>";
					}
					else
					{
						str += "<img src='" + floatBannerArr[keyIndex][3] + "' border='0' alt='" + floatBannerArr[keyIndex][6] + "'><br>";
					}
				}
				
				str += "</div>";
				break;
			case "right":
				str += '<div id=divAdRight style="right: -110px; width: 96px; position: absolute; top: 103px; padding-left: 3px;">';
				
				for (keyIndex in floatBannerArr)
				{
					if (floatBannerArr[keyIndex][4] != "")
					{
						str += "<a href='" + floatBannerArr[keyIndex][4] + "' target='" + floatBannerArr[keyIndex][5] + "'><img src='" + floatBannerArr[keyIndex][3] + "' border='0' alt='" + floatBannerArr[keyIndex][6] + "'></a><br>";
					}
					else
					{
						str += "<img src='" + floatBannerArr[keyIndex][3] + "' border='0' alt='" + floatBannerArr[keyIndex][6] + "'><br>";
					}
				}
				
				str += "</div>";
				break;
		}
	}
	
	document.write(str);
	
	switch (handSide)
	{
		case "left":
			ShowAdDiv("divAdLeft", handSide);
			break;
		case "right":
			ShowAdDiv("divAdRight", handSide);
			break;
	}
}

/**

 */
function ShowAdDiv(theBannerDivID, handSide)
{
	var theBannerDivObject = document.getElementById(theBannerDivID);
	
	if (document.body.clientWidth < 980)
	{
		theBannerDivObject.style.left = -110;
	}
	else
	{
		switch (handSide)
		{
			case "left":
				theBannerDivObject.style.left = 0;
				
				FloatTopDiv2(theBannerDivID);
				break;
			case "right":
				theBannerDivObject.style.left = document.body.clientWidth - 110;
				
				FloatTopDiv(theBannerDivID);
				break;
		}
	}
}

/**

 */
function FloatTopDiv2(leftFloatBanner)
{
	startX2 = document.body.clientWidth - 1004, startY2 = 103;
	var ns2 = (navigator.appName.indexOf("Netscape") != -1);
	var d2 = document;
		
	if (document.body.clientWidth < 980) startX2 = -110;
	
	function ml2(id)
	{
		var el2=d2.getElementById?d2.getElementById(id):d2.all?d2.all[id]:d2.layers[id];
		
		if(d2.layers)el2.style=el2;
		
		el2.sP = function(x,y)
		{
			this.style.left=x;
			this.style.top=y;
		};
		
		el2.x = startX2;
		el2.y = startY2;
		
		return el2;
	}
	
	window.stayTopLeft2=function()
	{
		if (document.body.clientWidth < 980)
		{
			ftlObj2.x = - 115;ftlObj2.y = 0;	ftlObj2.sP(ftlObj2.x, ftlObj2.y);
		}
		else
		{
			if (document.documentElement && document.documentElement.scrollTop)
				var pY2 = ns2 ? pageYOffset : document.documentElement.scrollTop;
			else if (document.body)
				var pY2 = ns2 ? pageYOffset : document.body.scrollTop;

			if (document.body.scrollTop > 103){startY2 = 17} else {startY2 = 103};

			if (document.body.clientWidth >= 1024)
			{
				ftlObj2.x =  0;ftlObj2.y += (pY2 + startY2 - ftlObj2.y)/32;	ftlObj2.sP(ftlObj2.x, ftlObj2.y);
			}
			else
			{		
				ftlObj2.x  = startX2;
				ftlObj2.y += (pY2 + startY2 - ftlObj2.y)/32;
				ftlObj2.sP(ftlObj2.x, ftlObj2.y);
			}
		}
		
		setTimeout("stayTopLeft2()", 1);
	}
	
	ftlObj2 = ml2(leftFloatBanner);
	stayTopLeft2();		
}

/**

 */
function FloatTopDiv(rightFloatBanner)
{
	startX = document.body.clientWidth - 110, startY = 103;
	var ns = (navigator.appName.indexOf("Netscape") != -1);
	var d = document;
		
	if (document.body.clientWidth < 980)
	{
		startX = -110;
	}
	
	function ml(id)
	{
		var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
		if (d.layers)
		{
			el.style=el;
		}
		
		el.sP = function(x,y)
		{
			this.style.left=x;
			this.style.top=y;
		};
		
		el.x = startX;
		el.y = startY;
		
		return el;
	}
	
	window.stayTopLeft=function()
	{		
		if (document.body.clientWidth < 980)
		{
			ftlObj.x = - 115;ftlObj.y = 0;	ftlObj.sP(ftlObj.x, ftlObj.y);
		}			
		else
		{
			if (document.documentElement && document.documentElement.scrollTop)
				var pY = ns ? pageYOffset : document.documentElement.scrollTop;
			else if (document.body)
				var pY = ns ? pageYOffset : document.body.scrollTop;

			if (document.body.scrollTop > 103){startY = 17} else {startY = 103};

			if (document.body.clientWidth >= 1024)
			{
				ftlObj.x = document.body.clientWidth - 110;ftlObj.y += (pY + startY - ftlObj.y)/32;ftlObj.sP(ftlObj.x, ftlObj.y);
			}
			else
			{			
				ftlObj.x  = startX;
				ftlObj.y += (pY + startY - ftlObj.y)/32;
				ftlObj.sP(ftlObj.x, ftlObj.y);
			}
		}
		
		setTimeout("stayTopLeft()", 1);
	}
	
	ftlObj = ml(rightFloatBanner);
	
	stayTopLeft();
}

function getRandom(theNumber)
{
	//alert(Math.round(Math.random() * theNumber));
	return Math.round(Math.random() * theNumber);
}

function randomArray(max) {
	finalArray = new Array();
	i = 0;
	while(i < max)
	{
		num = createNumRand(max);
		if(!searchIn(finalArray, num))
		{	finalArray[i] = num;
			i++;
		}
	}
	return finalArray;
}

function createNumRand(num) {
	rate = 1;
	while(num > 1) {
		rate *= 10;
		num /= 10;
	}
	num *= rate;
	return (Math.floor(Math.random()*rate)%num);
}

function searchIn(arr, num) {
	if(arr.length > 0) len = arr.length;
	else 
		return false;
	for(i=0; i < len; i++) {
		if(arr[i] == num)
			return true; //tim thay
	}
	return false;//khong tim thay
}