// JavaScript Document
//var WEB_URL = "http://www.cinebox.vn";
/////////////////////////////////////////////
function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}

var http = createObject();
var nocache = 0;
/* -------------------------- */
/* LOGIN AJAX */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */

function Login(opt) 
{
	
	// Optional: Show a waiting message in the layer with ID ajax_response
	//document.getElementById('alertFormRegister').innerHTML = "Đang check..."
	// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
	var username = encodeURI(document.getElementById('username').value);
	var password = encodeURI(document.getElementById('password').value);
	var filmID = encodeURI(document.getElementById('filmid').value);
	
	// Set te random number to add to URL request
	nocache = Math.random();
	// Pass the login variables like URL variable
	
	//if((username.length > 0) && (password.length > 0))
	http.open('get', 'login.php?id='+filmID+'&username='+username+'&password='+password+'&opt='+opt);
	
	http.onreadystatechange = loginReply;
	http.send(null);
}
function loginReply() 
{	
	divAlert = document.getElementById('alertFormFilmLogin');
	if(http.readyState == 4)
	{
		var response = http.responseText;
		if(response == 0){
			divAlert.style.display = "block";
			divAlert.innerHTML = 'Thông tin đăng nhập không chính xác.';			
			document.getElementById('username').focus();
		} 
		else
		{	
			window.location.href = response;	
		}
	}
}

////////////////////////////////////////////
/* -------------------------- */
/* ADDCOMMENT AJAX */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */

function AddComment() 
{
	
	// Optional: Show a waiting message in the layer with ID ajax_response
	//document.getElementById('alertFormRegister').innerHTML = "Đang check..."
	// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
	var content = encodeURI(document.getElementById('content').value);
	var userID = encodeURI(document.getElementById('userid').value);
	var filmID = encodeURI(document.getElementById('filmid').value);
	
	// Set te random number to add to URL request
	nocache = Math.random();
	// Pass the login variables like URL variable
	
	//if((username.length > 0) && (password.length > 0))
	http.open('get', 'addcomment.php?filmid='+filmID+'&userid='+userID+'&content='+content);
	
	http.onreadystatechange = AddCommemtReply;
	http.send(null);
}
function AddCommemtReply() 
{	
	divAlert = document.getElementById('alertFormFilmComment');
	if(http.readyState == 4)
	{
		var response = http.responseText;
		divAlert.style.display = "block";
		if(response == 0){			
			divAlert.innerHTML = 'Thông tin bình luận không hợp lệ.';			
			document.getElementById('content').focus();
		} 
		else if(response == 2){			
			divAlert.innerHTML = 'Bạn chưa đăng nhập! Vui lòng đăng nhập trước khi bình luận.';			
			document.getElementById('content').focus();
		} 
		else
		{	
			divAlert.innerHTML = 'Bạn đã gửi bình luận thành công. Bình luận của bạn sẽ được duyệt trong vòng 24 tiếng.';			
			document.getElementById('content').focus();	
		}
	}
}

////////////////////////////////////////////
////////////////////////////////////////////
/* -------------------------- */
/* ADD FAVOURITE FILM AJAX */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */

