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