
function hidePopUp(controlID) {
    document.getElementById(controlID).className = "";
    document.getElementById(controlID).style.display = 'none';
    document.getElementById("bgFilter").style.display = 'none';


}
function showPopup(controlID) {
    var control = document.getElementById(controlID);
    if (control) {
        control.style.display = 'block';
        control.style.zIndex = '1003';
        var top = (window.screen.availHeight - control.offsetHeight) / 2 - 100;
        var left = (document.documentElement.offsetWidth - control.offsetWidth) / 2;
        left += (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
        top += (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;

        control.style.left = (left) + 'px';
        control.style.top = (top) + 'px';

        return true;
    }
    return false;
}
function ShowBoxUnSubcribe() {
    showPopup('boxUnSubcribe');
    document.getElementById("bgFilter").style.display = 'block';
    document.getElementById("bgFilter").style.zIndex = '1002';
    document.getElementById("bgFilter").style.height = document.getElementById('maindiv').offsetHeight + 'px';
}




var offsetfromcursorX = 12 //Customize x offset of tooltip
var offsetfromcursorY = 10 //Customize y offset of tooltip

var offsetdivfrompointerX = 10 //Customize x offset of tooltip DIV relative to pointer image
var offsetdivfrompointerY = 14 //Customize y offset of tooltip DIV relative to pointer image. Tip: Set it to (height_of_pointer_image-1).

document.write('<div id="nss_tip"></div>') //write out tooltip DIV
document.write('<img id="nss_tip_pointer" style="display:none;" src="">') //write out pointer image

var ie = document.all
var ns6 = document.getElementById && !document.all
var enabletip = false
if (ie || ns6)
    var tipobj = document.all ? document.all["nss_tip"] : document.getElementById ? document.getElementById("nss_tip") : ""

function ietruebody() {
    return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
}
function ddrivetip(title, images, sapo, e) {
    e.title = '';

    if (ns6 || ie) {
        tipobj.style.width = "300px"

        if (typeof thecolor != "undefined" && thecolor != "") tipobj.style.backgroundColor = "";

        tipobj.innerHTML = '<div class="contentip"><h3>' + title + '</h3><p>' + sapo + '</p></div>';

        enabletip = true
        return false;
    } 
}

function positiontip(e) {
    if (enabletip) {
        var nondefaultpos = false
        var curX = (ns6) ? e.pageX : event.clientX + ietruebody().scrollLeft;
        var curY = (ns6) ? e.pageY : event.clientY + ietruebody().scrollTop;
        //Find out how close the mouse is to the corner of the window
        var winwidth = ie && !window.opera ? ietruebody().clientWidth : window.innerWidth - 20
        var winheight = ie && !window.opera ? ietruebody().clientHeight : window.innerHeight - 20

        var rightedge = ie && !window.opera ? winwidth - event.clientX - offsetfromcursorX : winwidth - e.clientX - offsetfromcursorX
        var bottomedge = ie && !window.opera ? winheight - event.clientY - offsetfromcursorY : winheight - e.clientY - offsetfromcursorY

        var leftedge = (offsetfromcursorX < 0) ? offsetfromcursorX * (-1) : -1000

        //if the horizontal distance isn't enough to accomodate the width of the context menu
        if (rightedge < tipobj.offsetWidth) {
            //move the horizontal position of the menu to the left by it's width
            tipobj.style.left = curX - tipobj.offsetWidth + "px"
            nondefaultpos = true
        }
        else if (curX < leftedge)
            tipobj.style.left = "5px"
        else {
            //position the horizontal position of the menu where the mouse is positioned
            tipobj.style.left = curX + offsetfromcursorX - offsetdivfrompointerX + "px"
           // pointerobj.style.left = curX + offsetfromcursorX + "px"
        }

        //same concept with the vertical position
        if (bottomedge < tipobj.offsetHeight) {
            tipobj.style.top = curY - tipobj.offsetHeight - offsetfromcursorY + "px"
            nondefaultpos = true;
        }
        else {
            tipobj.style.top = curY + offsetfromcursorY + offsetdivfrompointerY + "px"
            //pointerobj.style.top = curY + offsetfromcursorY + "px";
        }
        tipobj.style.visibility = "visible"
        
    }
}
function hideddrivetip() {
    if (ns6 || ie) {
        enabletip = false;
        tipobj.style.visibility = "hidden";
        tipobj.style.left = "-1000px";
        tipobj.style.backgroundColor = '';
        tipobj.style.width = '';
    } 
}
document.onmousemove = positiontip;

/*executed when a number link is selected */
function doNumber (event){
 var eventSource = typeof event.target != 'undefined' ? event.target:window.event.srcElement;
 /*get the number portion of the class name of the event source */ 
 currPic = eventSource.id.substring(1,2) - 1;
 paused = true;
 doImageSwap();
 clearTimeout(timer1);
 clearTimeout(timer2);
 change(currPic,1);

} 

/* Initialization and Unobtrusive Javascript Calls */

	var arCatID = new Array("66","2","21","18","27", "6", "89","99", "76");
	function DisableAll()
	{
		for (var j=0; j<arCatID.length; j++)
			document.getElementById("ul_" + arCatID[j]).style.display = "none";
	}
	
		function FocusMenu()
	{
		var search = document.getElementById("search");
		 DisableAll();
		var menu = document.getElementById('menu').value;
		var liMenu = document.getElementById('li_' + menu);
		var liMenu_sub_ul = document.getElementById('ul_' + menu);
		liMenu_sub_ul.style.display="block";
		if(liMenu_sub_ul.style.left != "")
			search.style.display = "none";
		
		liMenu.getElementsByTagName("a")[0].className = "active";
	}
	
	function SetOnMouseOver()
	{
		var search = document.getElementById("search");
		 // Set event OnMouseOver
		for (var i=0; i<arCatID.length-1; i++) 
		{	    
			document.getElementById("li_" + arCatID[i]).onmouseover=function() {
				// Diable Submenu all
				DisableAll();
				var ul_submenu = this.getElementsByTagName("ul")[0];
				ul_submenu.style.display = "block";
				if(ul_submenu.style.left != "")
					search.style.display = "none";
				else
					search.style.display = "block";
			}
			 document.getElementById("li_" + arCatID[i]).onmouseout = function(){
				FocusMenu();
			 }
			
		}
	}
	
	function LoadCatChild(id, bool)
	{
		var spChildCurrent = document.getElementById("spChildCurrent_" + id);
		var spChildNext = document.getElementById("spChildNext_" + id);
		
		if(bool)
		{
			spChildCurrent.style.display="none";
			spChildNext.style.display="";
		}
		else
		{
			spChildCurrent.style.display="";
			spChildNext.style.display="none";
		}
		
	
	}
	
	function LoadImage(id, src) {
		if (id.getAttribute("loi") == null) {
			id.setAttribute("loi", "1");
		}
		else {
			id.setAttribute("loi", eval(id.getAttribute("loi")) + 1);
		}
		if (eval(id.getAttribute("loi")) >= 2) {
			var width = src.substr(src.lastIndexOf("=") + 1, src.length - src.lastIndexOf("="));
			id.onerror = null;
			id.src = "/Images/no_image.jpg";
		}
		else {
			id.src = src;
		}
	}
	
	function SearchKeyPress(event) {
		var textSearch = document.getElementById('txtSearch');
		var strSearch = textSearch.value;
		var url = "";
		var key;
		if (window.event) {
			key = window.event.keyCode;
			if (key == 13) {
				if (textSearch.value != "") {
					strSearch = strSearch.replace('?', '').replace(/"/g, '').replace(/^\s+|\s+$/g, '');
					if (strSearch == "") return false;
					else if (strSearch.indexOf("?") != -1) return false;
					strSearch = encodeURIComponent(strSearch);
					url = "/tim-kiem.htm?KeySearch=" + strSearch + "&PageIndex=1";
					document.location.href = url;
					event.keyCode = 0;
					return false;
				}
				else {
					alert("Bạn chưa nhập từ khóa tìm kiếm !");
					return false;
				}
			}
		}
		else {
			key = event.which;
			if (key == 13) {
				if (textSearch.value != "") {
					strSearch = strSearch.replace('?', '').replace(/"/g, '').replace(/^\s+|\s+$/g, '');
					if (strSearch == "") return false;
					else if (strSearch.indexOf("?") != -1) return false;
					strSearch = encodeURIComponent(strSearch);
					url = "/tim-kiem.htm?KeySearch=" + strSearch + "&PageIndex=1";
					window.location.href = url;
					return false;
				}
				else {
					alert("Bạn chưa nhập từ khóa tìm kiếm !");
	
					return false;
				}
	
			}
		}
	}
	function OnSearchClick() {
		var textSearch = document.getElementById('txtSearch');
		if (textSearch.value != "") {
			var strSearch = textSearch.value;
			strSearch = strSearch.replace('?', '').replace(/"/g, '').replace(/^\s+|\s+$/g, '');
			if (strSearch == "") return false;
			else if (strSearch.indexOf("?") != -1) return false;
			strSearch = encodeURIComponent(strSearch)
			url = "/tim-kiem.htm?KeySearch=" + strSearch + "&PageIndex=1";
			if (document.all) {
				window.location.href = url;
			}
			else {
				document.location.href = url;
			}
			return false;
		}
		else {
			alert("Bạn chưa nhập từ khóa tìm kiếm!");
			return false;
		}
	}
	function SearchFocus() {
		var textSearch = document.getElementById('txtSearch');
		text = textSearch.value;
		if (text == "...điền từ khoá cần tìm") textSearch.value = "";
	}
	
	function SearchBlur() {
		var textSearch = document.getElementById('txtSearch');
		text = textSearch.value;
		if (text == "") textSearch.value = "...điền từ khoá cần tìm";
	}
	function InputFocus(textdefault,control) {
		text = control.value;
		if (text == textdefault) control.value = "";
	}
	
	function InputBlur(textdefault,control) {
		text = control.value;
		if (text == "") control.value = textdefault;
	}




/**
* @package      Qvv
* @author       QuynhVV.
* @copyright    Copyright (C) 2010 . All rights reserved.
* @version      1.0
*/ 

function CheckSendMailForm()
{
	var form = document.getElementById('frmSendMail');
	
	if(!CheckRequired(form.to_name.value))
	{
		alert('Xin lỗi, bạn chưa nhập tên người nhận!');
		form.to_name.focus();
		return false;
	}
	if(form.to_email.value =="")
	{
		alert('Xin lỗi, bạn chưa nhập địa chỉ email người nhận!');
		form.to_email.focus();
		return false;
	}
	if(form.to_email.value !="" && !CheckEMail(form.to_email.value))
	{
		alert('Xin lỗi, địa chỉ email người nhận không hợp lệ!');
		form.to_email.select();
		return false;
	}
	
	if(!CheckRequired(form.from_name.value))
	{
		alert('Xin lỗi, bạn chưa nhập tên người gửi!');
		form.from_name.focus();
		return false;
	}
	if(form.from_email.value =="")
	{
		alert('Xin lỗi, bạn chưa nhập địa chỉ email người gửi!');
		form.from_email.focus();
		return false;
	}
	if(form.from_email.value !="" && !CheckEMail(form.from_email.value))
	{
		alert('Xin lỗi, địa chỉ email người gửi không hợp lệ!');
		form.from_email.select();
		return false;
	}
	if(form.messages.value =="")
	{
		alert('Xin lỗi, bạn chưa nhập lời nhắn!');
		form.messages.focus();
		return false;
	}
}

/**
 * Check comment form
 * @return
 */
function CheckCommentForm()
{
	var form = document.getElementById('frmComments');
	
	if(!CheckRequired(form.full_name.value))
	{
		alert('Xin lỗi, vui lòng nhập họ tên!');
		form.full_name.focus();
		return false;
	}
	if(form.sender_email.value =="")
	{
		alert('Xin lỗi, vui lòng nhập địa chỉ email của bạn!');
		form.sender_email.focus();
		return false;
	}
	if(form.sender_email.value !="" && !CheckEMail(form.sender_email.value))
	{
		alert('Xin lỗi, địa chỉ email không hợp lệ!');
		form.sender_email.select();
		return false;
	}
	
	SaveComments();
	setTimeout( "ShowHide()", 2*1000 );
	ShowHide('comments');
}

/**
 * Check comment form
 * @return
 */
function CheckComment()
{
	var form = document.getElementById('frmComments');
	
	if(!CheckRequired(form.comments.value))
	{
		alert('Xin lỗi, vui lòng cho biết ý kiến của bạn!');
		form.comments.focus();
		return false;
	}
	SaveComments();
	setTimeout( "ShowHide()", 2*1000 );
	ShowHide('comments-form');
}

function ShowHide(elementID)
{
	var obj = document.getElementById(elementID);
	if(obj.style.display == 'none')
		obj.style.display = 'block';
	else
		obj.style.display = 'none';
}
/********************************************************Libray functions*****************************************************************/
function CheckEMail(value)
{ 
	var regular = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; 
	return regular.test(value); 
}	

function CheckSpace(value)
{
	var regular = /\s/g;
	return regular.test(value); 
}

function CheckRequired(value)
{
	var regular = /[^.*]/;
	return regular.test(value);
}

function CheckAlpha(value)
{
	var regular = /^[a-z ._-]+$/i;
	return regular.test(value);
}

function CheckAlphaNum(value)
{
	var regular = /^[a-z0-9 ._-]+$/i;
	return regular.test(value);
}

function CheckInteger(value)
{
	var regular = /^[-+]?\d+$/;
	return regular.test(value);
}

function CheckDate(value)
{
	var regular = /^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$/;
	return regular.test(value);
}

function CheckPhoneNumber(value)
{
	var regular = /^[\d\s ().-]+$/;
	return regular.test(value);
}

function CheckUrl(value)
{
	var regular = /^(http|https|ftp)\:\/\/[a-z0-9\-\.]+\.[a-z]{2,3}(:[a-z0-9]*)?\/?([a-z0-9\-\._\?\,\'\/\\\+&amp;%\$#\=~])*$/i;
	return regular.test(value);
}

// Funtion wanning user 
function Note()
{
    var noteObj = $('note');
    noteObj.setHTML('Note: You have to fill the required fields!');
    noteObj.setStyle("opacity", 0);
    noteObj.effect("opacity", {
        duration: 500,
        transition: Fx.Transitions.linear
    }).start(0, 1);
    return true;
}             

// Show Hide elements function
function ShowHide(elementID)
{
	var element = document.getElementById(elementID);
	//alert(element.className);
	if(element.className == 'hide')
	{
		$("#"+elementID).fadeIn("slow").slideDown("slow"); 
		element.className = 'show';
	}
	else
	{
		$("#"+elementID).fadeIn("slow").slideUp("slow");
		element.className = 'hide';
	}
	return true;
}
// Clear form elements values:
function ClearRegisterForm()
{
    var form = document.membershipform;
    for($i=0; $i<form.elements.length; $i++)
    {
        form.elements[$i].value = "";
    }
    form.username.focus();
}

function CheckAll()
{
	var toggle = document.getElementsByName('toggle');
	if(toggle.checked == false)
		toggle.checked = true;
    else
    	toggle.checked = false;
    var objCheckBox = document.getElementsByName('cid[]');
    if(objCheckBox.length == 0)
    {
        if(objCheckBox.checked == true)
            objCheckBox.checked = false;
        else
            objCheckBox.checked = true;
    }
    else
    {
        if(objCheckBox[0].checked == true)
        {
            for(i = 0; i < objCheckBox.length; i++)
                objCheckBox[i].checked = false;
        }
        else
        {
            for(i = 0; i < objCheckBox.length; i++)
                objCheckBox[i].checked = true;
        }
    }    
}

function Redirect(url)
{
	document.location = url;
}

function DeleteUser(baseURL)
{
	var form = document.getElementById("frmUserList");
	form.action = baseURL+"/admin/user/delete";
	var result = ConfirmDelete();
	
	if(result) form.submit();
}

function DeleteUserGroup(baseURL)
{
	var form = document.getElementById("frmUserGroupList");
	form.action = baseURL+"/admin/user/deletegroup";
	var result = ConfirmDelete();
	
	if(result) form.submit();
}

function DeleteCategory(baseURL)
{
	var form = document.getElementById("frmCategoryList");
	form.action = baseURL+"/admin/category/delete";
	var result = ConfirmDelete();
	
	if(result) form.submit();
}

function DeleteNewsSource(baseURL)
{
	var form = document.getElementById("frmNewsSourceList");
	form.action = baseURL+"/admin/newsSource/delete";
	var result = ConfirmDelete();
	
	if(result) form.submit();
}

function DeleteAuthor(baseURL)
{
	var form = document.getElementById("frmAuthorList");
	form.action = baseURL+"/admin/author/delete";
	var result = ConfirmDelete();
	
	if(result) form.submit();
}

function DeleteNews(baseURL)
{
	var form = document.getElementById("frmNewsList");
	form.action = baseURL+"/admin/news/delete";
	var result = ConfirmDelete();
	
	if(result) form.submit();
}

function ConfirmDelete()
{
	var objCheckBox = document.getElementsByName('cid[]');
	var str = "";
	
	if(objCheckBox.length == undefined)
	{
		if(objCheckBox.checked == true)
			str += "Bạn có chắc chắn muốn xoá các mục được chọn không ?";
		else
			return false;
	}
	else
	{	
		var qty = 0;
		for(i = 0; i < objCheckBox.length; i++)
		{
			if(objCheckBox[i].checked == true)	
				qty++;
		}
		if(qty == 0)
			return false;
			
		if(qty == 1)
		{
			str += "Bạn có chắc chắn muốn xoá mục được chọn không?";
		}
		else
		{			
			str += "Bạn có chắc chắn muốn xoá ";
			str += qty;
			str += " mục được chọn không ?";
		}			
	}
	return confirm(str);
}

function Reload()
{
	setTimeout( "Refresh()", 2*1000 );
	Refresh();
}

function Refresh()
{
	window.location.reload(true);
}


function CheckAllActionsOf(controlerName)
{
	var actions = new Array("index", "create", "edit", "delete", "detail", "list","login", "logout", "creategroup", "editgroup", "deletegroup", "detailgroup", "listgroup","editmedia","publishmedia", "setmediaprimary", "deletemedia");
	for (var i = 0; i < actions.length; i++)
	{
		var checkBoxID = 'acl_resources-admin_'+ controlerName +'_'+ actions[i];
		var objCheckBox = document.getElementById(checkBoxID);
		if(objCheckBox)
		{
			//alert(objCheckBox.checked);
			if(objCheckBox.checked == false)
				objCheckBox.checked = true;
			else if(objCheckBox.checked == true)
				objCheckBox.checked = false;
		}
	}
}

function AddMedia(type)
{
	if(type == 'image')
	{
		if(document.getElementById('image_'+numberImageMax).className == "show" ) 
		{
			alert("Chú ý: Bạn chỉ được phép thêm tối đa "+numberImageMax+" ảnh.");
			return true;
		}
		for(var i=1; i<=numberImageMax; i++)
		{
			var obj = document.getElementById('image_'+i);
			if(obj.className == 'hide')
			{
				obj.className = 'show';
				return true;
			}
		}
	}
	else if(type == 'video')
	{
		if(document.getElementById('video_'+numberVideoMax).className == "show" ) 
		{
			alert("Chú ý: Bạn chỉ được phép thêm tối đa "+numberVideoMax+" video.");
			return true;
		}
		for(var i=1; i<=numberVideoMax; i++)
		{
			var obj = document.getElementById('video_'+i);
			if(obj.className == 'hide')
			{
				obj.className = 'show';
				return true;
			}
		}
	}
}

function RemoveMedia(type)
{
	if(type == 'image')
	{
		for(var i=numberImageMax; i>=1; i--)
		{
			var obj = document.getElementById('image_'+i);
			if(obj.className == 'show')
			{
				obj.className = 'hide';
				return true;
			}
		}
	}
	else if(type == 'video')
	{
		for(var i=numberVideoMax; i>=1; i--)
		{
			var obj = document.getElementById('video_'+i);
			if(obj.className == 'show')
			{
				obj.className = 'hide';
				return true;
			}
		}

	}
}


