function openWindow(){
	var url = arguments[0]||'/';
	var width = arguments[1]||500;
	if(width==null || width=='') width = 500;
	var height = arguments[2]||500;
	if(height==null || height=='') height = 500;
	var left = (screen.width - width) / 2;
	if(!left) left = 400;
	var top = (screen.height - height) / 2;
	if(!top) top = 150;
	window.open(url, '', 'width='+width+',height='+height+',left='+left+',top='+top+',resizable=no,scrollbars=no', false);
}
function openPage(TextName, Page)
{
  var mypage = Page;
  var myname = 'ReferencePage';
  var scroll='yes';
  var w = 800;
  var h = 600;
  var wint = 100;
  var winl = 100;
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  var settings  ='height='+h+',';
      settings +='width='+w+',';
      settings +='top='+wint+',';
      settings +='left='+winl+',';
      settings +='scrollbars='+scroll+',';
      settings +='resizable=yes';

  window.val_pass_obj=document.all[TextName];
  var win=window.open(mypage,myname,settings);
  win.focus();
  return false;
}
function openFile(value)
{
  value=value.replace(/&/g,'%26').substring(0,2047);
  var mypage = 'AutoUploadFile.aspx?file='+value;
  var myname = 'FileUploader';
  var scroll='yes';
  var w = 760;
  var h = 580;
  var wint = 100;
  var winl = 100;
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  var settings  ='height='+h+',';
      settings +='width='+w+',';
      settings +='top='+wint+',';
      settings +='left='+winl+',';
      settings +='scrollbars='+scroll+',';
      settings +='resizable=yes';

  var win=window.open(mypage,myname,settings);
  win.focus();
  return false;
}
function openRTF(TextName)
{
  var mypage = 'uc/RichTextEdit.aspx';
  var myname = 'RTFEditor';
  var scroll='yes';
  var w = 800;
  var h = 600;
  var wint = 100;
  var winl = 100;
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  var settings  ='height='+h+',';
      settings +='width='+w+',';
      settings +='top='+wint+',';
      settings +='left='+winl+',';
      settings +='scrollbars='+scroll+',';
      settings +='resizable=yes';

  window.val_pass_obj=document.all[TextName];
  var win=window.open(mypage,myname,settings);
  win.focus();
  return false;
}
function EditHTML(vitualDir,TextName,DivName)
{
	try
	{
		var text = new Object();
		text.save = "Save";
		text.cancel = "Close";

		var params = new Object();
		params.field = document.all[TextName];
		params.text = text;
		params.grid = document.all[DivName];

		document.all[TextName].value=window.showModalDialog( vitualDir + '/grid/htmleditor/htmleditor.htm', params, "help=no;status=no;resizable=yes");
		document.all[TextName].focus();
	}
	catch(e) {}
}
function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}
function isEmpty(o) {
    var i, v;
    if (isObject(o)) {
        for (i in o) {
            v = o[i];
            if (isUndefined(v) && isFunction(v)) {
                return false;
            }
        }
    }
    return true;
}
function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}
function isDate(dateStr) {
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?

	if (dateStr=='')
		return "";
		
	if (matchArray == null) {
		return "Nhap theo dinh dang [dd/mm/yyyy] !";
	}

	month = matchArray[3]; // parse date into variables
	day = matchArray[1];
	year = matchArray[5];

	if (day < 1 || day > 31) {
		return "Ngay phai giua 1 va 31 !";
	}
	
	if (month < 1 || month > 12) { // check month range
		return "Thang phai giua 1 va 12 !";
	}
	
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		return "Thang "+month+" khong co 31 ngay !";
	}

	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			return "Thang 2 " + "nam "+ year + " khong co " + day + " ngay!";
		}
	}
	 
	if ((year<1900) || (year>2099)) {
		return "Nam gioi han trong khoang 1900->2099 !";
	}
	return ""; // date is valid
}


