//*****************************************************************************************************************
//*函數名稱 :X_CheckDate(txtObj)
//*傳回值   :true/false
//*函數說明 :日期格式檢核,若格式不正確則顯示訊息如alert("輸入格式錯誤！\n必須為日期格式")
//*參數說明 :txtObj:欲檢核之text物件
//*作者     :Vivian Huang
//*更新者   :1.Lillian,修改傳入值、及錯誤訊息、移除txtObj.value空白字元
//*使用範例 :寫於物件onblur事件 onblur="javascript:X_CheckDate(this)"
//*****************************************************************************************************************
function X_CheckDate(txtObj){
	var strValue;					//lillian
	strValue=X_VBTrim(txtObj.value);//lillian
	if (strValue==""){return true;} //lillian
	if (parseInt(strValue.length)!=10){
		alert ("您輸入的格式錯誤！\n必須為日期格式！\n例：2004/01/01");
		txtObj.focus();
		return false;
	}else if (strValue.indexOf("/")==-1){
		alert ("您輸入的格式錯誤！\n必須為日期格式！\n例：2004/01/01");
		txtObj.focus();
		return false;
	}else if (strValue.indexOf("/")!=-1){
		var ND1 = strValue;
		var ND2 = ND1.split("/");
		if (parseInt(ND2.length)!=3){
			alert ("您輸入的格式錯誤！\n必須為日期格式！\n例：2004/01/01");
			txtObj.focus();
			return false;
		}else{
			if (parseInt(ND2[0].length)<4){
				alert ("您輸入的格式錯誤！\n必須為日期格式！\n例：2004/01/01");
				txtObj.focus();
				return false;
			}else if (parseInt(ND2[1].length)<2){
				alert ("您輸入的格式錯誤！\n必須為日期格式！\n例：2004/01/01");
				txtObj.focus();
				return false;
			}else if (parseInt(ND2[2].length)<2){
				alert ("您輸入的格式錯誤！\n必須為日期格式！\n例：2004/01/01");
				txtObj.focus();
				return false;
			}else{
				return true;
			}
		}
	}else{
		return true;
	}
}
//******************************************************************************************************************
//*函數名稱 :X_SelectAll
//*傳回值   :
//*函數說明 :選取全部checkbox(chk)選項
//*參數說明 :checkbox物件
//*作者     :May
//*更新者   :
//使用範例  :寫於button onclick事件中 onclick="javascript:X_SelectAll(chk物件)"
//******************************************************************************************************************
function X_SelectAll(X_select){
	var X_select,i = X_select.length;
	if (X_select != undefined){
		if (i == undefined){
			X_select.checked = true;
		}
		while ( i-- ){
			X_select[i].checked = true;
		}		
	}
}
//******************************************************************************************************************
//*函數名稱 :X_CancelAll
//*傳回值   :
//*函數說明 :取消選取全部checkbox(chk)選項
//*參數說明 :checkbox(chk)物件
//*作者     :May
//*更新者   :
//使用範例  :寫於button onclick事件中 onclick="javascript:X_CancelAll(chk物件)"
//******************************************************************************************************************
function X_CancelAll(X_select){
	var X_select,i = X_select.length;
	if (X_select != undefined){
		if (i == undefined){
			X_select.checked = false;
		}
		while ( i-- ){
			X_select[i].checked = false;
		}		
	}
}
//******************************************************************************************************************
//*函數名稱 :X_SelectedNum
//*傳回值   :true/false
//*函數說明 :檢核checkbox可選取的數量，若不符合則顯示錯誤訊息
//*參數說明 :checkbox物件,欄位中文說明,可選取數量(0:表示只能選取1個，n:表示至少須選取n個。)
//*作者     :May
//*更新者   :
//*使用範例 :寫於button onclick事件中
//			if (X_SelectedNum(chk物件,"處理方式細項",n) == true){....}(0:表示只能選取1個，n:表示至少須選取n個。)
//******************************************************************************************************************
function X_SelectedNum(X_select,X_Title,intNum){  
	var X_select,X_Title,intNum,count,num;
	count = 0;
	num = 0;
	if (intNum == 0) { num++; } else { num = intNum; }
	var i = X_select.length;
	if (X_select != undefined) {
		if (i == undefined){
			if (X_select.checked == true){ count++; }
		}
		while ( i-- ){
			if(X_select[i].checked == true){ count++; }
		}
	}
	if (count != 0){
		if (intNum == 0){
			if (count == 1){
				return true;
			}else{
				alert('請選取1項'+X_Title+'！');
				return false;
			}
		}else{
			if (count >= intNum){
				return true;
			}else{
				alert(X_Title+'至少須選取'+intNum+'個項目！');
				return false;
			}
		}
	}else{
		alert("請選取"+X_Title+"！");
		return false;
	}
}
//*****************************************************************************************************************
//*函數名稱 :X_CheckNum(txtObj)
//*傳回值   :true/false
//*函數說明 :數字格式欄位檢核,若格式不正確則顯示訊息如alert("輸入格式錯誤！\n必須為數字")
//*參數說明 :txtObj:欲檢核之text物件
//*作者     :Lillian
//*更新者   :
//*使用範例 :寫於物件onblur事件 onblur="javascript:X_CheckNum(this)"
//******************************************************************************************************************
function X_CheckNum(txtObj){
	var strValue;
	strValue=X_VBTrim(txtObj.value);
	if(strValue==""){return true;}
	if(isNaN(strValue)){
		alert("輸入格式錯誤！\n必須為數字");
		txtObj.focus();
		return false;
	}
	return true;
}
//*****************************************************************************************************************
//*函數名稱 :X_CheckKeyField(strObjTitle,strObjNames)
//*傳回值   :true/false
//*函數說明 :檢核必要輸入欄位
//*參數說明 :strObjTitle:輸入欄位之中文說明
//*	     strObjNames:物件id,多個物件須檢核時以,分隔(如:txt1,txt2,cbo1)
//*作者     :Lillian
//*更新者   :
//*使用範例 :寫於button onclick事件中
//	     strObjTitle="姓名,地址,性別"
//	     strObjNames="txt1,txt2,cbo1";
//	     blnRV=X_CheckKeyField(strObjTitle,strObjNames);
//	     if (blnRV){....}else{....}
//******************************************************************************************************************
function X_CheckKeyField(strObjTitle,strObjNames){
	var aryNames;
	var aryTitle;
	var strMsg="";
	var strValue;
	var strType;
	var strTagName;
	
	aryNames=strObjNames.split(",");
	aryTitle=strObjTitle.split(",");
	if (aryTitle.length!=aryNames.length){
		alert("傳入參數錯誤！");
		return false;
	}
	for (var i=0;i<aryNames.length;i++){
		strValue="";		
		strType="";
		if (document.all[aryNames[i]].length>0){
			strTagName=document.all[aryNames[i]].tagName+"";	
			if (strTagName.toLowerCase()=="select"){
				strType="select";
			}else{
				strType=document.all[aryNames[i]][0].type;
			}
		}else{
			strType=document.all[aryNames[i]].type;
		}
		strType=strType.toLowerCase();
		switch (strType) {
		   case "select" :
		   		var intSelected=document.all[aryNames[i]].selectedIndex;
		   		strValue=document.all[aryNames[i]].options[intSelected].value+"";
		   		if (intSelected==-1||strValue==""){
		   			strMsg=strMsg+"請選取"+aryTitle[i]+"！\n";
		   		} 
		   		break;   
		   case "checkbo" :
		   case "radio" :
		   		if (document.all[aryNames[i]].checked==false){
		   			strMsg=strMsg+"請選取"+aryTitle[i]+"！\n"
		   		}   
		   		break;
		   default :
		   		strValue=X_VBTrim(document.all[aryNames[i]].value);
				if (strValue==""){strMsg=strMsg+aryTitle[i]+"欄位不可空白！\n"}
		} 		
	}
	if (strMsg!=""){
		strMsg="必要輸入欄位輸入錯誤:\n"+strMsg;
		alert(strMsg);
		return false;
	}
	return true;	
}
//*****************************************************************************************************************
//*函數名稱 :X_OpenWin(intWidth,intHeight,strAsp)
//*傳回值   :無
//*函數說明 :另開視窗
//*參數說明 :1.intWidth:視窗之Width
//*	     2.intHeight:視窗之Height
//*	     3.strAsp:欲另開視窗之網址
//*		 4.strType:視窗類別1,:有menubar
//*作者     :Lillian
//*更新者   :Lillian,2005/03/30 add strType
//*使用範例 :X_OpenWin('600','450','test.html')
//*備註     :畫面離開前關閉所有另開之視窗方法須配合X_CloseWin,如:<body onunload="javascript:X_CloseWin()">
//******************************************************************************************************************
var g_Window=new Array();
function X_OpenWin(intWidth,intHeight,strAsp,strType){
	var intTop,intLeft;
	var strState;
	var strWinId,strWinAry;
	
	intTop=(screen.height-intHeight)/2;
	intLeft=(screen.width-intWidth)/2;
	if (strType=="1"){
		strState="height="+ intHeight +",width="+intWidth+",top="+intTop+",left="+intLeft+",status=no,toolbar=no,menubar=yes,location=no,resizable=yes,scrollbars=yes"
	}else{
		strState="height="+ intHeight +",width="+intWidth+",top="+intTop+",left="+intLeft+",status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes"
	}
	obj=window.open(strAsp,"",strState);
	obj.focus();
	if (g_Window.length<=0){
		g_Window[0]=obj;
	}else{
		g_Window[g_Window.length]=obj;
	} 	
}
//*****************************************************************************************************************
//*函數名稱 :X_CloseWin()
//*傳回值   :無
//*函數說明 :關閉所有另開視窗
//*參數說明 :無
//*作者     :Lillian
//*更新者   :
//*使用範例 :<body onunload="javascript:X_CloseWin()">
//*備註	    :呼叫此函數,X_OpenWin才有作用
//******************************************************************************************************************
function X_CloseWin(){
	if (g_Window.length>0){
		for (var i=0;i<g_Window.length;i++){
			if(typeof(g_Window[i])!=null){
				try{
					g_Window[i].close();
				}catch(e){}
			}
		}
	}	
}

