var editwin = null;
function setCheckedAll(obj, fr) {
	for (var i = 0; i < fr.elements.length; i++) {
		var e = fr.elements[i];
		if ((e.name != obj.name) && (e.type=='checkbox'))
			e.checked = obj.checked;
	}
}

function setCheckID(obj, fr) {
	var TB = TO = 0;
	for (var i = 0; i < fr.elements.length; i++) {
		var e = fr.elements[i];
		if ((e.name != obj.name) && (e.type == 'checkbox')) {
			TB++;
			if (e.checked)
				TO++;
		}
	}
	if (TO == TB)
		obj.checked = true;
	else
		obj.checked = false;
}
function OWEdit(File, W, H) {
	W += 50;
	H += 50;
	if(H > screen.availHeight)
		H = screen.availHeight - 50;
	if(W > screen.availWidth)
		W = screen.availWidth - 50;
	Temp = "scrollbars=2, resizable=yes, width=" + W + ", height = " + H;
	window.open(File, "FullImage", Temp);
}
function CheckMarkVal(strss, mark) {
	for(i = 0; i < mark.length && strss.indexOf(mark.charAt(i)) < 0; i++);
	return ( i < mark.length) ? false : true;
}


var reAlphanumeric = /^[a-zA-Z0-9]+$/;

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}
function isEmpty(s){
	return ((s == null) || (s.length == 0))
}
function isAlphanumeric(s){
	if (isEmpty(s) == false)
		return reAlphanumeric.test(s);
}
function isValidChar(s){
	if(isEmpty(s) == false){
		var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
		var temp;

		for (var i = 0; i < s.length; i++){
			temp = "" + s.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") return false;
		}
		return true;
	}
}

