﻿
// JScript File
function isEmpty(ctl)
{    
    
    if (eval(ctl).value=='')
    {
        eval(ctl).focus();
        return true;
    }    
    return false;
}

function dtgSwapBG(obj, bgNew) {	
		tmp=obj.parentElement.parentElement.style.backgroundColor;		
		if (bgNew!=tmp) 
			bgOld=obj.parentElement.parentElement.style.backgroundColor;
		obj.parentElement.parentElement.style.backgroundColor=(obj.parentElement.parentElement.style.backgroundColor==bgNew) ?   bgOld : bgNew ;
}

// popCenter
function popCenter(URL,name,w,h) {	
    if (w == -1)
    {
        w = screen.width;
        h = screen.height;
    }
	l = (screen.width - w) / 2 ;
	t = (screen.height - h) / 2;
		
	params = '';
	//params = 'toolbars=1, location=0, statusbars=0, menubars=1, resizable=1,';
	popCenterWin = window.open(URL, name, params + 'width=' + w + ', height=' + h + ', left=' + l + ', top=' + t + ', location=0,status=0,scrollbars=1');
	popCenterWin.focus();
	return false;
}

function popHoSo(URL,name,w,h) {	
	l = 10 ;
	t = 10;
		
	params = '';
	//params = 'toolbars=1, location=0, statusbars=0, menubars=1, resizable=1,';
	popCenterWin = window.open(URL, name, params + 'width=' + w + ', height=' + h + ', left=' + l + ', top=' + t + ', location=0,status=0,scrollbars=1;resizable: Yes');
	popCenterWin.focus();
	return false;
}

function popCenter_TLKN(URL,name,w,h) {	
    if (w == -1)
    {
        w = screen.width;
        h = screen.height;
    }
	l = (screen.width - w) / 2 ;
	t = (screen.height - h) / 2;	
	window.open(URL,name,'width=' + w + ', height=' + h + ', left=' + l + ', top=' + t + ', location=0,status=0,scrollbars=0;resizable: Yes;');
	return false;
}
// popCenter
function popCenter1(URL,name,w,h) {	
			
	params = '';
	//params = 'toolbars=1, location=0, statusbars=0, menubars=1, resizable=1,';
	popCenterWin = window.open(URL, name, params + 'width=' + w + ', height=' + h + '",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=700,height=500,top=10,left=0');
	popCenterWin.focus();
	
	return false;
}

// popCenter
function popCenterSV(URL,name) {	
    w = screen.width / 2;
    h = screen.height / 2;
	l = (screen.width - w) / 2 - 20 ;
	t = (screen.height - h) / 2 - 40;
		
	params = '';
	//params = 'toolbars=1, location=0, statusbars=0, menubars=1, resizable=1,';
	popCenterWin = window.open(URL, name, params + 'width=' + (w + 100) + ', height=' + (h + 50) + ', left=' + l + ', top=' + t + ', location=0,status=0,scrollbars=1');
	popCenterWin.focus();
	return false;
}
function popCenterSV1(URL,name) {	
    w = screen.width / 2;
    h = screen.height / 2;
	l = (screen.width - w) / 2 - 20 ;
	t = (screen.height - h) / 2 - 40;
		
	params = '';
	//params = 'toolbars=1, location=0, statusbars=0, menubars=1, resizable=1,';
	popCenterWin = window.open(URL, name, params + 'width=' + (w + 100) + ', height=' + (h + 150) + ', left=' + l + ', top=' + t + ', location=0,status=0,scrollbars=1');
	popCenterWin.focus();
	return false;
}
// popCenter
function popModalCenter(URL,name,w,h) {	
    if (w == -1)
    {
        w = screen.width;
        h = screen.height;
    }
	l = (screen.width - w) / 2 ;
	t = (screen.height - h) / 2;	
	window.showModalDialog(URL,name,"dialogHeight: " + h + "px; dialogWidth: " + w + "px; dialogTop: " + t + "px; dialogLeft: " + l + "px; edge: Raised; center: Yes; help: Yes; resizable: Yes; status: Yes;");
	return false;
}


function AddItemForCombo(obj, text, value)
{
    eval('opener.document.forms[0].'+obj).options.length = eval('opener.document.forms[0].'+obj).options.length + 1;
    eval('opener.document.forms[0].'+obj).options[eval('opener.document.forms[0].'+obj).options.length - 1].text = text;
    eval('opener.document.forms[0].'+obj).options[eval('opener.document.forms[0].'+obj).options.length - 1].value = value;
}