//*****************************************************************************************************************
//*函數名稱 :X_CheckCondition(strThisId,strObjIds)
//*傳回值   :true/false(是否error,若兩個下拉選單選同一種條件則error)
//*函數說明 :檢核查詢條件是否選取相同項目(多個下拉選單之查詢條件)
//*參數說明 :strThisId:目前發生onchange之下拉選單id
//*			 strObjIds:所有下拉選單id(以,分隔)
//*作者     :Lillian
//*更新者   :
//*使用範例 :var blnRV=X_CheckCondition("cbo3","cbo1,cbo2,cbo3");
//*			 if (!blnRV){//hidden 相對印所有物件.....}
//******************************************************************************************************************
function X_CheckCondition(strThisId,strObjIds){
	var aryId,aryValue,intSelected;
	var strValues="";
	var blnRV=true;
	var strThisValue="";
	
	intSelected=document.all[strThisId].selectedIndex;
	if (intSelected==-1 || document.all[strThisId].options[intSelected].value==""){
		return true;
	}else{
		strThisValue=document.all[strThisId].options[intSelected].value;
	}
	
	aryId=strObjIds.split(",");
	for (var i=0;i<aryId.length;i++){
		if (aryId[i]!=strThisId){
			intSelected=document.all[aryId[i]].selectedIndex;
			if (intSelected!=-1){			
				if (document.all[aryId[i]].options[intSelected].value!=""){
					strValues=strValues+document.all[aryId[i]].options[intSelected].value+",";
				}
			}
		}
	}
	if (strValues!=""){
		aryValue=strValues.split(",");
		for (var j=0;j<aryValue.length-1;j++){
			if (strValues.indexOf(strThisValue+",")==-1){
				blnRV=true;			
			}else{
				blnRV=false;
				break;
			}
		}
	}
	if (!blnRV){
		alert("請選取不同查詢條件！");
		document.all[strThisId].selectedIndex=-1;
		//document.all[strThisId].focus();
	}
	return blnRV;
}

