source: WAeUP_SRP/trunk/skins/waeup_custom/functions.js.dtml @ 2032

Last change on this file since 2032 was 1700, checked in by joachim, 18 years ago

students_catalog reindexing implemented.

File size: 9.0 KB
Line 
1<dtml-comment>
2CPS JavaScript functions
3$Id: functions.js.dtml 32288 2006-01-26 17:36:06Z tziade $
4</dtml-comment>
5
6<dtml-let last_modified="_.DateTime()-14" expires="_.DateTime()+1" >
7<dtml-call "REQUEST.RESPONSE.setHeader( 'Content-Type', 'text/javascript' )">
8<dtml-call "REQUEST.RESPONSE.setHeader( 'Last-Modified', last_modified.toZone('GMT').rfc822() )">
9<dtml-call "REQUEST.RESPONSE.setHeader( 'Cache-Control', 'max-age=36000, must-revalidate' )">
10<dtml-call "REQUEST.RESPONSE.setHeader( 'Expires', expires.toZone('GMT').rfc822() )" >
11</dtml-let>
12
13var msie5 = (navigator.userAgent.indexOf('MSIE 5') != -1);
14
15//************************************************************
16// Folder content
17var isSelected = false;
18
19function toggleSelect(toggleSelectButton, selectAllText, deselectAllText) {
20  formElements = toggleSelectButton.form.elements;
21
22  if (isSelected) {
23    for (i = 0; i < formElements.length; i++) {
24      formElements[i].checked = false;
25    }
26    isSelected = false;
27    toggleSelectButton.value = selectAllText;
28  } else {
29    for (i = 0; i < formElements.length; i++) {
30      formElements[i].checked = true;
31    }
32    isSelected = true;
33    toggleSelectButton.value = deselectAllText;
34  }
35}
36
37//************************************************************
38/**
39 * Toggles an element's visibility.
40 * Function to show tooltips.
41 * If your element is a span, you must use inline display instead of block
42 * XXX recognize div and span then automaticly choose the best display rule
43 */
44function toggleElementVisibility(id) {
45  element = document.getElementById(id);
46  if (element) {
47    //window.alert(element.tagName)
48    if (element.style.visibility == 'hidden') {
49      element.style.visibility = 'visible';
50      if (element.tagName == 'DIV') {
51        element.style.display = 'block';
52      } else if (element.tagName == 'SPAN') {
53        element.style.display = 'inline';
54      } else {
55        element.style.display = 'block';
56      }
57    } else {
58      element.style.visibility = 'hidden';
59      element.style.display = 'none';
60    }
61  }
62}
63
64function showElement(show, id) {
65  element = document.getElementById(id);
66  if (element) {
67    //window.alert(element.tagName)
68    if (show) {
69      element.style.visibility = 'visible';
70      if (element.tagName in ['DIV', 'P']) {
71        element.style.display = 'block';
72      } else {
73        element.style.display = 'inline';
74      }
75    } else {
76      element.style.visibility = 'hidden';
77      element.style.display = 'none';
78    }
79  }
80}
81
82//************************************************************
83function trim(s) {
84  if (s) {
85    return s.replace(/^\s*|\s*$/g, "");
86  }
87  return "";
88}
89
90//************************************************************
91function checkEmptySearch(formElem) {
92  var query = trim(formElem.SearchableText.value);
93  if (query != '') {
94    formElem.SearchableText.value = query;
95    return true;
96  }
97  formElem.SearchableText.value = query;
98  formElem.SearchableText.focus();
99  return false;
100}
101
102//************************************************************
103/**
104 * Sets focus on <input> elements that have a class attribute
105 * containing the class 'focus'.
106 * Examples:
107 * <input type="text" id="username" name="__ac_name" class="focus"/>
108 * <input type="text" id="searchableText" class="standalone giant focus"/>
109 *
110 * This function does not work on crappy MSIE5.0 and MSIE5.5.
111 */
112function setFocus() {
113  if (msie5) {
114    return false;
115  }
116  var elements = document.getElementsByTagName('input');
117  for (var i = 0; i < elements.length; i++) {
118    var nodeClass = elements[i].getAttributeNode('class');
119    //alert("nodeClass = " + nodeClass);
120    if (nodeClass) {
121      var classes = nodeClass.value.split(' ');
122      for (var j = 0; j < classes.length; j++) {
123        if (classes[j] == 'focus') {
124          elements[i].focus();
125          return true;
126        }
127      }
128    }
129  }
130}
131
132/**
133 * Validates that the input fields designated by the given ids are not empty.
134 * It returns true if all the input fields are not empty, it returns false
135 * otherwise.
136 * Example:
137 * <form onsubmit="return validateRequiredFields(['field1', 'field2'],
138 * ['Title', 'Comments'], 'are empty while they are required fields.')">
139 */
140function validateRequiredFields(fieldIds, fieldLabels, informationText) {
141  for (i = 0; i < fieldIds.length; i++) {
142    element = document.getElementById(fieldIds[i]);
143    if (element && !element.value) {
144      window.alert("'" + fieldLabels[i] + "' " + informationText);
145      return false;
146    }
147  }
148  return true;
149}
150
151//************************************************************
152function getSelectedRadio(buttonGroup) {
153  if (buttonGroup[0]) {
154    for (var i=0; i<buttonGroup.length; i++) {
155      if (buttonGroup[i].checked) {
156        return i
157      }
158    }
159  } else {
160    if (buttonGroup.checked) { return 0; }
161  }
162  return -1;
163}
164
165function getSelectedRadioValue(buttonGroup) {
166  var i = getSelectedRadio(buttonGroup);
167  if (i == -1) {
168    return "";
169  } else {
170    if (buttonGroup[i]) {
171    return buttonGroup[i].value;
172    } else {
173    return buttonGroup.value;
174    }
175  }
176}
177
178function getSelectedRadioId(buttonGroup) {
179  var i = getSelectedRadio(buttonGroup);
180  if (i == -1) {
181    return "";
182  } else {
183    if (buttonGroup[i]) {
184      return buttonGroup[i].id;
185    } else {
186      return buttonGroup.id;
187    }
188  }
189}
190
191/**
192 * Return the label content corresponding to a radio selection
193 */
194function getSelectedRadioLabel(buttonGroup) {
195  var id = getSelectedRadioId(buttonGroup);
196  if (id == "") {
197    return "";
198  } else {
199    for (var i=0; i<document.getElementsByTagName("label").length; i++) {
200      var element_label = document.getElementsByTagName("label")[i];
201      if (element_label.htmlFor == id) {
202        return element_label.firstChild.nodeValue;
203      }
204    }
205  }
206}
207
208
209//************************************************************
210/*
211    Used to highlight search terms
212    from Geir Bækholt, adapted for CPS
213
214 */
215function highlightSearchTerm() {
216  var query_elem = document.getElementById('searchGadget')
217  if (! query_elem){
218    return false
219  }
220  var query = query_elem.value
221  // _robert_ ie 5 does not have decodeURI
222  if (typeof decodeURI != 'undefined'){
223    query = unescape(decodeURI(query)) // thanks, Casper
224  }
225  else {
226    return false
227  }
228  if (query){
229    queries = query.replace(/\+/g,' ').split(/\s+/)
230    // make sure we start the right place and not higlight menuitems or breadcrumb
231    searchresultnode = document.getElementById('searchResults')
232    if (searchresultnode) {
233      for (q=0;q<queries.length;q++) {
234        // don't highlight reserved catalog search terms
235        if (queries[q].toLowerCase() != 'not'
236          && queries[q].toLowerCase() != 'and'
237          && queries[q].toLowerCase() != 'or') {
238          climb(searchresultnode,queries[q]);
239        }
240      }
241    }
242  }
243}
244
245function climb(node, word){
246  // traverse childnodes
247  if (! node){
248    return false
249  }
250  if (node.hasChildNodes) {
251    var i;
252    for (i=0;i<node.childNodes.length;i++) {
253      climb(node.childNodes[i],word);
254    }
255    if (node.nodeType == 3) {
256      checkforhighlight(node, word);
257      // check all textnodes. Feels inefficient, but works
258    }
259  }
260}
261
262function checkforhighlight(node,word) {
263  ind = node.nodeValue.toLowerCase().indexOf(word.toLowerCase())
264  if (ind != -1) {
265    if (node.parentNode.className != "highlightedSearchTerm"){
266      par = node.parentNode;
267      contents = node.nodeValue;
268      // make 3 shiny new nodes
269      hiword = document.createElement("span");
270      hiword.className = "highlightedSearchTerm";
271      hiword.appendChild(document.createTextNode(contents.substr(ind,word.length)));
272      par.insertBefore(document.createTextNode(contents.substr(0,ind)),node);
273      par.insertBefore(hiword,node);
274      par.insertBefore(document.createTextNode( contents.substr(ind+word.length)),node);
275      par.removeChild(node);
276    }
277  }
278}
279
280/************************************************************
281/**
282 * searchLanguage widget functions
283 * used to auto select languages checkbox/radio
284 * cf CPSSchemas/skins/cps_schemas/widget_searchlanguage_render.pt
285 */
286function searchLanguageCheckSelected(languages, no_language, language) {
287  var count=0;
288  for (var i=0; i<languages.length; i++) {
289    if (languages[i].checked) {
290      count++;
291    }
292  }
293  no_language.checked = (count <= 0);
294  language.checked = (count > 0);
295}
296
297function searchLanguageClearSelected(languages) {
298  for (i=0; i<languages.length; i++) {
299    languages[i].checked = 0;
300  }
301}
302
303function toggleLayers(more_block, more_items) {
304  var objMoreBlock = document.getElementById(more_block).style;
305  var objMoreItems = document.getElementById(more_items).style;
306  if(objMoreBlock.display == "block")
307    objMoreBlock.display = "none";
308  if(objMoreItems.display == "none")
309    objMoreItems.display = "block";
310}
311
312/************************************************************
313/**
314 * waeup specific functions
315 */
316
317/**
318 * submit_once(): Avoid multiple sendings of forms...
319 */
320
321submitted = false;
322function submit_once() {
323    if (submitted) {
324        alert("Your request is being processed.\nPlease be patient.");
325        event.returnValue = 0;    // work-around for IE
326        return 0;
327    }
328    submitted = true;
329    return 1;
330}
Note: See TracBrowser for help on using the repository browser.