function AddFavouriteFilm() 
{
	
	// Optional: Show a waiting message in the layer with ID ajax_response
	//document.getElementById('alertFormRegister').innerHTML = "Đang check..."
	// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
	var answer = encodeURI(document.getElementById('answer').value);
	var userID = encodeURI(document.getElementById('userid').value);
	var filmID = encodeURI(document.getElementById('filmid').value);
	
	// Set te random number to add to URL request
	nocache = Math.random();
	// Pass the login variables like URL variable
	
	//if((username.length > 0) && (password.length > 0))
	http.open('get', 'addfilm.php?filmid='+filmID+'&userid='+userID+'&des='+answer);
	
	http.onreadystatechange = AddFavouriteFilmReply;
	http.send(null);
}
function AddFavouriteFilmReply() 
{	
	divAlert = document.getElementById('alertFormFavouriteFilm');
	if(http.readyState == 4)
	{
		var response = http.responseText;
		divAlert.style.display = "block";
		if(response == 0){			
			divAlert.innerHTML = 'Thông tin nhập vào không hợp lệ.';			
			document.getElementById('answer').focus();
		} 
		else if(response == 1)
		{	
			divAlert.innerHTML = 'Bộ phim này đã có trong danh sách phim yêu thích của bạn.';			
			document.getElementById('content').focus();	
		}
		else if(response == 3){			
			divAlert.innerHTML = 'Vui lòng đăng nhập trước khi thêm phim ưa thích vào danh sách.';			
			document.getElementById('content').focus();
		}
		else
		{	
			divAlert.innerHTML = 'Bạn đã cập nhật danh sách phim yêu thích thành công.';			
			document.getElementById('answer').focus();	
		}
	}
}

////////////////////////////////////////////
/* -------------------------- */
/* CHECK DATA AJAX */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;
function Check(opt) 
{
	
	// Optional: Show a waiting message in the layer with ID ajax_response
	//document.getElementById('alertFormRegister').innerHTML = "Đang check..."
	// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
	var username = encodeURI(document.getElementById('username').value);
	var email = encodeURI(document.getElementById('email').value);
	
	// Set te random number to add to URL request
	nocache = Math.random();
	// Pass the login variables like URL variable
	if(opt == 1)
	{
		if(username.length > 0)
			http.open('get', 'check.php?opt=1&value='+username);
	}
	if(opt == 2)
	{
		if(email.length > 0)
			http.open('get', 'check.php?opt=2&value='+email);
	}
	http.onreadystatechange = CheckedReply;
	http.send(null);
}
function CheckedReply() 
{	
	divAlert = document.getElementById('alertFormRegister');
	if(http.readyState == 4)
	{
		var response = http.responseText;
		if(response == 1){
			divAlert.style.display = "block";
			divAlert.innerHTML = 'Username đã tồn tại!';			
			document.getElementById('username').focus();
		} 
		else if(response == 3)
		{	
			divAlert.innerHTML = 'Email đã tồn tại!';
			divAlert.style.display = "block";			
			document.getElementById('email').focus();		
		}
		else
		{	
			divAlert.style.display = "none";						
		}
	}
}

////////////////////////////////////////////
var WEB_URL = ".";

function show_calendar(buttonlink, daID, ifID)
{
	Calendar.setup({"ifFormat":"%Y-%m-%d", "daFormat":"%d/%m/%Y", "inputField":ifID, "displayArea":daID, "button":buttonlink});
}

function isEmail(obj)
{
    var re = /^([A-Za-z0-9\_\-]+\.)*[A-Za-z0-9\_\-]+@[A-Za-z0-9\_\-]+(\.[A-Za-z0-9\_\-]+)+$/;
    var sEMail = obj.value;
	
    if (sEMail.search(re) == -1)
		return false;
	else
		return true;
}

function isPhone(obj)
{
    var re = /^[0-9]*$/;
    var sPhone = obj.value;
	
    if (sPhone.search(re) == -1)
		return false;
	else if (sPhone.length < 8)
		return false;
	else 
		return true;
}

function isURL(obj)
{
	var sURL = obj.value;
	
	if (sURL.indexOf("http://") > 0)
		return false;
	return true;
}

