var editwin = null;

function OWSend(File, W, H) {
	//Temp = "left=1, top=1, alwaysRaised=no, status=no, resizable=no, scrollbars=no, toolbar=0, directories=0, menubar=0, location=0, copyhistory=0, width=" + W + ", height = " + H;
	Temp = "left=0,top=1,scrollbars=2, resizable=no, width=" + W + ", height = " + H;	
	window.open(File, "FullImage", Temp);
}

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.disabled != true))
			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') && (e.disabled != true)) {
			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(W > 780)	W = 780;
	if(H > 600)	 H = 600;
	winDef = "scrollbars=2, resizable=yes, width=" + W + ", height = " + H;
	winhandle = window.open(File, "FullImage", winDef);
	winhandle.focus();
}

function OWImg(vLink, vWidth, vHeight ) 
{
	winDef = 'status=no,resizable=yes,scrollbars=2,toolbar=no,location=no,fullscreen=no,titlebar=yes,height='.concat(vHeight).concat(',').concat('width=').concat(vWidth).concat(',');
	winDef = winDef.concat('top=').concat((screen.height - vHeight)/2).concat(',');
	winDef = winDef.concat('left=').concat((screen.width - vWidth)/2);
	//newwin = open('', '_blank', winDef);
	newwin = open('', '_self', winDef);
	newwin = window.open(vLink, "FullImage", winDef);
	newwin.document.writeln('<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">');
	newwin.document.writeln('<a href="" onClick="window.close(); return false;">') ;
	newwin.document.writeln('<img src="', vLink, '" alt="Close window" border=0>');
	newwin.document.writeln('</a>');	
	newwin.document.writeln('</body>');
	newwin. focus();

}

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
//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);
}
//
//Ham nay tra ve Ngay trong chuoi strDate duoc format dd/mm/yyyy H:i
function getDaytimeToInt(strDate) {
	k = strDate.indexOf("/");			
	strDay = strDate.substring(0, k);
	return stringToInt(strDay);
}
//Ham nay tra ve Thang trong chuoi strDate duoc format dd/mm/yyyy H:i
function getMonthtimeToInt(strDate) {
	i = strDate.indexOf("/");
	strMonth = strDate.substring(i + 1, i+3);
	return stringToInt(strMonth);
}
//Ham nay tra ve Nam trong chuoi strDate duoc format format dd/mm/yyyy H:i
function getYeartimeToInt(strDate) {
	i = strDate.lastIndexOf("/");
	strYear = strDate.substring(i + 1, i+5);
	return stringToInt(strYear);
}
//Ham nay tra ve Hour trong chuoi strDate duoc format format dd/mm/yyyy H:i
function getHourtimeToInt(strDate) {
	i = strDate.indexOf(":");
	strHour = strDate.substring(i-2, i);
	return stringToInt(strHour);
}
//Ham nay tra ve Minute trong chuoi strDate duoc format format dd/mm/yyyy H:i
function getMinutetimeToInt(strDate) {
	i = strDate.lastIndexOf(":");
	strMinute = strDate.substring(i+1, strDate.length);
	return stringToInt(strMinute);
}

//
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;
}
// Kiem tra ngay theo chuan (dd/mm/yyyy H:i)
function checkFormatDatetime(strDate, sb_year, se_year) {
	if(!checkExistVal(strDate, '1234567890/: ') || countChar(strDate, '/') != 2 || countChar(strDate, ':') != 1 || countChar(strDate, ' ') != 1)
		return false;
	Year = getYeartimeToInt(strDate);
	Month = getMonthtimeToInt(strDate);
	Day = getDaytimeToInt(strDate);
	Hour = getHourtimeToInt(strDate);
	Minute = getMinutetimeToInt(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;
	if((Hour > 23) || ( Minute > 59)) 
		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;
}
//Kiem tra ngay bat dau va ket thuc co hop le khong
function checkCondDatetime(fromDate, toDate) {
	fromYear = getYeartimeToInt(fromDate);
	fromMonth = getMonthtimeToInt(fromDate);
	fromDay = getDaytimeToInt(fromDate);
	fromHour=getHourtimeToInt(fromDate);
	fromMinute=getMinutetimeToInt(fromDate);
	toYear = getYeartimeToInt(toDate);
	toMonth = getMonthtimeToInt(toDate);
	toDay = getDaytimeToInt(toDate);
	toHour=getHourtimeToInt(toDate);
	toMinute=getMinutetimeToInt(toDate);
	if(fromYear < toYear)
		return true;
	if(fromYear == toYear)
		if(fromMonth == toMonth) {
			if(fromDay == toDay){
				if(fromHour==toHour){
					if(fromMinute<=toMinute) return true;
				}else 
					if(fromHour<toHour) return true;
			}else 
				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, sb_year, se_year) {
	if(!checkFormatDate(strDate, sb_year, se_year))
		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 printArticle() {
	var agt=navigator.userAgent.toLowerCase();
	if (window.print) {
		setTimeout('window.print();',200);
	}
	else if (agt.indexOf("mac") != -1) {
		alert("Press 'Cmd+p' on your keyboard to print article.");
	}
	else {
		alert("Press 'Ctrl+p' on your keyboard to print article.")
	}
}

function kiemtrangay(date,month)
{
	switch(month)
	{
		case '04':
		case '06':
		case '09':
		case '11': 
			if(date>'30') return 0;
			break;
		case '02':
			if(date>'28') return 0;
			break;
	}
	return 1;
}
function kiemtrangaythangofsearch()
{
	date=searchtin.ngay.options[searchtin.ngay.selectedIndex].value;
	if(date.length<2) date="0"+date;
	month=searchtin.thang.options[searchtin.thang.selectedIndex].value;
	if(month.length<2) month="0"+month;
	year=searchtin.nam.value;
	if(!kiemtrangay(date,month))
	{
		alert("Date invalid, please select date again!");
		return;
	}
	else
	{
		searchtin.ketqua.value=year+"-"+month+"-"+date;
		if(date=="00")
		{
			searchtin.ketqua.value=year+"-"+month+"-";
			//alert(searchtin.ketqua.value);
		}
		document.searchtin.submit();
	}
}

//------------- Status message . begin ------------------
var timerId;
var counter = 0;
var nWait = 0.1
if (timerId != null) // is the timer already running?
   clearTimeout(timerId);

function pad() {
  var padding = "";
  for (var n=0; n<=(100); n++)
      padding += " ";
  return(padding);
}

function scroll() {
  window.status = newMsg.substring(counter,newMsg.length);
  if (counter == newMsg.length) {
     counter = 0;
  }
  counter ++;
  timerId = setTimeout("scroll()",nWait * 1000);
}

function startScroll(cMsgIn,nWaitIn) {
  msg = cMsgIn
  nWait = nWaitIn
  newMsg = pad() + msg + " ";
  scroll()
}
//------------- Status message . end ------------------
