var http = createRequestObject();
var lastUrl = '';
var current_url = '';
var field = '';
var interval = '';
var myplayer = '';
var player_field = 0;
var player_form = '';
var loading = 0;
var loadingText = '<span style="text-align:center; font-weight:bold;"><img src="templates/skin/img/loading.gif" alt="" />&nbsp;Đang tải dữ liệu ...</span>';

function createRequestObject() {
    if (typeof XMLHttpRequest != "undefined") {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
      var aVersions = [ "MSXML2.XMLHttp.5.0",
        "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
        "MSXML2.XMLHttp","Microsoft.XMLHttp"
      ];
    for (var i = 0; i < aVersions.length; i++) {
        try {
            var oXmlHttp = new ActiveXObject(aVersions[i]);
            return oXmlHttp;
        } catch (oError) {
            oXmlHttp=null;
        }
      }
    }
    throw new Error("XMLHttp object could be created.");
}

function sendRequest(current_url,act) {
	try{
		if (act == 'Play' || act == 'Gift' || act == 'Play_Album' || act == 'Play_Singer' || act == 'Play_Playlist'){
			field = document.getElementById("playing_field");
		if (act == 'Play_Album' || act == 'Play_Singer' || act == 'Play_Playlist') player_field = 1;
		}else field = document.getElementById("data_field");
		if (loading==0) {
			loading=1;
			show_Loading();
		}
		current_url = encodeURIComponent(current_url);
		http.open('POST',  'index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponse;
		http.send('url='+current_url);
	}
	catch(e){}
	finally{}
}
function handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			response = http.responseText;
			if (current_url.indexOf('Play') != -1 || current_url.indexOf('Gift') != -1) {
				field.style.marginBottom = "10";
			}
			field.innerHTML = response;
			hide_Loading();
            loading=0;
			field.style.display = "";
			field.scrollIntoView();
			if(player_field==1) {
				alert("Ấn OK để tiếp tục !!!!");
			 	if(myplayer = document.getElementById('myplayer')) {
				myplayer.addModelListener('STATE','auto_change_song');
				}
			  player_field = 0;
			}
		}
  	}
	catch(e){}
	finally{}
}
// + ---------------------- +
// |  BEGIN Loading Alpha   |
// + ---------------------- +
var ie45,ns6,ns4,dom;
if (navigator.appName=="Microsoft Internet Explorer") ie45=parseInt(navigator.appVersion)>=4;
else if (navigator.appName=="Netscape"){  ns6=parseInt(navigator.appVersion)>=5;  ns4=parseInt(navigator.appVersion)<5;}
dom=ie45 || ns6;
var timershow=false;
var curx=-200;
var cury=100;
var win_w=window.innerWidth ? window.innerWidth : document.body.offsetWidth;
var mid_w=win_w/2;
var timershow1=window.setInterval("stayMiddle()",1);
function getobj(id) {
el = document.all ? document.all[id] :   dom ? document.getElementById(id) :   document.layers[id];
return el;
}
function show_Loading() {
	obj = getobj('LoadingDiv');
	if (timershow) window.clearTimeout(timershow);
	timershow=window.setInterval("nshow()",1);
}
function hide_Loading() {
	obj = getobj('LoadingDiv');
	if (timershow) window.clearTimeout(timershow);
	timershow=window.setInterval("nhide()",1);
}
function moveobj(obj,x,y) {
	obj.style.left=x + "px";
	obj.style.top=y+ "px";
	curx=x;
	cury=y;
}
function stayMiddle() {
	if (document.documentElement && document.documentElement.scrollTop)
		var pY =  document.documentElement.scrollTop;
	else if (document.body)
		var pY =  document.body.scrollTop;
	obj = getobj('LoadingDiv');
	newy = cury+((pY-cury)/16)+12;
	moveobj(obj,curx, newy);
}

