Merge branch 'master' of github.com:kivitendo/kivitendo-erp
[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 set_longdescription_window(input_name) {
37   var parm = centerParms(600,500) + ",width=600,height=500,status=yes,scrollbars=yes";
38   var name = document.getElementsByName(input_name)[0].value;
39   url = "common.pl?" +
40     "INPUT_ENCODING=UTF-8&" +
41     "action=set_longdescription&" +
42     "longdescription=" + encodeURIComponent(document.getElementsByName(input_name)[0].value) + "&" +
43     "input_name=" + encodeURIComponent(input_name) + "&"
44   window.open(url, "_new_generic", parm);
45   }
46
47 function check_right_number_format(input_name) {
48   if(decpoint && thpoint && thpoint == decpoint) {
49     return show_alert_and_focus(input_name, wrongNumberFormat);
50   }
51   var test_val = input_name.value;
52   if(thpoint && thpoint == ','){
53     test_val = test_val.replace(/,/g, '');
54   }
55   if(thpoint && thpoint == '.'){
56     test_val = test_val.replace(/\./g, '');
57   }
58   if(decpoint && decpoint == ','){
59     test_val = test_val.replace(/,/g, '.');
60   }
61   var forbidden = test_val.match(/[^\s\d\(\)\-\+\*\/\.]/g);
62   if (forbidden && forbidden.length > 0 ){
63     return show_alert_and_focus(input_name, wrongNumberFormat);
64   }
65
66   try{
67     eval(test_val);
68   }catch(err){
69     return show_alert_and_focus(input_name, wrongNumberFormat);
70   }
71
72 }
73
74 function check_right_date_format(input_name) {
75   if(input_name.value == "") {
76     return true;
77   }
78   var matching = new RegExp(dateFormat.replace(/\w/g, '\\d') + "\$","ig");
79   if(!(dateFormat.lastIndexOf("y") == 3) && !matching.test(input_name.value)) {
80     matching = new RegExp(dateFormat.replace(/\w/g, '\\d') + '\\d\\d\$', "ig");
81     if(!matching.test(input_name.value)) {
82       return show_alert_and_focus(input_name, wrongDateFormat);
83     }
84   }
85   else {
86     if (dateFormat.lastIndexOf("y") == 3 && !matching.test(input_name.value)) {
87       return show_alert_and_focus(input_name, wrongDateFormat);
88     }
89   }
90 }
91
92 function validate_dates(input_name_1, input_name_2) {
93   var tempArray1 = new Array();
94   var tempArray2 = new Array();
95   tempArray1 = getDateArray(input_name_1);
96   tempArray2 = getDateArray(input_name_2);
97   if(check_right_date_format(input_name_1) && check_right_date_format(input_name_2)) {
98     if(!((new Date(tempArray2[0], tempArray2[1], tempArray2[2])).getTime() >= (new Date(tempArray1[0], tempArray1[1], tempArray1[2])).getTime())) {
99       show_alert_and_focus(input_name_1, wrongDateFormat);
100       return show_alert_and_focus(input_name_2, wrongDateFormat);
101     }
102     if(!((new Date(tempArray2[0], tempArray2[1], tempArray2[2])).getTime() >= (new Date(1900, 1, 1)).getTime())) {
103       show_alert_and_focus(input_name_1, wrongDateFormat);
104       return show_alert_and_focus(input_name_2, wrongDateFormat);
105     }
106   }
107 }
108
109 function getDateArray(input_name) {
110   formatArray[2] = input_name.value.substring(dateFormat.indexOf("d"), 2);
111   formatArray[1] = input_name.value.substring(dateFormat.indexOf("m"), 2);
112   formatArray[0] = input_name.value.substring(dateFormat.indexOf("y"), (dateFormat.length == 10 ? 4 : 2));
113   if(dateFormat.length == 8) {
114     formatArray[0] += (formatArray[0] < 70 ? 2000 : 1900);
115   }
116   return formatArray;
117 }
118
119 function show_alert_and_focus(input_name, errorMessage) {
120   input_name.select();
121   alert(errorMessage + "\n\r\n\r--> " + input_name.value);
122   input_name.focus();
123   return false;
124 }
125
126 function get_input_value(input_name) {
127   var the_input = document.getElementsByName(input_name);
128   if (the_input && the_input[0])
129     return the_input[0].value;
130   return '';
131 }
132
133 function set_cursor_position(n) {
134   $('[name=' + n + ']').focus();
135 }
136
137 function focussable(e) {
138   return e && e.name && e.type != 'hidden' && e.type != 'submit' && e.disabled != true;
139 }
140
141 function set_cursor_to_first_element(){
142   var df = document.forms;
143   for (var f = 0; f < df.length; f++)
144     for (var i = 0; i < df[f].length; i++)
145       if (focussable(df[f][i]))
146         try { df[f][i].focus(); return } catch (er) { }
147 }
148
149 function getElementByIndirectName(name){
150   var e = document.getElementsByName(name)[0];
151   if (e) return document.getElementsByName(e.value)[0];
152 }
153
154 function focus_by_name(name){
155   var f = getElementByIndirectName(name);
156   if (focussable(f)) {
157     set_cursor_position(f.name);
158     return true;
159   }
160   return false;
161 }
162
163 function open_jqm_window(params) {
164   params = params || { };
165   var id = params.id ? params.id : 'jqm_popup_dialog';
166
167   $('#' + id).remove();
168   var div     = $('<div id="' + id + '" class="jqmWindow jqModal_overlay ' + (params.class || '') + '"></div>').hide().appendTo('body');
169   var close   = $('<div class="close"></div>').appendTo(div);
170   var content = $('<div class="overlay_content"></div>').appendTo(div);
171
172   div.jqm({ modal: true });
173   div.jqmShow();
174   $(close).click(function() {
175     div.jqmClose();
176   });
177
178   $.ajax({
179     url:     params.url,
180     data:    params.data,
181     type:    params.type,
182     success: function(new_html) { $(content).html(new_html); }
183   });
184
185   return true;
186 }
187
188 $(document).ready(function () {
189   // initialize all jQuery UI tab elements:
190   $(".tabwidget").each(function(idx, element) { $(element).tabs(); });
191
192   $('input').focus(function(){
193     if (focussable(this)) window.focused_element = this;
194   });
195
196   var initial_focus = $(".initial_focus").filter(':visible')[0];
197   if (initial_focus)
198     $(initial_focus).focus();
199
200   // legacy. sone forms install these
201   if (typeof fokus == 'function') { fokus(); return; }
202   if (focus_by_name('cursor_fokus')) return;
203   set_cursor_to_first_element();
204 });
205
206 $('form').submit(function(){
207   if (window.focused_element)
208     document.forms[0].cursor_fokus.value = window.focused_element.name;
209 });