source: TI03-DataExtractor/branches/old_stuff/dx_source/for_usr_local/dx/jsFunctions.py @ 793

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI03-DataExtractor/branches/old_stuff/dx_source/for_usr_local/dx/jsFunctions.py
Revision 793, 7.0 KB checked in by astephen, 14 years ago (diff)

Put all the old code in the old_stuff branch.

Line 
1#   Copyright (C) 2004 CCLRC & NERC( Natural Environment Research Council ).
2#   This software may be distributed under the terms of the
3#   Q Public License, version 1.0 or later. http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
4
5"""
6jsFunctions.py
7===========
8
9Holder module for Javascript functions that will be inserted into the header
10of relevant web pages for checking purposes.
11
12"""
13
14# js_dateCheckFunctionGroup - a string containing a list of javascript
15# functions for checking date/time fields on a web form. These are
16# inserted into HTML output by the UserInterface class in the userInterface
17# module.
18js_dateCheckFunctionGroup="""
19<SCRIPT LANGUAGE="Javascript">
20
21// Set up global variables for all functions
22var frm=document.forms[1];
23var dateMinimum_1 = new Array(%s, %s, %s, %s, %s, %s);
24var dateMaximum_1 = new Array(%s, %s, %s, %s, %s, %s);
25var dateMinimum_2 = new Array(%s, %s, %s, %s, %s, %s);
26var dateMaximum_2 = new Array(%s, %s, %s, %s, %s, %s);
27
28function getFormFields(arg) {
29    // getFormFields - returns an array of form fields to manipulate based on the argument given.
30    if (arg == "start_1") {
31        var fields = new Array(frm.startYear_1, frm.startMonth_1, frm.startDay_1, frm.startHour_1, frm.startMinute_1, frm.startSecond_1);
32    } else if (arg == "end_1") {
33        var fields = new Array(frm.endYear_1, frm.endMonth_1, frm.endDay_1, frm.endHour_1, frm.endMinute_1, frm.endSecond_1);
34    } else if (arg == "start_2") {
35        var fields = new Array(frm.startYear_2, frm.startMonth_2, frm.startDay_2, frm.startHour_2, frm.startMinute_2, frm.startSecond_2);
36    } else if (arg == "end_2") {
37        var fields = new Array(frm.endYear_2, frm.endMonth_2, frm.endDay_2, frm.endHour_2, frm.endMinute_2, frm.endSecond_2);
38    }
39    return fields
40}
41
42function getOppositeFields(arg) {
43    // getOppositeFields - returns an array of form fields opposite to the argument argument given.
44    if (arg == "end_1") {
45        var fields = new Array(frm.startYear_1, frm.startMonth_1, frm.startDay_1, frm.startHour_1, frm.startMinute_1, frm.startSecond_1);
46    } else if (arg == "start_1") {
47        var fields = new Array(frm.endYear_1, frm.endMonth_1, frm.endDay_1, frm.endHour_1, frm.endMinute_1, frm.endSecond_1);
48    } else if (arg == "end_2") {
49        var fields = new Array(frm.startYear_2, frm.startMonth_2, frm.startDay_2, frm.startHour_2, frm.startMinute_2, frm.startSecond_2);
50    } else if (arg == "start_2") {
51        var fields = new Array(frm.endYear_2, frm.endMonth_2, frm.endDay_2, frm.endHour_2, frm.endMinute_2, frm.endSecond_2);
52    }
53    return fields
54}
55
56function matchFields(fields1, fields2) {
57    // matchFields - sets one set of form fields equal to another
58    for (i=0; i<6; i++) {
59        fields2[i].selectedIndex = fields1[i].selectedIndex;
60    }
61}
62
63function getDateMinimum(arg) {
64    // getDateMinimum function - to return the minimum date for the appropriate fields
65    if ((arg=="start_1") || (arg=="end_1")) {
66        var dateMinimum = dateMinimum_1;
67    } else if ((arg=="start_2") || (arg=="end_2")) {
68        var dateMinimum = dateMinimum_2;
69    }
70    return dateMinimum;
71}
72
73function getDateMaximum(arg) {
74    // getDateMaximum function - to return the maximum date for the appropriate fields
75    if ((arg=="start_1") || (arg=="end_1")) {
76        var dateMaximum = dateMaximum_1;
77    } else if ((arg=="start_2") || (arg=="end_2")) {
78        var dateMaximum = dateMaximum_2;
79    }
80    return dateMaximum;
81}
82
83function resetDate(arg) {
84    // resetDate function - resets the date field selected to either max or min possible.
85    var form_fields = getFormFields(arg);
86    var dateMinimum = getDateMinimum(arg);
87    for (i=0; i<6; i++) {
88        form_fields[i].selectedIndex = dateMinimum[i];
89    }
90}
91
92
93function resetAllDates(n) {
94    // resetAllDates function - resets all date fields to minimum allowed.
95    resetDate("start_1");
96    resetDate("end_1");
97    if (n>1) {
98        resetDate("start_2");
99        resetDate("end_2");
100    }
101}
102
103
104function checkDate(arg) {
105    // checkDate function - checks start date is not below minimum.
106    var form_fields = getFormFields(arg);
107    var dateMinimum = getDateMinimum(arg);
108    var dateMaximum = getDateMaximum(arg);
109     
110    if (form_fields[0].selectedIndex < dateMinimum[0]) {
111        resetDate(arg);
112    } else if (form_fields[0].selectedIndex == dateMinimum[0]) {
113        if (form_fields[1].selectedIndex < dateMinimum[1]) {
114            resetDate(arg);
115        } else if (form_fields[1].selectedIndex == dateMinimum[1]) {
116            if (form_fields[2].selectedIndex < dateMinimum[2]) {
117                resetDate(arg);
118            } else if (form_fields[2].selectedIndex == dateMinimum[2]) {
119                if (form_fields[3].selectedIndex < dateMinimum[3]) {
120                    resetDate(arg);
121                } else if (form_fields[3].selectedIndex == dateMinimum[3]) {
122                    if (form_fields[4].selectedIndex < dateMinimum[4]) {
123                        resetDate(arg);
124                    } else if (form_fields[4].selectedIndex == dateMinimum[4]) {
125                        if (form_fields[5].selectedIndex < dateMinimum[5]) {
126                            resetDate(arg);
127                        }
128                    }
129                }
130            }
131        }
132    } else if (form_fields[0].selectedIndex == dateMaximum[0]) {
133        if (form_fields[1].selectedIndex > dateMaximum[1]) {
134            resetDate(arg);
135        } else if (form_fields[1].selectedIndex == dateMaximum[1]) {
136            if (form_fields[2].selectedIndex > dateMaximum[2]) {
137                resetDate(arg);
138            } else if (form_fields[2].selectedIndex == dateMaximum[2]) {
139                if (form_fields[3].selectedIndex > dateMaximum[3]) {
140                    resetDate(arg);
141                } else if (form_fields[3].selectedIndex == dateMaximum[3]) {
142                    if (form_fields[4].selectedIndex > dateMaximum[4]) {
143                        resetDate(arg);
144                    } else if (form_fields[4].selectedIndex == dateMaximum[4]) {
145                        if (form_fields[5].selectedIndex > dateMaximum[5]) {
146                            resetDate(arg);
147                        }
148                    }
149                }
150            }
151        }   
152    }
153    // Now check the order of the fields to make sure start is not after end
154    var opp_fields  = getOppositeFields(arg);
155    for (i=0; i<6; i++) {
156        if ((arg=="start_1") || (arg=="start_2")) {
157            if (form_fields[i].selectedIndex > opp_fields[i].selectedIndex) {
158                matchFields(form_fields, opp_fields);
159                break;
160            } else if (form_fields[i].selectedIndex < opp_fields[i].selectedIndex) {
161                break;
162            }
163        }
164        if ((arg=="end_1") || (arg=="end_2")) {
165            if (form_fields[i].selectedIndex < opp_fields[i].selectedIndex) {
166                matchFields(form_fields, opp_fields);
167                break;
168            } else if (form_fields[i].selectedIndex > opp_fields[i].selectedIndex) {
169                break;
170            }
171        }
172    }
173}
174
175</SCRIPT>
176"""
177
178
179
Note: See TracBrowser for help on using the repository browser.