var http = createRequestObject();
var field;
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.style.marginBottom = "10";
			field.innerHTML = response;
			field.scrollIntoView();
			if(!response) window.location.href = serverURL+'/'+LAST;
			else if(response == 17) window.location.href = serverURL+'/phong-yeu-cau'+EXT;
		}
  	}
	catch(e){}
	finally{}
}
function sendRequest(act,div,type,number) {
	field = document.getElementById(div);
	field.innerHTML = loadingText;
	http.open('POST',  '/movie/index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
	http.send('act='+act+'&type='+type+'&number='+number);
	return false;
}
function trim(a) {
	return a.replace(/^s*(S*(s+S+)*)s*$/, "$1");
}
function doLogin(act,div) {
	name = document.frmlogin.name.value;
	pwd = document.frmlogin.pwd.value;
	name = eval('encodeURIComponent(name);');
	pwd = eval('encodeURIComponent(pwd);');
	if(	trim(name) == "" ||	trim(pwd) == "")
		alert("Bạn chưa nhập đầy đủ thông tin");
	else {
			field = document.getElementById(div);
			field.innerHTML = loadingText;
			http.open('POST',  '/movie/index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = handleResponse;
			http.send('act='+act+'&name='+name+'&pwd='+pwd);
			
		}
	return false;
}
function doReg(act,div) {
	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);
	//sDomain = document.getElementById("sDomain");
	//m_sDomain = sDomain.options[sDomain.selectedIndex].value;
	//if (m_sDomain == "khac") m_sDomain = encodeURIComponent(document.getElementById("sDomain").value);
	//email = mail + "@" + m_sDomain;
	u_area = document.getElementById("u_area");
	area = u_area.options[u_area.selectedIndex].value;
	//agree = document.getElementById("agree").checked;
	u_day = document.getElementById("u_day");
	bday = u_day.options[u_day.selectedIndex].value;
	u_month = document.getElementById("u_month");
	bmonth = u_month.options[u_month.selectedIndex].value;
	u_year = document.getElementById("u_year");
	byear = u_year.options[u_year.selectedIndex].value;
	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 {
			field = document.getElementById(div);
			field.innerHTML = loadingText;
			http.open('POST',  '/movie/index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = handleResponse;
			http.send('act='+act+'&name='+name+'&pwd='+pwd+'&email='+email+'&sex='+sex+'&area='+area+'&bday='+bday+'&bmonth='+bmonth+'&byear='+byear);
			
		}
	return false;
}
function doReq(act,div) {
	title = encodeURIComponent(document.getElementById("req_title").value);
	content = encodeURIComponent(document.getElementById("req_content").value);
	email = encodeURIComponent(document.getElementById("req_email").value);
	if(	trim(title) == "" || trim(email) == "" )
		alert("Bạn chưa nhập đầy đủ thông tin (*)");
		else {
			field = document.getElementById(div);
			field.innerHTML = loadingText;
			http.open('POST',  '/movie/index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = handleResponse;
			http.send('act='+act+'&title='+title+'&email='+email+'&content='+content);
		}
	return false;
}
function comment_film(id,div) {
  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 > 500)
  		alert("Nội dung cảm nhận quá 500 ký tự.");
 	else {
  		field = document.getElementById(div);
		field.innerHTML = loadingText;
   		http.open('POST',  '/movie/index.php');
   		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   		http.onreadystatechange = handleResponse;
   		http.send('act=9&id='+id+'&comment_content='+comment_content);
 		}
 	return false;
}
function rep_request(id,div) {
  rep_content = encodeURIComponent(document.getElementById("rep_content").value);
  rep_poster = encodeURIComponent(document.getElementById("rep_poster").value);
  rep_warn = encodeURIComponent(document.getElementById("rep_warn").value);
 	if(trim(rep_content) == "" || trim(rep_poster) == "" )
  		alert("Bạn chưa nhập đủ thông tin !");
 	else if (rep_warn)
  		alert("Bạn hãy đợi vài giây nữa mới có thể viết tiếp !");
 	//else if (rep_content.length > 1000)
  	//	alert("Nội dung cảm nhận quá 1000 ký tự.");
 	else {
  		field = document.getElementById(div);
		field.innerHTML = loadingText;
   		http.open('POST',  '/movie/index.php');
   		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   		http.onreadystatechange = handleResponse;
   		http.send('act=22&id='+id+'&rep_content='+rep_content+'&rep_poster='+rep_poster);
 		}
 	return false;
}
// BEGIN RATING
function Rating(film_id,star) {

		field = document.getElementById("rating_field");
		document.getElementById("rate_s").innerHTML = loadingText;
		hide_rating_process();
		http.open('POST',  '/movie/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponse;
		http.send('act=6&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) {
		document.getElementById("rate_text_d").innerHTML = rate_text;
		remove_star();
		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() {
		if(document.getElementById("rating_process")) document.getElementById("rating_process").style.display = "none";
		if(document.getElementById("rate_s")) document.getElementById("rate_s").style.display = "block";
	}
// END RATING
	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;
//	if(!winnameRequired)	winname = "";
	newwindow = window.open(filename,winname,features);
	newwindow.focus();
	}
	// Menu Main
	function loadMenu(type)
        {
        	var temp = ""; var vn=""; var fr=""; var tv=""; var yc=""; var dl=""; var pos1 = subMenu;
        	//if (type == 1)
        	//{
        		if(mainMenu=="vn") vn = "navselect";
        		if(mainMenu=="fr") fr = "navselect";
        		if(mainMenu=="tv") tv = "navselect";
        		if(mainMenu=="yc") yc = "navselect";
        		if(mainMenu=="dl") dl = "navselect";
        		if (type == 2) {yc = "navselect"; vn = ""; tv = ""; dl = ""; fr = ""; pos1 = "";}
        		temp = "<ul>";
				
				temp += "<li class=\"" + vn + "\"><a href=\"/movie/server-vn/\" title=\"Server Phim VietNam\"><span>Trang chủ</span></a></li>";
				temp += "<li class=\"" + fr + "\"><a href=\"/movie/server-fr/\"><span>Server Phim Quốc Tế</span></a></li>";
				temp += "<li class=\"" + tv + "\"><a href=\"" + serverURL + "/xem-tivi-online" + EXT + "\"><span>Tivi Online</span></a></li>";
				temp += "<li class=\"" + yc + "\"><a style=\"cursor: pointer;\" onclick=\"loadMenu(2)\"><span>Yêu Cầu Phim</span></a></li>";
				temp += "<li class=\"" + dl + "\"><a href=\"" + serverURL + "/download-dvd" + EXT + "\"><span class=\"lastitem\">Download Phim</span></a></li>";
				
				temp += "</ul>";
	            document.getElementById("mainmenu").innerHTML = temp;
	            if(type != 2 && mainMenu != 'yc'){
        		sub1 = "Phim nổi bật";
        		sub2 = "Phim bộ mới nhất";
        		sub3 = "Phim lẻ mới nhất";
        		sub4 = "Phim Xem nhiều nhất";
        		sub5 = "Phim bình chọn nhiều";
        		sub6 = "Phim chiếu Rạp";
        		sub7 = "Phim Hot";
	            if (pos1 != 1)
	            {
	            	sub1 = "<a href=\"" + serverURL + "/phim-noi-bat"  + EXT +  "\">" + sub1 + "</a>"
	            }
	            if (pos1 != 2)
	            {
	            	sub2 = "<a href=\"" + serverURL + "/phim-bo-moi-nhat"  + EXT +  "\">" + sub2 + "</a>"
	            }
	            if (pos1 != 3)
	            {
	            	sub3 = "<a href=\"" + serverURL + "/phim-le-moi-nhat"  + EXT +  "\">" + sub3 + "</a>"
	            }
	            if (pos1 != 4)
	            {
	            	sub4 = "<a href=\"" + serverURL + "/phim-xem-nhieu-nhat"  + EXT +  "\">" + sub4 + "</a>"
	            }
	            if (pos1 != 5)
	            {
	            	sub5 = "<a href=\"" + serverURL + "/phim-binh-chon"  + EXT +  "\">" + sub5 + "</a>"
	            }
	            if (pos1 != 6)
	            {
	            	sub6 = "<a href=\"" + serverURL + "/phim-chieu-rap"  + EXT +  "\">" + sub6 + "</a>"
	            }
	            if (pos1 != 7)
	            {
	            	sub7 = "<a href=\"" + serverURL + "/xem-phim-hot"  + EXT +  "\">" + sub7 + "</a>"
	            }
	            document.getElementById("submenu").innerHTML = sub1 + "<span>|</span>" + sub2 + "<span>|</span>" + sub3 + "<span>|</span>" + sub4 + "<span>|</span>" + sub5 + "<span>|</span>" + sub6 + "<span>|</span>" + sub7 + "<span style=\"padding-left:30px; padding-right:0px;\"><select style=\"font-size:10px; background:#DBDDDE; border:1px solid #FFFFFF;\" onchange=\"window.location=this.value\"><option value=\"\">Quốc gia</option><option value=\"" + serverURL + "/phim-dai-loan/phim-bo/c879"+ EXT +"\">Đài Loan</option><option value=\"" + serverURL + "/phim-hong-kong/hongkong/q126"+ EXT +"\">Hồng Kông</option><option value=\"" + serverURL + "/phim-trung-quoc/china/q879"+ EXT +"\">Trung Quốc</option><option value=\"" + serverURL + "/phim-han-quoc/korea/q128"+ EXT +"\">Hàn Quốc</option><option value=\"" + serverURL + "/phim-au-my/hollywod/q125"+ EXT +"\">Mỹ</option><option value=\"" + serverURL + "/phim-viet-nam/vietnam/q124"+ EXT +"\">Việt Nam</option><option value=\"" + serverURL + "/phim-thai-lan/asia/q880"+ EXT +"\">Thái Lan</option><option value=\"" + serverURL + "/phim-nhat-ban/japan/q968"+ EXT +"\">Nhật Bản</option></select></span>";
            }else{
            	sub1 = "Gửi yêu cầu";
        		sub2 = "Xem tất cả yêu cầu";
        		sub3 = "Danh sách phim Yêu cầu";
        		sub4 = "Hướng dẫn";
	            if (pos1 != 1)
	            {
	            	sub1 = "<a href=\"" + serverURL + "/yeu-cau-phim"  + EXT +  "\">" + sub1 + "</a>"
	            }
	            if (pos1 != 2)
	            {
	            	sub2 = "<a href=\"" + serverURL + "/phong-yeu-cau"  + EXT +  "\">" + sub2 + "</a>"
	            }
	            if (pos1 != 3)
	            {
	            	sub3 = "<a href=\"" + serverURL + "/phim-yeu-cau"  + EXT +  "\">" + sub3 + "</a>"
	            }
	            if (pos1 != 4)
	            {
	            	sub4 = "<a href=\"" + serverURL + "/huong-dan"  + EXT +  "\">" + sub4 + "</a>"
	            }
	            document.getElementById("submenu").innerHTML = sub1 + "<span>|</span>" + sub2 + "<span>|</span>" + sub3 + "<span>|</span>" + sub4 + "<span style=\"padding-left:350px; padding-right:0px;\"><select style=\"font-size:10px; background:#DBDDDE; border:1px solid #FFFFFF;\" onchange=\"window.location=this.value\"><option value=\"\">Quốc gia</option><option value=\"" + serverURL + "/phim-dai-loan/phim-bo/c879"+ EXT +"\">Đài Loan</option><option value=\"" + serverURL + "/phim-hong-kong/q126"+ EXT +"\">Hồng Kông</option><option value=\"" + serverURL + "/phim-trung-quoc/q879"+ EXT +"\">Trung Quốc</option><option value=\"" + serverURL + "/phim-han-quoc/q128"+ EXT +"\">Hàn Quốc</option><option value=\"" + serverURL + "/phim-my/q125"+ EXT +"\">Mỹ</option><option value=\"" + serverURL + "/phim-viet-nam/q124"+ EXT +"\">Việt Nam</option><option value=\"" + serverURL + "/phim-thai-lan/q880"+ EXT +"\">Thái Lan</option><option value=\"" + serverURL + "/phim-nhat-ban/q968"+ EXT +"\">Nhật Bản</option></select></span>";
            }
            
        }
        function logout()
		{
			if (confirm('Bạn thực sự muốn thoát?')) {
				window.location.href = serverURL + '/thoat';
			} else {
				return;
			}
		}
