function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


function setWaiting(id)
{
	var html = '<img src="images/icons/wait1e.gif" /> ';
	html += '<font style="font-style:italic;">Đang thực hiện tải dữ liệu. Vui lòng chờ trong ít phút...</font>';
	setInnerElementById(id, html);
}

function checkValueElementById(id)
{
	var e = document.getElementById(id);
	if (e && e.value)
	{
		return true;
	}
	if (e) e.focus();
	return false;
}

function checkNumberElementById(id)
{
	var e = document.getElementById(id);
	if (e && e.value && e.value*1>-1)
	{
		return true;
	}
	if (e) e.focus();
	return false;
}

function getValueElementById(id)
{
	var e = document.getElementById(id);
	return e ? e.value : '';
}

function setFocusById(id)
{
	var e = document.getElementById(id);
	if (e){
		e.focus();
	}
}

function setValueElementById(id, value)
{
	var e = document.getElementById(id);
	if (e)
	{
		e.value = '';
		e.value = value;
	}
}

function setInnerElementById(id, html)
{
	var e = document.getElementById(id);
	if (e){
		e.innerHTML = html;
	}
}

function setDisabledById(id, value)
{
	var e = document.getElementById(id);
	if (e){
		e.disabled = value;
	}
}

function in_array(value, arr)
{
	var n = arr.length;
	var i=0;
	for(i=0; i<n; i++)
	{
		if (arr[i]==value)
		{
			return true;
		}
	}
	return false;
}
function array_indexOf(value, arr)
{
	var n = arr.length;
	var i=0;
	for(i=0; i<n; i++)
	{
		if (arr[i]==value)
		{
			return i;
		}
	}
	return -1;
}

function isEmail(id) 
{
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var e = document.getElementById(id);
   if (e && e.value)
   {
		if( reg.test(e.value) )
		{
			return true;
		}
		e.focus();
   }
   return false;
}
/**************************************/
function isCheckAll(idParent, nameChild)
{
	var eParent = document.getElementById(idParent);
	if (eParent)
	{
		eParent.checked=false;
		var arrCheck = getArrCheck(nameChild);
		var bolFlag = 1;
		var n = arrCheck.length;
		for (var i=0; i<n; i++)
		{
			if (!arrCheck[i].checked)
			{
				bolFlag = 0;
				break;
			}
		}
		eParent.checked=bolFlag;
	}
}
function setCheckAll(nameChild, value)
{

	var arrCheck = getArrCheck(nameChild);
	var n = arrCheck.length;
	for (var i=0; i<n; i++)
	{
		arrCheck[i].checked = value;
	}

}
function getArrCheck(strName)
{
	var arrElement = document.getElementsByTagName('input');
	var n = arrElement.length;
	var arrRet = new Array();
	var index =0 ;
	for (var i=0; i<n; i++)
	{
		if (arrElement[i].name==strName)
		{
			arrRet[index++] = arrElement[i];
		}
	}
	return arrRet;
}
function getListCheckedValue(nameChild)
{
	var strRet = "";
	var arrCheck = getArrCheck(nameChild);
	var n = arrCheck.length;
	for (var i=0; i<n; i++)
	{
		if (arrCheck[i].checked && arrCheck[i].disabled==false)
		{
			strRet += strRet ? ","+arrCheck[i].value : arrCheck[i].value;
		}
	}
	return strRet;
}
function setCheckedByValue(nameChild, value)
{
	var arrCheck = getArrCheck(nameChild);
	var n = arrCheck.length;
	for (var i=0; i<n; i++)
	{
		if (arrCheck[i].value==value)
		{
			arrCheck[i].checked=true;
			break;
		}
	}
}
/**************************************/

