added missing numberformat in java-script
[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 validate_dates(input_name_1, input_name_2) {
125   var tempArray1 = new Array();
126   var tempArray2 = new Array();
127   tempArray1 = getDateArray(input_name_1);
128   tempArray2 = getDateArray(input_name_2);
129   if(check_right_date_format(input_name_1) && check_right_date_format(input_name_2)) {
130     if(!((new Date(tempArray2[0], tempArray2[1], tempArray2[2])).getTime() >= (new Date(tempArray1[0], tempArray1[1], tempArray1[2])).getTime())) {
131       show_alert_and_focus(input_name_1, wrongDateFormat);
132       return show_alert_and_focus(input_name_2, wrongDateFormat);
133     }
134     if(!((new Date(tempArray2[0], tempArray2[1], tempArray2[2])).getTime() >= (new Date(1900, 1, 1)).getTime())) {
135       show_alert_and_focus(input_name_1, wrongDateFormat);
136       return show_alert_and_focus(input_name_2, wrongDateFormat);
137     }
138   }
139 }
140
141 function getDateArray(input_name) {
142   formatArray[2] = input_name.value.substring(dateFormat.indexOf("d"), 2);
143   formatArray[1] = input_name.value.substring(dateFormat.indexOf("m"), 2);
144   formatArray[0] = input_name.value.substring(dateFormat.indexOf("y"), (dateFormat.length == 10 ? 4 : 2));
145   if(dateFormat.length == 8) {
146     formatArray[0] += (formatArray[0] < 70 ? 2000 : 1900);
147   }
148   return formatArray;
149 }
150
151 function show_alert_and_focus(input_name, errorMessage) {
152   input_name.select();
153   alert(errorMessage + "\n\r\n\r--> " + input_name.value);
154   input_name.focus();
155   return false;
156 }
157
158 function get_input_value(input_name) {
159   var the_input = document.getElementsByName(input_name);
160   if (the_input && the_input[0])
161     return the_input[0].value;
162   return '';
163 }
164
165 function set_cursor_position(n) {
166   $('[name=' + n + ']').focus();
167 }
168
169 function focussable(e) {
170   return e && e.name && e.type != 'hidden' && e.type != 'submit' && e.disabled != true;
171 }
172
173 function set_cursor_to_first_element(){
174   var df = document.forms;
175   for (var f = 0; f < df.length; f++)
176     for (var i = 0; i < df[f].length; i++)
177       if (focussable(df[f][i]))
178         try { df[f][i].focus(); return } catch (er) { }
179 }
180
181 function getElementByIndirectName(name){
182   var e = document.getElementsByName(name)[0];
183   if (e) return document.getElementsByName(e.value)[0];
184 }
185
186 function focus_by_name(name){
187   var f = getElementByIndirectName(name);
188   if (focussable(f)) {
189     set_cursor_position(f.name);
190     return true;
191   }
192   return false;
193 }
194
195 $(document).ready(function () {
196   $('input').focus(function(){
197     if (focussable(this)) window.focused_element = this;
198   });
199
200   // Lowest priority: first focussable element in form.
201   set_cursor_to_first_element();
202
203   // Medium priority: class set in template
204   var initial_focus = $(".initial_focus").filter(':visible')[0];
205   if (initial_focus)
206     $(initial_focus).focus();
207
208   // legacy. sone forms install these
209   if (typeof fokus == 'function') { fokus(); return; }
210   if (focus_by_name('cursor_fokus')) return;
211 });
212
213 $('form').submit(function(){
214   if (window.focused_element)
215     document.forms[0].cursor_fokus.value = window.focused_element.name;
216 });