var fBrw=(navigator.userAgent.indexOf('MSIE')!= -1 && navigator.userAgent.indexOf('Windows')!= -1);

// ------------------------------------------------------------------------- //
// JavaScript Function Library for Thanh Nien Online Newspaper               //
// Written by: VanHNN <vanhnn@hcm.fpt.vn>                                    //
// ------------------------------------------------------------------------- //

function openVoteWindow() {
	return window.open('','VOTE_WINDOW','width=300,height=400,resizable=1,scrollbars=1');
}

function displayNewsBodyBottom() {
	document.write('<div class=NewsBodyBottom>');
	document.write('<a onclick="location.href=\'?Print\'; return false;" href=""><img src=/Images/UI/ico.print.gif border=0> In</a> &nbsp;&nbsp; ');
	document.write('<a onclick="location.href=\'/Contact/?CMD=Feedback&NID=\'+ NewsID +\'&PTH=\'+ location.href; return false;" href=""><img src=/Images/UI/ico.feedback.gif border=0> Ph&#7843;n h&#7891;i</a> &nbsp;&nbsp; ');
	document.write('<a onclick="window.open(\'/Contact/TellAFriend.asp?p=\' + NewsPath + \'&NewsID=\' + NewsID, \'TELLAFRIEND\', \'width=300,height=200\'); return false;" href=""><img src=/Images/UI/ico.tellafriend.gif border=0> G&#7917;i</a> &nbsp;&nbsp; ');
	document.write('</div>');
}

function showNext(lastDate) {
	var re = / /g;
	var sDate = lastDate.replace(re, '+');
	location.href = '?Last=' + sDate;
	return false;
}

function MenuItem(id, title, link, parent, root) {
	this.id = id;
	this.title = title;
	this.link = link;
	this.parent = parent;
	this.root = root;
}

function displayPageNavigator(scriptName, queryString, currentOffset, pageCount, recordCount) {
	var previousOffset, nextOffset, offsetBegin, offsetEnd, nPage;
	currentOffset = currentOffset == '' ? 0 : currentOffset/1;
	pageCount = pageCount == '' ? 1 : pageCount/1;
	recordCount = recordCount == '' ? 0 : recordCount/1;
	if (currentOffset - 5 > 1) {
		if (currentOffset + 5 <= pageCount)
			offsetBegin = currentOffset - 5;
		else {
			if (pageCount - 10 >= 1)
				offsetBegin = pageCount - 10;
			else
				offsetBegin = 1;
		}
	} else
		offsetBegin = 1;
	
	if (offsetBegin + 10 < pageCount)
		offsetEnd = offsetBegin + 10;
	else
		offsetEnd = pageCount;
	
	if (currentOffset - 1 > 1)
		previousOffset = currentOffset - 1;
	else
		previousOffset = 1;
	
	if (currentOffset + 1 < pageCount)
		nextOffset = currentOffset + 1;
	else
		nextOffset = pageCount;
	
	
	var html = '';
	
	html += '<span class=SearchNavigation>';
	html += '<a href="' + scriptName + '?' + queryString + '&offs=' + previousOffset + '"><img src=/images/goback.gif border=0></a>&nbsp;';
	for (nPage=offsetBegin; nPage<=offsetEnd; nPage++) {
		if (nPage-currentOffset == 0)
			html += '<span class=SearchNavigationOffset>' + nPage + '</span>';
		else
			html += '<a href="' + scriptName + '?' + queryString + '&offs=' + nPage + '">' + nPage + '</a>';
		html += '&nbsp;';
	}
	html += '<a href="' + scriptName + '?' + queryString + '&offs=' + nextOffset + '"><img src=/images/goforward.gif border=0></a>';
	html += '</span>';
	document.write(html);
}