function openPopupCenter(popupwidth, popupheight, strPath, strName, boolWriteWait, scrollbars) 
{
    var popup_width		= popupwidth;
    var popup_height	= popupheight;
    var screen_width	= window.screen.width;
    var screen_height	= window.screen.height;
    var popup_left		= Math.round((screen_width-popup_width)/2);
    var popup_top		= Math.round((screen_height-popup_height)/2);
	var scroll_bars		= scrollbars ? scrollbars : 0;
	strName = strName.replace(/ /,'');
	window.open(strPath, strName, "status,height="+popup_height+",width="+popup_width+",resizable=yes,left="+popup_left+",top="+popup_top+",scrollbars=yes"+",menubar=0,status=1,fullscreen=0");
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function checkLengthContent(id, maxlength)
{
	var content = getValueElementById(id);
	if (content.length<=maxlength){
		return true;
	}
	return false;
}

function subContentByLength(id, maxlength)
{
	var e = document.getElementById(id);
	if (e)
	{
		e.value = e.value.substring(0, maxlength);
	}
}

function setLoading(id)
{
	document.getElementById(id).innerHTML='<div align="center"><img src="images/icons/loading.gif" border="0"/></div>';
}

//Convert no Vn
function setConvertNoVn(strInput)
{
	var arrCharFrom= new Array(
	   "ạ","á","à","ả","ã","Ạ","Á","À","Ả","Ã",
	   "â","ậ","ấ","ầ","ẩ","ẫ","Â","Ậ","Ấ","Ầ","Ẩ","Ẫ",
	   "ă","ặ","ắ","ằ","ẳ","ẫ","Ă","Ắ","Ằ","Ẳ","Ẵ","Ặ",
	   "ê","ẹ","é","è","ẻ","ẽ","Ê","Ẹ","É","È","Ẻ","Ẽ",
	   "ế","ề","ể","ễ","ệ","Ế","Ề","Ể","Ễ","Ệ",
	   "ọ","ộ","ổ","ỗ","ố","ồ","Ọ","Ộ","Ổ","Ỗ","Ố","Ồ","Ô","ô",
	   "ó","ò","ỏ","õ","Ó","Ò","Ỏ","Õ",
	   "ơ","ợ","ớ","ờ","ở","ỡ",
	   "Ơ","Ợ","Ớ","Ờ","Ở","Ỡ",
	   "ụ","ư","ứ","ừ","ử","ữ","ự","Ụ","Ư","Ứ","Ừ","Ử","Ữ","Ự",
	   "ú","ù","ủ","ũ","Ú","Ù","Ủ","Ũ",
	   "ị","í","ì","ỉ","ĩ","Ị","Í","Ì","Ỉ","Ĩ",
	   "ỵ","ý","ỳ","ỷ","ỹ","Ỵ","Ý","Ỳ","Ỷ","Ỹ",
	   "đ","Đ"
	);
		// 0
	var arrCharEndNoVn= new Array(
		"a","a","a","a","a","A","A","A","A","A",
		"a","a","a","a","a","a","A","A","A","A","A","A",
		"a","a","a","a","a","a","A","A","A","A","A","A",
		"e","e","e","e","e","e","E","E","E","E","E","E",
		"e","e","e","e","e","E","E","E","E","E",
		"o","o","o","o","o","o","O","O","O","O","O","O","O","o",
		"o","o","o","o","O","O","O","O",
		"o","o","o","o","o","o",
		"O","O","O","O","O","O",
		"u","u","u","u","u","u","u","U","U","U","U","U","U","U",
		"u","u","u","u","U","U","U","U",
		"i","i","i","i","i","I","I","I","I","I",
		"y","y","y","y","y","Y","Y","Y","Y","Y",
		"d","D"
	);
	
	var n = strInput.length;
	for(i=0; i<n; i++)
	{
		var index = array_indexOf(strInput[i], arrCharFrom);
		if (index>-1)
		{
			var char1 = arrCharFrom[index];
			var char2 = arrCharEndNoVn[index];
			strInput = strInput.replace(char1, char2);
		}
	}
	return strInput;
}
//Convert no Vn

function titleCase(string)
{
	var result = '';
	var nLength = string.length;
	for(i=0; i<nLength; i++)
	{
		var bol = true;
		var c = string[i].toLowerCase();
		if (i==0 || (i>0 && result[i-1]==' ' && result[i]!=' ') )
		{
			c = c.toUpperCase();
		}
		result+=c;
	}
	return result;
}

function getSelText()
{
    var txt = '';
	if (window.getSelection)
    {
        txt = window.getSelection();
	}
    else if (document.getSelection) // FireFox
    {
        txt = document.getSelection();
	}
    else if (document.selection)  // IE 6/7
    {
        txt = document.selection.createRange().text;
	}
    else return;
	return txt;
}

function getValueRadio(idForm, nameRadio)
{
	var e = document.getElementById(idForm);
	if (e)
	{
		var arrInput = e.getElementsByTagName('input');
		var n = arrInput.length;
		for(i=0; i<n; i++)
		{
			if (arrInput[i].type=='radio' && arrInput[i].name==nameRadio && arrInput[i].checked)
			{
				return arrInput[i].value;
			}
		}
	}
	alert('Can not read value.');
}

function getValueCheckbox(id)
{
	var e = document.getElementById(id);
	if (e && e.checked)
	{
		return e.value;
	}
	return '';
}

function setHomepage(url)
{
	if (document.all)
    {
		document.body.style.behavior='url(#default#homepage)';
		document.body.setHomePage(url);
	}
	else if (window.sidebar)
    {
		if(window.netscape)
		{
			try
			{
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			}
			catch(e)
			{
				var strAlert = "Trình duyệt của bạn hiện tại không cho phép thực hiện chức năng này.";
				strAlert += "\nNếu bạn muốn thực hiện thao tác này, vui lòng thực hiện theo các bước sau:";
				strAlert += "\n1) Mở một tab, hoặc cửa sổ mới bằng trình duyệt hiện tại.";
				strAlert += "\n2) Nhập giá trị [about:config] vào thanh địa chỉ của bạn.";
				strAlert += "\n3) Nhập giá trị [signed.applets.codebase_principal_support] vào công cụ Fillter, sau đó nhấn Show All.";
				strAlert += "\n4) Thay đổi giá trị của dòng dữ liệu tìm thấy được bằng [true].";
				strAlert += "\n\nXin chân thành cảm ơn quý khách. Ban quản trị sinhloi.com";
				alert(strAlert);
			}
		}
		var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
		prefs.setCharPref('browser.startup.homepage',url);
	}
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function expandE(id, value)
{
	if (value=='min')
	{
		$('#'+id).hide('slow');
	}
	else
	{
		$('#'+id).show();
	}
}