function isChar(val)
{
    varChars = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
	varChars += "1234567890";
	/*
	varChars += " ";
	varChars += "đĐ";
	varChars += "áàảãạâăÁÀẢÃẠÂĂ";
	varChars += "ấầẩẫậắằẳẵặẤẦẨẪẬẮẰẲẴẶ";
	varChars += "éèẻẽẹêÉÈẺẼẸÊ";
	varChars += "ếềểễệẾỀỂỄỆ";
	varChars += "íìỉĩịÍÌỈĨỊ";
	varChars += "óòỏõọôơÓÒỎÕỌÔƠ";
	varChars += "ốồổỗộớờởỡợỐỒỔỖỘỚỜỞỠỢ";
	varChars += "úùủũụưÚÙỦŨỤƯ";
	varChars += "ứừửữựỨỪỬỮỰ";
	varChars += "ýỳỷỹỵÝỲỶỸỴ";
	*/
	//varChars = "~!@#$%^&*()))_+|`1234567890-=\\[]{};':\",./<>?";
    var isChar = true;
    var index = 0;
    while (index < val.length && isChar) {    	
    	//alert(val.charAt(index) + "/" + index + "/" + varChars.indexOf(val.charAt(index)));
        isChar = (varChars.indexOf(val.charAt(index)) != -1);
        index++;
    }    
    return isChar;
}

function isShift(val)
{
	// condition 1
	if (val.length < 5 || val.toString().indexOf(":") == -1)
		return false;
	
	// condition 2
	var varChars = "1234567890:";
    var isShift = true;
    var index = 0;
    while (index < val.length && isShift) {
        isShift = (varChars.indexOf(val.charAt(index)) != -1);
        index++;
    }
	if (!isShift)
    	return false;
	
	// condition 3
	var arrShift = val.split(":");
	var hourShift = parseInt(arrShift[0]);
	var minuteShift = parseInt(arrShift[1]);
	if (hourShift < 0 || hourShift > 23)
		return false;
	if (minuteShift < 0 || minuteShift > 59)
		return false;
	
	return true;
}

function isValidDate(dayObj, monthObj, yearObj)
{
	dayVal = parseInt(dayObj.value);
	monthVal = parseInt(monthObj.value);
	yearVal = parseInt(yearObj.value);
	
	if (dayVal < 1 || dayVal > 31) {
		dayObj.focus();
		return false;
	}
	
	if (monthVal < 1 || monthVal > 12) {
		monthObj.focus();
		return false;
	}
	
	if (yearVal < 1) {
		yearObj.focus();
		return false;
	}
	
	switch (monthVal) {
		case 4:
		case 6:
		case 9:
		case 11:
			if (dayVal > 30) {
				dayObj.focus();
				return false;
			}
			break;
		
		case 2:
			if (yearVal % 4 == 0) {
				if (dayVal > 29) {
					dayObj.focus();
					return false;
				}
			}
			else {
				if (dayVal > 28) {
					dayObj.focus();
					return false;
				}
			}
			break;
		
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
		default:
			break;
	}
	
	return true;
}

function changePage(url)
{
	location.href = url;
    return;
}

function numberFormat(txtDisplayNumber, txtHiddenNumber)
{
	if (txtDisplayNumber.value != "") {
		var nf = new NumberFormat(txtDisplayNumber.value);
		nf.setPlaces(2);
		
		// used for display
		nf.setSeparators(true, nf.COMMA, nf.PERIOD);
		txtDisplayNumber.value = nf.toFormatted();
		
		// used for saving
		nf.setSeparators(false);
		txtHiddenNumber.value = nf.toFormatted();
	}
	else {
		txtHiddenNumber.value = "";
	}
}

function delItem(id, url)
{
	if (confirm("Bạn có muốn xóa thật không ?" )) {
		if (url.indexOf("task=") == -1) {
			// id is a form object
			form = id;
			valTask = url;
			
			form.task.value = valTask;
			form.submit();
			return;
		}
		else {
			window.location.replace(url + "&id=" + id);
			return;
		}
	}
}

function publishItem(form, valTask)
{
    if (valTask == undefined) {
        valTask = "publish";
    }
    form.task.value = valTask;
    form.submit();
    return;
}

function unPublishItem(form, valTask)
{
    if (valTask == undefined) {
        valTask = "unpublish";
    }
    form.task.value = valTask;
    form.submit();
    return;
}

function openWindow(url, width, height)
{
	if (width == undefined)
		width = 600;
	if (height == undefined)
		height = 350;
	window.open(url, "", "width=" + width + ",height=" + height + ",left=0,top=0,scrollbars=yes");
}

