﻿Function DeleteTrim(strSource)
    DeleteTrim = Trim(strSource)
End Function

Function SeprateString(ByRef strChar, ByVal strseprator)
    Dim intPos
    Dim sValue
    Dim i
    Dim nYHCount
    nYHCount = 0
    
    If InStr(strChar, strseprator) = 0 Then
        SeprateString = strChar
        strChar = ""
        Exit Function
    End If
    
    If strseprator = "=" Then
        sValue = strChar
        For i = 1 To Len(sValue)
            If Mid(sValue, i, 1) = "=" Then
                If (nYHCount Mod 2) = 0 Then
                    SeprateString = Left(strChar, i - 1)
                    strChar = Mid(strChar, i + Len(strseprator))
                    Exit Function
                End If
            ElseIf Mid(sValue, i, 1) = """" Then
                nYHCount = nYHCount + 1
            End If
        Next
        SeprateString = strChar
        strChar = ""
    Else
        intPos = InStr(strChar, strseprator)
        SeprateString = Left(strChar, intPos - 1)
        strChar = Mid(strChar, intPos + Len(strseprator))
    End If
End Function

Function IsMyField(ByVal strChar, ByVal IsField)
    Dim strFF

    strChar = Trim(UCase(strChar))
    strFF = "Author,Name,Title,Keyword,ISSN,Year,Name_C,Title_C,Keyword_C,任意字段"
    If strChar = "" Then
        IsMyField = False
        Exit Function
    End If
    If Not IsField Then strFF = "A,J,T,K,I,Y,S,N,C,U"

    IsMyField = CBool(InStr("," & strFF & ",", "," & strChar & ","))
End Function

Function IsMyRelation(ByVal strChar)
    If Trim(strChar) = "" Then
        IsMyRelation = False
        Exit Function
    End If

    If InStr(",=,>,<,>=,<=,<>,", "," & Trim(strChar) & ",") > 0 Then
        IsMyRelation = True
    Else
        IsMyRelation = False
    End If
End Function

Function IsBracket(ByVal strChar)
    If Trim(strChar) = "" Then
        IsBracket = False
        Exit Function
    End If
    IsBracket = CBool(InStr(",),(,", "," & Trim(strChar) & ","))
End Function

'翻译字段
Function TransField(ByVal strChar)
    Dim strMid
    Dim blnBracket

    If Left(strChar, 1) = "(" Then
        blnBracket = True
    Else
        blnBracket = False
    End If

    If blnBracket Then
        strMid = Trim(Mid(UCase(strChar), 2))
    Else
        strMid = UCase(Trim(strChar))
    End If
    Select Case strMid
    Case "J"        '刊名
        TransField = "NAME"
    Case "A"        '作者
        TransField = "AUTHOR"
    Case "I"        'ISSN
        TransField = "ISSN"
    Case "Y"        '出版年
        TransField = "YEAR"
    Case "T"        '篇名
        TransField = "TITLE"
    Case "K"        '关键词
        TransField = "KEYWORD"
    Case "S"        '中文题名
        TransField = "TITLE_C"
    Case "N"        '中文刊名
        TransField = "NAME_C"
    Case "C"        '中文关键词
        TransField = "KEYWORD_C"
    Case "U"        '任意字段
        TransField = "任意字段"
    End Select

    If blnBracket Then
        TransField = "(" & TransField
    End If
End Function

'翻译字段
Function TransSearchDoor(ByVal strField)
    Select Case UCase(strField)
    Case "NAME"
        TransSearchDoor = "J"
    Case "WRITER"
        TransSearchDoor = "A"
    Case "ISSN"
        TransSearchDoor = "I"
    Case "YEAR"
        TransSearchDoor = "Y"
    Case "TITLE"
        TransSearchDoor = "T"
    Case "KEYWORD"
        TransSearchDoor = "K"
    Case "TITLE_C"
        TransSearchDoor = "S"
    Case "NAME_C"
        TransSearchDoor = "N"
    Case "KEYWORD_C"
        TransSearchDoor = "C"
    Case "任意字段"
        TransSearchDoor = "U"
    End Select
End Function

'翻译字段
Function GetSubStr(ByRef strSource, ByRef strFlag, ByVal intIsLogic)
    Dim intPos
    Dim strMid
    Dim strLogic

    If intIsLogic = 1 Then
        strLogic = "*,+,-,=,>,<,>=,<=,<>,(,)"
    ElseIf intIsLogic = 2 Then
        strLogic = "=,>,<,>=,<=,<>"
    ElseIf intIsLogic = 3 Then
        strLogic = "(,)"
    ElseIf intIsLogic = 4 Then
        strLogic = "*,+,-"
    End If

    For intPos = 1 To Len(strSource)
        strMid = Mid(strSource, intPos, 1)

        If InStr(strLogic, strMid) > 0 Then
            If strMid = ">" Or strMid = "<" Then
                If InStr("=,>", Mid(strSource, intPos + 1, 1)) > 0 Then
                    strMid = Mid(strSource, intPos, 2)
                    GetSubStr = Left(strSource, intPos)
                    strSource = Mid(strSource, intPos + 3)
                    strFlag = strMid
                Else
                    GetSubStr = Left(strSource, intPos)
                    strSource = Mid(strSource, intPos + 1)
                    strFlag = strMid
                End If
            Else
                GetSubStr = Left(strSource, intPos - 1)
                strSource = Mid(strSource, intPos + 1)
                strFlag = strMid
            End If
            Exit Function
        End If
    Next
    GetSubStr = strSource
    strSource = ""
    strFlag = ""
End Function

Function AddShow(ByVal strText, ByVal strEntry)
    Dim strMid, strFlag, strBracket, str1
    If Len(strText) <= 2 Then
        AddShow = strEntry & "=" & strText
    Else
        strMid = GetSubStr(strText, strFlag, 2)
        str1 = Left(strMid, 1)
        Do While str1 = "("
            strBracket = strBracket & str1

            strMid = Mid(strMid, 2)

            str1 = Left(strMid, 1)
        Loop
        If IsMyField(strMid, False) Then
            AddShow = strBracket & strMid & strFlag & strText
        Else
            AddShow = strEntry & "=" & strBracket & strMid & strFlag & strText
        End If
    End If
    
	AddShow = Replace(AddShow, "n=", "中文刊名=")
    AddShow = Replace(AddShow, "a=", "作者=")
    AddShow = Replace(AddShow, "j=", "刊名=")
    AddShow = Replace(AddShow, "t=", "题名=")
    AddShow = Replace(AddShow, "k=", "关键词=")
    AddShow = Replace(AddShow, "i=", "ISSN=")
    AddShow = Replace(AddShow, "y=", "出版年=")
    AddShow = Replace(AddShow, "s=", "中文题名=")
    AddShow = Replace(AddShow, "c=", "中文关键词=")
    AddShow = Replace(AddShow, "u=", "任意字段=")
	
    AddShow = Replace(AddShow, "N=", "中文刊名=")
    AddShow = Replace(AddShow, "A=", "作者=")
    AddShow = Replace(AddShow, "J=", "刊名=")
    AddShow = Replace(AddShow, "T=", "题名=")
    AddShow = Replace(AddShow, "K=", "关键词=")
    AddShow = Replace(AddShow, "I=", "ISSN=")
    AddShow = Replace(AddShow, "Y=", "出版年=")
    AddShow = Replace(AddShow, "S=", "中文题名=")
    AddShow = Replace(AddShow, "C=", "中文关键词=")
    AddShow = Replace(AddShow, "U=", "任意字段=")
End Function

'添加字段
Function AddField(ByVal strText, ByVal strEntry)
    Dim strMid, strFlag, strBracket, str1
    If Len(strText) <= 2 Then
        AddField = strEntry & "=" & strText
        Exit Function
    End If
    strMid = UCase(GetSubStr(strText, strFlag, 2))
    str1 = Left(strMid, 1)
    Do While str1 = "("
        strBracket = strBracket & str1

        strMid = Mid(strMid, 2)

        str1 = Left(strMid, 1)
    Loop
    If IsMyField(strMid, False) Then
        AddField = strBracket & strMid & strFlag & strText
    Else
        AddField = strEntry & "=" & strBracket & strMid & strFlag & strText
    End If
End Function

'是否为操作符
Function IsOperation(ByVal sChar)
   Dim sXX
   
   sXX = ",*,+,-,"
   sChar = "," & sChar & ","
   IsOperation = (InStr(sXX, sChar) > 0)
End Function

'获取右边表达式
Function GetRigorExpress(ByVal sChar, ByVal bRigor)
    If sChar = "" Then Exit Function
    
    Dim i, J, bFound, strMid, strNewChar
    
    J = 1
    strMid = Mid(sChar, J, 1)
    Do While IsBracket(strMid) Or IsMyRelation(strMid) Or IsOperation(strMid)
        strMid = Mid(sChar, J, 1)
        J = J + 1
    Loop
    
    If J = 1 Then J = J + 1
    J = J - 1 - 1
    If bRigor Then
        strNewChar = Left(sChar, J) & "["
    Else
        strNewChar = Left(sChar, J) & ""
    End If
    bFound = False
    For i = J + 1 To Len(sChar)
        strMid = Mid(sChar, i, 1)
        
        If IsBracket(strMid) Or IsMyRelation(strMid) Or IsOperation(strMid) Then
            If Not bFound Then
                If bRigor Then
                    strNewChar = strNewChar & "]" & strMid
                Else
                    strNewChar = strNewChar & "" & strMid
                End If
            Else
                strNewChar = strNewChar & strMid
            End If
            
            bFound = True
        Else
            If bFound Then
                If bRigor Then
                    strNewChar = strNewChar & "[" & Trim(strMid)
                Else
                    strNewChar = strNewChar & "" & strMid
                End If
            Else
                strNewChar = strNewChar & strMid
            End If
            bFound = False
        End If
    Next
    If Not bFound Then
        If bRigor Then
            strNewChar = strNewChar & "]"
        Else
            strNewChar = strNewChar & ""
        End If
    End If
    
    GetRigorExpress = strNewChar
End Function

Function GetBlueValue(ByVal sValue)
    Dim nLeftBracketCount, nRightBracketCount
    Dim sTempValue
    Dim i, nPos
    Dim IsInYH
    
    If sValue = "" Then
        GetBlueValue = ""
        Exit Function
    End If
    
    sValue = Trim(sValue)
    
    nLeftBracketCount = 0: nRightBracketCount = 0
    IsInYH = False
    For i = 1 To Len(sValue)
        If Mid(sValue, i, 1) = "(" Then
            If Not IsInYH Then
                nLeftBracketCount = nLeftBracketCount + 1
            End If
        ElseIf Mid(sValue, i, 1) = ")" Then
            If Not IsInYH Then
                nRightBracketCount = nRightBracketCount + 1
            End If
        ElseIf Mid(sValue, i, 1) = """" Then
            If IsInYH Then
                IsInYH = False
            Else
                IsInYH = True
            End If
        End If
    Next

    If nLeftBracketCount <> nRightBracketCount Then
        If nLeftBracketCount > nRightBracketCount Then
            For i = 1 To (nLeftBracketCount - nRightBracketCount)
                nPos = InStr(sValue, "(")
                sValue = Left(sValue, nPos - 1) & Mid(sValue, nPos + 1)
            Next
        Else
            sValue = StrReverse(sValue)
            For i = 1 To (nRightBracketCount - nLeftBracketCount)
                nPos = InStr(sValue, ")")
                sValue = Left(sValue, nPos - 1) & Mid(sValue, nPos + 1)
            Next
            sValue = StrReverse(sValue)
        End If
        sValue = Trim(sValue)
        Do While IsBracket(Left(sValue, 1))
            sValue = Trim(Mid(sValue, 2))
        Loop
        Do While IsBracket(Right(sValue, 1))
            sValue = Trim(Left(sValue, Len(sValue) - 1))
        Loop
    End If

    sValue = Trim(sValue)
    Do While IsOperation(Left(sValue, 1)) Or IsMyRelation(Left(sValue, 1))
    sValue = Trim(Mid(sValue, 2))
    Loop
    Do While IsOperation(Right(sValue, 1)) Or IsMyRelation(Right(sValue, 1))
    sValue = Trim(Left(sValue, Len(sValue) - 1))
    Loop
    GetBlueValue = Trim(sValue)
