Separated ajax search box features from core ajax framework.
[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(result)
42 {
43 //body.innerHTML = result;
44 t = document.getElementById("searchTarget");
45 if ( t == null ) {
46 oldbody=body.innerHTML;
47 body.innerHTML= '<div id="searchTargetContainer"><div id="searchTarget" ></div></div>' ;
48 t = document.getElementById("searchTarget");
49 }
50 t.innerHTML = result;
51 t.style.display='block';
52 }
53
54 function Searching_Hide_Results()
55 {
56 t = document.getElementById("searchTarget");
57 t.style.display='none';
58 body.innerHTML = oldbody;
59 }
60
61
62 // This will call the php function that will eventually
63 // return a results table
64 function Searching_Call()
65 {
66 var x;
67 Searching_Go();
68
69 //Don't proceed if user is typing
70 if (typing)
71 return;
72
73 x = document.getElementById("searchInput").value;
74
75 // Don't search again if the query is the same
76 if (x==memory)
77 return;
78
79 memory=x;
80 if (started) {
81 // Don't search for blank or < 3 chars.
82 if ((x=="") || (x.length < 3))
83 {
84 return;
85 }
86 x_wfSajaxSearch(x, Searching_SetResult);
87 }
88 }
89
90 function x_wfSajaxSearch() {
91 sajax_do_call( "wfSajaxSearch", x_wfSajaxSearch.arguments );
92 }
93
94
95 //Initialize
96 function sajax_onload() {
97 x = document.getElementById( 'searchInput' );
98 x.onkeypress= function() { Search_Typing(); };
99 Searching_Go();
100 body = document.getElementById("content");
101 }