function nshow() {
	obj = getobj('LoadingDiv');
	newx = curx+((mid_w-curx)/16)-7;
	moveobj(obj,newx, cury);
}
function nhide() {
	obj = getobj('LoadingDiv');
	newx = curx+((0-curx)/16)-15;
	moveobj(obj,newx, cury);
}
// + ---------------------- +
// |    END Loading Alpha   |
// + ---------------------- +  
function getVar(url,cnt)
{
	url=url+'#';
	url=url.split('#');
	if (!url[1]) window.location.href = '#Home';
	url=url[1];
	url=url+'/';
	url=url.split('/');
	if (url[0] == 'Logout')
		window.location.href = '/ring/?refresh=1';
	if (cnt != -1) {
		url=url[cnt];
		if (!url) return '';
	}
	return url;
}

function loadPage() {
	act = getVar(window.location.href,0);
	if (act) {
		current_url=window.location.href;
		current_url=current_url+'#';
		current_url=current_url.split('#');
		current_url=current_url[1];
		if (current_url) sendRequest(current_url,act);
	}
}

function urlCheck()
{
	url=window.location.href;
	if (url.indexOf('?') != -1 ) {
		url_temp = url.split('?');
		clearInterval(interval);
		href = url_temp[0];
		
		url=url+'#';
		url=url.split('#');
		if (url[1]) window.location.href = href+'#'+url[1];
		return;
	}
	if (url != '' && url!=lastUrl)
	{
		loadPage();
		lastUrl=url;
	}
}

function startLoad() {
	interval = setInterval('urlCheck()',100);
}

function alertBrokenLink(id) {
	if (confirm("Báo Link nhạc này đã hỏng ?")) {
		try{
			http.open('POST',  'index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = BrokenResponse;
			http.send('url=Broken,'+id);
		}
		catch(e){}
		finally{}
	}
}

function BrokenResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			response = http.responseText;
			if (response == 1) alert("Thông báo đã được gởi đi. Cám ơn bạn đã báo cho chúng tôi.");
			else alert("Lỗi. Mong bạn thử lại.");
		}
  	}
	catch(e){
		alert("Lỗi. Mong bạn thử lại.");
	}
	finally{}
}
function do_search() {
	kw = document.getElementById("keyword_1").value;
	if (!kw) alert('Bạn chưa nhập từ khóa');
	else {
		kw = encodeURIComponent(kw);
		s_type = document.getElementById("searchType");
		type = s_type.options[s_type.selectedIndex].value;
		switch (type) {
			case 'song' : type = 1; break;
			case 'singer' : type = 2; break;
			case 'album' : type = 3; break;
			case 'user' : type = 6; break;
		}
		last_url = '';
		window.location.href = '#Search/'+type+'/'+kw+'/1';
	}
	
	return false;

}
// + ---------------------- +
// |        PLAYLIST        |
// + ---------------------- +

function reloadPlaylist(add_id,remove_id) {
	try{
		document.getElementById("playlist_field").innerHTML = loadingText;
		http.open('POST',  'index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = playlist_handleResponse;
		http.send('reloadPlaylist=1&add_id='+add_id+'&remove_id='+remove_id);
	}
	catch(e){}
	finally{}
}

function playlist_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			var response = http.responseText;
			document.getElementById("playlist_field").innerHTML = response;
		}
  	}
	catch(e){}
	finally{}
}

function addToPlaylist(song_id)
{
	reloadPlaylist(song_id,0);
}
function removeFromPlaylist(song_id)
{
	reloadPlaylist(0,song_id);
}

/*------------------------------------------------------*/


function trim(a) {
	return a.replace(/^s*(S*(s+S+)*)s*$/, "$1");
}

// + ------------------- +
// |        LOGIN        |
// + ------------------- +

function login_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			document.getElementById("login_loading").style.display = "none";
			var response = http.responseText;
			if (response) {
				document.getElementById("login_loading").innerHTML = response;
				document.getElementById("login_loading").style.display = "block";
			}
			else window.location.href = '/ring/?refresh=1';
		}
  	}
	catch(e){}
	finally{}
}

