Stricktere Überwachung der Nummernformate eingeführt. Auch 1000,00 wird bei Formaten...
[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 set_longdescription_window(input_name) {
38   var parm = centerParms(600,500) + ",width=600,height=500,status=yes,scrollbars=yes";
39   var name = document.getElementsByName(input_name)[0].value;
40   url = "common.pl?" +
41     "action=set_longdescription&" +
42     "login=" +  encodeURIComponent(document.getElementsByName("login")[0].value)+ "&"+
43     "password=" + encodeURIComponent(document.getElementsByName("password")[0].value) + "&" +
44     "longdescription=" + escape(document.getElementsByName(input_name)[0].value) + "&" +
45     "input_name=" + escape(input_name) + "&"
46   window.open(url, "_new_generic", parm);
47   }
48
49 function check_right_number_format(input_name) {
50   if(thpoint) {
51     if(thpoint == ','){
52       var thnumbers = input_name.value.split(',');  
53       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);
54     }
55     else{
56       var thnumbers = input_name.value.split('.');  
57       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);
58     }
59         
60     for(var i = 0; i < thnumbers.length; i++) {
61      if(i == 0 && thnumbers[i].length > 3) {
62       return show_alert_and_focus(input_name, wrongNumberFormat);
63      }
64      if(i > 0 && thnumbers[i].length != 3) {
65        return show_alert_and_focus(input_name, wrongNumberFormat);
66      }
67    }
68   }
69   if(decpoint == ',') {
70     var decnumbers = input_name.value.split(',');
71   }
72   else {
73     var decnumbers = input_name.value.split('.');
74   }
75   if(decnumbers.length == 2) {
76     if(decnumbers[1].length > 2)  {
77       return show_alert_and_focus(input_name, wrongNumberFormat);
78     }
79   }
80   else {
81     if(decnumbers.length > 2) {
82       return show_alert_and_focus(input_name, wrongNumberFormat);
83     }
84     if(!thpoint) {
85       if(decnumbers[0].match(/\D/)) {
86         return show_alert_and_focus(input_name, wrongNumberFormat);
87       }
88     }
89   }
90 }
91
92 function check_right_date_format(input_name) {
93   if(input_name.value == "") {
94     return true;
95   }
96   var matching = new RegExp(dateFormat.replace(/\w/g, '\\d') + "\$","ig");
97   if(!(dateFormat.lastIndexOf("y") == 3) && !matching.test(input_name.value)) {
98     matching = new RegExp(dateFormat.replace(/\w/g, '\\d') + '\\d\\d\$', "ig");
99     if(!matching.test(input_name.value)) {
100       return show_alert_and_focus(input_name, wrongDateFormat);
101     }
102   }
103   else {
104     if (dateFormat.lastIndexOf("y") == 3 && !matching.test(input_name.value)) { 
105       return show_alert_and_focus(input_name, wrongDateFormat);
106     }
107   }
108 }
109
110 function validate_dates(input_name_1, input_name_2) {
111   var tempArray1 = new Array();
112   var tempArray2 = new Array();
113   tempArray1 = getDateArray(input_name_1);
114   tempArray2 = getDateArray(input_name_2);
115   if(check_right_date_format(input_name_1) && check_right_date_format(input_name_2)) {
116     if(!((new Date(tempArray2[0], tempArray2[1], tempArray2[2])).getTime() >= (new Date(tempArray1[0], tempArray1[1], tempArray1[2])).getTime())) {
117       show_alert_and_focus(input_name_1, wrongDateFormat);
118       return show_alert_and_focus(input_name_2, wrongDateFormat);
119     }
120     if(!((new Date(tempArray2[0], tempArray2[1], tempArray2[2])).getTime() >= (new Date(1900, 1, 1)).getTime())) {
121       show_alert_and_focus(input_name_1, wrongDateFormat);
122       return show_alert_and_focus(input_name_2, wrongDateFormat);
123     }
124   }
125 }
126
127 function getDateArray(input_name) {
128   formatArray[2] = input_name.value.substring(dateFormat.indexOf("d"), 2);
129   formatArray[1] = input_name.value.substring(dateFormat.indexOf("m"), 2);
130   formatArray[0] = input_name.value.substring(dateFormat.indexOf("y"), (dateFormat.length == 10 ? 4 : 2));
131   if(dateFormat.length == 8) {
132     formatArray[0] += (formatArray[0] < 70 ? 2000 : 1900);
133   }
134   return formatArray;
135 }
136
137 function show_alert_and_focus(input_name, errorMessage) {
138   input_name.select();
139   alert(errorMessage + "\n\r\n\r--> " + input_name.value);
140   input_name.focus();
141   return false;
142 }
143