End Function

Private Function IssnCondition(ByVal strISSN, ByVal strFind)
    Dim strTemp, strMid, strLeft, strRight
    Dim intIssnPos
    Dim intNextFldPos
    Dim intPos, intStart
    Dim strMidLeft, strMidRight
    '处理ISSN,CNNO
    'intIssnPos = InStr(strISSN, "I=")
    intIssnPos = InStr(UCase(strISSN), strFind)
    If intIssnPos > 0 Then
        intNextFldPos = InStr(intIssnPos + 2, strISSN, "=")
        If intNextFldPos = 0 Then
            strMid = Mid(strISSN, intIssnPos)
            
            strLeft = Left(strISSN, intIssnPos - 1)
            strRight = ""
        Else
            strMid = Mid(strISSN, intIssnPos, intNextFldPos - intIssnPos - 1 - 1)
            strLeft = Left(strISSN, intIssnPos - 1)
            strRight = Mid(strISSN, intNextFldPos - 1 - 1)
        End If
        
        intStart = 1
        intPos = InStr(intStart, strMid, "-")
        Do While intPos > 0
            If intPos Mod 2 = 1 Then
                strMid = Left(strMid, intPos - 1) & "Y" & Mid(strMid, intPos + 1)
                intStart = intPos + 1
            Else
                    intStart = intPos + 1
            End If
            
            intPos = InStr(intStart, strMid, "-")
    Loop
    IssnCondition = strLeft & strMid & strRight
    Else
        IssnCondition = strISSN
    End If
