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