//This function is used to check the validation of email address.
function isEmail(value) {
	/*
		Email Address's Format: username@subdomain.domain
		Email Address must be include 3 part:
			part 1: username
			part 2: @
			part 3: <domainname[.domainname,...]>.<domainname>
	*/
	if (value==null || value=="")	return true;
	if (value.indexOf(" ")>=0)		return false;

	var state, code, username, domain, amountOfDot, i;
	state = 1; username=''; domain=''; amountOfDot = 0;
	for (i=0; i<value.length; i++) {
		code = value.charAt(i);
		if (state==1) {
			if (code == "<" || code == ">" || code == "(" || code == ")"	) return false;
			else if (	code == "@"	)
				if (username == '') return false;
				else state = 3;
			username += code;
		}
		else if (state==3) {
			if ((code >= "0" && code <= "9")
					|| (code >= "A" && code <= "Z")
					|| (code >= "a" && code <= "z")
					|| code == "_"
					|| code == "-"
				) ;
			else if (code == ".")
				if (domain == '' || domain.charAt(domain.length-1) == '.') return false;
				else amountOfDot++;
			else return false;
			domain += code;
		}
	}
	if (state != 3) return false;
	if (domain == '' || domain.charAt(domain.length-1) == '.') return false;
	if (amountOfDot <1) return false;
	return true;
}
//This function is used to check the validation of username.
function isUsername(userName)
{
  var pattern=/^[a-z][a-z0-9_-]{2,30}$/i;
  if (!pattern.test(userName)) return false;
  return true;
}
//var emailPatter=/^[-a-zA-Z0-9._]+@([-a-zA-Z0-9_]+\.)+[a-zA-Z0-9_]+$/;
//This function is used to check the validation of telephone, fax number.
function isTelephone(phone)
{
  var pattern = /^\+?[0-9 ()-.]+[0-9.]$/;
  var phoneval=trim(phone);
  if (!pattern.test(phoneval)) return false;
  return true;
}
///Kiem tra ngay thang hop le
function isNumber(num)
{
  var pattern = /^\+?[0-9]$/i;
  if (!pattern.test(num)) return false;
  return true;
}
//Dem so ky tu mark trong chuoi str
function countChar(str, mark) {
	var Count = 0;
	for(i = 0; i < str.length; i++)
		if (str.charAt(i) == mark) 
			Count++;
	return 	Count;
}
//Ham nay tra ve Ngay trong chuoi strDate duoc format thanh kieu so nguyen
function getDateToInt(strDate) {
	k = strDate.indexOf("/");			
	strDay = strDate.substring(0, k);
	return stringToInt(strDay);
}
//Ham nay tra ve Thang trong chuoi strDate duoc format thanh kieu so nguyen
function getMonthToInt(strDate) {
	i = strDate.lastIndexOf("/");
	j = strDate.lastIndexOf("/", i - 1) ;	
	strMonth = strDate.substring(j + 1, i);
	return stringToInt(strMonth);
}
//Ham nay tra ve Nam trong chuoi strDate duoc format thanh kieu so nguyen
function getYearToInt(strDate) {
	i = strDate.lastIndexOf("/");
	strYear = strDate.substring(i + 1, strDate.length);
	return stringToInt(strYear);
}
//
function checkIntVal(val, low, high) {
	if (val <= high && val >= low) 
		return true;
	return false;
}
//
function stringToInt(str) {
	if(!stringIsNum(str))
		return 0 ;
	if(str.charAt(0) == '0') 
		return stringToInt(str.substring(1, str.length));
	else 
		if(str.length > 0) 
			return parseInt(str);
		 else 
			return (0);
}
//
function stringIsNum(val) {
	var str = new String(val);
	for (i=0;i<str.length;i++) 
		if (isNaN(parseInt(str.charAt(i))))
			return false;
	return true;
}
//Kiem tra chuoi theo chuan ton tai cua chuoi mark trong chuoi str
function checkExistVal(str, mark) {
	for(i = 0; i < str.length; i++)
		if (mark.indexOf(str.charAt(i)) < 0) 
			return false;
	return 	true;
}
// Kiem tra ngay theo chuan (dd/mm/yyyy)
function checkFormatDate(strDate, sb_year, se_year) {
	if(!checkExistVal(strDate, '1234567890/') || countChar(strDate, '/') != 2)
		return false;
	Year = getYearToInt(strDate);
	Month = getMonthToInt(strDate);
	Day = getDateToInt(strDate);
	var objDate = new Date();
	var currYear = objDate.getYear();
	if(typeof(sb_year) == 'undefined')
		sb_year = currYear - 1;
	if(typeof(se_year) == 'undefined')
		se_year = currYear + 222;
	if(!checkIntVal(Year, sb_year, se_year))
		return false;	
	if(!checkIntVal(Month, 1, 12))
		return false;	
	if(!checkIntVal(Day, 1, 31))
		return false;	
	if (Month == 4 || Month == 6 || Month == 9 || Month == 11 )
		if(Day > 30)
			return false;
	if(((Year % 4 == 0) && (Year %100 != 0)) || Year % 400 == 0) {
		if((Month == 2 ) && ( Day > 29))
			return false;
	} else
		if((Month == 2 ) && ( Day > 28))
			return false;
	return 	true;
}
//
function checkFormatTime(strtime, hhd, hht) {
	if(!checkExistVal(strtime, '1234567890:') || countChar(strtime, ':') != 1)
		return false;
	k = strtime.indexOf(":");			
	strhh = stringToInt(strtime.substring(0, k));
	strmm = stringToInt(strtime.substring(k + 1, strtime.length));
	if(hhd > strhh || strhh > hht)
		return false;
	if(strmm < 0 || strmm > 59)
		return false;
	return true;
}
//
function checkTimeOk(ftime, ttime) {
	k = ftime.indexOf(":");			
	fhh = stringToInt(ftime.substring(0, k));
	fmm = stringToInt(ftime.substring(k + 1, ftime.length));
	j = ttime.indexOf(":");			
	thh = stringToInt(ttime.substring(0, j));
	tmm = stringToInt(ttime.substring(j + 1, ttime.length));
	if(fhh > thh)
		return false;
	if(fhh == thh)
		if(thh >= tmm)
			return false;
	return true;
}
//Kiem tra ngay bat dau va ket thuc co hop le khong
function checkCondDate(fromDate, toDate) {
	fromYear = getYearToInt(fromDate);
	fromMonth = getMonthToInt(fromDate);
	fromDay = getDateToInt(fromDate);
	toYear = getYearToInt(toDate);
	toMonth = getMonthToInt(toDate);
	toDay = getDateToInt(toDate);
	if(fromYear < toYear)
		return true;
	if(fromYear == toYear)
		if(fromMonth == toMonth) {
			if(fromDay <= toDay)
				return true;
		} else
			if(fromMonth < toMonth)
				return true;
	return false;
}
//Ham nay kiem tra ngay nhap vao so voi ngay hien hanh
function checkDateCurrent(strDate) {
	if(!checkFormatDate(strDate))
		return false;
	inputYear = getYearToInt(strDate);
	inputMonth = getMonthToInt(strDate);
	inputDate = getDateToInt(strDate);
	thisDate = new Date();
	currentDate = thisDate.getDate();
	currentMonth = thisDate.getMonth() + 1;
	currentYear = thisDate.getYear();
	if(inputYear > currentYear)
		return true;
	if(inputYear == currentYear)
		if(currentMonth == inputMonth) {
			if(currentDate < inputDate)
				return true;
		} else
			if(currentMonth < inputMonth)
				return true;
	return false;
}

