/*======================================================================*\
|| #################################################################### ||
|| # AT Movie 1.0                                                     # ||
|| # ---------------------------------------------------------------- # ||
|| # Copyright © 2008-2009 AnhTrang.Org . All Rights Reserved.         # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ----------- AT MOVIE IS FREE SOFTWARE - OPEN SOURCE ------------ # ||
|| # http://www.anhtrang.org | http://www.anhtrang.org/support.html   # ||
|| #################################################################### ||
\*======================================================================*/

var http = createRequestObject();
var field = '';
var i=0;
var MainLoading = "<div id='load'><img src='"+web_link+"/images/loading.gif'></div>";
var pl_loading = "<img src='"+web_link+"/images/pl_loading.gif'>";
var RATE_OBJECT_IMG = ""+web_link+"/images/rate/full.gif";
var RATE_OBJECT_IMG_HOVER = ""+web_link+"/images/rate/full.gif";
var RATE_OBJECT_IMG_HALF = ""+web_link+"/images/rate/half.gif";
var RATE_OBJECT_IMG_BG = ""+web_link+"/images/rate/none.gif";
		   
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 trim(a) {
	return a.replace(/^s*(S*(s+S+)*)s*$/, "$1");
}

function handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			document.getElementById("loading").style.display = "none";
			response = http.responseText;
			field.innerHTML = response;
			field.scrollIntoView();
			if(!response) window.location.href = url;
		}
  	}
	catch(e){}
	finally{}
}

function str_replace(search, replace, str){
	var ra = replace instanceof Array, sa = str instanceof Array, l = (search = [].concat(search)).length, replace = [].concat(replace), i = (str = [].concat(str)).length;
	while(j = 0, i--)
		while(str[i] = str[i].split(search[j]).join(ra ? replace[j] || "" : replace[0]), ++j < l);
	return sa ? str : str[0];
}

//#######################################
//# SEARCH
//#######################################
function do_search() {
	tab = document.getElementById("t").value;
	kw = document.getElementById("keyword").value;
	kw = str_replace(' ', '-', kw);
	kw = str_replace('%20', '-', kw);
	if (!kw) alert('Bạn chưa nhập từ khóa');
	else {
		kw = encodeURIComponent(kw);
		window.location.href = web_link+'/?act=search/'+kw+'/'+tab;	
	}
  return false;
}

//#######################################
//# ADD EMOTIONS
//#######################################
function addText(elname, wrap1, wrap2) {
	if (document.selection) { // for IE 
		var str = document.selection.createRange().text;
		document.forms['add'].elements[elname].focus();
		var sel = document.selection.createRange();
		sel.text = wrap1 + str + wrap2;
		return;
	} else if ((typeof document.forms['add'].elements[elname].selectionStart) != 'undefined') { // for Mozilla
		var txtarea = document.forms['add'].elements[elname];
		var selLength = txtarea.textLength;
		var selStart = txtarea.selectionStart;
		var selEnd = txtarea.selectionEnd;
		var oldScrollTop = txtarea.scrollTop;
		var s1 = (txtarea.value).substring(0,selStart);
		var s2 = (txtarea.value).substring(selStart, selEnd)
		var s3 = (txtarea.value).substring(selEnd, selLength);
		txtarea.value = s1 + wrap1 + s2 + wrap2 + s3;
		txtarea.selectionStart = s1.length;
		txtarea.selectionEnd = s1.length + s2.length + wrap1.length + wrap2.length;
		txtarea.scrollTop = oldScrollTop;
		txtarea.focus();
		return;
	} else {
		insertText(elname, wrap1 + wrap2);
	}
}
//#######################################
//# COUNT WORDS
//#######################################
var submitcount=0;
   function checkSubmit() {

      if (submitcount == 0)
      {
      submitcount++;
      document.Surv.submit();
      }
   }