// Kiem tra du lieu
//Check Date of dateobjects.
function CheckDate(obj, strMsg){	
	var strDate, strDay, strMonth, strYear;
	var dtDate, dtDay, dtMonth, dtYear;
	if (obj)
		strDate = eval(obj).value;	
	else
		strDate = obj;
	if (strDate != "") {
		strDay = strDate.substring(0, strDate.indexOf("/"));
		strMonth = strDate.substring(strDate.indexOf("/") + 1, strDate.lastIndexOf("/"));
		strYear = strDate.substring(strDate.lastIndexOf("/") + 1, strDate.length);
		dtDate = new Date (strMonth + "/" + strDay + "/" + strYear);
		dtDay = dtDate.getDate();
		dtMonth = dtDate.getMonth() + 1;
		dtYear = dtDate.getYear();
		if ((parseFloat(strDay) != parseFloat(dtDay)) || (parseFloat(strMonth) != parseFloat(dtMonth))|| (isNaN(strYear))){
				alert(strMsg);				
				if (obj){
					obj.value = "";					
					obj.focus();
				}
				return false;
		}		
	}		
	return true;
}
//

function CheckNull_DuyetDangKyXe(obj1,obj2,obj3,obj4,strMsg1,strMsg2,strMsg3,strMsg4)
{
    var strValue1,strValue2;
	var blnEmpty = true;
	if (obj1)
	 
	
		strValue1 =eval(obj1).value;
	
	
	else
		strValue1 = obj1;    
    if (strValue1 != ""){	
      for (i = 0; i < strValue1.length; i++) {
        if (strValue1.charAt(i) != " ") {
		   blnEmpty = false;           
           break;
        }
      }               
    }
    if (blnEmpty){
           alert(strMsg1);
           if (eval(obj1))
				eval(obj1).focus();
		   return false;
	}
	if(blnEmpty==false)
	    blnEmpty=true;
	if (obj2)
	 
	
		strValue2 =eval(obj2).value;
	
	
	else
		strValue2 = obj2;    
    if (strValue2 != ""){	
      for (i = 0; i < strValue2.length; i++) {
        if (strValue2.charAt(i) != " ") {
		   blnEmpty = false;           
           break;
        }
      }               
    }
   
    if (blnEmpty){
           alert(strMsg2);
           if (eval(obj2))
				eval(obj2).focus();
		   return false;
	}
	//obj3
	if(blnEmpty==false)
	    blnEmpty=true;
	if (obj3)
	 
	
		strValue3 =eval(obj3).value;
	
	
	else
		strValue3 = obj3;    
    if (strValue3 != ""){	
      for (i = 0; i < strValue3.length; i++) {
        if (strValue3.charAt(i) != " ") {
		   blnEmpty = false;           
           break;
        }
      }               
    }
   
    if (blnEmpty){
           alert(strMsg3);
           if (eval(obj3))
				eval(obj3).focus();
		   return false;
	}
	//obj4
	if(blnEmpty==false)
	    blnEmpty=true;
	if (obj4)
	 
	
		strValue4 =eval(obj4).value;
	
	
	else
		strValue4 = obj4;    
    if (strValue4 != ""){	
      for (i = 0; i < strValue4.length; i++) {
        if (strValue4.charAt(i) != " ") {
		   blnEmpty = false;           
           break;
        }
      }               
    }
   
    if (blnEmpty){
           alert(strMsg4);
           if (eval(obj4))
				eval(obj4).focus();
		   return false;
	}
    return true;
}
//Check Null Lich cong tac tuan
function CheckNull_LichTuan(obj1,obj2,obj3,strMsg1,strMsg2)
{
    var strValue1,strValue2;
	var blnEmpty = true;
	if (obj1)
	 
	
		strValue1 =eval(obj1).value;
	
	
	else
		strValue1 = obj1;    
    if (strValue1 != ""){	
      for (i = 0; i < strValue1.length; i++) {
        if (strValue1.charAt(i) != " ") {
		   blnEmpty = false;           
           break;
        }
      }               
    }
    if (blnEmpty){
           alert(strMsg1);
           if (eval(obj1))
				eval(obj1).focus();
		   return false;
	}
	if(blnEmpty==false)
	    blnEmpty=true;
	if (obj2)
	 
	
		strValue2 =eval(obj2).value;
	
	
	else
		strValue2 = obj2;    
    if (strValue2 != ""){	
      for (i = 0; i < strValue2.length; i++) {
        if (strValue2.charAt(i) != " ") {
		   blnEmpty = false;           
           break;
        }
      }               
    }
   
    if (blnEmpty){
           alert(strMsg2);
           if (eval(obj2))
				eval(obj2).focus();
		   return false;
	}
	
	
	return true; 
}
//Check Date of dateobjects.
function CheckYear(obj, strMsg){	
	var strDate, strDay, strMonth, strYear;
	var dtDate, dtDay, dtMonth, dtYear;
	if (obj)
		strDate = eval(obj).value;	
	else
		strDate = obj;		
	if (strDate.length!=4 && strDate.length!=0)
	{
	    alert(strMsg);				
		if (obj){
			obj.value = "";					
			obj.focus();
		}
	    return false;	
	}
	if (strDate != "") {
		strDay = "1";
		strMonth = "1";
		strYear = strDate;
		dtDate = new Date (strMonth + "/" + strDay + "/" + strYear);
		dtDay = dtDate.getDate();
		dtMonth = dtDate.getMonth() + 1;
		dtYear = dtDate.getYear();
		if ((parseFloat(strDay) != parseFloat(dtDay)) || (parseFloat(strMonth) != parseFloat(dtMonth))|| (isNaN(strYear))){
				alert(strMsg);				
				if (obj){
					obj.value = "";					
					obj.focus();
				}
				return false;
		}		
	}		
	return true;
}

