var http = createRequestObject();
var lastUrl = '';
var current_url = '';
var field = '';
var interval = '';


function createRequestObject() {
	var xmlhttp;
	try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }
	catch(e) {
    try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	catch(f) { xmlhttp=null; }
  }
  if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
	xmlhttp=new XMLHttpRequest();
  }
	return  xmlhttp;
}

function do_search(mainURL) {
	kw = document.getElementById("keyword").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 = 'song'; break;
			case 'singer' : type = 'singer'; break;
			case 'album' : type = 'album'; break;
		}
		last_url = '';
		window.location.href = '/index.php?go=search&keyword='+kw+'&type='+type+'#ListSearch';
	}
	return false;
}
function do_templates() {
	temp_id = document.getElementById("template").value;
	window.location.href = mainURL+'index.php?template='+temp_id;
	return false;
}
// + ---------------------- +
// |        PLAYLIST        |
// + ---------------------- +

function reloadPlaylist(add_id,remove_id) {
	try{
		document.getElementById("playlist_field").innerHTML = loadingText;
		http.open('POST',  mainURL+'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 = '?refresh=1';
		}
  	}
	catch(e){}
	finally{}
}

function login(form) {
name = encodeURIComponent(document.getElementById("name").value);
pwd = encodeURIComponent(document.getElementById("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',  mainURL+'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í thành công");
				window.location.href = mainURL+'index.php';
			}
		}
  	}
	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);
	agree = document.getElementById("agree").checked;
	
	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) == "" )
		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 trang Web");
		else {
			try{
				document.getElementById("reg_loading").innerHTML = loadingText;
				document.getElementById("reg_loading").style.display = "block";
				http.open('POST',  mainURL+'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);
				
			}
			catch(e){}
			finally{}
		}
	return ok;
}


// + ----------------------------- +
// |        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',  mainURL+'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(folder,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 broken(id,mainURL,width,height) {
	popup(mainURL+'brokenlink.php?id='+id,'broken',width,height);
}
function receive_gift(id) {
	window.location.href = mainURL+'index.php?go=gift_receive&id='+id;
}

function showComment(media_id) {
	try {
		
		document.getElementById("comment_field").innerHTML = loadingText;
		document.getElementById("comment_field").style.display = "block";
		http.open('POST',  mainURL+'comment.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("comment_field").innerHTML = http.responseText;
			}
		}
		http.send('showcomment=1&media_id='+media_id+'&pg=1');
	}
	catch(e){}
	finally{}
	return false;
}

