317922d3c5c543787b74c883c62301bddb3ac443
[freeside.git] / httemplate / elements / search-cust_main.html
1 <%doc>
2
3 Example:
4
5   include( '/elements/search-cust_main.html,
6              'field'       => 'custnum',
7              #slightly deprecated old synonym for field#'field_name'=>'custnum',
8              'find_button' => 1, #add a "find" button to the field
9              'curr_value'  => 54, #current value
10              'value        => 32, #deprecated synonym for curr_value
11   );
12
13 </%doc>
14 <INPUT TYPE="hidden" NAME="<% $field %>" VALUE="<% $value %>">
15
16 <!-- some false laziness w/ misc/batch-cust_pay.html, though not as bad as i'd thought at first... -->
17
18 <INPUT TYPE = "text"
19        NAME = "<% $field %>_search"
20        ID   = "<% $field %>_search"
21        SIZE = "32"
22        VALUE="<% $cust_main ? $cust_main->name : '(cust #, name or company)' %>"
23        onFocus="clearhint_<% $field %>_search(this);"
24        onClick="clearhint_<% $field %>_search(this);"
25        onChange="smart_<% $field %>_search(this);"
26 >
27
28 % if ( $opt{'find_button'} ) {
29     <INPUT TYPE    = "button"
30            VALUE   = 'Find',
31            NAME    = "<% $field %>_findbutton"
32            onClick = "smart_<% $field %>_search(this.form.<% $field %>_search);"
33     >
34 % }
35
36 <SELECT NAME="<% $field %>_select" ID="<% $field %>_select" STYLE="color:#ff0000; display:none" onChange="select_<% $field %>(this);">
37 </SELECT>
38
39 <% include('/elements/xmlhttp.html',
40               'url'  => $p. 'misc/xmlhttp-cust_main-search.cgi',
41               'subs' => [ 'smart_search' ],
42            )
43 %>
44
45 <SCRIPT TYPE="text/javascript">
46
47   function clearhint_<% $field %>_search (what) {
48
49     what.style.color = '#000000';
50
51     if ( what.value == '(cust #, name or company)' )
52       what.value = '';
53
54     if ( what.value.indexOf('Customer not found: ') == 0 )
55       what.value = what.value.substr(20);
56
57   }
58
59   var <% $field %>_search_active = false;
60
61   function smart_<% $field %>_search(what) {
62
63     var customer = what.value;
64
65     if ( customer == 'searching...' || customer == ''
66          || customer.indexOf('Customer not found: ') == 0 )
67       return;
68
69     if ( what.getAttribute('magic') == 'nosearch' ) {
70       what.setAttribute('magic', '');
71       return;
72     }
73
74     //what.value = 'searching...'
75     what.disabled = true;
76     what.style.color= '#000000';
77     what.style.backgroundColor = '#dddddd';
78
79     var customer_select = document.getElementById('<% $field %>_select');
80
81     //alert("search for customer " + customer);
82
83     function <% $field %>_search_update(customers) {
84
85       //alert('customers returned: ' + customers);
86
87       var customerArray = eval('(' + customers + ')');
88
89       what.disabled = false;
90       what.style.backgroundColor = '#ffffff';
91
92       if ( customerArray.length == 0 ) {
93
94         what.form.<% $field %>.value = '';
95
96         what.value = 'Customer not found: ' + what.value;
97         what.style.color = '#ff0000';
98
99         what.style.display = '';
100         customer_select.style.display = 'none';
101
102       } else if ( customerArray.length == 1 ) {
103
104         //alert('one customer found: ' + customerArray[0]);
105
106         what.form.<% $field %>.value = customerArray[0][0];
107         what.value = customerArray[0][1];
108
109         what.style.display = '';
110         customer_select.style.display = 'none';
111
112       } else {
113
114         //alert('multiple customers found, have to create select dropdown');
115
116         //blank the current list
117         for ( var i = customer_select.length; i >= 0; i-- )
118           customer_select.options[i] = null;
119
120         opt(customer_select, '', 'Multiple customers match "' + customer + '" - select one', '#ff0000');
121
122         //add the multiple customers
123         for ( var s = 0; s < customerArray.length; s++ )
124           opt(customer_select, customerArray[s][0], customerArray[s][1], '#000000');
125
126         opt(customer_select, 'cancel', '(Edit search string)', '#000000');
127
128         what.style.display = 'none';
129         customer_select.style.display = '';
130
131       }
132
133       <% $field %>_search_active = false;
134
135     }
136
137     <% $field %>_search_active = true;
138
139     smart_search( customer, <% $field %>_search_update );
140
141
142   }
143
144   function select_<% $field %> (what) {
145
146     var custnum = what.options[what.selectedIndex].value;
147     var customer = what.options[what.selectedIndex].text;
148
149     var customer_obj = document.getElementById('<% $field %>_search');
150
151     if ( custnum == '' ) {
152       //what.style.color = '#ff0000';
153
154     } else if ( custnum == 'cancel' ) {
155
156       customer_obj.style.color = '#000000';
157
158       what.style.display = 'none';
159       customer_obj.style.display = '';
160       customer_obj.focus();
161
162     } else {
163     
164       what.form.<% $field %>.value = custnum;
165
166       customer_obj.value = customer;
167       customer_obj.style.color = '#000000';
168
169       what.style.display = 'none';
170       customer_obj.style.display = '';
171
172     }
173
174   }
175
176   function opt(what,value,text,color) {
177     var optionName = new Option(text, value, false, false);
178     optionName.style.color = color;
179     var length = what.length;
180     what.options[length] = optionName;
181   }
182
183 </SCRIPT>
184 <%init>
185
186 my( %opt ) = @_;
187
188 my $field = $opt{'field'} || $opt{'field_name'} || 'custnum';
189
190 my $value = $opt{'curr_value'} || $opt{'value'};
191
192 my $cust_main = '';
193 if ( $value ) {
194   $cust_main = qsearchs({
195     'table'     => 'cust_main',
196     'hashref'   => { 'custnum' => $value },
197     'extra_sql' => " AND ". $FS::CurrentUser::CurrentUser->agentnums_sql,
198   });
199 }
200
201 </%init>