function BreakItUp()
{
  //Set the limit for field size.
  var FormLimit = 102399
  var i = 1;

  //Get the value of the large input object.
  var TempVar = new String
  TempVar = document.form1.contents.value

  //If the length of the object is greater than the limit, break it
  //into multiple objects.
  if (TempVar.length > FormLimit)
  {
	document.form1.contents.value = TempVar.substr(0, FormLimit)
	TempVar = TempVar.substr(FormLimit)
	while (TempVar.length > 0)
	{
	  var objTEXTAREA = document.createElement("TEXTAREA")
	  objTEXTAREA.name = "content" 
	  objTEXTAREA.value = TempVar.substr(0, FormLimit)
	  document.form1.appendChild(objTEXTAREA)
	  TempVar = TempVar.substr(FormLimit)
	  i++
	}
  }
}
//javascript library
function open_date(s){
//	alert(open_date_now.style.display);
	if (s.options[s.selectedIndex].value	==	3){
		open_date_now.style.display='';
	}else{
		open_date_now.style.display='none';
	}
}
function cofrsub(str_message,form_action){		
   cf=confirm(str_message);
	if(cf){		
		window.form1.action	=	form_action;
		BreakItUp();
		window.form1.submit();
	}	
	return false;
}
function show_on_home_click(str_message,form_action){	
   cf=confirm(str_message);
	if(cf){		
		window.form1.action	=	form_action;
		window.form1.submit();
	}	
	return false;
}
function cfr_action(str_message,form_action){	
   cf=confirm(str_message);
	if(cf){		
		window.form1.action	=	form_action;
		window.form1.submit();
	}	
	return false;
}
function cfr_sub_action(str_message,form_action){	
   cf=confirm(str_message);
	if(cf){		
		window.form2.action	=	form_action;
		window.form2.submit();
	}	
	return false;
}
function back_to_manager(form_action){	
		window.location=form_action;
}
function open_help(URL) {
		popupWin = window.open(URL, 'popup', 'dependent=0,height=400,width=500,screenX=1,left=100,screenY=1,top=70,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0', 'launcher');
		if (window.focus) {
		popupWin.focus();
	}
}	  
function open_preview(URL) {	
		window.form1.action = URL;
		window.form1.target = '_blank';
		window.form1.submit();
}	  
function open_upload(URL) {
		popupWin = window.open(URL, 'popup', 'dependent=0,height=600,width=680,screenX=1,left=100,screenY=1,top=70,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars=1,status=1,toolbar=0', 'launcher');
		if (window.focus) {
		popupWin.focus();
	}
}	  
function do_refresh(actionString){   
		window.location=actionString;
}
function overTables(
		str_tableId, // table id (req.)
		num_headerOffset, // how many rows to skip before applying effects at the begining (opt.)
		num_footerOffset, // how many rows to skip at the bottom of the table (opt.)
		str_moverColor // background color for rows with mouse over (opt.)
	)
{
		// validate required parameters
	if (!str_tableId) 
		return alert ("No table(s) ID specified in parameters");

	var objTables = (document.all ? document.all[str_tableId] : document.getElementById(str_tableId));
	if (!objTables) 
		return alert ("Can't find table(s) with specified ID (" + str_tableId + ")");

	// set defaults for optional parameters
	var rowConfig = [];

	rowConfig.headerOffset = (num_headerOffset ? num_headerOffset : 0);
	rowConfig.footerOffset = (num_footerOffset ? num_footerOffset : 0);

	rowConfig.oddColor = "";
	rowConfig.evenColor = "";
	//rowConfig.onClickColor = "#cccccc";

	rowConfig.moverColor = (str_moverColor ? str_moverColor : '#6699cc');
	
	// init multiple tables with same ID
	if (objTables.length)
		for (var i = 0; i < objTables.length; i++)
			tt_initTable(objTables[i], rowConfig);
	// init single table
	else
		tt_initTable(objTables, rowConfig);
}


function tt_initTable (objTable, rowConfig) 
{
	objTable.style.cursor = "pointer";

	var lastClickedRow = [],
	col_trs = objTable.rows;
	for (var i = rowConfig.headerOffset; i < col_trs.length - rowConfig.footerOffset; i++) 
	{
		col_trs[i].config = rowConfig;
		col_trs[i].lastClickedRow = lastClickedRow;
		col_trs[i].setColor = tt_setColor;
		col_trs[i].onmouseover = tt_mover; 
		col_trs[i].onmouseout = tt_mout;
		col_trs[i].order = (i - rowConfig.headerOffset) % 2;
		col_trs[i].onmouseout();
	}
}
function tt_setColor(str_color) 
{
	//this.style.backgroundColor = str_color;
	for(i=0;i<this.cells.length;i++)
		this.cells[i].style.backgroundColor = str_color;
}

// event handlers
function tt_mover () 
{
	if (this.lastClickedRow.clicked != this)
		this.setColor(this.config.moverColor);
}

function tt_mout () 
{
	if (this.lastClickedRow.clicked != this)
		this.setColor(this.order ? this.config.oddColor : this.config.evenColor);
}

