'#############################################################################
'#		系統名稱：一般控制項
'#		程式名稱：cGlobal.vbs
'#	 	日    期：2005/05/24
'#		語    言：JavaScript << Client端 >>
'#		內含部份：一般使用到的函式(javaScript不提供的函式)
'#				gfcTrim		- 刪除字串前後空白
'#				gfcLTrim		- 刪除字串前方空白
'#				gfcRTrim		- 刪除字串後方空白
'#				gfcLeft		- 傳回一字串由左算起特定數量的字元
'#				gfcRight		- 傳回一字串由右算起特定數量的字元
'#				gfcInstrRev	- 由字串右邊找第一個出現的字元位置
'#				gfcInstr		- 由字串左邊找第一個出現的字元位置
'#				gfcMsgbox	- 將訊息顯示在對話方塊中
'#				gfcReplace	- 取代字串
'#				gfcDateAdd	- 將日期加上數個時間間隔單位
'#				gfcFormat	- 將數值格式化為 999,999,999
'#				gfcDateDiff	- 傳回兩個日期間相差的時間間隔單位數目
'#############################################################################

'============================================================================
'=		功能說明：刪除字串前後空白
'=		傳入參數：pString：字串
'=		傳回參數：字串
'============================================================================
Function gfcTrim(pString)
    gfcTrim = Trim(pString)
End Function

'============================================================================
'=		功能說明：刪除字串前方空白
'=		傳入參數：pString：字串
'=		傳回參數：字串
'============================================================================
Function gfcLTrim(pString)
    gfcLTrim = LTrim(pString)
End Function

'============================================================================
'=		功能說明：刪除字串後方空白
'=		傳入參數：pString：字串
'=		傳回參數：字串
'============================================================================
Function gfcRTrim(pString)
    gfcRTrim = RTrim(pString)
End Function

'============================================================================
'=		功能說明：傳回一字串由左算起特定數量的字元
'=		傳入參數：pString：字串
'=						    pLength：長度
'=		傳回參數：字串
'============================================================================
Function gfcLeft(pString, pLength)
    gfcLeft = Left(pString, pLength)
End Function

'============================================================================
'=		功能說明：傳回一字串由右算起特定數量的字元
'=		傳入參數：pString：字串
'=						    pLength：長度
'=		傳回參數：字串
'============================================================================
Function gfcRight(pString, pLength)
    gfcRight = Right(pString, pLength)
End Function

'============================================================================
'=		功能說明：由字串右邊找第一個出現的字元位置
'=		傳入參數：pString：字串
'=						    pFindStr：找尋的字串
'=		傳回參數：數值
'============================================================================
Function gfcInstrRev(pString, pFindStr)
    gfcInstrRev = InstrRev(pString,pFindStr)
End Function

'============================================================================
'=		功能說明：由字串左邊找第一個出現的字元位置
'=		傳入參數：pString：字串
'=						    pFindStr：找尋的字串
'=		傳回參數：數值
'============================================================================
Function gfcInstr(pString, pFindStr)
    gfcInstr = Instr(pString,pFindStr)
End Function

'============================================================================
'=		功能說明：將訊息顯示在對話方塊中
'=		傳入參數：pPrompt：顯示訊息
'=						    pButton：顯示型態﹝0/確定, 1/確定;取消, 2/放棄;重試;忽略, 3/是;否;取消,
'=												4/是;否, 16/重要訊息, 32/詢問符號, 48/警告符號, 64/資訊符號..﹞
'=						    pTitle：標題
'=		傳回參數：字串
'============================================================================
Function gfcMsgBox(pPrompt, pButton, pTitle)
    gfcMsgBox = msgbox(pPrompt, pButton, pTitle)
End Function

'============================================================================
'=		功能說明：取代字串
'=		傳入參數：pStr：原始字串
'=						    pFindStr：欲取代字串
'=						    pReplaceStr：取代字串
'=		傳回參數：字串
'============================================================================
Function gfcReplace(pStr,pFindStr,pReplaceStr)
	gfcReplace=replace(pStr,pFindStr,pReplaceStr)
End Function

'============================================================================
'=		功能說明：將日期加上數個時間間隔單位
'=		傳入參數：pInterval：時間間隔單位
'=												(Y/y,Q/q,M/m,W/w,D/d,H/h,N/n,S/s    年,季,月,週,日,時,分,秒)
'=						    pNumber：要加上之時間間隔單位的次數
'=						    pDate：日期
'=		傳回參數：字串
'============================================================================
Function gfcDateAdd(pInterval, pNumber, pDate)
	Dim tDate
	Dim tY
	Dim tM
	Dim tD
	tDate=DateAdd(pInterval, pNumber, CDate(pDate))
	tY=Year(tDate)
	if Len(Month(tDate))=1 then
		tM="0"& Month(tDate)
	else
		tM=Month(tDate)
	end if
	if Len(Day(tDate))=1 then
		tD="0"& Day(tDate)
	else
		tD=Day(tDate)
	end if
	gfcDateAdd=tY &"/"& tM &"/" & tD
End Function

'============================================================================
'=		功能說明：將數值格式化為 999,999,999
'=		傳入參數：pNumStr：數值
'=		傳回參數：字串
'============================================================================
function gfcFormat(pNumStr)
	set chkNum = gfcChkNum(pNumStr)
	if chkNum.eErr then
		gfcFormat = ""
		exit function
	end if 
	oldStr = cstr(pNumStr)
	result = ""
	if pNumStr<0 then
		result = "-"
		oldStr = right(oldStr,len(oldStr)-1)
	end if 
	pointPos = instr(oldStr,".")
	if pointPos>0 then 
		tmpStr1 = left(oldStr,pointPos-1)
		tmpStr2 = right(oldStr,len(oldStr)-pointPos+1)
	else
		tmpStr1 = oldStr
		tmpStr2 = ""
	end if 
	mod3 = len(tmpStr1) mod 3
	if mod3 >0 and len(tmpStr1)>3 then 
		result = result & left(tmpStr1,mod3) & ","
		tmpStr1 = right(tmpStr1,len(tmpStr1)-mod3)
	end if 
	if len(tmpStr1)>=3 then 
		do while tmpStr1<>""
			result = result & left(tmpStr1,3) & ","
			tmpStr1 = right(tmpStr1,len(tmpStr1)-3)
		loop
	else
		result = result & tmpStr1
	end if 
	if len(result)>3 then result = left(result,len(result)-1)
	result = result & tmpStr2
	gfcFormat = result
end function 

'============================================================================
'=		功能說明：傳回兩個日期間相差的時間間隔單位數目
'=		傳入參數：pInterval：時間間隔單位﹝yyyy西元年／q季／m月／y一年的日數／d日／w一週的日數／ww週／h時／n分／s秒﹞
'=						    pDate1：開始日期
'=						    pDate2：結束日期
'=		傳回參數：日期
'============================================================================
Function gfcDateDiff(pInterval, pDate1, pDate2)
	gfcDateDiff=DateDiff(pDateType,CDate(pDay1),CDate(pDay2),vbSunday,vbFirstJan1)
End Function
