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 handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			response = http.responseText;
			field.innerHTML = response;
			field.scrollIntoView();
			if(!response) window.location.href = url;
		}
  	}
	catch(e){}
	finally{}
}
function wait() {
	document.getElementById("wait").innerHTML = "<img src='http://ratvui.info/mp3/images/loading.gif'> Đang xư lý dử liệu ... !";
}
function showRelate(num,album_id,page) { 
	field = document.getElementById("album_load");
	field.innerHTML = loadingText;
	http.open('POST',  mainURL+'/index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
    http.send('showrelate=1&album_id='+album_id+'&num='+num+'&page='+page); 
  	return false;
} 

function showRelateSinger(num,singer_id,page) { 
	field = document.getElementById("singer_load");
	field.innerHTML = loadingText;
	http.open('POST',  mainURL+'/index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
    http.send('showrelatesinger=1&singer_id='+singer_id+'&num='+num+'&page='+page); 
  	return false;
}
function showRs(num,singer_id,page) { 
	field = document.getElementById("rs_load");
	field.innerHTML = loadingText;
	http.open('POST',  mainURL+'/index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
    http.send('showrs=1&singer_id='+singer_id+'&num='+num+'&page='+page); 
  	return false;
}
function showRt(num,cat_id,page) { 
	field = document.getElementById("rt_load");
	field.innerHTML = loadingText;
	http.open('POST',  mainURL+'/index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
    http.send('showrt=1&cat_id='+cat_id+'&num='+num+'&page='+page); 
  	return false;
} 
function showCamnhan(num,song_id,page) { 
	field = document.getElementById("cam_nhan");
	field.innerHTML = loadingText;
	http.open('POST',  mainURL+'/index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
    http.send('showcamnhan=1&song_id='+song_id+'&num='+num+'&page='+page); 
  	return false;
}
function showTop(num,page,number,apr) { 
    field = document.getElementById(num);
	field.innerHTML = loadingText;
	http.open('POST', mainURL+'/index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
    http.send('showtop=1&num='+num+'&page='+page+'&number='+number+'&apr='+apr); 
  	return false; 
}
function do_search(folder) {
	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 = 1; break;
			case 'singer' : type = 2; break;
			case 'album' : type = 3; break;
			case 'video' : type = 4; break;
		}
		last_url = '';
		if (type==4) 
		window.location.href = 'http://ratvui.info/mp3/index.php?go=search&keyword='+kw+'&type=video';
		else
		window.location.href = 'http://ratvui.info/mp3/index.php?go=search&keyword='+kw+'&type='+type+'';
	}
	return false;
}


function do_templates(folder) {
	temp_id = document.getElementById("template").value;
	window.location.href = mainURL+'index.php?template='+temp_id;
	return false;
}
// + ---------------------- +
// |        PLAYLIST        |
// + ---------------------- +
function guestPlaylist(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('guestPlaylist=1&add_id='+add_id+'&remove_id='+remove_id);
	}
	catch(e){}
	finally{}
}

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(login,song_id)
{
	if(login) {
		reloadPlaylist(song_id,0);
		openPopup('Bài hát đã được thêm vào hộp nhạc của bạn .');
	}
	else {
		guestPlaylist(song_id,0);
		openPopup('Bài hát đã được thêm vào hộp nhạc của bạn .');
	}
}
function removeFromPlaylist(login,song_id)
{
	if(login) {
		reloadPlaylist(0,song_id);
		openPopup('Bài hát đã được xóa khỏi hộp nhạc của bạn .');
	}
	else {
		guestPlaylist(0,song_id);
		openPopup('Bài hát đã được xóa khỏi hộp nhạc của bạn .');
	}
}

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


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 
			openPopup('Bạn đã đăng nhập thành công.');
			window.location.href = 'javascript:history.go(-0)';
		}
  	}
	catch(e){}
	finally{}
}

function login(form) {
name = encodeURIComponent(document.getElementById("name").value);
pwd = encodeURIComponent(document.getElementById("pwd").value);
	if(	trim(name) == "" ||	trim(pwd) == "")
		openPopup('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 {
				openPopup('Chúc mừng bạn 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) == "" )
		openPopup('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) == "" )	openPopup('Bạn chưa nhập đỉa chỉ email của bạn.');
	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,folder,width,height) {
	popup(mainURL+'brokenlink.php?id='+id,'broken',width,height);
}
function receive_gift(id) {
	window.location.href = mainURL+'player-play.php?go=gift_receive&id='+id;
}

function comment_check_values() {
	media_id = encodeURIComponent(document.getElementById("media_id").value);
	comment_content = encodeURIComponent(document.getElementById("comment_content").value);
	if(trim(comment_content) == "")
		openPopup('Bạn chưa nhập nội dung bình luận .');
	else if (comment_content.length > 255)
		openPopup('Nội dung bình luận của bạn quá 225 ký tự .');
	else {
		try {
			field = document.getElementById("comment_loading");
			field.innerHTML = loadingText;
			//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 = handleResponse;
			http.send('comment=1&media_id='+media_id+'&comment_content='+comment_content);
			openPopup('Bình luận của bạn đã được đăng .');
		}
		catch(e){}
		finally{}
	}
	return false;
}

function comment_album_check_values() {
	media_id = encodeURIComponent(document.getElementById("media_id").value);
	comment_content = encodeURIComponent(document.getElementById("comment_content").value);
	if(trim(comment_content) == "")
		openPopup('Bạn chưa nhập nội dung bình luận .');
	else if (comment_content.length > 255)
		openPopup('Nội dung bình luận của bạn quá 225 ký tự.');
	else {
		try {
			field = document.getElementById("comment_loading");
			field.innerHTML = loadingText;
			//document.getElementById("comment_loading").innerHTML = loadingText;
			//document.getElementById("comment_loading").style.display = "block";
			http.open('POST',  mainURL+'comment_album.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = handleResponse;
			http.send('comment=1&media_id='+media_id+'&comment_content='+comment_content);
			openPopup('Cảm nhận của bạn đã được đăng .');
		}
		catch(e){}
		finally{}
	}
	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 = handleResponse;
			http.send('qua_tang=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); }

