Aufträge/Rechnungen dürfen Preise mit mehr als 2 Nachkommastellen haben.
[kivitendo-erp.git] / js / common.js
1 if (!window.a_onload_functions)   a_onload_functions   = new Object();
2 if (!window.a_onsubmit_functions) a_onsubmit_functions = new Object();
3 if (!window.a_onfocus_functions)  a_onfocus_functions  = new Object();
4 window.focused_element = null;
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 set_longdescription_window(input_name) {
42   var parm = centerParms(600,500) + ",width=600,height=500,status=yes,scrollbars=yes";
43   var name = document.getElementsByName(input_name)[0].value;
44   url = "common.pl?" +
45     "INPUT_ENCODING=UTF-8&" +
46     "action=set_longdescription&" +
47     "longdescription=" + encodeURIComponent(document.getElementsByName(input_name)[0].value) + "&" +
48     "input_name=" + encodeURIComponent(input_name) + "&"
49   window.open(url, "_new_generic", parm);
50   }
51
52 function check_right_number_format(input_name) {
53   if(decpoint == thpoint) {
54     return show_alert_and_focus(input_name, wrongNumberFormat);
55   }
56   if(decpoint == ',') {
57     var decnumbers = input_name.value.split(',');
58   }
59   else {
60     var decnumbers = input_name.value.split('.');
61   }
62   if(decnumbers.length == 2) {
63     if(decnumbers[1].length > 2)  {
64      /* return show_alert_and_focus(input_name, wrongNumberFormat); */
65     }
66   }
67   else {
68     if(decnumbers.length > 2) {
69       return show_alert_and_focus(input_name, wrongNumberFormat);
70     }
71     if(!thpoint) {
72       if(decnumbers[0].match(/\D/)) {
73         return show_alert_and_focus(input_name, wrongNumberFormat);
74       }
75     }
76   }
77 }
78
79 function check_right_date_format(input_name) {
80   if(input_name.value == "") {
81     return true;
82   }
83   var matching = new RegExp(dateFormat.replace(/\w/g, '\\d') + "\$","ig");
84   if(!(dateFormat.lastIndexOf("y") == 3) && !matching.test(input_name.value)) {
85     matching = new RegExp(dateFormat.replace(/\w/g, '\\d') + '\\d\\d\$', "ig");
86     if(!matching.test(input_name.value)) {
87       return show_alert_and_focus(input_name, wrongDateFormat);
88     }
89   }
90   else {
91     if (dateFormat.lastIndexOf("y") == 3 && !matching.test(input_name.value)) {
92       return show_alert_and_focus(input_name, wrongDateFormat);
93     }
94   }
95 }
96
97 function validate_dates(input_name_1, input_name_2) {
98   var tempArray1 = new Array();
99   var tempArray2 = new Array();
100   tempArray1 = getDateArray(input_name_1);
101   tempArray2 = getDateArray(input_name_2);
102   if(check_right_date_format(input_name_1) && check_right_date_format(input_name_2)) {
103     if(!((new Date(tempArray2[0], tempArray2[1], tempArray2[2])).getTime() >= (new Date(tempArray1[0], tempArray1[1], tempArray1[2])).getTime())) {
104       show_alert_and_focus(input_name_1, wrongDateFormat);
105       return show_alert_and_focus(input_name_2, wrongDateFormat);
106     }
107     if(!((new Date(tempArray2[0], tempArray2[1], tempArray2[2])).getTime() >= (new Date(1900, 1, 1)).getTime())) {
108       show_alert_and_focus(input_name_1, wrongDateFormat);
109       return show_alert_and_focus(input_name_2, wrongDateFormat);
110     }
111   }
112 }
113
114 function getDateArray(input_name) {
115   formatArray[2] = input_name.value.substring(dateFormat.indexOf("d"), 2);
116   formatArray[1] = input_name.value.substring(dateFormat.indexOf("m"), 2);
117   formatArray[0] = input_name.value.substring(dateFormat.indexOf("y"), (dateFormat.length == 10 ? 4 : 2));
118   if(dateFormat.length == 8) {
119     formatArray[0] += (formatArray[0] < 70 ? 2000 : 1900);
120   }
121   return formatArray;
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 a_onfocus_functions["focus_listener"] = (function (event) {
139   if (focussable(event.target)) window.focused_element = event.target;
140 });
141
142 a_onsubmit_functions["get_cursor_position"] = (function () {
143   if (window.focused_element)
144     document.forms[0].cursor_fokus.value = window.focused_element.name;
145 });
146
147 function set_cursor_position(n) {
148   document.getElementsByName(n)[0].focus();
149 }
150
151 a_onload_functions["restore_cursor_position"] = (function () {
152   var e = document.getElementsByName('cursor_fokus')[0];
153   if (e) var f = document.getElementsByName(e.value)[0];
154   if (focussable(f)) set_cursor_position(f.name)
155     else set_cursor_to_first_element();
156 });
157
158 function focussable(e) {
159   return e && e.name && e.type != 'hidden' && e.type != 'submit' && e.disabled != true;
160 }
161
162 function set_cursor_to_first_element(){
163   var df = document.forms;
164   for (var f = 0; f < df.length; f++)
165     for (var i = 0; i < df[f].length; i++)
166       if (focussable(df[f][i]))
167         try { df[f][i].focus(); return } catch (er) { }
168 }
169
170 function add_event(e, type, fn, c) {
171   var ret = 0;
172   if (e.addEventListener) e.addEventListener(type, fn, c)
173   else if (e.attachEvent) e.attachEvent('on' + type, fn)
174   else e['on' + type] = fn;
175 }
176
177 function do_load_events() {
178   for (var name in window.a_onload_functions)   add_event(window, "load",   window.a_onload_functions[name],   false);
179   for (var name in window.a_onsubmit_functions) add_event(window, "submit", window.a_onsubmit_functions[name], false);
180   for (var name in window.a_onfocus_functions)  add_event(window, "focus",  window.a_onfocus_functions[name],  false);
181 }
182 do_load_events();