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 %>" ID="<% $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 var <% $field %>_search_active = false;
61 function smart_<% $field %>_search(what) {
63 if ( <% $field %>_search_active )
66 var customer = what.value;
68 if ( customer == 'searching...' || customer == ''
69 || customer.indexOf('Customer not found: ') == 0 )
72 if ( what.getAttribute('magic') == 'nosearch' ) {
73 what.setAttribute('magic', '');
77 //what.value = 'searching...'
79 what.style.color= '#000000';
80 what.style.backgroundColor = '#dddddd';
82 var customer_select = document.getElementById('<% $field %>_select');
84 //alert("search for customer " + customer);
86 function <% $field %>_search_update(customers) {
88 //alert('customers returned: ' + customers);
90 var customerArray = eval('(' + customers + ')');
92 what.disabled = false;
93 what.style.backgroundColor = '#ffffff';
95 if ( customerArray.length == 0 ) {
97 what.form.<% $field %>.value = '';
99 what.value = 'Customer not found: ' + what.value;
100 what.style.color = '#ff0000';
102 what.style.display = '';
103 customer_select.style.display = 'none';
105 } else if ( customerArray.length == 1 ) {
107 //alert('one customer found: ' + customerArray[0]);
109 what.form.<% $field %>.value = customerArray[0][0];
110 what.value = customerArray[0][1];
112 what.style.display = '';
113 customer_select.style.display = 'none';
117 //alert('multiple customers found, have to create select dropdown');
119 //blank the current list
120 for ( var i = customer_select.length; i >= 0; i-- )
121 customer_select.options[i] = null;
123 opt(customer_select, '', 'Multiple customers match "' + customer + '" - select one', '#ff0000');
125 //add the multiple customers
126 for ( var s = 0; s < customerArray.length; s++ )
127 opt(customer_select, customerArray[s][0], customerArray[s][1], '#000000');
129 opt(customer_select, 'cancel', '(Edit search string)', '#000000');
131 what.style.display = 'none';
132 customer_select.style.display = '';
136 <% $field %>_search_active = false;
140 <% $field %>_search_active = true;
142 smart_search( customer, <% $field %>_search_update );
147 function select_<% $field %> (what) {
149 var custnum = what.options[what.selectedIndex].value;
150 var customer = what.options[what.selectedIndex].text;
152 var customer_obj = document.getElementById('<% $field %>_search');
154 if ( custnum == '' ) {
155 //what.style.color = '#ff0000';
157 } else if ( custnum == 'cancel' ) {
159 customer_obj.style.color = '#000000';
161 what.style.display = 'none';
162 customer_obj.style.display = '';
163 customer_obj.focus();
167 what.form.<% $field %>.value = custnum;
169 customer_obj.value = customer;
170 customer_obj.style.color = '#000000';
172 what.style.display = 'none';
173 customer_obj.style.display = '';
179 function opt(what,value,text,color) {
180 var optionName = new Option(text, value, false, false);
181 optionName.style.color = color;
182 var length = what.length;
183 what.options[length] = optionName;
191 my $field = $opt{'field'} || $opt{'field_name'} || 'custnum';
193 my $value = $opt{'curr_value'} || $opt{'value'};
197 $cust_main = qsearchs({
198 'table' => 'cust_main',
199 'hashref' => { 'custnum' => $value },
200 'extra_sql' => " AND ". $FS::CurrentUser::CurrentUser->agentnums_sql,