function wordCounter(field, countfield, maxlimit) {
wordcounter=0;
for (x=0;x<field.value.length;x++) {
      if (field.value.charAt(x) == " " && field.value.charAt(x-1) != " ")  {wordcounter++}
      if (wordcounter > 250) {field.value = field.value.substring(0, x);}
      else {countfield.value = maxlimit - wordcounter;}
      }
   }

function textCounter(field, countfield, maxlimit) {
  if (field.value.length > maxlimit)
      {field.value = field.value.substring(0, maxlimit);}
      else
      {countfield.value = maxlimit - field.value.length;}
  }
  
  
//#######################################
//# NEWS COMMENT
//#######################################
function show_news_comment(num,news_id,page) { 
	field = document.getElementById("news_comment_field");
	field.innerHTML = MainLoading;
	http.open('POST',  web_link+'/index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
    http.send('show_news_comment=1&num='+num+'&news_id='+news_id+'&page='+page); 
  return false; 
} 

function news_comment_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			var response = http.responseText;
			if (response == 'OK') {
				news_id = encodeURIComponent(document.getElementById("news_id").value);
				num = encodeURIComponent(document.getElementById("num").value);
				show_news_comment(num,news_id,1);
				document.getElementById("loading").style.display = "none";
			}
			else {
				document.getElementById("loading").style.display = "none";
				alert(response);
			}
		}
  	}
	catch(e){}
	finally{}
}

function news_comment_check_values() {
	news_id = encodeURIComponent(document.getElementById("news_id").value);
	num = encodeURIComponent(document.getElementById("num").value);
	comment_poster = encodeURIComponent(document.getElementById("comment_poster").value);
	comment_content = encodeURIComponent(document.getElementById("comment_content").value);
	try {
	    document.getElementById("loading").innerHTML = MainLoading;
		document.getElementById("loading").style.display = "block";
		http.open('POST',  web_link+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
		http.onreadystatechange = news_comment_handleResponse;
		http.send('news_comment=1&news_id='+news_id+'&num='+num+'&comment_poster='+comment_poster+'&comment_content='+comment_content);
	}
	catch(e){}
	finally{}
  return false;
}  
  
  
//#######################################
//# COMMENT
//#######################################
function showComment(num,media_id,page) { 
	field = document.getElementById("comment_field");
	field.innerHTML = MainLoading;
	http.open('POST',  web_link+'/index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
    http.send('showcomment=1&num='+num+'&media_id='+media_id+'&page='+page); 
  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);
				num = encodeURIComponent(document.getElementById("num").value);
				showComment(num,media_id,1);
			}
			else {
				document.getElementById("loading").style.display = "none";
				alert(response);
			}
		}
  	}
	catch(e){}
	finally{}
}

