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