//Check value of objects is empty.
function CheckNull(obj, strMsg){	
	var strValue;
	var blnEmpty = true;
	if (obj)
	 
	
		strValue =eval(obj).value;
	
	
	else
		strValue = obj;    
    if (strValue != ""){	
      for (i = 0; i < strValue.length; i++) {
        if (strValue.charAt(i) != " ") {
		   blnEmpty = false;           
           break;
        }
      }               
    }
    if (blnEmpty){
           alert(strMsg);
           if (eval(obj))
				eval(obj).focus();
		   return false;
	}
    return true;
}

//Check value of objects is empty.
function CheckNullCombo(obj, strMsg){		
	var strValue;
	var blnEmpty = true;
	var i;
    strValue = obj; 
    if (strValue != ""){	
      for (i = 0; i < strValue.length; i++) {
        if (strValue.charAt(i) != " ") {
		   blnEmpty = false;           
           break;
        }         
      } 
      // Do la combo nen can kiem tra 
      if (strValue == "0" || strValue == "-1")
         blnEmpty = true;        	                       
    }    
    if (blnEmpty){
           alert(strMsg);
           if (eval(obj))
				eval(obj).focus();
		   return false;
	}
    return true;    
}

//Check a variable is number
function CheckNum(obj, msg) {	
	var Result = true;
	var strValue;
	
	if (eval(obj))
		strValue = eval(obj).value;
	else
		strValue = obj;	
//	if (strValue.lastIndexOf(".") > 0 && strValue.lastIndexOf(".")<strValue.lastIndexOf(",")) 
//	{   
//	    Result = false;
//	} 		
//	strValue = strValue.replace(/,/g, "");
	if (isNaN(strValue)) 
	{   
	    Result = false;
	} 
	if (Result==false)
	{
	    alert(msg);
	    if (obj){
		    eval(obj).value = "";
		    eval(obj).focus();
	    }
	    return false;
	}
	
	return Result;	
}

function CheckNumFormat(obj, msg) {	
	var Result = true;
	var strValue;
	
	if (eval(obj))
		strValue = eval(obj).value;
	else
		strValue = obj;	
	if (strValue.lastIndexOf(".") > 0 && strValue.lastIndexOf(".")<strValue.lastIndexOf(",")) 
	{   
	    Result = false;
	} 		
	strValue = strValue.replace(/,/g, "");
	if (isNaN(strValue)) 
	{   
	    Result = false;
	} 
	if (Result==false)
	{
	    alert(msg);
	    if (obj){
		    eval(obj).value = "";
		    eval(obj).focus();
	    }
	    return false;
	}
	if (strValue!="")
	    FormatNumber(obj);
	return Result;	
}

function FormatNumber(obj)
{
    var strValue;
	if (eval(obj))
		strValue = eval(obj).value;
	else
		strValue = obj;	
	strValue = strValue.replace(/,/g, "");		
	var strTmp = "";
	var strTail = "";
	var strOut = "";
	
	if (strValue.indexOf(".")>0)
	{
        strTmp = strValue.substring(0, strValue.indexOf("."));        
        strTail = strValue.substring(strValue.indexOf("."), strValue.length);        
    }       
    else
        strTmp = strValue; 
    for (i=strTmp.length;i>3;i=i-3)
    {
        strOut = "," + strTmp.substring(i-3, i) + strOut;
    }
    strOut = strTmp.substring(0, i) + strOut + strTail;
    eval(obj).value = strOut;
}

