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(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 = 'song'; break;
			case 'singer' : type = 'singer'; break;
			case 'album' : type = 'album'; break;
		}
		last_url = '';
		window.location.href = '/'+folder+'/index.php?go=search&keyword='+kw+'&type='+type+'#ListSearch';
	}
	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 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;
			window.parent.location= nowURL;
		}
  	}
	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");
}
 // Replaces all instances of the given substring.
String.prototype.replaceAll = function(
strTarget, // The substring you want to replace
strSubString // The string you want to replace in.
){
var strText = this;
var intIndexOfMatch = strText.indexOf( strTarget );
 
// Keep looping while an instance of the target string
// still exists in the string.
while (intIndexOfMatch != -1){
// Relace out the current instance.
strText = strText.replace( strTarget, strSubString )
 
// Get the index of any next matching substring.
intIndexOfMatch = strText.indexOf( strTarget );
}
 
// Return the updated string with ALL the target strings
// replaced out with the new substring.
return( strText );
}

// + ------------------- +
// |        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 
			alert("Đăng nhập thành công");
			window.location.href = mainURL+'index.php';
		}
  	}
	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,folder,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);
	}
	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_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); }

function copy(text2copy) {
  if (window.clipboardData) {
    window.clipboardData.setData("Text",text2copy);
  }
  alert("Nhấn Ctrl + C để copy \n Và nhấn Ctrl + V để dán.");
}  
