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