function KiemTraBatBuoc(obj, objName, SoSanhTenDangNhapMatKhau)
{
    var i;    
    for (i=0;i<obj.length;i++)
    {
        switch (obj[i].substring(0, 3))
        {
            case 'txt':
                if (!CheckNull(document.getElementById(obj[i]), objName[i] + ' không được trống!'))
                    return false;	
                break;
            case 'ddl':                               
                if (!CheckNullCombo(document.getElementById(obj[i]).value, objName[i] + ' phải được chọn!'))
                {
                    document.getElementById(obj[i]).focus();
                    return false;	
                }
                break;
            case 'cbx':                               
                if (!(document.getElementById(obj[i]).checked))
                {
                    alert(objName[i] + ' phải được chọn!');
                    document.getElementById(obj[i]).focus();
                    return false;	
                }
                break;
            default:            
                break;
        }
    } 
    if (SoSanhTenDangNhapMatKhau==1)
    {
        if (document.getElementById('txtEmailDangNhap'))
        {
            if (document.getElementById('txtEmailDangNhap').value!=document.getElementById('txtEmailDangNhapXacNhan').value)
            {        
                alert('Email đăng nhập và email xác nhận không khớp!');
                document.getElementById('txtEmailDangNhap').focus();
                return false;
            }
        }
        if (document.getElementById('txtMatKhau'))
        {
            if (document.getElementById('txtMatKhau').value!=document.getElementById('txtMatKhauXacNhan').value)
            {        
                alert('Mật khẩu và mật khẩu xác nhận không khớp!');
                document.getElementById('txtMatKhau').focus();
                return false;
            }
        }
    }
	return true;
}

function SetUrlCanBo(url)
{
    var indFront = url.indexOf("TC_") + 3;
    var indTail = url.indexOf(".aspx");
    //alert(url.substring(indFront, indTail));
    DoiMau(url.substring(indFront, indTail));
    var today=new Date();
    var h=today.getHours();
    var m=today.getMinutes();
    var s=today.getSeconds();
    var ngay=today.getDay();
    var thang=today.getMonth();    
    var nam=today.getYear();    
    if (url.indexOf("?")<0)                                                                    
        document.getElementById('ifCanBo').src=url + '?x=' + nam + thang + ngay + h + m + s;
    else
        document.getElementById('ifCanBo').src=url + '&x=' + nam + thang + ngay + h + m + s;
}

var oldTdId;
var tdId;
var oldColor;
function DoiMau(tdId)
{   
    tdId = 'td' + tdId;
    
    if (document.getElementById(oldTdId) != null)
    {
         document.getElementById(oldTdId).bgColor=oldColor;
    }
    
    oldTdId = tdId; 
    oldColor = document.getElementById(tdId).bgColor;
    document.getElementById(tdId).bgColor="#f0f3f4";
}    

// Lam tron so
function round(pnumber,decimals){
	if (isNaN(pnumber)) { return 0};
	if (pnumber=='') { return 0};
	
	var snum = new String(pnumber);
	var sec = snum.split('.');
	var whole = parseFloat(sec[0]);
	var result = '';
	
	if(sec.length > 1){
		var dec = new String(sec[1]);
		dec = String(parseFloat(sec[1])/Math.pow(10,(dec.length - decimals)));
		dec = String(whole + Math.round(parseFloat(dec))/Math.pow(10,decimals));
		var dot = dec.indexOf('.');
		if(dot == -1){
			dec += '.'; 
			dot = dec.indexOf('.');
		}
		while(dec.length <= dot + decimals) { dec += '0'; }
		result = dec;
	} else{
		var dot;
		var dec = new String(whole);
		dec += '.';
		dot = dec.indexOf('.');		
		while(dec.length <= dot + decimals) { dec += '0'; }
		result = dec;
	}	
	return result;
}

function HienThiAnh(ctl)
{
   i = window.event.srcElement.id;
   alert('Hi');
   if(i=="")
        return;
   opener.document.getElementById(ctl).visiable= false;
}

function GetID_DonVi(ctl,ctlhid)
{
    alert('hi');
    i = window.event.srcElement.id;
    if(i=="")
        return;
    //opener.document.getElementById('txtDonVi').value=window.event.srcElement.title;
    opener.document.getElementById(ctl).value=window.event.srcElement.title;
    opener.document.getElementById(ctlhid).value=i;
    
    if (document.getElementById(oldId))
        document.getElementById(oldId).style.background = '#f0f3f4';
   
    document.getElementById(i).style.background = '#00f3f4';    
    oldId = i;  
    
    self.close(); 
}

 function BatPhim(){            
    var keyCode = event.keyCode;
    var key_f5 = 116;

    if (key_f5==keyCode){                
        window.event.keyCode = 27;
        window.event.returnValue = false;
    }
}