23c4369e857e2363b268d0a4492aa298bee0fc90
[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   function smart_<% $field %>_search(what) {
60
61     var customer = what.value;
62
63     if ( customer == 'searching...' || customer == ''
64          || customer.indexOf('Customer not found: ') == 0 )
65       return;
66
67     if ( what.getAttribute('magic') == 'nosearch' ) {
68       what.setAttribute('magic', '');
69       return;
70     }
71
72     //what.value = 'searching...'
73     what.disabled = true;
74     what.style.color= '#000000';
75     what.style.backgroundColor = '#dddddd';
76
77     var customer_select = document.getElementById('<% $field %>_select');
78
79     //alert("search for customer " + customer);
80
81     function <% $field %>_search_update(customers) {
82
83       //alert('customers returned: ' + customers);
84
85       var customerArray = eval('(' + customers + ')');
86
87       what.disabled = false;
88       what.style.backgroundColor = '#ffffff';
89
90       if ( customerArray.length == 0 ) {
91
92         what.form.<% $field %>.value = '';
93
94         what.value = 'Customer not found: ' + what.value;
95         what.style.color = '#ff0000';
96
97         what.style.display = '';
98         customer_select.style.display = 'none';
99
100       } else if ( customerArray.length == 1 ) {
101
102         //alert('one customer found: ' + customerArray[0]);
103
104         what.form.<% $field %>.value = customerArray[0][0];
105         what.value = customerArray[0][1];
106
107         what.style.display = '';
108         customer_select.style.display = 'none';
109
110       } else {
111
112         //alert('multiple customers found, have to create select dropdown');
113
114         //blank the current list
115         for ( var i = customer_select.length; i >= 0; i-- )
116           customer_select.options[i] = null;
117
118         opt(customer_select, '', 'Multiple customers match "' + customer + '" - select one', '#ff0000');
119
120         //add the multiple customers
121         for ( var s = 0; s < customerArray.length; s++ )
122           opt(customer_select, customerArray[s][0], customerArray[s][1], '#000000');
123
124         opt(customer_select, 'cancel', '(Edit search string)', '#000000');
125
126         what.style.display = 'none';
127         customer_select.style.display = '';
128
129       }
130
131     }
132
133     smart_search( customer, <% $field %>_search_update );
134
135
136   }
137
138   function select_<% $field %> (what) {
139
140     var custnum = what.options[what.selectedIndex].value;
141     var customer = what.options[what.selectedIndex].text;
142
143     var customer_obj = document.getElementById('<% $field %>_search');
144
145     if ( custnum == '' ) {
146       //what.style.color = '#ff0000';
147
148     } else if ( custnum == 'cancel' ) {
149
150       customer_obj.style.color = '#000000';
151
152       what.style.display = 'none';
153       customer_obj.style.display = '';
154       customer_obj.focus();
155
156     } else {
157     
158       what.form.<% $field %>.value = custnum;
159
160       customer_obj.value = customer;
161       customer_obj.style.color = '#000000';
162
163       what.style.display = 'none';
164       customer_obj.style.display = '';
165
166     }
167
168   }
169
170   function opt(what,value,text,color) {
171     var optionName = new Option(text, value, false, false);
172     optionName.style.color = color;
173     var length = what.length;
174     what.options[length] = optionName;
175   }
176
177 </SCRIPT>
178 <%init>
179
180 my( %opt ) = @_;
181
182 my $field = $opt{'field'} || $opt{'field_name'} || 'custnum';
183
184 my $value = $opt{'curr_value'} || $opt{'value'};
185
186 my $cust_main = '';
187 if ( $value ) {
188   $cust_main = qsearchs({
189     'table'     => 'cust_main',
190     'hashref'   => { 'custnum' => $value },
191     'extra_sql' => " AND ". $FS::CurrentUser::CurrentUser->agentnums_sql,
192   });
193 }
194
195 </%init>