'******************* Client Function 打開，關閉 Form *************************** 
'傳入1：Form參照

'結果：將整個Form設為唯讀及Disabled

    Function SetFormClose(FormID)
        For Each FormChildObject in FormID
            on error resume next
            FormChildObject.readonly = true
            FormChildObject.disabled = true
        next
    end Function

    Function SetFormoPEN(FormID)
        For Each FormChildObject in FormID
            on error resume next
            FormChildObject.readonly = false
            FormChildObject.disabled = false
        next
    end Function    
    
'******** Client Function 指定 Select(DropDowMenu) 選項 ***************************    

'傳入1：Select物件(例：Action.SelectCountry)
'傳入2：欲指定Option的Value

'結果：將Select物件的選項指定至傳入的Value

Function ChangeSelected(SelectID,OptionValue)
    'Msgbox SelectID.value
    
    If IsNull(OptionValue) then '處理null
        'Msgbox OptionValue
        exit Function
    end If
   
    For ii = 0 to SelectID.length - 1
        'Msgbox SelectID(ii).value
        'Msgbox OptionValue
        If UCase(OptionValue) = UCase(SelectID(ii).value) then 
            SelectID.SelectedIndex = ii
            'Msgbox ii
            exit for
        end if
    next    
end Function

'******** Client Function 指定 Input(Radio) 選項 ***************************
'傳入1.FormName 2.ID 3.Value          
'功能：將傳入的Value設定相同的Value之Radio Checked