function displayMenu(cateId) {
	function getLevel0(title, link, style, selected) {
		var html = '';
		html += '<tr>';
		html += '<td width="100%" height="20" class=' + (selected ? 'MenuLevel0Selected' : 'MenuLevel0' ) + '>&nbsp;';
		html += '<img border="0" src="/images/bullet01.gif" width="3" height="5">&nbsp;';
		html += '<a href="';
		html += link;
		html += '">';
		html += title;
		html += '</a></td></tr>';
		return html;
	}
	function getLevel1(title, link, style, selected) {
		var html = '';
		html += '<tr>';
		html += '<td width="100%" height="20" class=' + (selected ? 'MenuLevel1Selected' : 'MenuLevel1' ) + '>&nbsp;&nbsp;&nbsp;';
//		html += '<b><font face="Arial" color="#cc3300" size="1">&#187; </font></b>';
		html += '<b><font face="Arial" color="#c0c0c0">. </font></b>';
		html += '<a href="';
		html += link;
		html += '">';
		html += title;
		html += '</a></td></tr>';
		return html;
	}
	
	if (typeof(cateId) == 'undefined' || cateId == null)
		if (typeof(CurrentCateID) != 'undefined' && CurrentCateID != null)
			cateId = CurrentCateID;
		else
			cateId = 0;

	var html = '';
	
	html += '<table border="0" cellpadding="0" cellspacing="0" width="100%">';
	html += '<tr>';
	html += '<td width="100%" align="center">';
	html += '<a href="/TinTuc/"><img border="0" src="/images/title_tintuc.gif" width="147" height="25"></a></td>';
	html += '</tr>';
	html += '<tr>';
	html += '<td width="100%">';
	html += '<table border="0" cellpadding="0" cellspacing="1" width="100%">';
	html += '<tr>';
	html += '<td width="100%" bgcolor=#F6F5F5>'; 
	html += '<table border="0" cellpadding="0" cellspacing="0" width="100%">';
	
	// find parent of the current cateId
	var parentCateId = 0;
	for (i=0; i<menuItems.length; i++)
		if (menuItems[i].id == cateId) {
			parentCateId = menuItems[i].parent;
			break;
		}

	for (i=0; i<menuItems.length; i++) {
		if (menuItems[i].root)
			html += getLevel0(menuItems[i].title, menuItems[i].link, menuItems[i].style, (menuItems[i].id == cateId) || (menuItems[i].id == parentCateId));
		else if (menuItems[i].parent == cateId || parentCateId == menuItems[i].parent)
			html += getLevel1(menuItems[i].title, menuItems[i].link, menuItems[i].style, (menuItems[i].id == cateId) || (menuItems[i].id== parentCateId));
	}
	
	
	html += '</table>';
	html += '</td>';
	html += '</tr>';
	html += '</table>';
	html += '</td>';
	html += '</tr>';
	html += '<tr>';
	html += '<td width="100%" height="5">';
	html += '<img border="0" src="images/space.gif" width="1" height="1"></td>';
	html += '</tr>';
	html += '</table>';
	
	document.write(html);
}

function leadZero(n, units) {
	var sN = new String(n);
	var i, sRet = sN;
	for (i=sN.length; i<units; i++) {
		sRet = '0' + sRet;
	}
	return sRet;
}

function displayDate(dDate) {
	var today;
	if (dDate)
		today = new Date(dDate);
	else
		today = new Date(); 
//	var dow = new Array('CH&#7910; NH&#7852;T','TH&#7912; HAI','TH&#7912; BA','TH&#7912; T&#431;','TH&#7912; N&#258;M','TH&#7912; S&#193;U','TH&#7912; B&#7842;Y');
	var dow = new Array('Ch&#7911; Nh&#7853;t','Th&#7913; Hai','Th&#7913; Ba','Th&#7913; T&#432;','Th&#7913; N&#259;m','Th&#7913; S&#225;u','Th&#7913; B&#7843;y');
	var months = new Array('01','02','03','04','05','06','07','08','09','10','11','12');
	var year = today.getYear();
	year = (year <= 1888) ? (1900 + year) : year;
	document.write(dow[today.getDay()] + ' ng&#224;y ' + today.getDate() + '/' + months[today.getMonth()] + '/' + year); // + ', ' + leadZero(today.getHours(), 2) + 'h' + leadZero(today.getMinutes(), 2) + ' (GMT+7)'
}

function displayPublishedTime(dtTime) {
	displayDate(dtTime);
}

function displayDateViewer(currentDate) {
	var i;
	var days = new Array('1','1','2','2','3','3','4','4','5','5','6','6','7','7','8','8','9','9','10','10','11','11','12','12','13','13','14','14','15','15','16','16','17','17','18','18','19','19','20','20','21','21','22','22','23','23','24','24','25','25','26','26','27','27','28','28','29','29','30','30','31','31');
	var months = new Array('January','1','February','2','March','3','April','4','May','5','June','6','July','7','August','8','September','9','October','10','November','11','December','12');
	var years = new Array();
	var today = new Date();
	var thisDay = today.getDate(), thisMonth = today.getMonth()+1, thisYear = today.getYear();

	var ds = currentDate.split('/');
	if (ds.length == 3) {
		cMonth = ds[0]; cDay = ds[1]; cYear = ds[2];
	} else {
		cMonth = thisMonth; cDay = (thisDay > 1 ? thisDay - 1 : 31); cYear = thisYear;
	}

	var index=0, iYear = thisYear;
	for (i=0; iYear>2002; i++) {
		years[index++] = iYear;
		years[index++] = iYear;
		iYear--;
	}
	document.write('<FORM method=get style=margin:0>');
	
	writeSelect('Day', days, cDay);
	writeSelect('Month', months, cMonth);
	writeSelect('Year', years, cYear);
	
	document.write('<INPUT type=button onclick="location.href=\'?Date=\' + this.form.Month.value + \'/\' + this.form.Day.value + \'/\' + this.form.Year.value; " value=Xem name=View>');
	document.write('</FORM>');
}