//*****************************************************************************************************************
//*函數名稱 :X_GetUsrData(strUNO,strFieldName)
//*傳回值   :"" 或 員工資料(若傳回""則表示查無此員工資料)
//*函數說明 :查詢員工基本資料
//*參數說明 :strUNO:員工工號
//*			 strFieldName:欲傳回之欄位名稱,預設傳回員工姓名,若有多個時則以","分開欄位名稱請參考TABLE EMPLOYEE
//*							,另外若須傳回年齡則傳入YEAR
//*作者     :Lillian
//*更新者   :
//*使用範例 :
//*		1.var strNO=document.form1.txtUno.value //儲存輸入之工號
//*	     var strRV=X_GetUsrData(strNO)	  //call X_GetUsrData取員工姓名
//*	     document.form1.txtUName.value=strRV //顯示員工姓名
//*		2.var strRV=X_GetUsrData(strNO,"EMP_NAME,LOCATION,DEPTID,DEPTAME,SEX,YEAR")	  //call X_GetUsrData取員工姓名
//*		  則會傳回"張三,FAB3,00001,xx部門,M,20"	
//******************************************************************************************************************
var g_strP_UNO="";
var g_strP_UsrRV="";
function X_GetUsrData(strUNO,strFieldName){
	var strURL;
	var strRV;
	var strFeatures;
	var strField;
	if (strUNO==""){
		g_strP_UNO="";
		g_strP_UsrRV="";
		return "";
	}
	if (g_strP_UNO==strUNO){
		strRV=g_strP_UsrRV;
	}else{
	
		if (strFieldName==null||strFieldName=="undefined"||strFieldName==""){strFieldName="EMP_NAME"}
		strFeatures="dialogHeight:0;dialogWidth:0;dialogLeft:0;dialogTop:0;help:no;resizable:no;scroll:no;status:no"
		strURL="GetUsrData.asp?UNO="+strUNO+"&Field="+strFieldName;
		strRV=window.showModalDialog(strURL,"",strFeatures)
		g_strP_UNO=strUNO;
	}
	if (strRV==""){
		alert("查無此員工資料！")
		g_strP_UNO="";
	}
	g_strP_UsrRV=strRV;
	return strRV;
}
function X_CheckSEDate(objS,sTitle,objE,eTitle){   //start date 須小於end date
		var p_date = new Date(objS.value);
		var e_date = new Date(objE.value);
		var blnRV;
		
		//var today = new Date();
		//diffMNum = 0 即為同年同月 //start date 須大於或等於當日，
		//var diffMNum = (p_date.getYear() - today.getYear())*12 + (p_date.getMonth() - today.getMonth());
		//if (diffMNum == 0){
		//	var diffDNum = p_date.getDate() - today.getDate();
		//	if (diffDNum >= 0){
		//		blnRV = true;
		//	}else{
		//		alert(sTitle+" 必須 > 或 = 今日!");
		//		blnRV = false;
		//	}
		//}else if (diffMNum > 0){
		//	blnRV = true;
		//}else{
		//	alert(sTitle+" 必須 > 或 = 今日!");
		//	blnRV = false;
		//}
		//if (blnRV){
		
			if (p_date > e_date){
				alert(sTitle+" 必須 < 或 = "+eTitle+"!");
				blnRV = false;
			}else{
				blnRV = true;
			}
		
		//}
		
		return blnRV;
	}