Sub DoRadioChecked(FormName, ID, Value)  
	If Value = "True" or Value = "true" Then Value = 1 '在SQL中True or False在瀏覽器中以1或0代替之
	If Value = "False" or Value = "false" Then Value = 0   
       
	'加錯誤處理是因為並不知道Radio共有幾個。最多30個Radio。
	
	ExecuteString = " Dim i: i = 0:" &_
					" On Error Resume Next:" &_
					" Do While i < 30:" &_           
					"	If " & FormName & "." & ID & "(i).Value = """ & Value & """ Then:" &_          
					"		" & FormName & "." & ID & "(i).Checked = True:" &_           
					"		exit do:" &_           
					"	end If:" &_          
					"	i = i + 1:" &_          
					" Loop:" &_
					" On Error Goto 0"
					   
	Execute(ExecuteString)
	'Msgbox ActionForm.R_GetInNeed(0).value				 
	'Msgbox FormName
	'Msgbox Id
	'Msgbox Value
        
END Sub


'********  回傳目前所選定Radio的值(若Radio目前沒有被核取則回傳Null) ***************

Function ReturnRadioValue(FormID,RadioID)
	For RR=0 To 30
		On Error Resume Next
		ExecuteString=FormID & "." & RadioID & "(" & RR & ").Checked=True"
		'Msgbox Eval(ExecuteString)
		If Eval(ExecuteString) Then
			ExecuteString=" ReturnRadioValue=" & FormID & "." & RadioID & "(" & RR & ").value"
			'Msgbox ExecuteString
			Execute(ExecuteString) 
			Exit Function
		End If
	Next
	ReturnRadioValue=""
End Function



'************* Client '由Script來指定到 TextArea中 **********************************
Function ParseStringToScript(InputString)
    
    If IsNull(InputString) then '處理null
        ParseStringToScript = ""
        exit Function
    end If

    ResultString = Replace(InputString,Chr(34),Chr(34) & Chr(34)) '處理 "
    ResultString = Replace(ResultString,vbCRLF,chr(34) & "&vbCRLF&" & chr(34)) '處理換行符號
    
    ParseStringToScript = """" & ResultString & """"  

end function 

'************* Client檢查日期格式是否正確 *********************************
Function CheckDateValid(SrcElement)
    on error Resume next
    'Msgbox SrcElement
    'Set SrcElement = Window.event.srcElement
    
        If SrcElement.value <> "" then
            Position1 = Instr(1,SrcElement.value,"/")
            Position2 = Instr(Position1+1,SrcElement.value,"/")
                
            If Position1 = 0 or Position2 = 0 then 
                Error("日期格式錯誤，請包含兩個 ""/"" 符號")
                CheckDateValid = false
                exit function
            end if
            
            If Position1 <> 5 then
                Error("日期格式錯誤，前四碼需為數字")
                CheckDateValid = false
                exit function
            end if
            
            ValidDate = Cdate(SrcElement.value)	

	        If Err.number <> 0 then '此行處理錯誤。
                Error("日期錯誤，可能是格式錯誤，或日期不存在。請確認。")
                CheckDateValid = false
                exit function
            end if
            
            If ValidDate > CDate("2079/6/6") then
                ValidDate = "2079/6/6"
            end if
            
            If ValidDate < CDate("1900/1/1") then
                ValidDate = "1900/1/1"
            end if
                         
            SrcElement.value = ValidDate
            CheckDateValid = true
    end if 
end function

'***** Client 檢查是否全為整數 ***********************************
Function CheckAllInt(InputString)
    Dim Error
    for h=1 to Len(InputString)
        ASCIICode = ASC(Mid(InputString,h,1))
        If ASCIICode < 48 or ASCIICode > 57 then
            Error = "Error"
        end if
    Next    
    If Error = "" then
        CheckAllInt = True
      else
        CheckAllInt = False
    end if
end Function
'***** Client 檢查是否全為英文字 ***********************************
Function CheckAllCharacter(InputString)
    Dim Error
    for m=1 to Len(InputString)
        ASCIICode = ASC(Mid(Lcase(InputString),m,1))
        'Msgbox ASCIICode
        If not (ASCIICode >= 97 And ASCIICode <= 122) then
            Error = "Error"
        end if
    Next
    
    If Error = "" then
        CheckAllCharacter = True
      else
        CheckAllCharacter = False
    end if
end Function
'***** Client 檢查是否全為英文字或數字 ***********************************
Function CheckAllCharacterOrInt(InputString)
    Dim Error
    for n = 1 to Len(InputString)
        ASCIICode = ASC(Mid(Lcase(InputString),n,1))
        'Msgbox ASCIICode
        If not (ASCIICode >= 97 And ASCIICode <= 122) And not (ASCIICode >= 48 And ASCIICode <= 57 ) then
            Error = "Error"
        end if
    Next    
    If Error = "" then
        CheckAllCharacterOrInt = True
      else
        CheckAllCharacterOrInt = False
    end if
end Function


'*********** Client 取得所選Option的 "顯示值" (此Function主要目的為<Option>內顯示值與Option之Value不同時使用之 ) ******************
'傳入：Form名稱，Select名稱，指定名稱(為額外之屬性例如：<option value=11 value1=中國>美國</option>，則Function回傳"中國")
'回傳：此Select的顯示值

Function ReplySelectDisplay(FormName,SelectName,CustomerValue)
    Execute( "ReplySelectDisplay = " & FormName & "." & SelectName & ".Item(" & FormName & "." & SelectName & ".SelectedIndex)." & CustomerValue)
    'QueryForm.S_AgentOfficeID.item(QueryForm.S_AgentOfficeID.selectedIndex).value1
End Function    

'*********** Client 錯誤處理含式 ********************************
Function Error(ErrMsg)'所有錯誤最終處理處。呼叫此涵式代表程式出錯
    If ErrMsg = "" then ErrMsg = "錯誤"
    Alert ErrMsg     
end Function


'*********** 呼叫日期輸入項 *************************************
SUB SelDate(Object)
	If Not IsDate(window.event.srcElement.value) Then
        TodayDate = Date()
      else
        TodayDate = window.event.srcElement.value
    End If
    	ReturnValue = window.showModalDialog("../calendar/calendar.htm", TodayDate, "dialogWidth:300px;dialogHeight:208px;help:no;scroll:off;status:no;")
    If ReturnValue <> "" then
        Object.value = ReturnValue
    End IF 
END SUB

'***************************** Client Mood 同位化 ********************************************             

FUNCTION S_MoodID_ONCHANGE          
    Execute "ActionForm.R_MoodID(" & "ActionForm.S_MoodID.selectedIndex" & ").checked=true"          
END FUNCTION          
          
FUNCTION R_MoodIDChange          
Dim i          
i=0          
   Do          
       if ActionForm.R_MoodID(i).checked then          
          ActionForm.S_MoodID.selectedIndex = i          
          exit do           
       end if          
       i=i+1          
   loop            
END FUNCTION

' ***************** Client 開新視窗秀圖 *********************************     
Function OpenImageWindow(TableName,UploadFileID) '變數以參數的方式傳入。勿寫死之後才可共用
    Execute "window.open ""../IncludeFile/ShowGraphic.asp?UploadFileID=" & UploadFileID & "&TableName=" & TableName & """,null,""directories=no,height=480,width=640,Top=70,Left=70,no,status=yes,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=yes"" "
end Function     


'************ 置換干擾字 **********
'將查詢條件之干擾字去除

Function DropNoiseWord(InputString)	
	NoiseString = "古道,溫泉,林道,行程,紀錄,記錄,國家公園,生態,保護區,縱走,橫斷"
	NoiseArray = Split(NoiseString, ",")

	For Each NoiseWord In NoiseArray
		InputString = Replace(InputString, NoiseWord, "")
	Next
	DropNoiseWord = InputString
End Function


'*********** 測試區 **************

'Msgbox CheckAllCharacterOrInt("js%iodafj2qjfalkfjaqowetyq123456789jkj8")