function AdWrapper(AdSpecs) {
	this.ID = AdSpecs[0];
	this.Link = AdSpecs[1];
	this.Title = AdSpecs[2];
	this.ImageSource = AdSpecs[3];
	this.Type = AdSpecs[4];
	this.Level = AdSpecs[5];
	this.StartDate = AdSpecs[6];
	this.ExpiresDate = AdSpecs[7];
	this.OpenMode = AdSpecs[8];
	this.WinWidth = AdSpecs[9];
	this.WinHeight = AdSpecs[10];
	this.WinFeatures = AdSpecs[11];


	this.isVisible = function() {
		var AdStartDate = new Date(this.StartDate);
		var AdExpiresDate = new Date(this.ExpiresDate);
		var CurrentDate;
		if (typeof(ServerTime) == 'string')
			CurrentDate = new Date(ServerTime);
		else
			CurrentDate = new Date();
		return (AdStartDate <= CurrentDate && CurrentDate <= AdExpiresDate);
	}

	this.getHTML = function () {
		var sAd = '';
		if (AdSpecs[8] == 'Compact')
			sAd = '<a href="' + this.Link + '" onclick="window.open(\'/Bin/AdClick/?' + this.ID + '\',\'_blank\',\'width=' + this.WinWidth + ',height=' + this.WinHeight + ',' + this.WinFeatures + '\'); return false;"><img _alt="' + this.Title + '" src="' + this.ImageSource + '" border=0></a>';
		else
			sAd = '<a href="' + this.Link + '" onclick="window.open(\'/Bin/AdClick/?' + this.ID + '\',\'' + (this.OpenMode == 'Popup' ? '_blank' : '_top') + '\'); return false;"><img _alt="' + this.Title + '" src="' + this.ImageSource + '" border=0></a>';
		return sAd;
	}
}

function getAd(AdSpecs) {
	var sAd = '';
	if (AdSpecs[8] == 'Compact')
		sAd = '<a href="' + AdSpecs[1] + '" onclick="window.open(\'/Bin/AdClick/?' + AdSpecs[0] + '\',\'_blank\',\'width=' + AdSpecs[9] + ',height=' + AdSpecs[10] + ',' + AdSpecs[11] + '\'); return false;"><img _alt="' + AdSpecs[2] + '" src="' + AdSpecs[3] + '" border=0></a>';
	else
		sAd = '<a href="' + AdSpecs[1] + '" onclick="window.open(\'/Bin/AdClick/?' + AdSpecs[0] + '\',\'' + (AdSpecs[8] == 'Popup' ? '_blank' : '_top') + '\'); return false;"><img _alt="' + AdSpecs[2] + '" src="' + AdSpecs[3] + '" border=0></a>';
	return sAd;
}

function isAdVisible(AdSpecs) {
	var AdStartDate = new Date(AdSpecs[6]);
	var AdExpiresDate = new Date(AdSpecs[7]);
	var CurrentDate;
	if (typeof(ServerTime) == 'string')
		CurrentDate = new Date(ServerTime);
	else
		CurrentDate = new Date();
	return (AdStartDate <= CurrentDate && CurrentDate <= AdExpiresDate);
}

function displayLogos(nLevel) {
	var i;
	if (typeof(AdItems) == 'object') {
		var html = '';
		//html += '<table cellPadding=2 cellSpacing=0 class=Box width=165><tr><th background=/images/right_brg03.gif class=BoxHeader>';
		//html += 'Qu&#7843;ng c&#225;o';
		//html += '</th></tr><tr><td class=BoxBody>';

		html += '<table cellpadding=0 cellspacing=0 width="100%">';
		for (i=0; i<AdItems.length; i++) {
			if (AdItems[i][4] == 2 && AdItems[i][5] == nLevel && isAdVisible(AdItems[i]))	// ad type = Logo AND level = assign level
				html += '<tr><td bgcolor=#f0f0f0>' + getAd(AdItems[i]) + '</td></tr> <tr><td><img src=/images/spacer.gif width=2 height=2></td></tr>';
		}

		html += '</table>';
		//html += '</td></tr></table>';
		document.write(html);
	}
}

function displayMenuLogos() {
	var i;
	if (typeof(AdItems) == 'object') {
		var html = '';
		html += '<table cellpadding=0 cellspacing=0 width="100%">';
		for (i=0; i<AdItems.length; i++) {
			if (AdItems[i][4] == 3 && isAdVisible(AdItems[i]))	// ad type = Menu Logo
				html += '<tr><td bgcolor=#f0f0f0>' + getAd(AdItems[i]) + '</td></tr> <tr><td><img src=/images/spacer.gif width=2 height=2></td></tr>';
		}

		html += '</table>';
		document.write(html);
	}
}


var nAdIndex = -1, bStillHaveBanner = true;
if (typeof(AdItems) == 'object')
	if (AdItems.length > 0) {
		var i, n = 0, aBannerIndexes = new Array();
		for (i=0; i<AdItems.length; i++)
			if (AdItems[i][4] == 1)		// gotcha, a banner!
				aBannerIndexes[n++] = i;
		nAdIndex = aBannerIndexes[Math.round(Math.random() * 1000) % (aBannerIndexes.length)] - 1;
	}