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