function comment_check_values() {
	media_id = encodeURIComponent(document.getElementById("media_id").value);
	num = encodeURIComponent(document.getElementById("num").value);
	comment_poster = encodeURIComponent(document.getElementById("comment_poster").value);
	comment_content = encodeURIComponent(document.getElementById("comment_content").value);
	try {
	    document.getElementById("loading").innerHTML = MainLoading;
		document.getElementById("loading").style.display = "block";
		http.open('POST',  web_link+'/index.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+'&num='+num+'&comment_poster='+comment_poster+'&comment_content='+comment_content);
	}
	catch(e){}
	finally{}
  return false;
}
//#######################################
//# RATING
//#######################################
function rating(media_id,star) {
	try {
		document.getElementById("rate_s").innerHTML = MainLoading;
		document.getElementById("rate_s").style.display = "block";
		hide_rating_process();
		http.open('POST', web_link+'/index.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,rate_text) {
		remove_star();
		document.getElementById("rate_text_d").innerHTML = rate_text;
		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() {
		document.getElementById("rating_process").style.display = "none";
		if(document.getElementById("rate_s")) document.getElementById("rate_s").style.display = "block";
	}

//#######################################
//# BROKEN
//#######################################
function showBroken(media_id) {
	try {
		document.getElementById("broken_field").innerHTML = MainLoading;
		document.getElementById('broken_field').style.display='block';
		http.open('POST', web_link+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("broken_field").innerHTML = http.responseText;
			}
		}
		http.send('broken=1&media_id='+media_id);
	}
	catch(e){}
	finally{}
	return false;
}
//#######################################
//# SHOW media
//#######################################
function showMedia(num,page,number,apr,cat_id) { 
    field = document.getElementById(num);
	document.getElementById("loading").innerHTML = MainLoading;
	document.getElementById("loading").style.display = "block";
	http.open('POST', web_link+'/index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
    http.send('showmedia=1&num='+num+'&page='+page+'&number='+number+'&apr='+apr+'&cat_id='+cat_id); 
  return false; 
}
function imposeMaxLength(Object, MaxLen)
{
  return (Object.value.length <= MaxLen);
}

function applyType(obj, value) {
    var arrType = document.getElementsByName('searchType');
    for (var i = 0; i < arrType.length; i++) {
        arrType[i].className = '';
    }
    obj.className = 'curnav';
    document.getElementById("frmsearch").t.value = value;
}
// + ---------------------- +
// |        POPUP           |
// + ---------------------- +
function openWindow(filename, winname, width, height, feature) {
    var features, top, left;
    var reOpera = /opera/i;
    var winnameRequired = ((navigator.appName == "Netscape" && parseInt(navigator.appVersion) == 4) || reOpera.test(navigator.userAgent));

    left = (window.screen.width - width) / 2;
    top = (window.screen.height - height) / 2;
    if (feature == '')
        features = "width=" + width + ",height=" + height + ",top=" + top + ",left=" + left + ",status=0,location=0";
    else
        features = "width=" + width + ",height=" + height + ",top=" + top + ",left=" + left + "," + feature;
    newwindow = window.open(filename, winname, features);
    newwindow.focus();
}
// + ---------------------- +
// |        PLAYLIST        |
// + ---------------------- +

function reloadPlaylist(add_id,remove_id,field) {
	try{
		if(add_id == 0) {
			document.getElementById("loading").innerHTML = MainLoading;
			document.getElementById("loading").style.display = "block";
		}
		else {
			if(field == "player") document.getElementById("pl_loading_player").innerHTML = '<b>Wait...</b>';
			else document.getElementById("pl_loading_"+field).innerHTML = pl_loading;
		}
		http.open('POST',  web_link+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function () {
										try {
											if((http.readyState == 4)&&(http.status == 200)){
												if(add_id == 0) document.getElementById("loading").style.display = "none";
												else {
													if(field == "player") document.getElementById("pl_loading_player").innerHTML = '<b>Playlist</b>';
													else document.getElementById("pl_loading_"+field).innerHTML = "<img src='"+web_link+"/images/media/add.gif'>";
												}
												var response = http.responseText;
												document.getElementById("playlist_field").innerHTML = response;
											}
										}
										catch(e){}
										finally{}
									}
		http.send('reloadPlaylist=1&add_id='+add_id+'&remove_id='+remove_id);
	}
	catch(e){}
	finally{}
}

function addToPlaylist(song_id,field)
{
	reloadPlaylist(song_id,0,field);
}
function removeFromPlaylist(song_id)
{
	reloadPlaylist(0,song_id);
}
// + ------------------- +
// |        LOGIN        |
// + ------------------- +

function quick_login() {
	name = document.getElementById("frmlogin").name.value;
	pwd = document.getElementById("frmlogin").pwd.value;
	if(	name == "" ||	pwd == "")
		alert("Bạn chưa nhập đầy đủ thông tin");
	else {
		try{
			document.getElementById("loading").innerHTML = MainLoading;
			document.getElementById("loading").style.display = "block";
			http.open('POST',  web_link+'/index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = function() {
												try {
													if((http.readyState == 4)&&(http.status == 200)){
														document.getElementById("loading").style.display = "none";
														var response = http.responseText;
														if (response) {
															alert(response);
														}
														else window.location.reload( false );
													}
												}
												catch(e){}
												finally{}
			}
			http.send('login=1&name='+name+'&pwd='+pwd);
			
		}
		catch(e){}
		finally{}
	}
	return false;
}

function logout() {
		try{
			document.getElementById("loading").innerHTML = MainLoading;
			document.getElementById("loading").style.display = "block";
			http.open('POST',  web_link+'/index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
						http.onreadystatechange = function() {
												try {
													if((http.readyState == 4)&&(http.status == 200)){
														document.getElementById("loading").style.display = "none";
														window.location.reload( false );
													}
												}
												catch(e){}
												finally{}
			}
			http.send('logout=1');
		}
		catch(e){}
		finally{}
	return false;
}

function language(lang) {
		try{
			http.open('POST',  web_link+'/index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = window.location.reload( false );
			http.send('language='+lang);
		}
		catch(e){}
		finally{}
	return false;
}

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

function forgot() {
	email = document.getElementById("frmforgot").u_email.value;
	if(email == "" )
	alert("Bạn chưa nhập email");
	else {
		try{
			document.getElementById("loading").innerHTML = MainLoading;
			document.getElementById("loading").style.display = "block";
			http.open('POST',  web_link+'/index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = function () {
											try {
												if((http.readyState == 4)&&(http.status == 200)){
												document.getElementById("loading").style.display = "none";
												var response = http.responseText;
													if (response) {
														document.getElementById('forgot_field').style.display = 'none'
														document.getElementById('frmlogin').style.display = 'block'
														alert(response);
													}
													else window.location.reload( false );
												}
											}
											catch(e){}
											finally{}
										}
			http.send('forgot=1&email='+email);
		}
		catch(e){}
		finally{}
	}
	return false;
}

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 = document.getElementById("sec_num").value; 
	address = document.getElementById("address").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 NgheRock.Net");
		else if (sec_num=='') alert("Bạn chưa nhập mã số bảo mật");
		else {
			try{
				document.getElementById("loading").innerHTML = MainLoading;
				document.getElementById("loading").style.display = "block";
				http.open('POST',  web_link+'/index.php');
				http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				http.onreadystatechange = function() {
													try {
														if((http.readyState == 4)&&(http.status == 200)){
															document.getElementById("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('Chúc mừng bạn đã đăng ký thành công');
																alert('Bạn có thể đăng nhập ngay bay giờ');
																window.location.href = web_link;
															}
														}
													}
													catch(e){}
													finally{}
												}
				http.send('reg=1&name='+name+'&pwd='+pwd+'&email='+email+'&sex='+sex+'&address='+address+'&sec_num='+sec_num);
			}
			catch(e){}
			finally{}
		}
	return ok;
}

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

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); 
	address = encodeURIComponent(document.getElementById("address").value); 
	public_playlist = encodeURIComponent(document.getElementById("public_playlist").value);
	playlist_name = encodeURIComponent(document.getElementById("playlist_name").value); 
	playlist_img = encodeURIComponent(document.getElementById("playlist_img").value); 
	playlist_img_old = encodeURIComponent(document.getElementById("playlist_img_old").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("loading").innerHTML = MainLoading;
			document.getElementById("loading").style.display = "block";
			http.open('POST',  web_link+'/index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = function () {
											try {
												if((http.readyState == 4)&&(http.status == 200)){
													document.getElementById("loading").style.display = "none";
													var response = http.responseText;
													if (response) {
														alert(response);
													}
													else window.location.reload( false );
												}
											}
											catch(e){}
											finally{}
										}
			http.send('change_info=1&email='+email+'&address='+address+'&playlist='+public_playlist+'&playlist_name='+playlist_name+'&playlist_img_old='+playlist_img_old+'&playlist_img='+playlist_img+'&oldpwd='+oldpwd+'&newpwd='+newpwd_1+'&sex='+sex+'&avatar_input='+avatar_input+'&avatar_local='+avatar_local);
		}
		catch(e){}
		finally{}
	}
	return false;
}