Administration: Einen Button zum Testen der Datenbankeinstellung in der Maske zur...
[kivitendo-erp.git] / js / common.js
1
2 function setupPoints(numberformat, wrongFormat) {
3   decpoint = numberformat.substring((numberformat.substring(1, 2).match(/\.|\,/) ? 5 : 4), (numberformat.substring(1, 2).match(/\.|\,/) ? 6 : 5));
4   if (numberformat.substring(1, 2).match(/\.|\,/)) {
5     thpoint = numberformat.substring(1, 2); 
6   }
7   else {
8     thpoint = null;
9   }
10   wrongNumberFormat = wrongFormat + " ( " + numberformat + " ) ";  
11 }
12
13 function setupDateFormat(setDateFormat, setWrongDateFormat) {
14   dateFormat = setDateFormat;
15   wrongDateFormat = setWrongDateFormat + " ( " + setDateFormat + " ) ";
16   formatArray = new Array();
17   if(dateFormat.match(/^\w\w\W/)) {
18     seperator = dateFormat.substring(2,3);
19   }
20   else {
21     seperator = dateFormat.substring(4,5);
22   }
23 }
24
25 function centerParms(width,height,extra) {
26   xPos = (screen.width - width) / 2;
27   yPos = (screen.height - height) / 2;
28
29   string = "left=" + xPos + ",top=" + yPos;
30
31   if (extra)
32     string += "width=" + width + ",height=" + height;
33
34   return string;
35 }
36
37 function escape_more(s) {
38   s = escape(s);
39   return s.replace(/\+/g, '%2b');
40 }
41
42 function set_longdescription_window(input_name) {
43   var parm = centerParms(600,500) + ",width=600,height=500,status=yes,scrollbars=yes";
44   var name = document.getElementsByName(input_name)[0].value;
45   url = "common.pl?" +
46     "action=set_longdescription&" +
47     "longdescription=" + escape_more(document.getElementsByName(input_name)[0].value) + "&" +
48     "input_name=" + escape_more(input_name) + "&"
49   window.open(url, "_new_generic", parm);
50   }
51
52 function check_right_number_format(input_name) {
53 //   if(thpoint) {
54 //     if(thpoint == ','){
55 //       var thnumbers = input_name.value.split(',');  
56 //       thnumbers[thnumbers.length-1] = thnumbers[thnumbers.length-1].substring((thnumbers[thnumbers.length-1].lastIndexOf(".") !== -1 ? thnumbers[thnumbers.length-1].lastIndexOf(".") : thnumbers[thnumbers.length-1].length), 0);
57 //     }
58 //     else{
59 //       var thnumbers = input_name.value.split('.');  
60 //       thnumbers[thnumbers.length-1] = thnumbers[thnumbers.length-1].substring((thnumbers[thnumbers.length-1].lastIndexOf(",") !== -1 ? thnumbers[thnumbers.length-1].lastIndexOf(",") : thnumbers[thnumbers.length-1].length), 0);
61 //     }
62 //         
63 //     for(var i = 0; i < thnumbers.length; i++) {
64 //      if(i == 0 && thnumbers[i].length > 3) {
65 //       return show_alert_and_focus(input_name, wrongNumberFormat);
66 //      }
67 //      if(i > 0 && thnumbers[i].length != 3) {
68 //        return show_alert_and_focus(input_name, wrongNumberFormat);
69 //      }
70 //    }
71 //   }
72   if(decpoint == thpoint) {
73     return show_alert_and_focus(input_name, wrongNumberFormat);
74   }
75   if(decpoint == ',') {
76     var decnumbers = input_name.value.split(',');
77   }
78   else {
79     var decnumbers = input_name.value.split('.');
80   }
81   if(decnumbers.length == 2) {
82     if(decnumbers[1].length > 2)  {
83       return show_alert_and_focus(input_name, wrongNumberFormat);
84     }
85   }
86   else {
87     if(decnumbers.length > 2) {
88       return show_alert_and_focus(input_name, wrongNumberFormat);
89     }
90     if(!thpoint) {
91       if(decnumbers[0].match(/\D/)) {
92         return show_alert_and_focus(input_name, wrongNumberFormat);
93       }
94     }
95   }
96 }
97
98 function check_right_date_format(input_name) {
99   if(input_name.value == "") {
100     return true;
101   }
102   var matching = new RegExp(dateFormat.replace(/\w/g, '\\d') + "\$","ig");
103   if(!(dateFormat.lastIndexOf("y") == 3) && !matching.test(input_name.value)) {
104     matching = new RegExp(dateFormat.replace(/\w/g, '\\d') + '\\d\\d\$', "ig");
105     if(!matching.test(input_name.value)) {
106       return show_alert_and_focus(input_name, wrongDateFormat);
107     }
108   }
109   else {
110     if (dateFormat.lastIndexOf("y") == 3 && !matching.test(input_name.value)) { 
111       return show_alert_and_focus(input_name, wrongDateFormat);
112     }
113   }
114 }
115
116 function validate_dates(input_name_1, input_name_2) {
117   var tempArray1 = new Array();
118   var tempArray2 = new Array();
119   tempArray1 = getDateArray(input_name_1);
120   tempArray2 = getDateArray(input_name_2);
121   if(check_right_date_format(input_name_1) && check_right_date_format(input_name_2)) {
122     if(!((new Date(tempArray2[0], tempArray2[1], tempArray2[2])).getTime() >= (new Date(tempArray1[0], tempArray1[1], tempArray1[2])).getTime())) {
123       show_alert_and_focus(input_name_1, wrongDateFormat);
124       return show_alert_and_focus(input_name_2, wrongDateFormat);
125     }
126     if(!((new Date(tempArray2[0], tempArray2[1], tempArray2[2])).getTime() >= (new Date(1900, 1, 1)).getTime())) {
127       show_alert_and_focus(input_name_1, wrongDateFormat);
128       return show_alert_and_focus(input_name_2, wrongDateFormat);
129     }
130   }
131 }
132
133 function getDateArray(input_name) {
134   formatArray[2] = input_name.value.substring(dateFormat.indexOf("d"), 2);
135   formatArray[1] = input_name.value.substring(dateFormat.indexOf("m"), 2);
136   formatArray[0] = input_name.value.substring(dateFormat.indexOf("y"), (dateFormat.length == 10 ? 4 : 2));
137   if(dateFormat.length == 8) {
138     formatArray[0] += (formatArray[0] < 70 ? 2000 : 1900);
139   }
140   return formatArray;
141 }
142
143 function show_alert_and_focus(input_name, errorMessage) {
144   input_name.select();
145   alert(errorMessage + "\n\r\n\r--> " + input_name.value);
146   input_name.focus();
147   return false;
148 }
149   
150 function get_input_value(input_name) {
151   var the_input = document.getElementsByName(input_name);
152   if (the_input && the_input[0])
153     return the_input[0].value;
154   return '';
155 }