﻿var http = createRequestObject();
var field = '';

function createRequestObject() {
    if (typeof XMLHttpRequest != 'undefined') {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
      var aVersions = [ 'MSXML2.XMLHttp.5.0', 'MSXML2.XMLHttp.4.0', 'MSXML2.XMLHttp.3.0', 'MSXML2.XMLHttp', 'Microsoft.XMLHttp' ];
      for (var i = 0; i < aVersions.length; i++) {
        try {
            var oXmlHttp = new ActiveXObject(aVersions[i]);
            return oXmlHttp;
        } catch (oError) {
            oXmlHttp=null;
        }
      }
    }
    throw new Error('XMLHttp object could be created.');
}

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 load_pages(type,num,apr,id,page) {
	field = document.getElementById(type);
	field.innerHTML = loadingText;
	http.open('POST',  'index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
    http.send('load_pages=1&type='+type+'&num='+num+'&apr='+apr+'&id='+id+'&page='+page); 
  return false; 
} 
//#######################################
//# SEARCH
//#######################################
function do_search() {
	kw = document.getElementById("keyword").value;
	kw = ConvertNonMark(kw);
	if (!kw) alert('Bạn chưa nhập từ khóa');
	else {
		kw = encodeURIComponent(kw);
		window.location.href = '?v=/search/'+kw;	
	}
  return false;
}
//#######################################
//# ADD EMOTIONS
//#######################################
function addText(elname, wrap1) {
	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;
		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 + s3;
		txtarea.selectionStart = s1.length;
		txtarea.selectionEnd = s1.length + s2.length + wrap1.length;
		txtarea.scrollTop = oldScrollTop;
		txtarea.focus();
		return;
	} else {
		insertText(elname, wrap1);
	}
}
//#######################################
//# 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;}
  }
//#######################################
//# COMMENT
//#######################################
function comment_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			var response = http.responseText;
			if (response == 'OK') {
				film_id = encodeURIComponent(document.getElementById("film_id").value);
				num = encodeURIComponent(document.getElementById("num").value);
                load_pages('comment',num,film_id,0,1);
            }
			else document.getElementById("comment_loading").innerHTML = response;

		}
  	}
	catch(e){}
	finally{}
}

function comment_check_values() {
	film_id = encodeURIComponent(document.getElementById("film_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("comment_loading").innerHTML = loadingText;
		document.getElementById("comment_loading").style.display = "block";
		http.open('POST',  'index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
		http.onreadystatechange = comment_handleResponse;
		http.send('comment=1&film_id='+film_id+'&num='+num+'&comment_poster='+comment_poster+'&comment_content='+comment_content);
		document.getElementById("submit").disabled=true;document.getElementById("submit").value="Wait...";
	}
	catch(e){}
	finally{}
  return false;
}
//#######################################
//# RATING
//#######################################
function rating(film_id,star) {
   		field = document.getElementById("rating_field");
		field.innerHTML = loadingText;
   		http.open('POST',  'index.php');
   		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   		http.onreadystatechange = handleResponse;
		http.send('rating=1&film_id='+film_id+'&star='+star);
 	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";
	}
//#######################################
//# REQUEST
//#######################################
function request_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			var response = http.responseText;
			if (response == 'OK') {
				num = encodeURIComponent(document.getElementById("num").value);
                load_pages('request',num,0,0,1);
			}
			else document.getElementById("request_loading").innerHTML = response;
			
		}
  	}
	catch(e){}
	finally{}
}

function request_check_values() {
	num = encodeURIComponent(document.getElementById("num").value);
	request_name = encodeURIComponent(document.getElementById("request_name").value);
	request_email = encodeURIComponent(document.getElementById("request_email").value);
	request_sec = encodeURIComponent(document.getElementById("request_sec").value);
	try{
			document.getElementById("request_loading").innerHTML = loadingText;
			document.getElementById("request_loading").style.display = "block";
			http.open('POST',  'index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
			http.onreadystatechange = request_handleResponse;
			http.send('request=1&num='+num+'&request_name='+request_name+'&request_email='+request_email+'&request_sec='+request_sec);
			//document.getElementById("send").disabled=true;document.getElementById("send").value="Wait...";
		}
	  catch(e){}
	  finally{}
	return false;
}
//#######################################
//# BROKEN
//#######################################
function showBroken(film_id,episode_id) {
   		field = document.getElementById("broken_field");
		field.innerHTML = loadingText;
   		http.open('POST',  'index.php');
   		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   		http.onreadystatechange = handleResponse;
		http.send('broken=1&film_id='+film_id+'&episode_id='+episode_id);
 	return false;
}
//#######################################
//# GIFT
//#######################################
function gift(url) {
	field = document.getElementById("gift");
	field.innerHTML = '<input type="text" size="70" value="'+url+'" onclick=\"this.select()\"/>';
	return false;
}
//#######################################
//# WATCH MOVIE
//#######################################
function xem_movie(episode_id) {
		field = document.getElementById('xem_movie');
		field.innerHTML = loadingText;
		http.open('POST',  'index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponse;
		http.send('xem_movie=1&episode_id='+episode_id);
	return false;
}
