916011503adb49b45b25f0bc7d783904dc3cb2f2
[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   var matching = new RegExp(dateFormat.replace(/\w/g, '\\d') + "\$","ig");
68   if(!(dateFormat.lastIndexOf("y") == 3) && !matching.test(input_name.value)) {
69     matching = new RegExp(dateFormat.replace(/\w/g, '\\d') + '\\d\\d\$', "ig");
70     if(!matching.test(input_name.value)) {
71       return show_alert_and_focus(input_name, wrongDateFormat);
72     }
73   }
74   else {
75     if (dateFormat.lastIndexOf("y") == 3 && !matching.test(input_name.value)) {
76       return show_alert_and_focus(input_name, wrongDateFormat);
77     }
78   }
79 }
80
81 function validate_dates(input_name_1, input_name_2) {
82   var tempArray1 = new Array();
83   var tempArray2 = new Array();
84   tempArray1 = getDateArray(input_name_1);
85   tempArray2 = getDateArray(input_name_2);
86   if(check_right_date_format(input_name_1) && check_right_date_format(input_name_2)) {
87     if(!((new Date(tempArray2[0], tempArray2[1], tempArray2[2])).getTime() >= (new Date(tempArray1[0], tempArray1[1], tempArray1[2])).getTime())) {
88       show_alert_and_focus(input_name_1, wrongDateFormat);
89       return show_alert_and_focus(input_name_2, wrongDateFormat);
90     }
91     if(!((new Date(tempArray2[0], tempArray2[1], tempArray2[2])).getTime() >= (new Date(1900, 1, 1)).getTime())) {
92       show_alert_and_focus(input_name_1, wrongDateFormat);
93       return show_alert_and_focus(input_name_2, wrongDateFormat);
94     }
95   }
96 }
97
98 function getDateArray(input_name) {
99   formatArray[2] = input_name.value.substring(dateFormat.indexOf("d"), 2);
100   formatArray[1] = input_name.value.substring(dateFormat.indexOf("m"), 2);
101   formatArray[0] = input_name.value.substring(dateFormat.indexOf("y"), (dateFormat.length == 10 ? 4 : 2));
102   if(dateFormat.length == 8) {
103     formatArray[0] += (formatArray[0] < 70 ? 2000 : 1900);
104   }
105   return formatArray;
106 }
107
108 function show_alert_and_focus(input_name, errorMessage) {
109   input_name.select();
110   alert(errorMessage + "\n\r\n\r--> " + input_name.value);
111   input_name.focus();
112   return false;
113 }
114
115 function get_input_value(input_name) {
116   var the_input = document.getElementsByName(input_name);
117   if (the_input && the_input[0])
118     return the_input[0].value;
119   return '';
120 }
121
122 function set_cursor_position(n) {
123   $('[name=' + n + ']').focus();
124 }
125
126 function focussable(e) {
127   return e && e.name && e.type != 'hidden' && e.type != 'submit' && e.disabled != true;
128 }
129
130 function set_cursor_to_first_element(){
131   var df = document.forms;
132   for (var f = 0; f < df.length; f++)
133     for (var i = 0; i < df[f].length; i++)
134       if (focussable(df[f][i]))
135         try { df[f][i].focus(); return } catch (er) { }
136 }
137
138 function getElementByIndirectName(name){
139   var e = document.getElementsByName(name)[0];
140   if (e) return document.getElementsByName(e.value)[0];
141 }
142
143 function focus_by_name(name){
144   var f = getElementByIndirectName(name);
145   if (focussable(f)) {
146     set_cursor_position(f.name);
147     return true;
148   }
149   return false;
150 }
151
152 $(document).ready(function () {
153   $('input').focus(function(){
154     if (focussable(this)) window.focused_element = this;
155   });
156
157   var initial_focus = $(".initial_focus").filter(':visible')[0];
158   if (initial_focus)
159     $(initial_focus).focus();
160
161   // legacy. sone forms install these
162   if (typeof fokus == 'function') { fokus(); return; }
163   if (focus_by_name('cursor_fokus')) return;
164   set_cursor_to_first_element();
165 });
166
167 $('form').submit(function(){
168   if (window.focused_element)
169     document.forms[0].cursor_fokus.value = window.focused_element.name;
170 });