function addOption(value, text)
{
	eOption = new Option();
	eOption.value = value;
	eOption.text = text;	
	
	return eOption;
}

function addItem()
{
    var target = arguments[0]; // it must be a select box
    var omitIndex = arguments[1]; // omitting item on target object, -1 means not use it
    var glueVal = arguments[2]; // glue to connect values
    var glueTxt = arguments[3]; // glue to connect text
    var emptyVal = arguments[4]; // index of object maybe is emtpy, separate by commas

    if (omitIndex != -1 && target.length && target.options[omitIndex].value == "")
        target.remove(omitIndex);

    var arrObj = new Array();
    for (i=5; i<arguments.length; i++) {
        obj = arguments[i];
        if (i != (emptyVal+4) && obj.value == "") {
            alert("Hãy nhập đầy đủ thông tin trước khi thêm !");
            return;
        }
        arrObj.push(obj.value);
    }
    
    value = arrObj.join(glueVal);
    text = arrObj.join(glueTxt);
    eOption = addOption(value, text);
    target.options[target.length] = eOption;

    return;
}

function removeItem(listObject, listIndex)
{
    listObject.remove(listIndex);
    return;
}

function moveItem(fromList, toList, omitIndex)
{
	var removeList = new Array();
	var removeIdx = 0;

    if (omitIndex != undefined && toList.length && toList.options[omitIndex].value == "")
        toList.remove(omitIndex);
	
	for (i=0; i<fromList.length; i++)
		if (fromList.options[i].selected) {
			eOption = addOption(fromList.options[i].value, fromList.options[i].text);
			toList.options[toList.length] = eOption;
			
			removeList[removeIdx++] = i;
		}
		
	// remove all selected items
	for (i=removeList.length-1; i>=0; i--)
		fromList.remove(removeList[i]);
}

function moveAllItems(fromList, toList)
{
	for (i=0; i<fromList.length; i++) {
		eOption = addOption(fromList.options[i].value, fromList.options[i].text);
		toList.options[toList.length] = eOption;
	}
	fromList.length = 0;
}

function showMovie(fileName, divId, id, width, height, version, backgroundColor)
{
	var so = new SWFObject(WEB_URL + "/FLVPlayer.swf", id, width, height, version, backgroundColor);
	so.addParam("allowfullscreen", "true");
    so.addParam("scale", "noscale");
	so.addVariable("autostart","true");
	so.addVariable("quality", "high");
	so.addVariable("wmode", "transparent");
	so.addVariable("menu", "false");
	so.addVariable("width", width);
	so.addVariable("height", height);
	so.addVariable("displayheight", height);
	so.addVariable("backcolor","0x03204F");
	so.addVariable("frontcolor","0xffffff");
	so.addVariable("lightcolor","0xE39300");
	so.addVariable("file", fileName);
	so.write(divId);
}

function newcap()
{
	if (document.images) {
		var now = new Date();
		var reload = new Image();
		reload.src = WEB_URL + "/images/capimg.php?date=" + now.getTime();
	
		document.images["cap"].src = eval("reload.src");
		return true;
	}
    return false;
}

function checkUser(username)
{
	document.getElementById("iframe_user").src = WEB_URL + "/pages/page_registration/checkuser.php?user=" + username;
}

function checkAll(total)
{
	for (var i=0; i<total; i++)
		document.getElementById("cb" + i).checked = true;
}

function uncheckAll(total)
{
	for (var i=0; i<total; i++)
		document.getElementById("cb" + i).checked = false;
}

function checkSelected(chkName)
{
    var chk = document.getElementsByName(chkName);

	for (var i=0; i<chk.length; i++)
		if (chk[i].checked)
			return true;
	
	return false;
}

function toggleCheck(chkTotal, total)
{
	if (chkTotal.checked)
		checkAll(total);
	else
		uncheckAll(total);
}