js/common.js: show_alert_and_focus auf annotate umgeschrieben
[kivitendo-erp.git] / js / common.js
1 function setupPoints(numberformat, wrongFormat) {
2   decpoint = numberformat.substring((numberformat.substring(1, 2).match(/\.|\,|\'/) ? 5 : 4), (numberformat.substring(1, 2).match(/\.|\,|\'/) ? 6 : 5));
3   if (numberformat.substring(1, 2).match(/\.|\,|\'/)) {
4     thpoint = numberformat.substring(1, 2);
5   }
6   else {
7     thpoint = null;
8   }
9   wrongNumberFormat = wrongFormat + " ( " + numberformat + " ) ";
10 }
11
12 function setupDateFormat(setDateFormat, setWrongDateFormat) {
13   dateFormat = setDateFormat;
14   wrongDateFormat = setWrongDateFormat + " ( " + setDateFormat + " ) ";
15   formatArray = new Array();
16   if(dateFormat.match(/^\w\w\W/)) {
17     seperator = dateFormat.substring(2,3);
18   }
19   else {
20     seperator = dateFormat.substring(4,5);
21   }
22 }
23
24 function centerParms(width,height,extra) {
25   xPos = (screen.width - width) / 2;
26   yPos = (screen.height - height) / 2;
27
28   string = "left=" + xPos + ",top=" + yPos;
29
30   if (extra)
31     string += "width=" + width + ",height=" + height;
32
33   return string;
34 }
35
36 function check_right_number_format(input_name) {
37   var test_val = input_name.value;
38   if(thpoint && thpoint == ','){
39     test_val = test_val.replace(/,/g, '');
40   }
41   if(thpoint && thpoint == '.'){
42     test_val = test_val.replace(/\./g, '');
43   }
44   if(thpoint && thpoint == "'"){
45     test_val = test_val.replace(/\'/g, '');
46   }
47   if(decpoint && decpoint == ','){
48     test_val = test_val.replace(/,/g, '.');
49   }
50   var forbidden = test_val.match(/[^\s\d\(\)\-\+\*\/\.]/g);
51   if (forbidden && forbidden.length > 0 ){
52     return annotate(input_name, kivi.t8('wrongformat'), kivi.myconfig.numberformat);
53   }
54
55   try{
56     eval(test_val);
57   }catch(err){
58     return annotate(input_name, kivi.t8('wrongformat'), kivi.myconfig.numberformat);
59   }
60
61   return annotate(input_name);
62 }
63
64 function check_right_date_format(input_name) {
65   if(input_name.value == "") {
66     annotate(input_name);
67     return true;
68   }
69
70   if ( ( input_name.value.match(/^\d+$/ ) ) && !(dateFormat.lastIndexOf("y") == 3) ) {
71     // date shortcuts for entering date without separator for three date styles, e.g.
72     // 31122014 -> 12.04.2014
73     // 12312014 -> 12/31/2014
74     // 31122014 -> 31/12/2014
75
76     if (input_name.value.match(/^\d{8}$/)) {
77       input_name.value = input_name.value.replace(/^(\d\d)(\d\d)(\d\d\d\d)$/, "$1" + seperator + "$2" + seperator + "$3")
78     } else if (input_name.value.match(/^\d{6}$/)) {
79       // 120414 -> 12.04.2014
80       input_name.value = input_name.value.replace(/^(\d\d)(\d\d)(\d\d)$/, "$1" + seperator + "$2" + seperator + "$3")
81     } else if (input_name.value.match(/^\d{4}$/)) {
82       // 1204 -> 12.04.2014
83       var today = new Date();
84       var year = today.getYear();
85       if (year < 999) year += 1900;
86       input_name.value = input_name.value.replace(/^(\d\d)(\d\d)$/, "$1" + seperator + "$2");
87       input_name.value = input_name.value + seperator + year;
88     } else  if ( input_name.value.match(/^\d{1,2}$/ ) ) {
89       // assume the entry is the day of the current month and current year
90       var today = new Date();
91       var day = input_name.value;
92       var month = today.getMonth() + 1;
93       var year = today.getYear();
94       if( day.length == 1 && day < 10) {
95         day='0'+day;
96       };
97       if(month<10) {
98         month='0'+month;
99       };
100       if (year < 999) year += 1900;
101       if ( dateFormat.lastIndexOf("d") == 1) {
102         input_name.value = day + seperator + month + seperator + year;
103       } else {
104         input_name.value = month + seperator + day + seperator + year;
105       }
106     };
107   }
108
109   var matching = new RegExp(dateFormat.replace(/\w/g, '\\d') + "\$","ig");
110   if(!(dateFormat.lastIndexOf("y") == 3) && !matching.test(input_name.value)) {
111     matching = new RegExp(dateFormat.replace(/\w/g, '\\d') + '\\d\\d\$', "ig");
112     if(!matching.test(input_name.value)) {
113       return annotate(input_name, kivi.t8('Falsches Datumsformat!'), kivi.myconfig.dateformat);
114     }
115   }
116   else {
117     if (dateFormat.lastIndexOf("y") == 3 && !matching.test(input_name.value)) {
118       return annotate(input_name, kivi.t8('Falsches Datumsformat!'), kivi.myconfig.dateformat);
119     }
120   }
121   return annotate(input_name);
122 }
123
124 function annotate(input_name, error, expected) {
125   var $e = $(input_name);
126   if (error) {
127     $e.addClass('kivi-validator-invalid');
128     var tooltip = error + ' (' + expected + ')';
129     if ($e.hasClass('tooltipstered'))
130       $e.tooltipster('destroy');
131
132     $e.tooltipster({
133       content: tooltip,
134       theme: 'tooltipster-light',
135     });
136     $e.tooltipster('show');
137   } else {
138     $e.removeClass('kivi-validator-invalid');
139     if ($e.hasClass('tooltipstered'))
140       $e.tooltipster('destroy');
141   }
142 }
143
144 function get_input_value(input_name) {
145   var the_input = document.getElementsByName(input_name);
146   if (the_input && the_input[0])
147     return the_input[0].value;
148   return '';
149 }
150
151 function set_cursor_position(n) {
152   $('[name=' + n + ']').focus();
153 }
154
155 function focussable(e) {
156   return e && e.name && e.type != 'hidden' && e.type != 'submit' && e.disabled != true;
157 }
158
159 function set_cursor_to_first_element(){
160   var df = document.forms;
161   for (var f = 0; f < df.length; f++)
162     for (var i = 0; i < df[f].length; i++)
163       if (focussable(df[f][i]))
164         try { df[f][i].focus(); return } catch (er) { }
165 }
166
167 function getElementByIndirectName(name){
168   var e = document.getElementsByName(name)[0];
169   if (e) return document.getElementsByName(e.value)[0];
170 }
171
172 function focus_by_name(name){
173   var f = getElementByIndirectName(name);
174   if (focussable(f)) {
175     set_cursor_position(f.name);
176     return true;
177   }
178   return false;
179 }
180
181 $(function () {
182   $('input').focus(function(){
183     if (focussable(this)) window.focused_element = this;
184   });
185
186   // setting focus inside a tabbed area fails if this is encountered before the tabbing is complete
187   // in that case the elements count as hidden and jquery aborts .focus()
188   setTimeout(function(){
189     // Lowest priority: first focussable element in form.
190     set_cursor_to_first_element();
191
192     // Medium priority: class set in template
193     var initial_focus = $(".initial_focus").filter(':visible')[0];
194     if (initial_focus)
195       $(initial_focus).focus();
196
197     // special: honour focus_position
198     // if no higher priority applies set focus to the appropriate element
199     if ($("#display_row")[0] && kivi.myconfig.focus_position) {
200       switch(kivi.myconfig.focus_position) {
201         case 'last_partnumber'  : $('#display_row tr.row:gt(-3):lt(-1) input[name*="partnumber"]').focus(); break;
202         case 'last_description' : $('#display_row tr.row:gt(-3):lt(-1) input[name*="description"]').focus(); break;
203         case 'last_qty'         : $('#display_row tr.row:gt(-3):lt(-1) input[name*="qty"]').focus(); break;
204         case 'new_partnumber'   : $('#display_row tr:gt(1) input[name*="partnumber"]').focus(); break;
205         case 'new_description'  : $('#display_row tr:gt(1) input[name*="description"]').focus(); break;
206         case 'new_qty'          : $('#display_row tr:gt(1) input[name*="qty"]').focus(); break;
207       }
208     }
209
210     // all of this screws with the native location.hash focus, so reimplement this as well
211     if (location.hash) {
212       var hash_name = location.hash.substr(1);
213       var $hash_by_id = $(location.hash + ':visible');
214       if ($hash_by_id.length > 0) {
215         $hash_by_id.get(0).focus();
216       } else {
217         var $by_name = $('[name=' + hash_name + ']:visible');
218         if ($by_name.length > 0) {
219           $by_name.get(0).focus();
220         }
221       }
222     }
223
224     // legacy. some forms install these
225     if (typeof fokus == 'function') { fokus(); return; }
226     if (focus_by_name('cursor_fokus')) return;
227   }, 0);
228 });
229
230 $('form').submit(function(){
231   if (window.focused_element)
232     document.forms[0].cursor_fokus.value = window.focused_element.name;
233 });