function OpenUpload(vitualDir,TextName){

try
	{
  var mypage = vitualDir+'/include/uploadnow.aspx?file_id=';
  var myname = 'UploadPager';
  var scroll='yes';
  var w = 620;
  var h = 440;
  var wint = 100;
  var winl = 100;
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  var settings  ='height='+h+',';
      settings +='width='+w+',';
      settings +='top='+wint+',';
      settings +='left='+winl+',';
      settings +='scrollbars='+scroll+',';
      settings +='resizable=no';

  window.val_pass_obj=document.all[TextName];
  mypage +=window.val_pass_obj.value
  var win=window.open(mypage,myname,settings);
}
catch(e){alert('Error: OpenUpload '+e)}

  return false;
}

function ConfirmDel(ItemName) 
	{ 
		if (confirm("Ban muon xoa "+ItemName+" that khong ?")==true) 
		return true; 
		else 
		return false; 
	}
	
function CheckDel(CheckName,ItemName) 
	{ 
		if (CheckOne(CheckName,ItemName)==true) 
		{
			if (ConfirmDel(ItemName)==true)
			{
				return true; 
			}
		}

		return false; 
	}
function CheckOne(CheckName,ItemName)
{
		var oRadio = getControls(CheckName);
		var i=0; 

		if (typeof(oRadio)=='undefined') return false;
		if (typeof(oRadio.length)=='undefined')
		{
			if (oRadio.checked) return true;
		}
		for (;i<oRadio.length;i++)
		{		
			if (oRadio[i].checked) return true;
		}
		alert("Ban phai chon mot "+ItemName+" truoc khi thuc hien chuc nang nay !");
		return false;
	}

function CheckOnOff(CheckName)
	{
		var oRadio = document.all[CheckName];
		var i=0; 

		if (typeof(oRadio)=='undefined') return false;
		if (typeof(oRadio.length)=='undefined')
		{
			if (oRadio.checked) oRadio.checked=!oRadio.checked;
		}
		for (;i<oRadio.length;i++)
		{		
			if (oRadio[i].checked) oRadio[i].checked=!oRadio[i].checked;
		}
		return true;
	}
