// + ---------------------- +
// |        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 = '#s/Media_Post';
		}
  	}
	catch(e){}
		finally{}
}

function post() {
	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('media_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 media_edit_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			document.getElementById("edit_loading").style.display = "none";
			var response = http.responseText;
			if (response){	
				document.getElementById("edit_loading").innerHTML = response;
				document.getElementById("edit_loading").style.display = "block";
				document.getElementById("submit").disabled=false;document.getElementById("submit").value="Đăng lại";
			}else window.location.href = '?refresh=1';	
		}
	}
	catch(e){}
		finally{}
}

function media_edit(id) {
	song = encodeURIComponent(document.getElementById("edit_song").value);
	artist = encodeURIComponent(document.getElementById("edit_artist").value);
	//album = encodeURIComponent(document.getElementById("edit_album").value);
	cat = encodeURIComponent(document.getElementById("edit_cat").value);
	lyric = encodeURIComponent(document.getElementById("edit_lyric").value);
	url = encodeURIComponent(document.getElementById("edit_url").value);

	if(	trim(song) == "" || trim(url) == "")
		alert("Bạn chưa nhập đầy đủ thông tin (*)");
	else {
		try{
			document.getElementById("edit_loading").innerHTML = loadingText;
			document.getElementById("edit_loading").style.display = "block";
			http.open('POST',  'index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
			http.onreadystatechange = media_edit_handleResponse;
			http.send('media_edit=1&song='+song+'&artist='+artist+'&cat='+cat+'&lyric='+lyric+'&url='+url+'&media_id='+id); //+'&album='+album
			document.getElementById("submit").disabled=true;document.getElementById("submit").value="Wait...";
		}
		catch(e){}
		finally{}
	}
	return false;
}
function m_del_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			var response = http.responseText;
			if (response == 1) {
				alert("Media đã được xoá.");
				window.location.href = '?refresh=1';
			}
		}
  	}
	catch(e){}
	finally{}
}
function media_delete(media_id) {
	if (confirm("Bạn có chắc chắn muốn xóa ca khúc này không ?")) {
		http.open('POST',  'index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = m_del_handleResponse;
		http.send('media_delete=1&media_id='+media_id);
	}
	return false;
}
// + ------------------------- +
// |        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 == 1)	window.location.href = '?refresh=1';

			else  {
				document.getElementById("change_info_loading").innerHTML = response;
				document.getElementById("change_info_loading").style.display = "block";
			}
		}
  	}
	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);
	yim = encodeURIComponent(document.getElementById("u_yim").value);
	signature = encodeURIComponent(document.getElementById("u_sig").value);
	b_day = encodeURIComponent(document.getElementById("u_day").value);
	b_month = encodeURIComponent(document.getElementById("u_month").value);
	b_year = encodeURIComponent(document.getElementById("u_year").value);
	h_sex = document.getElementById("hide_sex").checked;
	h_email = document.getElementById("hide_email").checked;
	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; charset=UTF-8');
			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+'&u_area='+area+'&b_day='+b_day+'&b_month='+b_month+'&b_year='+b_year+'&yim='+yim+'&avatar_input='+avatar_input+'&avatar_local='+avatar_local+'&u_sig='+signature);
		}
		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 > 10000)
		alert("Nội dung cảm nhận quá 10000 ký tự.");
	else {
		try {
			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; charset=UTF-8');
			http.onreadystatechange = comment_handleResponse;
			http.send('comment=1&media_id='+media_id+'&comment_content='+comment_content);
			document.getElementById("submit").disabled=true;document.getElementById("submit").value="Wait...";
		}
		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',  'comment.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
		http.onreadystatechange = comment_handleResponse;
		http.send('delete=1&media_id='+media_id+'&comment_id='+comment_id);
	}
	return false;
}