function tt_onClick () 
{
	if (this.lastClickedRow.clicked == this) 
	{
		this.lastClickedRow.clicked = null;
		this.onmouseover();
	}
	else 
	{
		var last_clicked = this.lastClickedRow.clicked;
		this.lastClickedRow.clicked = this;

		if (last_clicked) 
			last_clicked.onmouseout();

		this.setColor(this.config.onClickColor);
	}
}
function	NextList(_Page,manager_file)
{
	if (_Page=="Goto")
	{
		x	=	parseInt(frmSelectPage.AbsolutePage.value);
		if (isNaN(x))
		{
			alert("Please enter a numeric value");
			frmSelectPage.AbsolutePage.select();
			return;
		}
		else
		{
			frmSelectPage.AbsolutePage.value=x;
		}
	}
	else
	{
		frmSelectPage.AbsolutePage.value	=	_Page;
	}
//	document.frmSelectPage.hidOrderType.value	=	"GotoPage";
	document.frmSelectPage.action	=	manager_file;
	document.frmSelectPage.submit();
}
function expand_send_mail(){
//	alert(open_date_now.style.display);
	if(document.form1.click_send_mail.checked){
		open_send_mail.style.display='';
	}else{
		open_send_mail.style.display='none';
	}
}
function docheckone()
{
	var alen=document.form1.elements.length;
	var isChecked=true;
	alen=(alen>2)?document.form1.chkid.length:0;
	if (alen>0)
	{
		for(var i=0;i<alen;i++)
			if(document.form1.chkid[i].checked==false)
				isChecked=false;
	}else
	{
		if(document.form1.chkid.checked==false)
			isChecked=false;
	}				
	document.form1.chkall.checked=isChecked;
}
function docheck(status,from_)
{
	var alen=document.form1.elements.length;
	alen=(alen>2)?document.form1.chkid.length:0;
	if (alen>0)
	{
		for(var i=0;i<alen;i++)
			document.form1.chkid[i].checked=status;
	}else
	{
			document.form1.chkid.checked=status;
	}
	if(from_>0)
		document.form1.chkall.checked=status;
}
function calculatechon()
{			
	var strchon="";
	var alen=document.form1.elements.length;				
	alen=(alen>2)?document.form1.chkid.length:0;
	if (alen>0)
	{
		for(var i=0;i<alen;i++)
			if(document.form1.chkid[i].checked==true)
				strchon+=document.form1.chkid[i].value+",";
	}else
	{
		if(document.form1.chkid.checked==true)
			strchon=document.form1.chkid[i].value;
	}
	document.form1.checkID.value=strchon;					
}	
function doAction(ActionString,strMess){
    cfRestore=confirm(strMess);
	if (cfRestore){		
		document.form1.action=ActionString;	
			var alen=document.form1.elements.length;
			var isChecked=false;
			var isNum=true;
			alen=(alen>2)?document.form1.chkid.length:0;
			if (alen>0)
			{
				for(var i=0;i<alen;i++)
				{
					if(document.form1.chkid[i].checked==true)
					{
						isChecked=true;							
						break;
					}
				}
			}else
			{
				if(document.form1.chkid.checked==true)
					isChecked=true;
			}			
	if (isChecked){
		calculatechon();
		window.form1.submit();
		}
	else
		{alert("Ban phai chon it nhat mot tin");
		return false;
		}
	}	
}
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//============================================================================================================
// Kiem tra nhap lieu
//============================================================================================================
function validate_ex(theForm, _ErrMsg, arrNotRequired) {
	validForm = true;
	firstError = null;
	errorstring = '';
	var x = document.forms[theForm].elements;
	var arr = ','+arrNotRequired.toString()+ ',';
	for (var i=0;i<x.length;i++) {
		if (arr.indexOf(','+x[i].name+',')==-1) {
			if (!x[i].value)
			{
				var str="";
				str = "".concat(x[i].type);
				if (str.toString() != "undefined")
				{
					writeError(x[i], _ErrMsg);
					if (firstError ==null)
					firstError = x[i];
				}
			}
			if (x[i].name == 'txtEmail')
			{
				var strEmail = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
				if(!strEmail.test(x['txtEmail'].value))
					writeError(x['txtEmail'],' E-mail khong hop le!');
			}
			if (x[i].name == 'txtRePassword')
			{
				if (x[i].value != x['txtPassword'].value)
					writeError(x[i], ' Xac nhan mat khau khong hop le!');
			}
			if (x[i].name == 'chkAgree')
			{
				if (x[i].checked == false)
					writeError(x[i], ' Ban chua chap thuan dieu khoan!');
			}
		}
	}
	
	if (!W3CDOM)
		alert(errorstring);
//	if (firstError)

//		firstError.focus();
	if (validForm)
		return true;
		
	return false; // I return false anyway to prevent actual form submission. Don't do this at home!
}
function validate(theForm, _ErrMsg ) {
	validForm = true;
	firstError = null;
	errorstring = '';
	var x = document.forms[theForm].elements;
	for (var i=0;i<x.length;i++) {
		if (!x[i].value)
		{
			var str="";
			str = "".concat(x[i].type);
			if (str.toString() != "undefined")
			{
				writeError(x[i], _ErrMsg);
				if (firstError ==null)
				firstError = x[i];
			}
		}
		if (x[i].name == 'txtEmail')
		{
			var strEmail = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
				if(!strEmail.test(x['txtEmail'].value))
					writeError(x['txtEmail'],'**');
		}
		if (x[i].name == 'txtReNewPassword')
		{
			if (x[i].value != x['txtNewPassword'].value)
				writeError(x[i], '**');
		}
	}
	
	if (!W3CDOM)
		alert(errorstring);
	if (firstError)
		firstError.focus();
	if (validForm)
		return true;
		
	return false; // I return false anyway to prevent actual form submission. Don't do this at home!
}

