5 include( '/elements/search-cust_main.html,
6 'field_name' => 'custnum',
7 'find_button' => 1, #add a "find" button to the field
8 'curr_value' => 54, #current value
9 'value => 32, #deprecated synonym for curr_value
13 <INPUT TYPE="hidden" NAME="<% $opt{'field_name'} %>" VALUE="<% $value %>">
15 <!-- some false laziness w/ misc/batch-cust_pay.html, though not as bad as i'd thought at first... -->
18 NAME = "<% $opt{'field_name'} %>_search"
19 ID = "<% $opt{'field_name'} %>_search"
21 VALUE="<% $cust_main ? $cust_main->name : '(cust #, name or company)' %>"
22 onFocus="clearhint_<% $opt{'field_name'} %>_search(this);"
23 onClick="clearhint_<% $opt{'field_name'} %>_search(this);"
24 onChange="smart_<% $opt{'field_name'} %>_search(this);"
27 % if ( $opt{'find_button'} ) {
28 <INPUT TYPE = "button"
30 NAME = "<% $opt{'field_name'} %>_findbutton"
31 onClick = "smart_<% $opt{'field_name'} %>_search(this.form.<% $opt{'field_name'} %>_search);"
35 <SELECT NAME="<% $opt{'field_name'} %>_select" ID="<% $opt{'field_name'} %>_select" STYLE="color:#ff0000; display:none" onChange="select_<% $opt{'field_name'} %>(this);">
38 <% include('/elements/xmlhttp.html',
39 'url' => $p. 'misc/xmlhttp-cust_main-search.cgi',
40 'subs' => [ 'smart_search' ],
44 <SCRIPT TYPE="text/javascript">
46 function clearhint_<% $opt{'field_name'} %>_search (what) {
48 what.style.color = '#000000';
50 if ( what.value == '(cust #, name or company)' )
53 if ( what.value.indexOf('Customer not found: ') == 0 )
54 what.value = what.value.substr(20);
58 function smart_<% $opt{'field_name'} %>_search(what) {
60 var customer = what.value;
62 if ( customer == 'searching...' || customer == ''
63 || customer.indexOf('Customer not found: ') == 0 )
66 if ( what.getAttribute('magic') == 'nosearch' ) {
67 what.setAttribute('magic', '');
71 //what.value = 'searching...'
73 what.style.color= '#000000';
74 what.style.backgroundColor = '#dddddd';
76 var customer_select = document.getElementById('<% $opt{'field_name'} %>_select');
78 //alert("search for customer " + customer);
80 function <% $opt{'field_name'} %>_search_update(customers) {
82 //alert('customers returned: ' + customers);
84 var customerArray = eval('(' + customers + ')');
86 what.disabled = false;
87 what.style.backgroundColor = '#ffffff';
89 if ( customerArray.length == 0 ) {
91 what.form.<% $opt{'field_name'} %>.value = '';
93 what.value = 'Customer not found: ' + what.value;
94 what.style.color = '#ff0000';
96 what.style.display = '';
97 customer_select.style.display = 'none';
99 } else if ( customerArray.length == 1 ) {
101 //alert('one customer found: ' + customerArray[0]);
103 what.form.<% $opt{'field_name'} %>.value = customerArray[0][0];
104 what.value = customerArray[0][1];
106 what.style.display = '';
107 customer_select.style.display = 'none';
111 //alert('multiple customers found, have to create select dropdown');
113 //blank the current list
114 for ( var i = customer_select.length; i >= 0; i-- )
115 customer_select.options[i] = null;
117 opt(customer_select, '', 'Multiple customers match "' + customer + '" - select one', '#ff0000');
119 //add the multiple customers
120 for ( var s = 0; s < customerArray.length; s++ )
121 opt(customer_select, customerArray[s][0], customerArray[s][1], '#000000');
123 opt(customer_select, 'cancel', '(Edit search string)', '#000000');
125 what.style.display = 'none';
126 customer_select.style.display = '';
132 smart_search( customer, <% $opt{'field_name'} %>_search_update );
137 function select_<% $opt{'field_name'} %> (what) {
139 var custnum = what.options[what.selectedIndex].value;
140 var customer = what.options[what.selectedIndex].text;
142 var customer_obj = document.getElementById('<% $opt{'field_name'} %>_search');
144 if ( custnum == '' ) {
145 //what.style.color = '#ff0000';
147 } else if ( custnum == 'cancel' ) {
149 customer_obj.style.color = '#000000';
151 what.style.display = 'none';
152 customer_obj.style.display = '';
153 customer_obj.focus();
157 what.form.<% $opt{'field_name'} %>.value = custnum;
159 customer_obj.value = customer;
160 customer_obj.style.color = '#000000';
162 what.style.display = 'none';
163 customer_obj.style.display = '';
169 function opt(what,value,text,color) {
170 var optionName = new Option(text, value, false, false);
171 optionName.style.color = color;
172 var length = what.length;
173 what.options[length] = optionName;
180 $opt{'field_name'} ||= 'custnum';
182 my $value = $opt{'curr_value'} || $opt{'value'};
186 $cust_main = qsearchs({
187 'table' => 'cust_main',
188 'hashref' => { 'custnum' => $value },
189 'extra_sql' => " AND ". $FS::CurrentUser::CurrentUser->agentnums_sql,