function login(form) {
	name = encodeURIComponent(document.getElementById("login_name").value);
	pwd = encodeURIComponent(document.getElementById("login_pwd").value);
	if(	trim(name) == "" ||	trim(pwd) == "")
		alert("Bạn chưa nhập đầy đủ thông tin");
	else {
		try{
			document.getElementById("login_loading").innerHTML = loadingText;
			document.getElementById("login_loading").style.display = "block";
			http.open('POST',  'index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = login_handleResponse;
			http.send('login=1&name='+name+'&pwd='+pwd);
			
		}
		catch(e){}
		finally{}
	}
	return false;
}

// + ---------------------- +
// |        REGISTER        |
// + ---------------------- +

function reg_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			document.getElementById("reg_loading").style.display = "none";
			var response = http.responseText;
			if (response) {
				document.getElementById("reg_loading").innerHTML = response;
				document.getElementById("reg_loading").style.display = "block";
			}
			else {
				alert("Bạn đã đăng kí là thành viên Kho Nhạc Chuông");
				window.location.href = '/ring/?refresh=1';
			}
		}
  	}
	catch(e){}
	finally{}
}

function reg_check_values() {
	ok = false;
	name = encodeURIComponent(document.getElementById("reg_name").value);
	pwd = encodeURIComponent(document.getElementById("reg_pwd").value);
	pwd2 = encodeURIComponent(document.getElementById("reg_pwd2").value);
	email = encodeURIComponent(document.getElementById("reg_email").value);
	sec_num = encodeURIComponent(document.getElementById("sec_num").value);
	agree = document.getElementById("agree").checked;
	u_area = document.getElementById("u_area");
	area = u_area.options[u_area.selectedIndex].value;
	yahoo = encodeURIComponent(document.getElementById("reg_yahoo").value);
	s = document.getElementsByName("reg_sex");
	if (s[0].checked) sex = s[0].value;
	if (s[1].checked) sex = s[1].value;
	
	if(	trim(name) == "" ||	trim(pwd) == "" ||	trim(pwd2) == "" ||	trim(email) == "" ||	trim(yahoo) == "" )
		alert("Bạn chưa nhập đầy đủ thông tin");
	else
		if (pwd != pwd2) alert("Xác nhận mật khẩu không chính xác");
		else if (!agree) alert("Bạn chưa đồng ý với các quy định của Kho Nhạc Chuông");
		else {
			try{
				document.getElementById("reg_loading").innerHTML = loadingText;
				document.getElementById("reg_loading").style.display = "block";
				http.open('POST',  'index.php');
				http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				http.onreadystatechange = reg_handleResponse;
			http.send('reg=1&name='+name+'&pwd='+pwd+'&email='+email+'&sex='+sex+'&sec_num='+sec_num+'&area='+area+'&yahoo='+yahoo);
				
			}
			catch(e){}
			finally{}
		}
	return ok;
}

// + ------------------------- +
// |        CHANGE INFO        |
// + ------------------------- +

function change_info_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			document.getElementById("change_info_loading").style.display = "none";
			var response = http.responseText;
			if (response) {
				document.getElementById("change_info_loading").innerHTML = response;
				document.getElementById("change_info_loading").style.display = "block";
			}
			else window.location.href = '/ring/?refresh=1';
		}
  	}
	catch(e){}
	finally{}
}

function change_info() {
	email = encodeURIComponent(document.getElementById("u_email").value);
	oldpwd = encodeURIComponent(document.getElementById("u_oldpwd").value);
	newpwd_1 = encodeURIComponent(document.getElementById("u_newpwd_1").value);
	newpwd_2 = encodeURIComponent(document.getElementById("u_newpwd_2").value);
	avatar_input = encodeURIComponent(document.getElementById("u_avatar_input").value);
    avatar_local = encodeURIComponent(document.getElementById("u_avatar_local").value);
	h_sex = document.getElementById("hide_sex").checked;
	h_email = document.getElementById("hide_email").checked;
	yim = encodeURIComponent(document.getElementById("u_yim").value);
	u_area = document.getElementById("u_area");
	area = u_area.options[u_area.selectedIndex].value;
	s = document.getElementsByName("u_sex");
	if (s[0].checked) sex = s[0].value;
	if (s[1].checked) sex = s[1].value;
	if(	trim(email) == "" )
		alert("Bạn chưa nhập đầy đủ thông tin");
	else if (newpwd_1 != newpwd_2)
		alert("Xác nhận mật khẩu không đúng");
	else {
		try{
			document.getElementById("change_info_loading").innerHTML = loadingText;
			document.getElementById("change_info_loading").style.display = "block";
			http.open('POST',  'index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = change_info_handleResponse;
			http.send('change_info=1&email='+email+'&oldpwd='+oldpwd+'&newpwd='+newpwd_1+'&sex='+sex+'&hide_sex='+h_sex+'&hide_email='+h_email+'&avatar_input='+avatar_input+'&avatar_local='+avatar_local+'&u_area='+area+'&yim='+yim);
		}
		catch(e){}
		finally{}
	}
	return false;
}

// + ----------------------------- +
// |        FORGOT PASSWORD        |
// + ----------------------------- +

function forgot_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			document.getElementById("forgot_loading").style.display = "none";
			var response = http.responseText;
			if (response) {
				document.getElementById("forgot_loading").innerHTML = response;
				document.getElementById("forgot_loading").style.display = "block";
			}
		}
  	}
	catch(e){}
	finally{}
}