End Function

Function GetShowExp(ByVal strText, ByVal strField)
    strText = Trim(strText)
    If strText = "" Then
        GetShowExp = ""
        Exit Function
    End If
    strText = Replace(strText, "（", "(")
    strText = Replace(strText, "）", ")")
    strText = Replace(strText, "＋", "+")
    strText = Replace(strText, "＊", "*")
    strText = Replace(strText, "－", "-")
    strText = Replace(strText, "＝", "=")
     strText = Replace(strText, """", "")
    GetShowExp = AddShow(strText, strField)
End Function

Function YearCondition(ByVal strText, ByVal strFind)
    'E.G.   Y=2008 || Y=1998-2008
    Dim i, strPref, strYear, arrYear, strExpress, iYearPos, iNextFldPos
    Dim BeginYear, EndYear, strMid, strLeft, strRight
    
    iYearPos = InStr(UCase(strText), strFind)
    If iYearPos > 0 Then
        iNextFldPos = InStr(iYearPos + 2, strText, "=")
        If iNextFldPos = 0 Then
            strMid = Mid(strText, iYearPos)
            strLeft = Left(strText, iYearPos - 1)
            strRight = ""
        Else
            strMid = Mid(strText, iYearPos, iNextFldPos - iYearPos - 1 - 1)
            strLeft = Left(strText, iYearPos - 1)
            strRight = Mid(strText, iNextFldPos - 1 - 1)
        End If
        
		'如果用户已经输入()
        If Right(strMid, 1) = ")" Then
            strMid = Left(strMid, Len(strMid) - 1)
            strRight = ")" & strRight
        End If
		
        strPref = UCase(Left(strMid, 2)) '前缀Y=
        strYear = Right(strMid, Len(strMid) - 2)
        
        '----- 若包含'-',则含有多个年份
        If InStr(strYear, "-") > 0 Then
            arrYear = Split(strYear, "-")
            
            '验证数据有效性
            If Not IsNumeric(arrYear(0)) Then
                YearCondition = Left(strLeft, Len(strLeft) - 1) & strRight '验证数据有效性
                Exit Function
            End If
            If Not IsNumeric(arrYear(1)) Then arrYear(1) = Year(Now())
            
            BeginYear = CInt(arrYear(0))
            EndYear = CInt(arrYear(1))
            
            '判断是否转换失败
            If (BeginYear = 0 Or BeginYear > Year(Now())) Then
                YearCondition = Left(strLeft, Len(strLeft) - 1) & strRight '若初始年为空，则去掉出版年条件
                Exit Function
            End If
            If (EndYear = 0 Or EndYear > Year(Now())) Then EndYear = Year(Now())
            
            '判断年度，若开始>结束，则两者互换
            If BeginYear > EndYear Then
                BeginYear = BeginYear + EndYear
                EndYear = BeginYear - EndYear
                BeginYear = BeginYear - EndYear
            End If
            
            strMid = ""
            For i = BeginYear To EndYear
                strMid = strMid & strPref & CStr(i) & "+"
            Next
            strMid = Left(strMid, Len(strMid) - 1)
			strMid = "(" & strMid & ")"
        Else
            If Not IsNumeric(strYear) Then
                YearCondition = Left(strLeft, Len(strLeft) - 1) & strRight '验证数据有效性
                Exit Function
            End If
            EndYear = CInt(strYear)
            If (EndYear = 0 Or EndYear > Year(Now())) Then EndYear = Year(Now())
            
            strMid = strPref & CStr(EndYear)
        End If
        YearCondition = strLeft & strMid & strRight
    Else
        YearCondition = strText
    End If
End Function

Function GetExpress(ByVal strText, ByVal strField)
    strText = Trim(strText)
    If strText = "" Then
        GetExpress = ""
        Exit Function
    End If
    strShowExpress = ""
    '----- 过滤非法符号
    'strText = UCase(strText)
    strText = Replace(strText, "（", "(")
    strText = Replace(strText, "）", ")")
    strText = Replace(strText, "＋", "+")
    strText = Replace(strText, "＊", "*")
    strText = Replace(strText, "－", "-")
    strText = Replace(strText, "＝", "=")
    strText = AddField(strText, strField)
    strText = IssnCondition(strText, "I=")
    strText = IssnCondition(strText, "N=")
    strText = YearCondition(strText, "Y=")   '确定是否为Year,并分隔Year
    Dim sField, sExpress, sMid, i, J, K, sNewExpress, sValue, sXX, sXY
    Dim sNewField, bFound, sExtractValue, sBracket
    Dim sOldValue, sBlueValue
    Dim sLeftValue, sRightValue
    
    sOldValue = "": sBlueValue = ""
    i = 0
    sMid = SeprateString(strText, "=")
    Do While sMid <> ""
        If i = 0 Then
            sField = sField & "\~" & sMid
        Else
            If strText <> "" Then
                sField = sField & "\~" & Right(RTrim(sMid), 1) '保存键
                sExpress = sExpress & "\~" & Left(RTrim(sMid), Len(RTrim(sMid)) - 1) '保存值
            Else
                sExpress = sExpress & "\~" & sMid
            End If
        End If
        i = i + 1
        sMid = SeprateString(strText, "=")
    Loop
    If sField <> "" Then sField = Mid(UCase(sField), 3)
    If sExpress <> "" Then sExpress = Mid(sExpress, 3)
    sMid = SeprateString(sField, "\~")
    sValue = SeprateString(sExpress, "\~")
    Do While sMid <> ""
        If InStr(sMid, "U") > 0 Then
        '要求进行特殊处理，空格要处理成与的关系
        sLeftValue = "": sRightValue = ""
        Do While (IsOperation(Left(sValue, 1)) Or IsBracket(Left(sValue, 1)) Or IsMyRelation(Left(sValue, 1)))
            sLeftValue = sLeftValue & Left(sValue, 1)
            sValue = Trim(Mid(sValue, 2))
        Loop
        Do While (IsOperation(Right(sValue, 1)) Or IsBracket(Right(sValue, 1)) Or IsMyRelation(Right(sValue, 1)))
            sRightValue = Right(sValue, 1) & sRightValue
            sValue = Trim(Left(sValue, Len(sValue) - 1))
        Loop
        sValue = sLeftValue & sValue & sRightValue
        
        '把空格处理成＊。因为中间可能有多个空格，所以不能用Replace函数
        sRightValue = ""
        sLeftValue = SeprateString(sValue, " ")
        ncount = 0
        Do While (sLeftValue <> "") And (ncount <= 5)
            sRightValue = sRightValue & "*" & Trim(sLeftValue)
            sValue = Trim(sValue)
            sLeftValue = SeprateString(sValue, " ")
            ncount = ncount + 1
        Loop
        If sRightValue <> "" Then sRightValue = Mid(sRightValue, 2)
        '处理在末尾或的特殊符号
        If Len(sRightValue) > 1 Then
            If Left(sRightValue, 1) = "." Then sRightValue = Mid(sRightValue, 2)
            If Right(sRightValue, 1) = "." Then sRightValue = Mid(sRightValue, 1, Len(sRightValue) - 1)
        Else
            If sRightValue = "." Then sRightValue = "%0x002E%"
        End If
        sRightValue = Replace(sRightValue, ".", "*") '2003-07-30  LiuHui request
        sRightValue = Replace(sRightValue, "-", "%0x002D%") '2003-08-07  LiuHui request
        Do While InStr(sRightValue, "**") > 0
            sRightValue = Replace(sRightValue, "**", "*")
        Loop
'           DO While Right(sRightValue,1)="*"
'               sRightValue = Left(sRightValue,len(sRightValue)-1)
'           Loop
            
        sValue = sRightValue
            sNewExpress = sNewExpress & Replace(sMid, "U", "任意字段") & "=" & sValue
        Else
        sExtractValue = Trim(GetRigorExpress(sValue, False))
        
        '要求进行特殊处理，空格要处理成与的关系
        sLeftValue = "": sRightValue = ""
        Do While (IsOperation(Left(sExtractValue, 1)) Or IsBracket(Left(sExtractValue, 1)) Or IsMyRelation(Left(sExtractValue, 1)))
            sLeftValue = sLeftValue & Left(sExtractValue, 1)
            sExtractValue = Trim(Mid(sExtractValue, 2))
        Loop
        Do While (IsOperation(Right(sExtractValue, 1)) Or IsBracket(Right(sExtractValue, 1)) Or IsMyRelation(Right(sExtractValue, 1)))
            sRightValue = Right(sExtractValue, 1) & sRightValue
            sExtractValue = Trim(Left(sExtractValue, Len(sExtractValue) - 1))
        Loop
        sExtractValue = sLeftValue & sExtractValue & sRightValue
        
        //把空格处理成＊。因为中间可能有多个空格，所以不能用Replace函数
        sRightValue = ""
        sLeftValue = SeprateString(sExtractValue, " ")
        Do While sLeftValue <> ""
            sRightValue = sRightValue & "*" & Trim(sLeftValue)
            sExtractValue = Trim(sExtractValue)
            sLeftValue = SeprateString(sExtractValue, " ")
        Loop
        If sRightValue <> "" Then sRightValue = Mid(sRightValue, 2)
        sExtractValue = sRightValue

        '处理字段名。注意不能用 TranceField。因为sMid中可能有括号等符号
        If InStr(sMid, "I") > 0 Then
                sMid = Replace(sMid, "I", "ISSN")
                sMid = Replace(sMid, "-", "*")
                sExtractValue = Replace(sExtractValue, "Y", "%0x002D%")
        ElseIf InStr(sMid, "Y") > 0 Then
                sMid = Replace(sMid, "Y", "Year")
        ElseIf InStr(sMid, "A") > 0 Then
                sMid = Replace(sMid, "A", "Author")
                If Len(sExtractValue) > 1 Then
                    If Left(sExtractValue, 1) = "," Then sExtractValue = Mid(sExtractValue, 2)
                    If Right(sExtractValue, 1) = "," Then sExtractValue = Mid(sExtractValue, 1, Len(sExtractValue) - 1)
                    If Left(sExtractValue, 1) = ";" Then sExtractValue = Mid(sExtractValue, 2)
                    If Right(sExtractValue, 1) = ";" Then sExtractValue = Mid(sExtractValue, 1, Len(sExtractValue) - 1)
                Else
                    If sExtractValue = "," Then sExtractValue = "%0x002D%"
                    If sExtractValue = ";" Then sExtractValue = "%0x003B%"
                End If
                sExtractValue = Replace(sExtractValue, ",", "*")
                sExtractValue = Replace(sExtractValue, ";", "*")
        ElseIf InStr(sMid, "T") > 0 Then
                sMid = Replace(sMid, "T", "TITLE")
        ElseIf InStr(sMid, "J") > 0 Then
                sMid = Replace(sMid, "J", "NAME")
        ElseIf InStr(sMid, "S") > 0 Then
                sMid = Replace(sMid, "S", "TITLE_C")
        ElseIf InStr(sMid, "N") > 0 Then
                sMid = Replace(sMid, "N", "NAME_C")
        ElseIf InStr(sMid, "C") > 0 Then
                sMid = Replace(sMid, "C", "KEYWORD_C")
        ElseIf InStr(sMid, "K") > 0 Then
                sMid = Replace(sMid, "K", "KEYWORD")
        Else
                sMid = Replace(sMid, "U", "任意字段")
        End If
        
        If Len(sExtractValue) > 1 Then
                If Left(sExtractValue, 1) = "." Then sExtractValue = Mid(sExtractValue, 2)
                If Right(sExtractValue, 1) = "." Then sExtractValue = Mid(sExtractValue, 1, Len(sExtractValue) - 1)
        Else
                If sExtractValue = "." Then sExtractValue = "%0x002E%"
        End If
        sExtractValue = Replace(sExtractValue, ".", "*") '2003-07-30  LiuHui request
        sExtractValue = Replace(sExtractValue, "_", "%0x002D%") '2003-08-07  LiuHui request
        
        Do While InStr(sExtractValue, "**") > 0
            sExtractValue = Replace(sExtractValue, "**", "*")
        Loop
 '          DO While Right(sExtractValue,1)="*"
 '              sExtractValue = Left(sExtractValue,len(sExtractValue)-1)
 '          Loop
        
            sNewExpress = sNewExpress & sMid & "=" & sExtractValue
        End If
        
        If IsOperation(Right(sExtractValue, 1)) Then sOldValue = Right(sExtractValue, 1)
        i = i + 1
        sMid = SeprateString(sField, "\~")
        sValue = SeprateString(sExpress, "\~")
    Loop
    'GetExpress = sNewExpress
    Dim strMid, strDeal
    Dim nTemp, nBegin, nEnd, strRes
    strRes = ""
    For nTemp = 1 To Len(sNewExpress)
        strMid = Mid(sNewExpress, nTemp, 1)
        If strMid = """" Then
            If nBegin > 0 Then
                nEnd = nTemp
                strDeal = Mid(sNewExpress, nBegin + 1, nEnd - nBegin - 1)
                strDeal = Replace(strDeal, "+", "%0x002B%")
                strDeal = Replace(strDeal, "-", "%0x002D%")
                strDeal = Replace(strDeal, "*", "%0x002A%")
                strDeal = Replace(strDeal, "(", "%0x0028%")
                strDeal = Replace(strDeal, ")", "%0x0029%")
                strDeal = Replace(strDeal, "{", "%0x007B%")
                strDeal = Replace(strDeal, "}", "%0x007D%")
                strDeal = Replace(strDeal, "=", "%0x003D%")
                strRes = strRes & strDeal
                
                nBegin = 0
                nEnd = 0
            Else
                nBegin = nTemp
            End If
        Else
            If nBegin = 0 Then
                strRes = strRes & strMid
            End If
        End If
    Next
    If nBegin > 0 And nEnd = 0 Then
        strRes = strRes & Mid(sNewExpress, nBegin)
    End If
    strRes = Replace(strRes, "[[", "[")
    strRes = Replace(strRes, "]]", "]")
    GetExpress = strRes
End Function

'取得年度条件
Function GetYear()
    If Trim(document.All("BeginYear").Value) = "" And Trim(document.All("EndYear").Value) = "" Then
        GetYear = ""
        Exit Function
    End If
    If CInt(document.All("BeginYear").Value) > CInt(document.All("EndYear").Value) Then
        MsgBox "您输入的开始年大于了结束年!", vbExclamation, "提示信息"
        GetYear = "Err"
        Exit Function
    End If
    If CInt(document.All("BeginYear").Value) = CInt(document.All("EndYear").Value) Then
        GetYear = "Year=" & document.All("BeginYear").Value
        Exit Function
    End If
    If CInt(document.All("BeginYear").Value) = 2005 And CInt(document.All("EndYear").Value) = Year(Now()) Then
        GetYear = ""
        Exit Function
    End If
    If CInt(document.All("BeginYear").Value) > 2005 Then
        For i = CInt(document.All("BeginYear").Value) To CInt(document.All("EndYear").Value)
            GetYear = GetYear & "+Year=" & i
        Next
        GetYear = "(" & Mid(GetYear, 2) & ")"
    End If
    If CInt(document.All("BeginYear").Value) <= 2005 Then
        For i = 2005 To CInt(document.All("EndYear").Value)
            GetYear = GetYear & "+Year=" & i
        Next
        GetYear = "(" & Mid(GetYear, 2) & ")"
    End If
End Function

'取得检索范围
Function GetRange()
    GetDB = ""
End Function

Function GetRel(ByVal strVV)
    If strVV = "与" Then
        GetRel = "*"
    ElseIf strVV = "或" Then
        GetRel = "+"
    Else
        GetRel = "-"
    End If
End Function
Function GetField(ByVal i)
    Dim strField
    Dim strKey
    strKey = document.All("select" & CStr(i)).Value
    strField = strKey
    GetField = strField
End Function

Sub CheckRadio(ByVal strValue)
    'for i=0 to aa.length-1
        'if document.all.aa(i).checked = true then
            'msgbox document.all.aa(i).value
        'end if
    'next
    'dim rb=document.all("sel"+m);
End Sub

Function DoAdvSearch()
    Dim strYear
    Dim i, strExpress, strField, strValue, strRange
    Dim strAllExpress, strShowExpress
    Dim strSearch
    Dim strShow
    Dim bFlag
    Dim strTS
    strRange = "0" // 判断选择类型 如 前方一致 还是后方一致 默认0 表示任意位置
    strYear = GetYear: If strYear = "Err" Then Exit Function
    For i = 0 To 4
        strField = GetField(i)
        strValue = Trim(document.All("text" & CStr(i)).Value)
        
        // 判断选择类型开始
        strRange = document.getElementById("ddlRange" & i).value
        // 不限定类型  检索条件不为空  检索条件中不包含字符'='(包含=的表示检索表达式 暂时不考虑)
        if (strRange <> "0" and strValue<>"" and instr(strValue,"=") = 0) then
            //前方一致 相当于 SQL中的like '[字符]%'
            if (strRange = "1") then  
                strValue = "[" & strValue 
            //后方一致 相当于 SQL中的like '%[字符]'
            elseif (strRange = "2") then  
                strValue = strValue & "]"
            // 精确搜索 相当于 SQL中的 = '[字符]'
            elseif (strRange = "3") then  
                strValue= "[" & strValue & "]"
            end if
        end if
        //判断选定类型结束
        
        If strTS = "" Then
            strTS = strValue
        Else
            strTS = strTS & " " & strValue
        End If
        strExpress = GetExpress(strValue, GetField(i))
        strShow = GetShowExp(strValue, GetField(i))
        If strExpress <> "" Then
            If strAllExpress = "" Then
                strAllExpress = "(" & strExpress & ")"
            Else
                strAllExpress = strAllExpress & GetRel(document.All("Relation" & i).Value) & "(" & strExpress & ")"
            End If
            If strShowExpress = "" Then
                strShowExpress = strShow
            Else
                strShowExpress = strShowExpress & GetRel(document.All("Relation" & i).Value) & strShow
            End If
        End If
    Next
    
    If strShowExpress = "" Then
        strShowExpress = "年=" & document.All("BeginYear").Value & "-" & document.All("EndYear").Value
    Else
        strShowExpress = strShowExpress & "*年=" & document.All("BeginYear").Value & "-" & document.All("EndYear").Value
    End If
    
    If strYear <> "" Then
        If strAllExpress <> "" Then
            strAllExpress = "(" & strAllExpress & ")*(" & strYear & ")"
        Else
            strAllExpress = strYear
        End If
    End If
    
    if strAllExpress="" Then
        DoAdvSearch=""
    Else
        DoAdvSearch = strAllExpress & "/" & strShowExpress  // 检索表达式/显示检索表达式
    End If
End Function
