Subversion Repositories Sites.tela-botanica.org

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 david 1
/*
2
  Author: Justin Whitford
3
  Source: www.evolt.org
4
*/
5
function filtery(pattern, list){
6
  /*
7
  if the dropdown list passed in hasn't
8
  already been backed up, we'll do that now
9
  */
10
  if (!list.bak){
11
    /*
12
    We're going to attach an array to the select object
13
    where we'll keep a backup of the original dropdown list
14
    */
15
    list.bak = new Array();
16
    for (n=0; n<list.length; n++){
17
      list.bak[list.bak.length] = new Array(list[n].value, list[n].text);
18
    }
19
    bakselected = list.selectedIndex;
20
  }
21
 
22
  /*
23
  We're going to iterate through the backed up dropdown
24
  list. If an item matches, it is added to the list of
25
  matches. If not, then it is added to the list of non matches.
26
  */
27
  match = new Array();
28
  nomatch = new Array();
29
 
30
  if (pattern.length != 0) {
31
	  for (n=0; n<list.bak.length; n++){
32
		if(list.bak[n][1].toLowerCase().indexOf(pattern.toLowerCase())!=-1 || list.bak[n][0] == pattern ){
33
		  match[match.length] = new Array(list.bak[n][0], list.bak[n][1]);
34
		}else{
35
		  nomatch[nomatch.length] = new Array(list.bak[n][0], list.bak[n][1]);
36
		}
37
	  }
38
  }
39
 
40
  /*
41
  Now we completely rewrite the dropdown list.
42
  First we write in the matches, then we write
43
  in the non matches
44
  */
45
 
46
  if (match.length > 0) {
47
	  list.options.length = match.length;
48
	  for (n=0; n<match.length; n++){
49
		list[n].value = match[n][0];
50
		list[n].text = match[n][1];
51
	  }
52
 
53
    list.selectedIndex=0;
54
 
55
  }
56
  else {
57
	  list.options.length = list.bak.length;
58
	  for (n=0; n<list.bak.length; n++){
59
		list[n].value =  list.bak[n][0];
60
		list[n].text =  list.bak[n][1];
61
	  }
62
  list.selectedIndex = bakselected;
63
  }
64
 
65
}