+
+ global setNo
+ global nextSetNo
+ global hist
+
+ set host [lindex $sessionParms 0]
+ set initSet [lindex $sessionParms 1]
+ set scanLine [lindex $sessionParms 2]
+ set scanTerm [lrange $sessionParms 3 end]
+
+ if {[catch {set setNo $nextSetNo}]} {
+ set nextSetNo 1
+ set setNo 1
+ }
+ if {[catch {set oldHost [z39 connect]}]} {
+ set oldHost ""
+ }
+
+ set hist($setNo,host) $host
+ set hist($setNo,idAuthentication) [lindex $targets($host) 3]
+
+ if {[catch {z39 failback fail-response}]} {
+ ir z39
+ }
+
+ html "<h2>Search in " [lindex $targets($host) 0] "</h2>\n"
+ z39 callback ok-response
+ z39 failback fail-response
+ if {$host != $oldHost} {
+ catch {z39 disconnect}
+
+ set sessionWait 0
+ if {[catch {z39 connect $host}]} {
+ displayError "Cannot connect to target ${host}" {}
+ html "</body></html>\n"
+ z39 disconnect
+ wabort
+ } elseif {$sessionWait == 0} {
+ if {[catch {zwait sessionWait 35}]} {
+ displayError "Cannot connect to target ${host}" {}
+ html "</body></html>\n"
+ z39 disconnect
+ wabort
+ }
+ if {$sessionWait != 1} {
+ displayError "Cannot connect to target ${host}" {}
+ html "</body></html>\n"
+ z39 disconnect
+ wabort
+ }
+ }
+ set sessionWait 0
+ z39 idAuthentication $hist($setNo,idAuthentication)
+ if {[catch {z39 init}]} {
+ displayError "Cannot initialize target ${host}" {}
+ html "</body></html>\n"
+ wabort
+ }
+ if {[catch {zwait sessionWait 60}]} {
+ displayError "Cannot initialize target ${host}" {}
+ html "</body></html>\n"
+ wabort
+ }
+ if {$sessionWait != "1"} {
+ displayError "Cannot initialize target ${host}" {}
+ htmlr "</body></html>\n"
+ wabort
+ }
+ if {![z39 initResult]} {
+ displayError "Connection rejected by target ${host}" \
+ [z39 userInformationField]
+ z39 disconnect
+ htmlr "</body></html>"
+ wabort
+ }
+ }
+ set databases [lindex $targets($host) 1]
+
+# if {[catch {set prevHost $hist([expr $setNo - 1],host)}]} {
+# set databaseDefault [lindex $databases 0]
+# set oSetNo 0
+# } else {
+# set oSetNo [expr $setNo - 1]
+# set databaseDefault $hist($oSetNo,database)
+# }
+ if {$initSet == ""} {
+ set databaseDefault [lindex $databases 0]
+ set oSetNo 0
+ } else {
+ set oSetNo $initSet
+ set databaseDefault $hist($oSetNo,database)
+ }
+
+ html {<form action="http:} $env(SCRIPT_NAME)
+ html / $sessionId {/search.egw/} $setNo {" method=get>} \n