function SetUnicode(iStr)
{
	for (i=0, oStr=''; i < iStr.length; )
	{
		if (iStr.charCodeAt(i)==38)
		{
			if (iStr.charCodeAt(i + 1)==35)
			{
				p=iStr.indexOf(';', i  + 2);
				if (p!=-1)
				{
					if (p - i <= 7)
					{
						if (isFinite(iStr.substr(i + 2, p - i - 2)))
						{
							oStr = oStr.concat(String.fromCharCode(iStr.substr(i + 2, p - i - 2)));
							i = p + 1;
							continue;
						}
					}
				}
			}

		}
	
		oStr=oStr.concat(iStr.charAt(i));
		i++;
	}
	
	return oStr;
}

function UnicodeSet(iStr)
{
	for (i=0, oStr=''; i < iStr.length; i++)
	{
		j=iStr.charCodeAt(i);
		
		if (j < 32 || j > 127 || j==34 || j==39)
		{
			oStr=oStr.concat('&#').concat(j).concat(';');
		}
		else
		{
			oStr=oStr.concat(iStr.charAt(i)); 
		}
	}
	
	return oStr;
}
function showWindow(url, isStatus, isResizeable, isScrollbars, isToolbar, isLocation, isFullscreen, isTitlebar, isCentered, width, height, top, left) {
	if(isCentered) {
		top = (screen.height - height) / 2;
		left = (screen.width - width) / 2;
	}

	open(url, '_blank', 'status=' + (isStatus ? 'yes' : 'no') + ','
	+ 'resizable=' + (isResizeable ? 'yes' : 'no') + ','
	+ 'scrollbars=' + (isScrollbars ? 'yes' : 'no') + ','
	+ 'toolbar=' + (isToolbar ? 'yes' : 'no') + ','
	+ 'location=' + (isLocation ? 'yes' : 'no') + ','
	+ 'fullscreen=' + (isFullscreen ? 'yes' : 'no') + ','
	+ 'titlebar=' + (isTitlebar ? 'yes' : 'no') + ','
	+ 'height=' + height + ',' + 'width=' + width + ','
	+ 'top=' + top + ',' + 'left=' + left);
}
function showDialog(url, width, height) {
	return showWindow(url, false, false, false, false, false, false, true, true, width, height, 0, 0);
}
function displayInfo(info)
{
	if (info == 'weather')
		showDialog('http://www3.tuoitre.com.vn/transweb/rams.htm', 492, 659);
	if (info == 'forex')
		showDialog('http://www3.tuoitre.com.vn/transweb/tygia.htm', 450, 362);
	if (info == 'gold')
		showDialog('http://www3.tuoitre.com.vn/transweb/giavang.htm', 450, 199);
	if (info == 'tvguide')
		showWindow('http://www3.tuoitre.com.vn/transweb/truyenhinh.htm', false, false, true, false, false, false, true, true, 450, 354, 0, 0);
	else if (info == 'lottery')
		showDialog('http://www3.tuoitre.com.vn/transweb/xoso.htm', 600, 344);
	if (info == 'stock')
		showDialog('http://www3.tuoitre.com.vn/transweb/chungkhoan.htm', 770, 750);
}
function aceptFileAttach(filename) {
	ii = filename.lastIndexOf(".");
	extfile = filename.substring(ii + 1, filename.length);
	switch(extfile) {
		case "doc":
		case "DOC":
		case "jpg":
		case "JPG":
		case "gif":
		case "GIF":
		case "bmp":
		case "BMP":
		case "htm":
		case "HTM":
		case "html":
		case "HTML":
		case "zip":
		case "ZIP":
		case "rar":
		case "RAR":
		case "avi":
		case "AVI":
		case "mpeg":
		case "MPEG":
		case "mpg":
		case "MPG":
		case "wmv":
		case "WMV":
		case "swf":
		case "SWF":
		case "pdf":
		case "PDF":
		case "xls":
		case "XLS":
		case "ppt":
		case "PPT":
			return true;
		default:
			return false;
	}
	return false;
}