function getXMLHTTPRequest() { // Coded by ndthuan05@yahoo.com
	var xRequest = null;
	if (window.XMLHttpRequest) {
		xRequest = new XMLHttpRequest();
		if (xRequest.overrideMimeType) xRequest.overrideMimeType('text/xml');
	} else if (window.ActiveXObject){
		try	{
			xRequest = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			try	{
				xRequest = new ActiveXObject('Microsoft.XMLHTTP');
			} catch (e) {
				alert('Trinh duyet cua ban khong the xem trang web nay');
				self.location.href = 'errorBrowser.htm';
			}
		}
	}

	return xRequest;
}

var req = null;
function sendRequest(url, params, HttpMethod, divID, showProcess, useAlerter) { // Coded by ndthuan05@yahoo.com
	if (!HttpMethod) HttpMethod = 'GET';
	req = getXMLHTTPRequest();
	if (req) {
		req.onreadystatechange = function() {dumpData(divID, showProcess, useAlerter)};
		req.open(HttpMethod, url, true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send(params);
	}
}

var responseXML = null, responseText = null;
function callServer(url, params, HttpMethod, handler, showProcess) { // Coded by ndthuan05@yahoo.com
	if (!HttpMethod) HttpMethod = 'GET';
	req = getXMLHTTPRequest();
	if (req) {
		req.onreadystatechange = function() {
			if (req.readyState == 4 && req.status == 200 && req.responseXML && req.responseText) {
				
				responseXML = req.responseXML;
				responseText = req.responseText;

				if (handler) eval(handler);

				if (showProcess) crossObj('ChieuDaLatOK').style.display = 'none';
			} else {
				if (showProcess) {
					crossObj('ChieuDaLatOK').style.display = '';
				}
			}
		}
		req.open(HttpMethod, url, true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send(params);
	}
}

function formatOutput(text) {
	if (text) return text.replace("'", "\'");
}

var loggedPages = new Array();
var logID = 0;
function dumpData(divID, showProcess, useAlerter){ // Coded by ndthuan05@yahoo.com
	var sdata = null;
	var showAlertMessage = false;
	if (req.readyState == 4){
		crossObj('ChieuDaLatOK').style.display = 'none';
		if (req.status == 200) {
			sdata = req.responseText;
			showAlertMessage = true;

			// Kiem tra de hien thi link nhac
			if (sdata.indexOf('<!--showPlayer=') > -1) {
				var wmpLink = getHiddenContent('showPlayer', sdata);
				sdata = sdata.replace(sdata.substring(sdata.indexOf('<!--showPlayer='), sdata.indexOf('***showPlayer-->') + 16), showFakePlayer() + showPlayer(wmpLink));
			}

			// Kiem tra de luu lai
			if (divID == 'SPMusic_MainContent') {
				var pageTitle = getHiddenContent('pageTitle', sdata);

				var loggedTime = new Date();
				var hour = loggedTime.getHours();
				var minute = loggedTime.getMinutes();
				var sec = loggedTime.getSeconds();
				var day = loggedTime.getDate();
				var month = loggedTime.getMonth() + 1;
				var year = loggedTime.getFullYear();
				var timeString = ((hour < 10) ? '0' + hour : hour) + ':';
				timeString += ((minute < 10) ? '0' + minute : minute) + ':';
				timeString += ((sec < 10) ? '0' + sec : sec) + ' ng&#224;y ';
				timeString += ((day < 10) ? '0' + day : day) + '.';
				timeString += ((month < 10) ? '0' + month : month) + '.';
				timeString += year;

				logID += 1;
				loggedPages[logID] = pageTitle + '*****' + timeString + '*****' + sdata + '*****' + (self.location.href);
				//loadLoggedPages(1);

				//document.title = pageTitle;
			}
		} else {
			sdata = '<div align="center" style="color:red; font-weight:bold;">&#272;&#227; xu&#7845;t hi&#7879;n l&#7895;i trong qu&#225; tr&#236;nh x&#7917; l&#253; y&#234;u c&#7847;u c&#7911;a b&#7841;n, vui l&#242;ng th&#7917; l&#7841;i. N&#7871;u v&#7851;n kh&#244;ng &#273;&#432;&#7907;c vui l&#242;ng th&#244;ng b&#225;o cho ch&#250;ng t&#244;i bi&#7871;t.<br /><br />Xin ch&#226;n th&#224;nh c&#7843;m &#417;n b&#7841;n!</div>';
		}
	} else {
		if (showProcess) {
			crossObj('ChieuDaLatOK').style.display = '';
		}
	}

	if (divID && sdata && !useAlerter) {
		crossObj(divID).innerHTML = sdata;
	} else if (useAlerter && showAlertMessage) {
		alert(sdata);
	}
}

function getHiddenContent(tagName, textToGet) { //coded by ndthuan05@yahoo.com
	var startTagIndex = textToGet.indexOf('<!--' + tagName + '=') + tagName.length + 5;
	var endTagIndex = textToGet.indexOf('***' + tagName + '-->', startTagIndex);
	var content = textToGet.substring(startTagIndex, endTagIndex);

	return content;
}
function crossObj(id){ //coded by ndthuan05@yahoo.com
	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;
	}
}

function getO(id) {
	return crossObj(id);
}

function hideO(id) {
	getO(id).style.display = 'none';
}

function showO(id) {
	getO(id).style.display = '';
}

function permuteObj(id1, id2) { //coded by ndthuan05@yahoo.com
	var html = crossObj(id1).innerHTML;
	crossObj(id1).innerHTML = crossObj(id2).innerHTML;
	crossObj(id2).innerHTML = html;
}

var imgPlus, imgSub;
function preLoadImg() { // Coded by ndthuan05@yahoo.com
	imgPlus = new Image();
	imgPlus.src = 'images/plus.gif';
	imgSub = new Image();
	imgSub.src = 'images/sub.gif';
}

function setLocation(url) {
	if (url) self.location.href = url;
}

function openWin(url, winName, features) {
	window.open(url, winName, features);
}

function showLoggedPageContent(logID) {
	var rowContent = loggedPages[logID].split('*****');
	crossObj('SPMusic_MainContent').innerHTML = rowContent[2];
}

function page_Load() { // Coded by ndthuan05@yahoo.com
	preLoadImg();
	locatePlaylistPosition();
	locateLoadingMessagePosition();
	locateLoggedPagesPosition();
	if ((self.location.href).indexOf('#') == -1) self.location.href = self.location.href + '#show=home';
	callServer('showGenreList.asp?parent=0', null, 'GET', 'showGenreList(true);');
}

function toggleObj(imgID, divID) { // Coded by ndthuan05@yahoo.com
	if (crossObj(divID).style.display == 'none') {
		crossObj(divID).style.display = '';
		if (imgID) crossObj(imgID).src = imgSub.src;
	} else {
		crossObj(divID).style.display = 'none';
		if (imgID) crossObj(imgID).src = imgPlus.src;
	}
}