﻿/****************************************************************************************
* Trong File Common.js chua tat ca cac Main Function sau day:
* 1. isBlank(obj)
*		Kiem tra du lieu la Null thi se return True
* 2. CheckDate(obj)
*		Kiem tra du lieu kieu ngay theo dang dd/mm/yyyy
* 3. FromSmallToDate(objFromDate,objToDate)
*		Du lieu Ngay1 phai nho hon du lieu Ngay2
* 4. DispDate(obj)
*		Hien thi calendar de chon ngay thang nam
* 5. isWeb(obj)
*		Kiem tra du lieu nhap co phai dia chi WebSite (http://www.yahoo.com)
* 6. isEmail(obj)
*		Kiem tra du lieu la Dia chi Email (FSOFT@yahoo.com)
*----------------------------------------------------------------------------------------
* Ngoai ra co cac function phu sau day:
* 1. isValidDate(strDate)
* 2. IsSmaller(inputStr1, inputStr2)
*****************************************************************************************/

//--------------------------------------------
// Them hang nay vao Code HTML
// <script language="JavaScript" src="Common.js"></script>
//--------------------------------------------

/*********************************************
****  Kiem tra ngay hop le (dd/mm/yyyy)  *****
*********************************************/
function isValidDate(strDate)
{    
  var retval = 0    
  var aDDMMCCYY    
  var dtest    
  
  // Kiem tra dung format    
  if (/^(\d\d?-\d\d?-\d{4})|(\d\d?\/\d\d?\/\d{4})|(\d{8})$/.test(strDate))    
  {    
    if (/\//.test(strDate))    
    {    
      aDDMMCCYY = strDate.split("/");    
    }    
    /*
    else    
    if (/-/.test(strDate))    
    {    
      aDDMMCCYY = strDate.split("-");    
    } 
    */   
    else    
    {    
      aDDMMCCYY = Array(strDate.substr(0,2), strDate.substr(2,2), strDate.substr(4,4))    
    }        
	dtest = new Date(aDDMMCCYY[1] + "/" + aDDMMCCYY[0] + "/" + aDDMMCCYY[2]);          
    if (dtest.getDate() != aDDMMCCYY[0] || dtest.getMonth() +1 != aDDMMCCYY[1] || dtest.getFullYear() != aDDMMCCYY[2])    
    {    
      retval = 2    
    }    
  }    
  else    
  {    
	retval = 1    
  }
  
  //Kiem tra do dai field date
  if (strDate.length != 10) {
	retval = 3;
  }    
  return retval    
}    

function isValidDateEnglish(strDate)
{    
  var retval = 0    
  var aMMDDCCYY    
  var dtest    
  
  // Kiem tra dung format    
  if (/^(\d\d?-\d\d?-\d{4})|(\d\d?\/\d\d?\/\d{4})|(\d{8})$/.test(strDate))    
  {    
    if (/\//.test(strDate))    
    {    
      aMMDDCCYY = strDate.split("/");    
    }    
  
    else    
    {    
      aMMDDCCYY = Array(strDate.substr(0,2), strDate.substr(2,2), strDate.substr(4,4))    
    }        
	dtest = new Date(aMMDDCCYY[1] + "/" + aMMDDCCYY[0] + "/" + aMMDDCCYY[2]);          
    if (dtest.getMonth() + 1 != aMMDDCCYY[0] || dtest.getDate() != aMMDDCCYY[1] || dtest.getFullYear() != aMMDDCCYY[2])    
    {    
      retval = 2    
    }    
  }    
  else    
  {    
	retval = 1    
  }
  
  //Kiem tra do dai field date
  if (strDate.length != 10) {
	retval = 3;
  }    
  return retval    
}    

/*********************************************
**** Neu du lieu NULL se return True *********
*********************************************/
function isBlank(obj)
{
	if (obj.length <1 || obj.value=="")
		return true;
	else
		return false;
}
/********************************
**** Goi ham kiem tra ngay ******
********************************/
function CheckDate(obj)    
{    
	if (!isBlank(obj))    
	{    
		if (isValidDate(obj.value)!=0)    
		{		    
			alert("Ngày " + obj.value + " không hợp lệ!");    
			obj.focus();
			return;    
		}    
	}    
}    

function CheckDate(obj, style)    
{    
	if (!isBlank(obj))    
	{    
		if (style=="dd/mm/yyyy"){
			if (isValidDate(obj.value)!=0)    
			{		    
				alert("Ngày " + obj.value + " không hợp lệ!");    
				obj.focus();
				return;    
			} 
		}else{
			if (isValidDateEnglish(obj.value)!=0)    
			{		    
				alert("Ngày " + obj.value + " không hợp lệ!");    
				obj.focus();
				return;    
			} 
		}
	}    
}   
/*********************************************
*****  Fromdate < ToDate return True    ******
**  Duoc Call boi function FromSmallToDate ***
*********************************************/
function IsSmaller(inputStr1, inputStr2)
{
	var delim1 = inputStr1.indexOf("/")
	var delim2 = inputStr1.lastIndexOf("/")
	
	// lay ngay, thang, nam cua ngay1
	var dd1 = parseInt(inputStr1.substring(0,delim1),10)
	var mm1 = parseInt(inputStr1.substring(delim1 + 1,delim2),10)
	var yyyy1 = parseInt(inputStr1.substring(delim2 + 1,inputStr1.length),10)

	delim1 = inputStr2.indexOf("/")
	delim2 = inputStr2.lastIndexOf("/")

	// lay ngay, thang, nam cua ngay2
	dd2 = parseInt(inputStr2.substring(0,delim1),10)
	mm2 = parseInt(inputStr2.substring(delim1 + 1,delim2),10)
	yyyy2 = parseInt(inputStr2.substring(delim2 + 1,inputStr2.length),10)

	// ngay1 nho hon ngay 2 ?
	if (yyyy2 > yyyy1)
	{
		return true;
	}
	else if  (yyyy2 == yyyy1)
	{
		if (mm2 == mm1) 
			if (dd2 >= dd1) return true;
			else return false;
		else
			if (mm2 > mm1) return true;
			else return false;
	}
	else
		return false;
}
/******************************************
**** Kiem tra tu ngay nho hon Den ngay ****
**** objFromDate : Thanh phan cua form ****
**** objToDate : Thanh phan cua form   ****
******************************************/
function FromSmallToDate(objFromDate,objToDate,strMessage)
{
	FromDate = objFromDate.value
	ToDate = objToDate.value
	if ((!isBlank(objFromDate)) && (!isBlank(objToDate)))
	{ 
		if  (!IsSmaller(FromDate, ToDate)) 
		{
			alert(strMessage);
			//objToDate.focus()
			return false;
		}
		else
			return true;
	}
	else
		return true;
}
/*******************************************
******          CALENDAR             *******
******  File dinh kem Cal_Dialog.asp *******
*******************************************/

var oDialog = "Cal_Dialog.asp";  // Passes URL and filename of dialog box as a variable
var bDialogStatus = false;     // Indicates whether modeless dialog box is currently open
var sDate=""; 
var oSelected = ""

function DispDate(obj){
	sDate = obj.value;
	oSelected = obj
	window.showModalDialog(oDialog, window , "dialogHeight: 260px; dialogWidth: 200px;  center: Yes; help: No; resizable: No; status: No; scroll = no");
}
function fnUpdate()
{  
	oSelected.value = sDate;
}

/***********************************************************************
***** Kiem tra so le cua kieu float neu <>0 and <>5  tra ve False ******
************************************************************************/
function checkQty(str)
{
	sQty = str;
	pos = sQty.indexOf(".");
	if (pos!=-1)
	{
		n1 = sQty.substr(0,pos)
		n2 = sQty.substr(pos+1,sQty.length - pos)

		if (n2!='0' && n2!='5')
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	else
		return true;	
}
/*********************************************************
***** Kiem tra du lieu kieu thap phan (float,double)******
*********************************************************/
function checkfloat(value)
{
	if (value=='' || (value<=0) || !isFinite(value))
	{
		return false;
		
	}
	else
	{
		return true;
	}
}




/*******************************************
***** Kiem tra Dia Chi WebSite hop le ******
***** Co dang http://www.yahoo.com    ******
*******************************************/
function isWeb(obj)
{	
	if (!isBlank(obj))
	{
		var strweb=obj.value;
		if ((strweb.indexOf("http://") == -1) || strweb.indexOf(".") == -1)
		{
			alert("Xin nhập tên website hợp lệ");
			obj.focus();
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return true;
	}
}
/**************************************
***** Kiem tra dia chi Email hop le ***
***** Co dang FSOFT@yahoo.com       ***
**************************************/
function isEmail(obj)
{
	if (!isBlank(obj))
	{
		var str=obj.value;
		if ((str.indexOf('@', 0) == -1) || (str.indexOf('.') == -1)|| str.length<5)
		{
			alert("Xin nhập địa chỉ email hợp lệ");
			obj.focus();
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return true;
	}
}
function isValidString(obj)
{
	if (!isBlank(obj))
	{
		var str=obj.value;
		if ((str.indexOf('', 0) == -1) || (str.indexOf('.') == -1)|| str.length<5)
		{
			alert("Vui lòng nhập địa chỉ email hợp lệ");
			obj.focus();
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return true;
	}
}

/*************************************************
*******  Kiem tra du lieu kieu so nguyen**********
**************************************************/
function checknum(obj)
{
	if (isNaN(obj.value) || !isFinite(obj.value))
	{
		return false;
		
	}
	else
	{
		return true;
	}
}

/************************************************
***** Ham xu ly cho button Soan moi cong van ****
*************************************************/
function AddNewCVDi(strUserRole){
	if (strUserRole == 'VT') {location.href = 'VT_CVDIAddNew.asp'};
	if (strUserRole == 'CV') {location.href = 'CV_CVDIAddNew.asp'};
}

/*********************************************
***** Ham xu ly cho button Hieu chinh cong van di ***
**********************************************/
function EditCVDi(iRec){
	var bCont = false;
	if (iRec > 0) {
		if (iRec!= 1){
			for (i=0;i<=iRec -1;i++){				
				if (frm.chkID[i].checked){
					frm.hID.value = frm.chkID[i].value;
					bCont = true;
					break;
				}
			}
		}
		else{
			if (frm.chkID.checked) {
				bCont = true;
				frm.hID.value = frm.chkID.value;
			}			
		}
	}
	else{
		bCont = true
	}
	if(!bCont){
		alert('Xin chon mau tin can hieu chinh')
	}else{
		frm.action = 'CV_CVDiEdit.asp';
		frm.hAction.value = 'EDIT';
		frm.submit();
	}	
}

/*********************************************
***** Ham xu ly cho button Xoa cong van di ***
**********************************************/
function DeleteCVDi(iRec){
	var bCont = false;
	var IDlist='';
	if (iRec > 0) {
		if (iRec!= 1){
			for (i=0;i<=iRec -1;i++){				
				if (frm.chkID[i].checked){
					bCont = true;
					IDlist += frm.chkID[i].value + ",";
				}
			}
		}
		else{
			if (frm.chkID.checked) {
				bCont = true;
				IDlist += frm.chkID.value + ",";
			}			
		};
		IDlist = IDlist.substring(0,IDlist.length-1);
	}
	else{
		bCont = true
	}	
	if(!bCont){
		alert('Xin chon mau tin can xoa')
	}else if(confirm('Ban that su muon xoa ?')){
		frm.action = 'Comm_CVDiProcess.asp';
		frm.hAction.value = 'DELETE';
		frm.hID.value = IDlist;
		frm.submit();
	}	
}

/*********************************************
***** Ham xu ly cho button Thay doi tinh trang CVDi ***
**********************************************/
function ChangeStatus(iRec,NextStatusID){
	var bCont = false;
	var IDlist='';	
	if (iRec > 0) {
		if (iRec!= 1){
			for (i=0;i<=iRec -1;i++){				
				if (frm.chkID[i].checked){
					IDlist += frm.chkID[i].value + ",";
					bCont = true;
				}						
			}
		}
		else{
			if (frm.chkID.checked) {
				bCont = true;
				IDlist += frm.chkID.value + ",";
			}
		};
		IDlist = IDlist.substring(0,IDlist.length-1);
	}
	else{
		bCont = true
	}	
	
	if(!bCont){
		alert('Xin chon mau tin can chuyen tinh trang')
	}else if(confirm('Ban that su muon chuyen tinh trang?')){
		frm.action = 'Comm_CVDIProcess.asp';
		frm.hAction.value = "CHANGESTATUS";
		frm.hNextStatusID.value = NextStatusID;		
		frm.hID.value = IDlist;
		frm.submit();
	}
}


function SetChecked(val) {
	len = frm.elements.length;
	var i=0;
	for( i=0 ; i<len ; i++) {
		if (frm.elements[i].name=='chkID') {
		//alert (len);
			frm.elements[i].checked=val;
		}
	}
}

function ShowDate(dt){
	var month,day,year;
   	month = dt.getMonth()+1;
   	day = dt.getDate();
   	year = dt.getFullYear();

   	if (month < 10) month = "0" + month;
   	if (day < 10) day = "0" + day;

   	return day + "/" + month + "/" + year;
}

function FirstDateOfWeek(dDate){	
	var offset;
	var dt = dDate;

	offset = dt.getDay() - 1;
   	dt.setDate(dt.getDate() - offset);
	return dt;
}

function LastDateOfWeek(dDate){	
	var offset;
	var dt = dDate;

	offset = 6 - dt.getDay() + 1;
   	dt.setDate(dt.getDate() + offset);
	return dt;
}

function FirstDateOfMonth(dDate){	
	var dt = dDate;
   	dt.setMonth(dt.getMonth());
   	dt.setDate(1);
	return dt;
}

function LastDateOfMonth(dDate){	
	var dt = dDate;
   	dt.setMonth(dt.getMonth() + 1 );
   	dt.setDate(1);
   	dt.setDate( dt.getDate() - 1);
	return dt;
}
function FirstDateOfYear(dDate){	
	var dt = dDate;
   	dt.setMonth(0);
   	dt.setDate(1);
	return dt;
}

function LastDateOfYear(dDate){	
	var dt = dDate;
   	dt.setMonth(11);
   	dt.setDate(31);
	return dt;
}

function isSoCVDen(obj){	
	var intYear = obj.value.substr(0,2);
	var strSo = obj.value.substr(2,1);
	var intCounter = obj.value.substr(3,obj.value.length-3);
	var strSoConst = "ABCDEFabcdef";

	if (isBlank(obj)){
		return true;
	}
	if (obj.value.length > 8){
		alert ("Do dai chuoi qua qui dinh" + String.fromCharCode(13) + "Vui long nhap dung format so CV den (yyAxxxxx)");
		obj.focus();
		return false;
		
	}
	if (isNaN(intYear)){
		alert ("Nam phai la kieu so" + String.fromCharCode(13) + "Vui long nhap dung format so CV den (yyAxxxxx)")
		obj.focus();
		return false;
	
	}
	if (strSoConst.search(strSo)==-1){
		alert ("So " + strSo.toUpperCase() + " khong ton tai" + String.fromCharCode(13) + "Vui long nhap dung format so CV den (yyAxxxxx)")
		obj.focus();
		return false;
	
	}
	if (isNaN(intCounter)){
		alert ("So den phai la kieu so" + String.fromCharCode(13) + "Vui long nhap dung format so CV den (yyAxxxxx)")
		obj.focus();
		return false;
	}
	return true;
}
//******************************************
// Xu ly khi nhan phim Enter se focus() den obj
//******************************************
function keyPress(obj)
{
	sAppName = navigator.appName;
	if(sAppName == 'Microsoft Internet Explorer')
	{
		var nKey = event.keyCode;
		if (nKey == '13') 
		{	
			obj.focus();
		}	
	}	
}

//******************************************
// Kiem tra su ton tai cua HTML controls
// Return: -1: Khong ton tai;0:Uncheck;1:Checked
//******************************************
function checkObject(chkObj){
	var retValue = 0;
	if(chkObj){
		if(!chkObj.length){
			if(chkObj.checked){
				retValue = 1;	
			}	
		}else{
			for(var i = 0;i<chkObj.length;i++){
				if(chkObj[i].checked){
					retValue = 1;
					break;
				}
			}
		}
	}else{
		retValue = -1;
	}
	return retValue;
}

//******************************************
// 
// Return: MaCongVanDen
//******************************************
function getID(iRec,optObj){
	var str;
	if (iRec==1){   
		str = optObj.value;
	}else{
		for (i=0;i<=iRec-1;i++)   
			if (optObj[i].checked){   
				str = optObj[i].value;
				break;   
			}
	}
	return 	str;
}	

//******************************************
// Xu ly picture
//******************************************
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
  if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//*********************************************************
//Ham tao chu chay tren statur bar                        *
//Original script by Brent Plugg, adapted by Rob Schluter.*
//*********************************************************
var timerId;
var msg = ""
var newMsg = ""
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<=(89); 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()
}

function displayMenu(SpanID,ImageID, Path)
	{
		if (document.all[SpanID].style.display=="") 
		{
			document.all[SpanID].style.display="none";
			document.images[ImageID].src=Path + "title_maximize.gif";
		}
		else {
			document.all[SpanID].style.display="";
			document.images[ImageID].src=Path + "title_minimize.gif";
		}
		
	}
function swapClass(obj,stylename){
		obj.className = stylename
	}
	
function numeralsOnly(evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
        ((evt.which) ? evt.which : 0));
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        alert("Xin nhập số");
        return false;
    }
    return true;
}