8ab2a5bc8f9c00c4ac95ac3b3ceca620db812951
[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(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 show_alert_and_focus(input_name, wrongNumberFormat);
53   }
54
55   try{
56     eval(test_val);
57   }catch(err){
58     return show_alert_and_focus(input_name, wrongNumberFormat);
59   }
60
61 }
62
63 function check_right_date_format(input_name) {
64   if(input_name.value == "") {
65     return true;
66   }
67
68   if ( ( input_name.value.match(/^\d+$/ ) ) && !(dateFormat.lastIndexOf("y") == 3) ) {
69     // date shortcuts for entering date without separator for three date styles, e.g.
70     // 31122014 -> 12.04.2014
71     // 12312014 -> 12/31/2014
72     // 31122014 -> 31/12/2014
73     
74     if (input_name.value.match(/^\d{8}$/)) {
75       input_name.value = input_name.value.replace(/^(\d\d)(\d\d)(\d\d\d\d)$/, "$1" + seperator + "$2" + seperator + "$3")
76     } else if (input_name.value.match(/^\d{6}$/)) {
77       // 120414 -> 12.04.2014
78       input_name.value = input_name.value.replace(/^(\d\d)(\d\d)(\d\d)$/, "$1" + seperator + "$2" + seperator + "$3")
79     } else if (input_name.value.match(/^\d{4}$/)) {
80       // 1204 -> 12.04.2014
81       var today = new Date();
82       var year = today.getYear();
83       if (year < 999) year += 1900;
84       input_name.value = input_name.value.replace(/^(\d\d)(\d\d)$/, "$1" + seperator + "$2");
85       input_name.value = input_name.value + seperator + year;
86     } else  if ( input_name.value.match(/^\d{1,2}$/ ) ) {
87       // assume the entry is the day of the current month and current year
88       var today = new Date();
89       var day = input_name.value;
90       var month = today.getMonth() + 1;
91       var year = today.getYear();
92       if( day.length == 1 && day < 10) {
93         day='0'+day; 
94       };
95       if(month<10) {
96         month='0'+month;
97       };
98       if (year < 999) year += 1900;
99       if ( dateFormat.lastIndexOf("d") == 1) {
100         input_name.value = day + seperator + month + seperator + year;
101       } else {
102         input_name.value = month + seperator + day + seperator + year;
103       } 
104     };
105   }
106
107   var matching = new RegExp(dateFormat.replace(/\w/g, '\\d') + "\$","ig");
108   if(!(dateFormat.lastIndexOf("y") == 3) && !matching.test(input_name.value)) {
109     matching = new RegExp(dateFormat.replace(/\w/g, '\\d') + '\\d\\d\$', "ig");
110     if(!matching.test(input_name.value)) {
111       return show_alert_and_focus(input_name, wrongDateFormat);
112     }
113   }
114   else {
115     if (dateFormat.lastIndexOf("y") == 3 && !matching.test(input_name.value)) {
116       return show_alert_and_focus(input_name, wrongDateFormat);
117     }
118   }
119 }
120
121 function validate_dates(input_name_1, input_name_2) {
122   var tempArray1 = new Array();
123   var tempArray2 = new Array();
124   tempArray1 = getDateArray(input_name_1);
125   tempArray2 = getDateArray(input_name_2);
126   if(check_right_date_format(input_name_1) && check_right_date_format(input_name_2)) {
127     if(!((new Date(tempArray2[0], tempArray2[1], tempArray2[2])).getTime() >= (new Date(tempArray1[0], tempArray1[1], tempArray1[2])).getTime())) {
128       show_alert_and_focus(input_name_1, wrongDateFormat);
129       return show_alert_and_focus(input_name_2, wrongDateFormat);
130     }
131     if(!((new Date(tempArray2[0], tempArray2[1], tempArray2[2])).getTime() >= (new Date(1900, 1, 1)).getTime())) {
132       show_alert_and_focus(input_name_1, wrongDateFormat);
133       return show_alert_and_focus(input_name_2, wrongDateFormat);
134     }
135   }
136 }
137
138 function getDateArray(input_name) {
139   formatArray[2] = input_name.value.substring(dateFormat.indexOf("d"), 2);
140   formatArray[1] = input_name.value.substring(dateFormat.indexOf("m"), 2);
141   formatArray[0] = input_name.value.substring(dateFormat.indexOf("y"), (dateFormat.length == 10 ? 4 : 2));
142   if(dateFormat.length == 8) {
143     formatArray[0] += (formatArray[0] < 70 ? 2000 : 1900);
144   }
145   return formatArray;
146 }
147
148 function show_alert_and_focus(input_name, errorMessage) {
149   input_name.select();
150   alert(errorMessage + "\n\r\n\r--> " + input_name.value);
151   input_name.focus();
152   return false;
153 }
154
155 function get_input_value(input_name) {
156   var the_input = document.getElementsByName(input_name);
157   if (the_input && the_input[0])
158     return the_input[0].value;
159   return '';
160 }
161
162 function set_cursor_position(n) {
163   $('[name=' + n + ']').focus();
164 }
165
166 function focussable(e) {
167   return e && e.name && e.type != 'hidden' && e.type != 'submit' && e.disabled != true;
168 }
169
170 function set_cursor_to_first_element(){
171   var df = document.forms;
172   for (var f = 0; f < df.length; f++)
173     for (var i = 0; i < df[f].length; i++)
174       if (focussable(df[f][i]))
175         try { df[f][i].focus(); return } catch (er) { }
176 }
177
178 function getElementByIndirectName(name){
179   var e = document.getElementsByName(name)[0];
180   if (e) return document.getElementsByName(e.value)[0];
181 }
182
183 function focus_by_name(name){
184   var f = getElementByIndirectName(name);
185   if (focussable(f)) {
186     set_cursor_position(f.name);
187     return true;
188   }
189   return false;
190 }
191
192 $(document).ready(function () {
193   $('input').focus(function(){
194     if (focussable(this)) window.focused_element = this;
195   });
196
197   // Lowest priority: first focussable element in form.
198   set_cursor_to_first_element();
199
200   // Medium priority: class set in template
201   var initial_focus = $(".initial_focus").filter(':visible')[0];
202   if (initial_focus)
203     $(initial_focus).focus();
204
205   // legacy. sone forms install these
206   if (typeof fokus == 'function') { fokus(); return; }
207   if (focus_by_name('cursor_fokus')) return;
208 });
209
210 $('form').submit(function(){
211   if (window.focused_element)
212     document.forms[0].cursor_fokus.value = window.focused_element.name;
213 });