Revamped ajax interface, see release notes.
[lhc/web/wiklou.git] / skins / common / ajaxsearch.js
1 // remote scripting library
2 // (c) copyright 2005 modernmethod, inc
3
4 var started;
5 var typing;
6 var memory=null;
7 var body=null;
8 var oldbody=null;
9
10 // Remove the typing barrier to allow call() to complete
11 function Search_doneTyping()
12 {
13 typing=false;
14 }
15
16 // Wait 500ms to run call()
17 function Searching_Go()
18 {
19 setTimeout("Searching_Call()", 500);
20 }
21
22 // If the user is typing wait until they are done.
23 function Search_Typing() {
24 started=true;
25 typing=true;
26 window.status = "Waiting until you're done typing...";
27 setTimeout("Search_doneTyping()", 500);
28
29 // I believe these are needed by IE for when the users press return?
30 if (window.event)
31 {
32 if (event.keyCode == 13)
33 {
34 event.cancelBubble = true;
35 event.returnValue = false;
36 }
37 }
38 }
39
40 // Set the body div to the results
41 function Searching_SetResult( request )
42 {
43 if ( request.status != 200 ) {
44 alert("Error: " + request.status + " " + request.statusText + ": " + request.responseText);
45 return;
46 }
47
48 var result = request.responseText;
49
50 //body.innerHTML = result;
51 t = document.getElementById("searchTarget");
52 if ( t == null ) {
53 oldbody=body.innerHTML;
54 body.innerHTML= '<div id="searchTargetContainer"><div id="searchTarget" ></div></div>' ;
55 t = document.getElementById("searchTarget");
56 }
57 t.innerHTML = result;
58 t.style.display='block';
59 }
60
61 function Searching_Hide_Results()
62 {
63 t = document.getElementById("searchTarget");
64 t.style.display='none';
65 body.innerHTML = oldbody;
66 }
67
68
69 // This will call the php function that will eventually
70 // return a results table
71 function Searching_Call()
72 {
73 var x;
74 Searching_Go();
75
76 //Don't proceed if user is typing
77 if (typing)
78 return;
79
80 x = document.getElementById("searchInput").value;
81
82 // Don't search again if the query is the same
83 if (x==memory)
84 return;
85
86 memory=x;
87 if (started) {
88 // Don't search for blank or < 3 chars.
89 if ((x=="") || (x.length < 3))
90 {
91 return;
92 }
93
94 sajax_do_call( "wfSajaxSearch", [ x ], Searching_SetResult );
95 }
96 }
97
98 //Initialize
99 function sajax_onload() {
100 x = document.getElementById( 'searchInput' );
101 x.onkeypress= function() { Search_Typing(); };
102 Searching_Go();
103 body = document.getElementById("content");
104 }