function comment_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			var response = http.responseText;
			if (response == 'OK') {
				media_id = encodeURIComponent(document.getElementById("media_id").value);
				showComment(media_id);
			}
			else document.getElementById("comment_loading").innerHTML = response;

		}
  	}
	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 > 255)
		alert("Nội dung cảm nhận quá 255 ký tự.");
	else {
		try {
			document.getElementById("comment_loading").innerHTML = loadingText;
			document.getElementById("comment_loading").style.display = "block";
			http.open('POST',  mainURL+'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_page(media_id,pg) {
	if (confirm("Bạn có muốn sang trang không ?")) {
		document.getElementById("comment_field").innerHTML = loadingText;
		document.getElementById("comment_field").style.display = "block";
		http.open('POST',  mainURL+'comment.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = comment_handleResponse;
		http.send('showcomment=1&media_id='+media_id+'&pg='+pg);
	}
	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 ?")) {
		document.getElementById("comment_loading").innerHTML = loadingText;
		document.getElementById("comment_loading").style.display = "block";
		http.open('POST',  mainURL+'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 gift_check_values() {
	media_id = encodeURIComponent(document.getElementById("media_id").value);
	sender_name = encodeURIComponent(document.getElementById("sender_name").value);
	recip_name = encodeURIComponent(document.getElementById("recip_name").value);
	sender_email = encodeURIComponent(document.getElementById("sender_email").value);
	recip_email = encodeURIComponent(document.getElementById("recip_email").value);
	message = encodeURIComponent(document.getElementById("message").value);
	if(	trim(sender_name) == "" ||	trim(recip_name) == "" ||	trim(sender_email) == "" ||	trim(recip_email) == "" || trim(message) == "" )
		alert("Bạn chưa nhập đầy đủ thông tin");
	else {
		try {
			document.getElementById("gift_loading").innerHTML = loadingText;
			document.getElementById("gift_loading").style.display = "block";
			http.open('POST',  mainURL+'gift.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = gift_handleResponse;
			http.send('gift=1&media_id='+media_id+'&sender_name='+sender_name+'&recip_name='+recip_name+'&sender_email='+sender_email+'&recip_email='+recip_email+'&message='+message);
		}
		catch(e){}
		finally{}
	}
	return false;
}

if (!document.all) 

document.captureEvents(Event.MOUSEMOVE) ;

// On the move of the mouse, it will call the function getPosition 

document.onmousemove = getPosition;  

// These varibles will be used to store the position of the mouse 

var X = 0 

var Y = 0  

// This is the function that will set the position in the above varibles 

function getPosition(args) 

{ 

  // Gets IE browser position 

  if (document.all) 

  { 

    X = event.clientX + document.body.scrollLeft 

    Y = event.clientY + document.body.scrollTop 

  } 

  // Gets position for other browsers 

  else 

  {  

    X = args.pageX 

    Y = args.pageY 

  }  

}
  

 // To enable/disable the background:  

function backgroundFilter() 

{ 

    var div; 

    if(document.getElementById) 

    // Standard way to get element 

    div = document.getElementById('backgroundFilter'); 

    else if(document.all) 

    // Get the element in old IE's 

    div = document.all['backgroundFilter'];    

    // if the style.display value is blank we try to check it out here 

    if(div.style.display== '' && div.offsetWidth != undefined&&div.offsetHeight != undefined) 

    { 

        div.style.display = (div.offsetWidth!=0 && div.offsetHeight!=0)?'block':'none'; 

    } 

    // If the background is hidden ('none') then it will display it ('block'). 

    // If the background is displayed ('block') then it will hide it ('none'). 

    div.style.display = (div.style.display==''||div.style.display=='block')?'none':'block'; 

} 

 // To display/hide the popup:  

function popUp() 

{ 

    var div; 

    if(document.getElementById) 

    // Standard way to get element 

    div = document.getElementById('popupWindow'); 

    else if(document.all) 

    // Get the element in old IE's 

    div = document.all['popupWindow'];    

    // if the style.display value is blank we try to check it out here 

    if(div.style.display== '' && div.offsetWidth != undefined && div.offsetHeight != undefined) 

    { 

        div.style.display = (div.offsetWidth!=0 && elem.offsetHeight!=0)?'block':'none'; 

    } 

    // If the PopUp is hidden ('none') then it will display it ('block'). 

    // If the PopUp is displayed ('block') then it will hide it ('none'). 

    div.style.display = (div.style.display==''||div.style.display=='block')?'none':'block'; 

    // Off-sets the X position by 15px 

    X = X + 5;    

    // Sets the position of the DIV 

    div.style.left = X+'px'; 

    div.style.top = Y+'px'; 

} 
var win = null;
function NewWindow(mypage,myname,w,h,scroll){
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
settings =
'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
win = window.open(mypage,myname,settings)
}

function tabview_aux(TabViewId, id)
{
  var TabView = document.getElementById(TabViewId);

  var Tabs = TabView.firstChild;
  while (Tabs.className != "Tabs" ) Tabs = Tabs.nextSibling;

  var Tab = Tabs.firstChild;
  var i   = 0;

  do
  {
    if (Tab.tagName == "A")
    {
      i++;
      Tab.href      = "javascript:tabview_switch('"+TabViewId+"', "+i+");";
      Tab.className = (i == id) ? "Active" : "";
      Tab.blur();
    }
  }
  while (Tab = Tab.nextSibling);

  var Pages = TabView.firstChild;
  while (Pages.className != 'Pages') Pages = Pages.nextSibling;

  var Page = Pages.firstChild;
  var i    = 0;

  do
  {
    if (Page.className == 'Page')
    {
      i++;
      if (Pages.offsetHeight) Page.style.height = (Pages.offsetHeight-2)+"px";
      Page.style.overflow = "auto";
      Page.style.display  = (i == id) ? 'block' : 'none';
    }
  }
  while (Page = Page.nextSibling);
}

function tabview_switch(TabViewId, id) { tabview_aux(TabViewId, id); }

function tabview_initialize(TabViewId) { tabview_aux(TabViewId,  1); }



// BEGIN RATING
function Rating(media_id,star) {
	try {
		hide_rating_process();
		http.open('POST',  mainURL + '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;
}


	// pre-fetch image
	(new Image()).src = RATE_OBJECT_IMG;
	(new Image()).src = RATE_OBJECT_IMG_HALF;
	(new Image()).src = RATE_OBJECT_IMG_BG;

	function show_star(starNum) {
		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 show_rating_process() {
		document.getElementById("rating_process").style.display = "block";
		document.getElementById("rate_s").style.display = "none";
	}
	function hide_rating_process() {
		document.getElementById("rating_process").style.display = "none";
		document.getElementById("rate_s").style.display = "block";
	}

// END RATING

// recent post

function showPost(media_id) {
	try {
		document.getElementById("randompost_field").innerHTML = loadingText;
		document.getElementById("randompost_field").style.display = "block";
		http.open('POST',  mainURL+'randompost.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("randompost_field").innerHTML = http.responseText;
			}
		}
		http.send('showPost=1&media_id='+media_id);
	}
	catch(e){}
	finally{}
	return false;
}
if(window.location.host.search('www') == 0)window.location.replace(window.location.href.replace('http://www.','http://'));

