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.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;
	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
//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) {
	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 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.")
	}
}
/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object    the table row
 * @param   interger  the row number
 * @param   string    the action calling this script (over, out or click)
 * @param   string    the default background color
 * @param   string    the color to use for mouseover
 * @param   string    the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 */
var marked_row = new Array;

function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 3.3 ... Opera changes colors set via HTML to rgb(r,g,b) format so fix it
    if (currentColor.indexOf("rgb") >= 0) 
    {
        var rgbStr = currentColor.slice(currentColor.indexOf('(') + 1,
                                     currentColor.indexOf(')'));
        var rgbValues = rgbStr.split(",");
        currentColor = "#";
        var hexChars = "0123456789ABCDEF";
        for (var i = 0; i < 3; i++)
        {
            var v = rgbValues[i].valueOf();
            currentColor += hexChars.charAt(v/16) + hexChars.charAt(v%16);
        }
    }

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
            // Garvin: deactivated onclick marking of the checkbox because it's also executed
            // when an action (like edit/delete) on a single item is performed. Then the checkbox
            // would get deactived, even though we need it activated. Maybe there is a way
            // to detect if the row was clicked, and not an item therein...
            // document.getElementById('id_rows_to_delete' + theRowNum).checked = true;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
            // document.getElementById('id_rows_to_delete' + theRowNum).checked = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
            // document.getElementById('id_rows_to_delete' + theRowNum).checked = false;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function
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();
	}
}