function forgot() {
	email = encodeURIComponent(document.getElementById("u_email").value);
	if(	trim(email) == "" )	alert("Bạn chưa nhập email");
	else {
		try{
			document.getElementById("forgot_loading").innerHTML = loadingText;
			document.getElementById("forgot_loading").style.display = "block";
			http.open('POST',  'index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = forgot_handleResponse;
			http.send('forgot=1&email='+email);
		}
		catch(e){}
		finally{}
	}
	return false;
}

function popup(url,wdname,width,height)
{
	if (width == null)  { width  = 200; }   // default width
	if (height == null) { height = 400; }   // default height
	newwin=window.open(url,wdname,'fullscreen=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width='+width+',height='+height);
	if (document.all)
	{
		newwin.moveTo(0,0);
		newwin.focus();
	}
}

function gift(id,width,height) {
	popup('/ring/gift.php?id='+id,'gift',width,height);
}
function receive_gift(id) {
	window.location.href = '#Gift/'+id;
}
// + ---------------------- +
// |        COMMENT         |
// + ---------------------- +
function showComment(media_id,pg) {
 try {
  //document.getElementById("loading_comment").innerHTML = loadingText;
  document.getElementById("loading_comment").style.display = "block";
  if (loading==0) {
			loading=1;
			show_Loading();
		}
  http.open('POST',  'comment.php');
  http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  http.onreadystatechange = function() {
   if((http.readyState == 4)&&(http.status == 200)){
    //document.getElementById("loading_comment").style.display = "none";
    document.getElementById("comment_field").innerHTML = http.responseText;
    			hide_Loading();
				loading=0;
   		}
  }
  http.send('lvcomment=1&media_id='+media_id+'&pg='+pg);
 }
 catch(e){}
 finally{}
 return false;
}
function comment_handleResponse() {
 try {
 	if((http.readyState == 4)&&(http.status == 200)){
   	document.getElementById("comment_loading").style.display = "none";
   	hide_Loading();
   	loading=0;
   	var response = http.responseText;
   	if (response != 1) {
   		media_id = encodeURIComponent(document.getElementById("media_id").value);
		showComment(media_id,'1');
   		}
   		//else document.getElementById("comment_loading").innerHTML = response;
   		else alert("Bạn hãy đợi vài giây nữa mới có thể viết tiếp");
   	  }
  }
 catch(e){}
 finally{}
}
function comment_check_values() {
 media_id = encodeURIComponent(document.getElementById("media_id").value);
 comment_content = encodeURIComponent(document.getElementById("comment_content").value);
 if(trim(comment_content) == "")
  alert("Bạn chưa nhập cảm nhận");
 else if (comment_content.length > 250)
  alert("Nội dung cảm nhận quá 250 ký tự.");
 else {
  try {
   //document.getElementById("loading_comment").innerHTML = loadingText;
   //document.getElementById("loading_comment").style.display = "block";
   		if (loading==0) {
		loading=1;
		show_Loading();
		}
   		http.open('POST',  'comment.php');
  		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   		http.onreadystatechange = comment_handleResponse;
	   	http.send('comment=1&media_id='+media_id+'&comment_content='+comment_content);
  }
  catch(e){}
  finally{}
 }
 return false;
}
function comment_delete(media_id,comment_id) {
 		if (confirm("Bạn có muốn xóa cảm nhận này không ?")) {
 			if (loading==0) {
			loading=1;
			show_Loading();
		}
  //document.getElementById("comment_loading").innerHTML = loadingText;
  //document.getElementById("comment_loading").style.display = "block";
  		http.open('POST',  'comment.php');
  		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  		http.onreadystatechange = comment_handleResponse;
  		http.send('delete=1&media_id='+media_id+'&comment_id='+comment_id);
 	}
 return false;
}
function textCounter(field, countfield, maxlimit) {
  if (field.value.length > maxlimit)
      {field.value = field.value.substring(0, maxlimit);}
      else
      {countfield.value = maxlimit - field.value.length;}
  }
// + ---------------------- +
// |        REQUEST         |
// + ---------------------- +
function rq_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			document.getElementById("reg_loading").style.display = "none";
			var response = http.responseText;
			if (response) {
				document.getElementById("reg_loading").innerHTML = response;
				document.getElementById("reg_loading").style.display = "block";
			}
			else {
				alert("Yêu Cầu Của Bạn Đã Đươc Gửi");
				document.getElementById("r_title").value = '';
				document.getElementById("r_singer").value = '';
				document.getElementById("r_album").value = '';
				document.getElementById("lyric").value = '';
			}
		}
  	}
	catch(e){}
	finally{}
}
function request_check_values() {
	r_title = encodeURIComponent(document.getElementById("r_title").value);
	r_singer = encodeURIComponent(document.getElementById("r_singer").value);
	r_album = encodeURIComponent(document.getElementById("r_album").value);
	r_lyric = encodeURIComponent(document.getElementById("r_lyric").value);
	r_email = encodeURIComponent(document.getElementById("r_email").value);

	if(	trim(r_title) == "" || trim(r_email) == "")
		alert("Bạn chưa nhập đầy đủ thông tin (*)");
	else {
		try{
			document.getElementById("reg_loading").innerHTML = loadingText;
			document.getElementById("reg_loading").style.display = "block";
			http.open('POST',  'index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
			http.onreadystatechange = rq_handleResponse;
			http.send('request=1&r_title='+r_title+'&r_singer='+r_singer+'&r_album='+r_album+'&r_lyric='+r_lyric+'&r_email='+r_email);
		}
		catch(e){}
		finally{}
	}
	return false;
}
// + ---------------------------------- +
// |            RATING                  |
// + ---------------------------------- +
function Rating(media_id,star) {
	try {
		document.getElementById("rate_s").innerHTML = loadingText;
		document.getElementById("rate_s").style.display = "block";
		hide_rating_process();
		http.open('POST',  'rating.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("rating_field").innerHTML = http.responseText;
			}
		}
		http.send('rating=1&media_id='+media_id+'&star='+star);
	}
	catch(e){}
	finally{}
	return false;
}
	(new Image()).src = RATE_OBJECT_IMG;
	(new Image()).src = RATE_OBJECT_IMG_HALF;
	(new Image()).src = RATE_OBJECT_IMG_BG;
	function show_star(starNum,rate_text) {
		document.getElementById("rate_text_d").innerHTML = rate_text;
		remove_star();
		full_star(starNum);
	}
	function full_star(starNum) {
		for (var i=0; i < starNum; i++)
			document.getElementById('star'+ (i+1)).src = RATE_OBJECT_IMG;
	}
	function remove_star() {
		for (var i=0; i < 5; i++)
			document.getElementById('star' + (i+1)).src = RATE_OBJECT_IMG_BG; // RATE_OBJECT_IMG_REMOVED;
	}
	function remove_all_star() {
		for (var i=0; i < 5; i++) document.getElementById('star' + (i+1)).src = RATE_OBJECT_IMG_BG; // RATE_OBJECT_IMG_REMOVED;
		document.getElementById("rate_text_d").innerHTML = 'Bình Chọn';
	}
	function show_rating_process() {
		if(document.getElementById("rating_process")) document.getElementById("rating_process").style.display = "block";
		if(document.getElementById("rate_s")) document.getElementById("rate_s").style.display = "none";
	}
	function hide_rating_process() {
		if(document.getElementById("rating_process")) document.getElementById("rating_process").style.display = "none";
		if(document.getElementById("rate_s")) document.getElementById("rate_s").style.display = "block";
	}
