Fokus Patch.
[kivitendo-erp.git] / js / common.js
1
2 if (!window.a_onload_functions)   var a_onload_functions   = new Object();
3 if (!window.a_onsubmit_functions) var a_onsubmit_functions = new Object();
4
5
6 function setupPoints(numberformat, wrongFormat) {
7   decpoint = numberformat.substring((numberformat.substring(1, 2).match(/\.|\,/) ? 5 : 4), (numberformat.substring(1, 2).match(/\.|\,/) ? 6 : 5));
8   if (numberformat.substring(1, 2).match(/\.|\,/)) {
9     thpoint = numberformat.substring(1, 2); 
10   }
11   else {
12     thpoint = null;
13   }
14   wrongNumberFormat = wrongFormat + " ( " + numberformat + " ) ";  
15 }
16
17 function setupDateFormat(setDateFormat, setWrongDateFormat) {
18   dateFormat = setDateFormat;
19   wrongDateFormat = setWrongDateFormat + " ( " + setDateFormat + " ) ";
20   formatArray = new Array();
21   if(dateFormat.match(/^\w\w\W/)) {
22     seperator = dateFormat.substring(2,3);
23   }
24   else {
25     seperator = dateFormat.substring(4,5);
26   }
27 }
28
29 function centerParms(width,height,extra) {
30   xPos = (screen.width - width) / 2;
31   yPos = (screen.height - height) / 2;
32
33   string = "left=" + xPos + ",top=" + yPos;
34
35   if (extra)
36     string += "width=" + width + ",height=" + height;
37
38   return string;
39 }
40
41 function escape_more(s) {
42   s = escape(s);
43   return s.replace(/\+/g, '%2b');
44 }
45
46 function set_longdescription_window(input_name) {
47   var parm = centerParms(600,500) + ",width=600,height=500,status=yes,scrollbars=yes";
48   var name = document.getElementsByName(input_name)[0].value;
49   url = "common.pl?" +
50     "action=set_longdescription&" +
51     "longdescription=" + escape_more(document.getElementsByName(input_name)[0].value) + "&" +
52     "input_name=" + escape_more(input_name) + "&"
53   window.open(url, "_new_generic", parm);
54   }
55
56 function check_right_number_format(input_name) {
57   if(decpoint == thpoint) {
58     return show_alert_and_focus(input_name, wrongNumberFormat);
59   }
60   if(decpoint == ',') {
61     var decnumbers = input_name.value.split(',');
62   }
63   else {
64     var decnumbers = input_name.value.split('.');
65   }
66   if(decnumbers.length == 2) {
67     if(decnumbers[1].length > 2)  {
68       return show_alert_and_focus(input_name, wrongNumberFormat);
69     }
70   }
71   else {
72     if(decnumbers.length > 2) {
73       return show_alert_and_focus(input_name, wrongNumberFormat);
74     }
75     if(!thpoint) {
76       if(decnumbers[0].match(/\D/)) {
77         return show_alert_and_focus(input_name, wrongNumberFormat);
78       }
79     }
80   }
81 }
82
83 function check_right_date_format(input_name) {
84   if(input_name.value == "") {
85     return true;
86   }
87   var matching = new RegExp(dateFormat.replace(/\w/g, '\\d') + "\$","ig");
88   if(!(dateFormat.lastIndexOf("y") == 3) && !matching.test(input_name.value)) {
89     matching = new RegExp(dateFormat.replace(/\w/g, '\\d') + '\\d\\d\$', "ig");
90     if(!matching.test(input_name.value)) {
91       return show_alert_and_focus(input_name, wrongDateFormat);
92     }
93   }
94   else {
95     if (dateFormat.lastIndexOf("y") == 3 && !matching.test(input_name.value)) { 
96       return show_alert_and_focus(input_name, wrongDateFormat);
97     }
98   }
99 }
100
101 function validate_dates(input_name_1, input_name_2) {
102   var tempArray1 = new Array();
103   var tempArray2 = new Array();
104   tempArray1 = getDateArray(input_name_1);
105   tempArray2 = getDateArray(input_name_2);
106   if(check_right_date_format(input_name_1) && check_right_date_format(input_name_2)) {
107     if(!((new Date(tempArray2[0], tempArray2[1], tempArray2[2])).getTime() >= (new Date(tempArray1[0], tempArray1[1], tempArray1[2])).getTime())) {
108       show_alert_and_focus(input_name_1, wrongDateFormat);
109       return show_alert_and_focus(input_name_2, wrongDateFormat);
110     }
111     if(!((new Date(tempArray2[0], tempArray2[1], tempArray2[2])).getTime() >= (new Date(1900, 1, 1)).getTime())) {
112       show_alert_and_focus(input_name_1, wrongDateFormat);
113       return show_alert_and_focus(input_name_2, wrongDateFormat);
114     }
115   }
116 }
117
118 function getDateArray(input_name) {
119   formatArray[2] = input_name.value.substring(dateFormat.indexOf("d"), 2);
120   formatArray[1] = input_name.value.substring(dateFormat.indexOf("m"), 2);
121   formatArray[0] = input_name.value.substring(dateFormat.indexOf("y"), (dateFormat.length == 10 ? 4 : 2));
122   if(dateFormat.length == 8) {
123     formatArray[0] += (formatArray[0] < 70 ? 2000 : 1900);
124   }
125   return formatArray;
126 }
127
128 function show_alert_and_focus(input_name, errorMessage) {
129   input_name.select();
130   alert(errorMessage + "\n\r\n\r--> " + input_name.value);
131   input_name.focus();
132   return false;
133 }
134   
135 function get_input_value(input_name) {
136   var the_input = document.getElementsByName(input_name);
137   if (the_input && the_input[0])
138     return the_input[0].value;
139   return '';
140 }
141
142 window.focused_element = null;
143 document.addEventListener("focus", function(event){ 
144   var e = event.target;
145   if (is_element_focussable(e)) window.focused_element = e;
146 }, true);
147
148 function get_cursor_position() {
149   if (window.focused_element)
150   document.forms[0].cursor_fokus.value = window.focused_element.name;
151 }
152
153 function set_cursor_position(n) {
154   document.getElementsByName(n)[0].focus();
155 }
156
157 function restore_cursor_position() {
158   var e = document.getElementsByName('cursor_fokus')[0];
159   var f = document.getElementsByName(e.value)[0];
160   if (is_element_focussable(f)) set_cursor_position(f.name)
161    else set_cursor_to_first_element();
162 }
163
164 function is_element_focussable(e) {
165   return e && e.type != 'hidden' && e.type != 'submit' && e.disabled != true;
166 }
167
168 function set_cursor_to_first_element(){
169   var df = document.forms;
170   for (var f = 0; f < df.length; f++)
171     for (var i = 0; i < df[f].length; i++)
172       if (is_element_focussable(df[f][i]))
173         try { df[f][i].focus(); return } catch (er) { }
174 }
175 a_onload_functions["restore_cursor_position"] = restore_cursor_position;
176 a_onsubmit_functions["get_cursor_position"]   = get_cursor_position;
177
178 function do_load_events() {
179   var oldl = window.onload;
180   window.onload = function() {
181     if (oldl) oldl();
182     if (window.a_onload_functions) 
183       for (var name in window.a_onload_functions) 
184         a_onload_functions[name]();
185   }
186   window.onsubmit = function() {
187     if (window.a_onsubmit_functions) 
188       for (var name in window.a_onsubmit_functions) 
189         a_onsubmit_functions[name]();
190   }
191 }
192 do_load_events();