function doAddnew(url,id)
{
	doPost(url,id,'ADDNEW')
}
function doUpdate(url,id)
{
	doPost(url,id,'UPDATE')
}
function doDelete(url,id,text1,text2)
{
	if (CheckDel('chkDelete','ban ghi')==true)
	{
		doPost(url,id,'DELETE'+text1)
	}
}
function doGoto()
{
	var sArg = "";
	for (var i=0; i<arguments.length; i++)
		sArg += arguments[i];
		
	doPost("",sArg,"GOTO")
}
function doPost(url,id,cmd)
{
	var oForm = null;

	if (url=='' || url.indexOf('?')==0)
		oForm = document.forms["Form1"];
	else
		oForm = document.forms["fAutoSumitForEdit"];		

	if (oForm!=null)
	{
		if (oForm['vIdentifier']==null && (url=='' || url.indexOf('?')==0))
		{
			oForm = document.forms["fAutoSumitForEdit"];
		}
		
		if (oForm['vIdentifier']!=null)
		{
			if (typeof(oForm['vIdentifier'].length)!='undefined')
				oForm['vIdentifier'][0].value=id;
			else
				oForm['vIdentifier'].value=id;
		}

		if (oForm['vCommand']!=null)
		{
			if (typeof(oForm['vCommand'].length)!='undefined')
			{			
				oForm['vCommand'][0].value=cmd;
			}
			else
				oForm['vCommand'].value=cmd;		
		}

		oForm.action=url;
		oForm.submit();
	}
}
function getControl(c_name, c_tag)
{
	var i=0;
	if (c_tag==null) c_tag="input";
	for (i=0;i<=document.getElementsByTagName(c_tag).length-1;i++)
	{
	
		if ( (typeof(document.getElementsByTagName(c_tag)[i].name)!='undefined' && (document.getElementsByTagName(c_tag)[i].name==c_name || document.getElementsByTagName(c_tag)[i].name.indexOf(":"+c_name)>0 || document.getElementsByTagName(c_tag)[i].name.indexOf("_"+c_name)>0)) || (typeof(document.getElementsByTagName(c_tag)[i].id)!='undefined' && (document.getElementsByTagName(c_tag)[i].id==c_name || document.getElementsByTagName(c_tag)[i].id.indexOf(":"+c_name)>0 || document.getElementsByTagName(c_tag)[i].id.indexOf("_"+c_name)>0)) )
		{
			return document.getElementsByTagName(c_tag)[i];
		}
	}

	return null;
}
function doCheckForm()
{
/*
Argument: [s]namecontrol
s=1 ky tu
0=kieu so
2=kieu ngay>ngay hien tai
3=kieu ngay tuy
9=kieu so sanh 2 control/1 control voi so, rong, ngay
*/
	var sName = "";
	var sType = "";
	var sMessage = "";
	var oFocus = null;
	var bstop = false;
	var o;

	for (var i=0; i<arguments.length; i++) {
		sName = arguments[i]
		sType = sName.substring(0,1);
		sName = sName.substring(1,sName.length);

		if (sType=="0")
		{//check number
			o=getControl(sName);
			if (typeof(o)=='object' && o!=null)
				if (o.value!='' && IsNumeric(o.value)!=true)				
				{
				    var sTitle = "";
			        if (o.title) sTitle = o.title+": ";
			        
					bstop = true;
					sMessage += sTitle+"Sai kieu so!\n";
					if (oFocus == null)
						oFocus = o;
				}
		}
		if (sType=="2")
		{//check date
			o=getControl(sName);
			if (typeof(o)=='object' && o!=null)
			{
			    var sTitle = "";
			    if (o.title) sTitle = o.title+": ";
				if (o.value!='' && isDate(o.value)!='')
				{
					bstop = true;
					sMessage += sTitle+"Sai kieu ngay - "+isDate(o.value)+"\n";
					if (oFocus == null)
						oFocus = o;
				}
				else
				{
					var dToday = new Date();
					dToday = new Date(dToday.toDateString());
					
					if (o.value!='' && isDate(o.value)=='' && cDate(o.value)>=dToday)
					{
						sMessage += sTitle+"Ngay "+isDate(o.value)+" phai truoc ngay hom nay !\n";
						bstop = true;
						if (oFocus == null)
							oFocus = o;
					}
				}
			}
		} 
		if (sType=="3")
		{//check date
			o=getControl(sName);
			if (typeof(o)=='object' && o!=null)
				if (o.value!='' && isDate(o.value)!='')
				{
				    var sTitle = "";
			        if (o.title) sTitle = o.title+": ";
			        
					bstop = true;
					sMessage += sTitle+"Sai kieu ngay - "+isDate(o.value)+"\n";
					if (oFocus == null)
						oFocus = o;
				}
		} 
		if (sType=="9")
		{//check dk
			var sName1, sName2, sDK;
			if (sName.indexOf(">=")>0)
			{
				sName1=sName.split(">=")[0];
				sName2=sName.split(">=")[1];
				sDK=">=";
			}
			else if (sName.indexOf("<=")>0)
			{
				sName1=sName.split("<=")[0];
				sName2=sName.split("<=")[1];
				sDK="<=";
			}else if (sName.indexOf(">")>0)
			{
				sName1=sName.split(">")[0];
				sName2=sName.split(">")[1];
				sDK=">";
			}else if (sName.indexOf("<")>0)
			{
				sName1=sName.split("<")[0];
				sName2=sName.split("<")[1];
				sDK="<";
			}
			
			var o1,o2;
			o1=getControl(sName1);
			o2=getControl(sName2);
			//alert(sName1+ ' -----'+sName2+cDate(o1.value));

			if (typeof(o1)=='object' && o1!=null && typeof(o2)=='object' && o2!=null)
			{			
				if (o1.value!='' && o2.value!='')
				{
					var bDate=false;
					var sTitle1 = "";
					var sTitle2 = "";
			        if (o1.title) sTitle1 = o1.title+": ";
			        if (o2.title) sTitle2 = o2.title+": ";
			        
					if (cDate(o1.value)!=null) bDate=true;
					switch (sDK) {
						case ">=": 
							if (bDate && !(cDate(o1.value)>=cDate(o2.value)))
							{
								o1.focus();
								alert(sTitle1+"["+o1.value+"] phai >= "+sTitle2+"["+o2.value+"]");
								return false;
								}
							break;
						case "<=": 
							if (bDate && !(cDate(o1.value)<=cDate(o2.value)))
							{
								o1.focus();								
								alert(sTitle1+"["+o1.value+"] phai <= "+sTitle2+"["+o2.value+"]");
								return false;
								}
							break;
						case ">": 
							if (bDate && !(cDate(o1.value)>cDate(o2.value)))
							{
								o1.focus();								
								alert(sTitle1+"["+o1.value+"] phai > "+sTitle2+"["+o2.value+"]");
								return false;
								}
							break;
						case "<": 
							if (bDate && !(cDate(o1.value)<cDate(o2.value)))
							{
								o1.focus();
								alert(sTitle1+"["+o1.value+"] phai < "+sTitle2+"["+o2.value+"]");
								return false;
								}
							break;
					}
				}				
			}	//kthuc			
			else
			{
				//Loai 1 gia tri
				if (typeof(o1)=='object' && o1!=null && o2==null)
				{					
					if (sName2=='' && o1.value=='')
					{
					    var sTitle = "";
			            if (o1.title) sTitle = o1.title+": ";
			            
						o1.focus();
						alert(sTitle+"Khong duoc de trong !");
						return false;						
					}
					var bDate=false;
					var sTitle1 = "";
			        if (o1.title) sTitle1 = o1.title+": ";
			        
					if (cDate(o1.value)!=null && cDate(sName2)!=null ) bDate=true;					
					switch (sDK) {
						case ">=": 
							if (bDate && !(cDate(o1.value)>=cDate(sName2)))
							{
								o1.focus();
								alert(sTitle1+"["+o1.value+"] phai >= ["+sName2+"]");
								return false;
								}
							break;
						case "<=": 
							if (bDate && !(cDate(o1.value)<=cDate(sName2)))
							{
								o1.focus();
								alert(sTitle1+"["+o1.value+"] phai <= ["+sName2+"]");
								return false;
								}
							break;
						case ">": 
							if (bDate && !(cDate(o1.value)>cDate(sName2)))
							{
								o1.focus();
								alert(sTitle1+"["+o1.value+"] phai > ["+sName2+"]");
								return false;
								}
							break;
						case "<": 
							if (bDate && !(cDate(o1.value)<cDate(sName2)))
							{
								o1.focus();
								alert(sTitle1+"["+o1.value+"] phai < ["+sName2+"]");
								return false;
								}
							break;
					}
				}
			}
		} 
	}

	if (bstop==false)
	{
		if (typeof(Page_ClientValidate) == 'function') 
		{
			return Page_ClientValidate();
		}
		else 
			return true;
	}
	else
	{
		alert(sMessage);
		oFocus.focus();
	}
	return false;
}
function doRefreshImage(oFile,sImageName)
{
	doIsImage(oFile);
	var o=getControl(sImageName, "img");	
	if (typeof(o)=='object')
	{
		if (oFile.value!='')
			o.src=oFile.value;
	}
	return true;
}
function doIsImage(oFile)
{
	//check image type
	var aPart = oFile.value.split(".");
	var sExt = aPart[aPart.length-1].toLowerCase();
	var bImage = false;
	if (oFile.value=='' || sExt=='gif' || sExt=='jpg' || sExt=='jpeg' || sExt=='png' || sExt=='bmp' || sExt=='ico' || sExt=='pcx' )
		bImage = true;
		
	if (!bImage)
	{
		alert('Upload image file only!');
		return false;
	}
	else
		return true;
}
function doOpen(url)
{
	var o=window.open( url, 0, "resizable=1;help=0;status=0;");
	o.focus();
	return false;
}
function doReOrder(direction,id)
{
	doPost('',id,'REORDER_' + direction);
}
function doDataOnly(obj,text)
{
	var s = obj.value;
	
	for (var i=0;i<=s.length+1;i++)
	{
		if (text.indexOf(s.substring(i,1))<0)
		{
			alert("Please enter some char in ["+text+"]");
			return false;
		}	
	}
	return true;
}
function doFlyOverTable(e,table)
{
//alert(3);

	var field = e.target;
	var bIE = false;

	if (!field) bIE = true;
	if (!field) field=window.event.srcElement;

	for (var i=0;i<table.rows.length;i++) 
	{
		var row = table.rows[i];
		row.style.backgroundColor='';

		if ((bIE && row.contains(field)) || (!bIE && JScontains(row,field)))
		{
			if (i!=0 && row.style.backgroundColor!='#DDEEFF')
				row.style.backgroundColor='#DDEEFF';

			if(i==0)
			{
				for (var j=0;j<table.rows[i].cells.length;j++) 
				{
					var cell = table.rows[i].cells[j];
					cell.style.borderBottomColor='';

					if ((bIE && cell.contains(field)) || (!bIE && JScontains(cell,field)))
					{					
						if (cell.style.borderBottomColor!='#F9A900')
							cell.style.borderBottomColor='#F9A900';
					}
				}
			}
			//
		}
	}
}
function JScontains (container, containee) {
//for Mozilla, Netscape
//similar obj.contains() in IE
  while (containee) {
    if (container == containee) {
      return true;
    }
    containee = containee.parentNode;
  }
  return false;
}
function cDate(dateStr) {
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?

	if (dateStr=='')
		return null;
		
	if (matchArray == null) {
		return null;
	}

	month = matchArray[3]; // parse date into variables
	day = matchArray[1];
	year = matchArray[5];
	
	var d = new Date(month+"/"+day+"/"+year);
	return d;
}
function getControls(c_name, c_tag)
{
	var i=0,j=0;
	var orr=new Array();
	if (c_tag==null) c_tag="input";
	for (i=0;i<=document.getElementsByTagName(c_tag).length-1;i++)
	{
		if ( (typeof(document.getElementsByTagName(c_tag)[i].name)!='undefined' && (document.getElementsByTagName(c_tag)[i].name==c_name || document.getElementsByTagName(c_tag)[i].name.indexOf(":"+c_name)>0 || document.getElementsByTagName(c_tag)[i].name.indexOf("_"+c_name)>0)) || (typeof(document.getElementsByTagName(c_tag)[i].id)!='undefined' && (document.getElementsByTagName(c_tag)[i].id==c_name || document.getElementsByTagName(c_tag)[i].id.indexOf(":"+c_name)>0 || document.getElementsByTagName(c_tag)[i].id.indexOf("_"+c_name)>0)) )
		{
			orr[j]=document.getElementsByTagName(c_tag)[i];
			j++;
		}
	}

	return orr;
}
function doCheckAll(obj,chk_name)
{
	for(var i=0;i<getControls(chk_name).length;i++)
	{
		getControls(chk_name)[i].checked=obj.checked;
	}
}
function ChangeImage(file,img)
{
    var oimg=getControls(img,"img");
    if(file.value!='' && oimg.length>0)
    {
        oimg[0].src="";//file:///C:/3_02.jpg
        //oimg[0].src="file:///"+file.value.replace("\\","/");
        oimg[0].src="file:////"+file.value;
        oimg[0].alt=oimg[0].src;
    }
}
function Order(stt,evt)
{
   evt = (evt) ? evt : event;   // equalize event models
   var elem = (evt.target) ? evt.target : evt.srcElement;
   elem.parentNode.nextSibling.value=parseInt(elem.parentNode.nextSibling.value==""?"0":elem.parentNode.nextSibling.value)+stt;
}
function isNumericKeyStroke()
{

var returnValue = false;
var keyCode = (window.event.which) ? window.event.which : window.event.keyCode;
if ( ((keyCode >= 48) && (keyCode <= 57)) ||
((keyCode >= 96) && (keyCode <= 105)) ||
((keyCode >= 33) && (keyCode <= 40)) ||
(keyCode == 8) ||
(keyCode == 9) ||
(keyCode == 46) ||
(keyCode == 13) )
returnValue = true;

if ( window.event.returnValue )
window.event.returnValue = returnValue;
return returnValue;
}
function checkForInt(evt) {
var charCode = ( evt.which ) ? evt.which : event.keyCode;
return ( (charCode >= 48 && charCode <= 57) || charCode==45);
}