function writeError(obj,message) {
	validForm = false;
	if (obj.hasError) return;
	if (W3CDOM) {
		obj.className += ' error';
		obj.onchange = removeError;
		var sp = document.createElement('span');
		sp.className = 'error';
		sp.appendChild(document.createTextNode(message));
		obj.parentNode.appendChild(sp);
		obj.hasError = sp;
	}
	else {
		errorstring += obj.name + ': ' + message + '\n';
		obj.hasError = true;
	}
	if (!firstError)
		firstError = obj;
}

function removeError() {
	this.className = this.className.substring(0,this.className.lastIndexOf(' '));
	this.parentNode.removeChild(this.hasError);
	this.hasError = null;
	this.onchange = null;
}
function validate_3(theForm, _ErrMsg, _MsgDiv, arrNotRequired) {
	validForm = true;
	firstError = null;
	errorstring = '';
	var x = document.forms[theForm].elements;
	var arr = ','+arrNotRequired.toString()+ ',';
	for (var i=0;i<x.length;i++) {
		if (arr.indexOf(','+x[i].name+',')==-1) {
			if (!x[i].value)
			{
				var str="";
				str = "".concat(x[i].type);
				if (str.toString() != "undefined" && !x[i].disabled )
				{
					writeError(x[i], _ErrMsg);
					if (firstError ==null)
					firstError = x[i];				
				}
			}
			//else
			if (x[i].name == 'txtEmail')
			{
				//if (x['email'].value.indexOf('@') == -1)
				//writeError(x['email'],'Hay nhap email hop le !');
				var strEmail = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
				if(!strEmail.test(x['txtEmail'].value))
					writeError(x['txtEmail'],'Hay nhap email hop le !');
			}
			if (x[i].name == 'txtConfirm')
			{
				if(x['txtConfirm'].value != x['txtPassword'].value)
					writeError(x['txtConfirm'],'Mat khau va xac nhan khong trung nhau !');
			}
			if(x[i].name == 'txtAccount')
			{
				var strAccount = /^[a-zA-Z0-9]{3,150}$/;
				if(!strAccount.test(x['txtAccount'].value))
					writeError(x['txtAccount'],'Ten tai khoan phai la ki tu hoac chu so !');
			}
			if(x[i].name == 'txtPassword')
			{
				if(x['txtPassword'].value.length < 6)
					writeError(x['txtPassword'],'Mat khau phai dai hon 6 ki tu !')
			}
		}
	}
	
	if (!W3CDOM)
		alert(errorstring);
	if (firstError)
		firstError.focus();
	if (validForm)
		return true;
//display message
	document.getElementById(_MsgDiv).style.visibility = "visible";	
	return false; // I return false anyway to prevent actual form submission. Don't do this at home!
}
//===== open weblinks===========
function changeme(sltlink,theForm)
{
	sForm = eval('document.' + theForm)
	if(document.getElementById(sltlink).options[document.getElementById(sltlink).selectedIndex].value != '') 
	{
		sForm.target = '_blank';
		sForm.action = document.getElementById(sltlink).options[document.getElementById(sltlink).selectedIndex].value;
		sForm.submit();
	}
}
//========== Button dem nguoc 10 seconds ===================
function fnSetTimeOut(btnSubmit,strVal)
{
	var objBtn = eval(btnSubmit)
	var secs = 10;
	var wait = secs * 1000;
	objBtn.disabled=true;
		
	for(i=1;i<=secs;i++) {
	 window.setTimeout("update(" + i + "," + wait + ","+btnSubmit+",'"+strVal+"')", i * 1000);
	}
	
	window.setTimeout("myTimer("+btnSubmit+")", wait);
	
}
function update(num,wait,btnSubmit,strVal) {
	var objBtn = eval(btnSubmit)
	if(num == (wait/1000)) {
		objBtn.value = strVal;
	}
 	else {
  		printnr = (wait/1000)-num;
  		objBtn.value = strVal + " (" + printnr + ")";
 	}
}

function myTimer(btnSubmit) {
	var objBtn = eval(btnSubmit)
	objBtn.disabled=false;
}