5 include( '/elements/search-cust_main.html,
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
14 <INPUT TYPE="hidden" NAME="<% $field %>" VALUE="<% $value %>">
16 <!-- some false laziness w/ misc/batch-cust_pay.html, though not as bad as i'd thought at first... -->
19 NAME = "<% $field %>_search"
20 ID = "<% $field %>_search"
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);"
28 % if ( $opt{'find_button'} ) {
29 <INPUT TYPE = "button"
31 NAME = "<% $field %>_findbutton"
32 onClick = "smart_<% $field %>_search(this.form.<% $field %>_search);"
36 <SELECT NAME="<% $field %>_select" ID="<% $field %>_select" STYLE="color:#ff0000; display:none" onChange="select_<% $field %>(this);">
39 <% include('/elements/xmlhttp.html',
40 'url' => $p. 'misc/xmlhttp-cust_main-search.cgi',
41 'subs' => [ 'smart_search' ],
45 <SCRIPT TYPE="text/javascript">
47 function clearhint_<% $field %>_search (what) {
49 what.style.color = '#000000';
51 if ( what.value == '(cust #, name or company)' )
54 if ( what.value.indexOf('Customer not found: ') == 0 )
55 what.value = what.value.substr(20);
59 function smart_<% $field %>_search(what) {
61 var customer = what.value;
63 if ( customer == 'searching...' || customer == ''
64 || customer.indexOf('Customer not found: ') == 0 )
67 if ( what.getAttribute('magic') == 'nosearch' ) {
68 what.setAttribute('magic', '');
72 //what.value = 'searching...'
74 what.style.color= '#000000';
75 what.style.backgroundColor = '#dddddd';
77 var customer_select = document.getElementById('<% $field %>_select');
79 //alert("search for customer " + customer);
81 function <% $field %>_search_update(customers) {
83 //alert('customers returned: ' + customers);
85 var customerArray = eval('(' + customers + ')');
87 what.disabled = false;
88 what.style.backgroundColor = '#ffffff';
90 if ( customerArray.length == 0 ) {
92 what.form.<% $field %>.value = '';
94 what.value = 'Customer not found: ' + what.value;
95 what.style.color = '#ff0000';
97 what.style.display = '';
98 customer_select.style.display = 'none';
100 } else if ( customerArray.length == 1 ) {
102 //alert('one customer found: ' + customerArray[0]);
104 what.form.<% $field %>.value = customerArray[0][0];
105 what.value = customerArray[0][1];
107 what.style.display = '';
108 customer_select.style.display = 'none';
112 //alert('multiple customers found, have to create select dropdown');
114 //blank the current list
115 for ( var i = customer_select.length; i >= 0; i-- )
116 customer_select.options[i] = null;
118 opt(customer_select, '', 'Multiple customers match "' + customer + '" - select one', '#ff0000');
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');
124 opt(customer_select, 'cancel', '(Edit search string)', '#000000');
126 what.style.display = 'none';
127 customer_select.style.display = '';
133 smart_search( customer, <% $field %>_search_update );
138 function select_<% $field %> (what) {
140 var custnum = what.options[what.selectedIndex].value;
141 var customer = what.options[what.selectedIndex].text;
143 var customer_obj = document.getElementById('<% $field %>_search');
145 if ( custnum == '' ) {
146 //what.style.color = '#ff0000';
148 } else if ( custnum == 'cancel' ) {
150 customer_obj.style.color = '#000000';
152 what.style.display = 'none';
153 customer_obj.style.display = '';
154 customer_obj.focus();
158 what.form.<% $field %>.value = custnum;
160 customer_obj.value = customer;
161 customer_obj.style.color = '#000000';
163 what.style.display = 'none';
164 customer_obj.style.display = '';
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;
182 my $field = $opt{'field'} || $opt{'field_name'} || 'custnum';
184 my $value = $opt{'curr_value'} || $opt{'value'};
188 $cust_main = qsearchs({
189 'table' => 'cust_main',
190 'hashref' => { 'custnum' => $value },
191 'extra_sql' => " AND ". $FS::CurrentUser::CurrentUser->agentnums_sql,