// + ---------------------- +
// |        LYRIC           |
// + ---------------------- +
function ht_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			document.getElementById("reg_loading").style.display = "none";
			var response = http.responseText;
			if (response) {
				document.getElementById("reg_loading").innerHTML = response;
				document.getElementById("reg_loading").style.display = "block";
			}
			else {
				alert("Lời bài nhạc chuông Đã Đươc Gửi");
				document.getElementById("t_title").value = '';
				
			}
		}
  	}
	catch(e){}
	finally{}
}
function lyric_check_values() {
	t_title = encodeURIComponent(document.getElementById("t_title").value);
	t_lyric = encodeURIComponent(document.getElementById("t_lyric").value);
	t_email = encodeURIComponent(document.getElementById("t_email").value);
	if(	trim(t_title) == "" || trim(t_email) == "")
		alert("Bạn chưa nhập đầy đủ thông tin (*)");
	else {
		try{
			document.getElementById("reg_loading").innerHTML = loadingText;
			document.getElementById("reg_loading").style.display = "block";
			http.open('POST',  'index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
			http.onreadystatechange = ht_handleResponse;
			http.send('lyric=1&t_title='+t_title+'&t_lyric='+t_lyric+'&t_email='+t_email);
		}
		catch(e){}
		finally{}
	}
	return false;
}
function show_Loading() {
	obj = getobj();
	if (timershow) window.clearTimeout(timershow);
	timershow=window.setInterval("nshow()",1);
}
function hide_Loading() {
	obj = getobj();
	if (timershow) window.clearTimeout(timershow);
	timershow=window.setInterval("nhide()",1);
}
function show_relate(type,id,page) {
	try {
		document.getElementById("relate_"+type+"_field").style.display = "block";
		if (loading==0) {
			loading=1;
			show_Loading();
		}
		http.open('POST',  'index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("relate_"+type+"_field").innerHTML = http.responseText;
				hide_Loading();
				loading=0;
			}
		}
		http.send('relate=1&'+type+'='+id+'&pg='+page);
		}
	catch(e){}
	finally{}
	return false;
}
// + ---------------------------------- +
// |         PLAY WMP - ALBUM           |
// + ---------------------------------- +
function changeplayer(type,id,w,h) {
	document.getElementById("old_player_field").innerHTML = '';
	document.getElementById("new_player_field").innerHTML = loadingText;
    document.getElementById("new_player_field").style.display = "block";
	var player = '<OBJECT type="application/x-oleobject" CLASSID="CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" width="'+w+'" height="100">';
	player += '<param name="url" value="ring/mtv/asx.php?id='+type+','+id+'">';
 	player += '<param name="playCount" value="200">';
	player += '<param name="EnableContextMenu" value="0">';
	player += '<param name="volume" value="100">';
	player += '<embed type="application/x-mplayer2" quality="high" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" file="ring/mtv/asx.php?id='+type+','+id+'" src="ring/mtv/asx.php?id='+type+','+id+'" width="'+w+'" height="100" AutoStart="1" EnableContextMenu="0" Mute="0" ShowStatusBar="1">';
	player += '</OBJECT>';
	player += '<br /><br /><a style="cursor: pointer" onclick="return returnplayer('+type+','+id+','+w+','+h+')"><b>Nhấn vào đây để nghe bằng Flash Player</b></a>';
	document.getElementById("new_player_field").innerHTML = player;
}
function returnplayer(type,id,w,h) {
	document.getElementById("new_player_field").innerHTML = '';
	document.getElementById("old_player_field").innerHTML = loadingText;
    document.getElementById("old_player_field").style.display = "block";
	var player = '<embed src="ring/mediaplayer.swf" id="player" name="player" allowfullscreen="true" allowscriptaccess="always" bgcolor="#66CC99" flashvars="&file=ring/mtv/xml.php?id='+type+','+id+'&bgcolor=#66CC99&autostart=true&repeat=always&skin=m/black.swf" height="60" width="'+w+'">';
	player += '<br /><br /><a style="cursor: pointer" onclick="return changeplayer('+type+','+id+','+w+','+h+')">';
	document.getElementById("old_player_field").innerHTML = player;
}
// + ---------------------------------- +
// |      CHANGE SINGER - ALBUM         |
// + ---------------------------------- +
function change_vnsinger(page) {
	try {
		document.getElementById("vnsinger").innerHTML = loadingText;
		document.getElementById("vnsinger").style.display = "block";
		http.open('POST',  'index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("vnsinger").innerHTML = http.responseText;
			}
		}
		http.send('vnsinger=1&pg='+page);
		}
	catch(e){}
	finally{}
	return false;
}
function change_new_album(page) {
	try {
		document.getElementById("new_album").innerHTML = loadingText;
		document.getElementById("new_album").style.display = "block";
		http.open('POST',  'index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("new_album").innerHTML = http.responseText;
			}
		}
		http.send('top_album=1&pg='+page);
		}
	catch(e){}
	finally{}
	return false;
}
// + ------------------------------------------------- +
// |    CHANGE SONGS PLAY SINGER - ALBUM - PLAYLIST    |
// + ------------------------------------------------- +
function auto_change_song(obj) {
	var next_id;
	var i=0;
	if(obj.newstate=='COMPLETED') {
		var currentid = document.getElementById("current_id").value;
		var check = document.getElementsByName('check');
		while(currentid != check[i].value && i<=(check.length-1)) i++;
		if(i == (check.length-1)) next_id =check[0].value;
		else next_id = check[i+1].value;
		manual_change_song(next_id);
	}
	return false;
}
function manual_change_song(id) {
		var xml_file = 'mtv/xml.php?id=6,'+id;
		var current_id = document.getElementById("current_id").value;
		document.getElementById("zlist_"+current_id).style.background="#FFFFFF";
		document.getElementById("zlist_"+id).style.background="#00CCFF";
		document.getElementById("current_id").value = id;
		myplayer.sendEvent('LOAD',xml_file);
}
// + ---------------------- + 
// |        POST MEDIA      | 
// + ---------------------- + 
function post_handleResponse() { 
    try { 
        if((http.readyState == 4)&&(http.status == 200)){ 
            document.getElementById("post_loading").style.display = "none"; 
            var response = http.responseText; 
            if (response) { 
                document.getElementById("post_loading").innerHTML = response; 
                document.getElementById("post_loading").style.display = "block"; 
                document.getElementById("submit").disabled=false;document.getElementById("submit").value="Đăng lại"; 
            } 
            else window.location.href = '#Media_Post'; 
        } 
      } 
    catch(e){} 
        finally{} 
} 

