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