function post_media() { 
    song = encodeURIComponent(document.getElementById("post_song").value); 
    artist = encodeURIComponent(document.getElementById("post_artist").value); 
    album = encodeURIComponent(document.getElementById("post_album").value); 
    cat = encodeURIComponent(document.getElementById("post_cat").value); 
    lyric = encodeURIComponent(document.getElementById("post_lyric").value); 
    url = encodeURIComponent(document.getElementById("post_url").value); 
    if(    trim(song) == "" || trim(url) == "") 
        alert("Bạn chưa nhập đầy đủ thông tin (*)"); 
    else { 
        try{ 
            document.getElementById("post_loading").innerHTML = loadingText; 
            document.getElementById("post_loading").style.display = "block"; 
            http.open('POST',  'index.php'); 
            http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); 
            http.onreadystatechange = post_handleResponse; 
            http.send('post=1&song='+song+'&artist='+artist+'&cat='+cat+'&lyric='+lyric+'&url='+url+'&album='+album);
        document.getElementById("submit").disabled=true;document.getElementById("submit").value="Wait ..."; 
        } 
        catch(e){} 
        finally{} 
    } 
    return false; 
}
function show_download(media_id) {
	try{
		if (loading==0) {
			loading=1;
			show_Loading();
		}
		http.open('POST',  'index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				DragHandler.attach(document.getElementById('field_download'));
				document.getElementById("field_download").style.display = 'block';
				document.getElementById("field_download").innerHTML = http.responseText;
				hide_Loading();
				loading=0;
			}
		}
		http.send('Download='+media_id);
	}
	catch(e){}
	finally{}
	return false;
}
function go_play(link_play){web_url = link_play;document.location.href = web_url+window.location.hash;document.getElementById('thePlayerId').sendEvent('stop');}