1 function $(id) { return getId(id); }
3 return document.getElementById(id);
6 function swapCSSClass(obj, old, newc ) {
7 removeCSSClass(obj, old );
8 addCSSClass(obj, newc );
12 function addCSSClass(e,c) {
15 var css_class_string = e.className;
19 css_class_array = css_class_string.split(/\s+/);
21 var string_ip = ""; /*strip out nulls*/
22 for (var css_class in css_class_array) {
23 if (css_class_array[css_class] == c) { return; }
24 if(css_class_array[css_class] !=null)
25 string_ip += css_class_array[css_class] + " ";
28 e.className = string_ip;
31 function removeCSSClass(e, c) {
34 var css_class_string = '';
36 var css_class_array = e.className;
38 css_class_array = css_class_array.split(/\s+/);
41 for (var css_class in css_class_array) {
42 if (css_class_array[css_class] != c) {
44 css_class_string = css_class_array[css_class];
47 css_class_string = css_class_string + ' ' +
48 css_class_array[css_class];
52 e.className = css_class_string;
56 /*returns the character code pressed that caused the event */
57 function grabCharCode(evt) {
58 evt = (evt) ? evt : ((window.event) ? event : null);
60 return (evt.charCode ? evt.charCode :
61 ((evt.which) ? evt.which : evt.keyCode ));
66 /* returns true if the user pressed enter */
67 function userPressedEnter(evt) {
68 var code = grabCharCode(evt);
69 if(code==13||code==3) return true;
75 /* setTimeout because ie sux */
76 setTimeout( function(){ location.href = url; }, 0 );
80 function removeChildren(dom) {
82 while(dom.childNodes[0])
83 dom.removeChild(dom.childNodes[0]);
86 function appendClear(node, child) {
88 node.appendChild(child);
92 function instanceOf(object, constructorFunction) {
95 while (object != null) {
96 if (object == constructorFunction.prototype)
98 object = object.__proto__;
101 while(object != null) {
102 if( object instanceof constructorFunction )
104 object = object.__proto__;
111 /* ------------------------------------------------------------------------------------------- */
112 /* detect my browser */
113 var isMac, NS, NS4, NS6, IE, IE4, IE4mac, IE4plus, IE5, IE5plus, IE6, IEMajor, ver4;
114 function detect_browser() {
116 isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
117 NS = (navigator.appName == "Netscape") ? true : false;
118 NS4 = (document.layers) ? true : false;
119 IE = (navigator.appName == "Microsoft Internet Explorer") ? true : false;
120 IEmac = ((document.all)&&(isMac)) ? true : false;
121 IE4plus = (document.all) ? true : false;
122 IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
123 IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
124 IE6 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 6.")!=-1)) ? true : false;
125 ver4 = (NS4 || IE4plus) ? true : false;
126 NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape')!=-1)?true:false;
128 IE5plus = IE5 || IE6;
132 var start = navigator.appVersion.indexOf("MSIE");
133 var end = navigator.appVersion.indexOf(".",start);
134 IEMajor = parseInt(navigator.appVersion.substring(start+5,end));
135 IE5plus = (IEMajor>=5) ? true : false;
139 /* ------------------------------------------------------------------------------------------- */
143 if(t == null) t = "";
144 return document.createTextNode(t);
147 function elem(name, attrs, txt) {
148 var e = document.createElement(name);
151 if( key == 'id') e.id = attrs[key];
152 else e.setAttribute(key, attrs[key]);
155 if (txt) e.appendChild(text(txt));
160 /* sel is the selector object, sets selected on the
161 option with the given value. case does not matter...*/
162 function setSelector( sel, value ) {
163 if(sel && value != null) {
164 for( var i = 0; i!= sel.options.length; i++ ) {
165 if( sel.options[i] ) {
166 var val = sel.options[i].value;
167 if( val == null || val == "" ) /* for IE */
168 val = sel.options[i].innerHTML;
169 value += ""; /* in case of number */
170 if( val && val.toLowerCase() == value.toLowerCase() ) {
171 sel.selectedIndex = i;
172 sel.options[i].selected = true;
181 function setSelectorRegex( sel, regex ) {
182 if(sel && regex != null) {
183 for( var i = 0; i!= sel.options.length; i++ ) {
184 if( sel.options[i] ) {
185 var val = sel.options[i].value;
186 if( val == null || val == "" ) /* for IE */
187 val = sel.options[i].innerHTML;
188 value += ""; /* in case of number */
189 if( val && val.match(regex) ) {
190 sel.selectedIndex = i;
191 sel.options[i].selected = true;
200 function getSelectorVal( sel ) {
201 if(!sel) return null;
202 var idx = sel.selectedIndex;
203 if( idx < 0 ) return null;
204 var o = sel.options[idx];
206 if(v == null) v = o.innerHTML;
210 function getSelectorName( sel ) {
211 var o = sel.options[sel.selectedIndex];
213 if(v == null || v == undefined || v == "") v = o.innerHTML;
217 function setSelectorByName( sel, name ) {
218 for( var o in sel.options ) {
219 var opt = sel.options[o];
220 if( opt.name == name || opt.innerHTML == name ) {
221 sel.selectedIndex = o;
227 function findSelectorOptByValue( sel, val ) {
228 for( var i = 0; i < sel.options.length; i++ ) {
229 var opt = sel.options[i];
230 if( opt.value == val ) return opt;
235 function debugSelector(sel) {
236 var s = 'Selector\n';
237 for( var i = 0; i != sel.options.length; i++ ) {
238 var o = sel.options[i];
239 s += "\t" + o.innerHTML + "\n";
244 function findParentByNodeName(node, name) {
245 while( ( node = node.parentNode) )
246 if (node.nodeName == name) return node;
250 /* returns only elements in nodes childNodes list, not sub-children */
251 function getElementsByTagNameFlat( node, name ) {
253 for( var e in node.childNodes ) {
254 var n = node.childNodes[e];
255 if( n && n.nodeName == name ) elements.push(n);
260 /* expects a tree with a id() method on each node and a
261 children() method to get to each node */
262 function findTreeItemById( tree, id ) {
263 if( tree.id() == id ) return tree;
264 for( var c in tree.children() ) {
265 var found = findTreeItemById( tree.children()[c], id );
266 if(found) return found;
271 /* returns null if none of the tests are true. returns sub-array of
272 matching array items otherwise */
273 function grep( arr, func ) {
275 if(!arr) return null;
276 if( arr.constructor == Array ) {
277 for( var i = 0; i < arr.length; i++ ) {
279 results.push(arr[i]);
282 for( var i in arr ) {
284 results.push(arr[i]);
287 if(results.length > 0) return results;
291 function doSelectorActions(sel) {
293 sel.onchange = function() {
294 var o = sel.options[sel.selectedIndex];
295 if(o && o.onclick) o.onclick()
300 /* if index < 0, the item is pushed onto the end */
301 function insertSelectorVal( selector, index, name, value, action, indent ) {
302 if( index < 0 ) index = selector.options.length;
304 for( var i = selector.options.length; i != index; i-- )
305 a[i] = selector.options[i-1];
307 var opt = setSelectorVal( selector, index, name, value, action, indent );
309 for( var i = index + 1; i < a.length; i++ )
310 selector.options[i] = a[i];
315 /* changes the value of the option at the specified index */
316 function setSelectorVal( selector, index, name, value, action, indent ) {
317 if(!indent || indent < 0) indent = 0;
318 indent = parseInt(indent);
323 var pre = elem("pre");
324 for( var i = 0; i != indent; i++ )
325 pre.appendChild(text(" "));
327 pre.appendChild(text(name));
328 option = new Option("", value);
329 selector.options[index] = option;
330 option.appendChild(pre);
333 indent = indent * 14;
334 option= new Option(name, value);
335 option.setAttribute("style", "padding-left: "+indent+'px;');
336 selector.options[index] = option;
337 if(action) option.onclick = action;
340 if(action) option.onclick = action;
345 /* split on spaces. capitalize the first /\w/ character in
347 function normalize(val) {
352 try {val = val.split(' ');} catch(E) {return val;}
355 for( var c = 0; c < val.length; c++) {
359 for(var x = 0; x != string.length; x++) {
362 var ch = string.charAt(x);
363 if(reg.exec(ch + "")) {
364 newVal += string.charAt(x).toUpperCase();
370 newVal += string.charAt(x).toLowerCase();
372 if(c < (val.length-1)) newVal += " ";
375 newVal = newVal.replace(/\s*\.\s*$/,'');
376 newVal = newVal.replace(/\s*\/\s*\/\s*$/,' / ');
377 newVal = newVal.replace(/\s*\/\s*$/,'');
383 /* returns true if n is null or stringifies to 'undefined' */
385 if( n == null || n == undefined || n.toString().toLowerCase() == "undefined"
386 || n.toString().toLowerCase() == "null" )
392 /* find nodes with an attribute of 'name' that equals nodeName */
394 function $n( root, nodeName ) { return findNodeByName(root,nodeName); }
396 function findNodeByName(root, nodeName) {
397 if( !root || !nodeName) return null;
399 if(root.nodeType != 1) return null;
401 if(root.getAttribute("name") == nodeName || root.name == nodeName )
404 var children = root.childNodes;
406 for( var i = 0; i != children.length; i++ ) {
407 var n = findNodeByName(children[i], nodeName);
415 /* truncates the string at 'size' characters and appends a '...' to the end */
416 function truncate(string, size) {
417 if(string && size != null &&
418 size > -1 && string.length > size)
419 return string.substr(0, size) + "... ";
424 /* style sheets must have a 'name' attribute for these functions to work */
425 function setActivateStyleSheet(name) {
427 for (i = 0; (a = document.getElementsByTagName ("link")[i]); i++) {
428 if (a.getAttribute ("rel").indexOf ("style") != -1 && a.getAttribute ("name")) {
430 if (a.getAttribute ("name").indexOf(name) != -1)
437 /* ----------------------------------------------------- */
439 function scaleFonts(type) {
446 if(!currentFontSize) currentFontSize = 'regular';
447 if(currentFontSize == 'regular' && type == 'regular' ) return;
448 if( currentFontSize == type ) return;
449 currentFontSize = type;
452 case "large": /* these are arbitrary.. but they seem to work ok in FF/IE */
459 document.getElementsByTagName('body')[0].style.fontSize = size;
460 for (i = 0; (a = document.getElementsByTagName ("td")[i]); i++) a.style.fontSize = size;;
461 for (i = 0; (a = document.getElementsByTagName ("div")[i]); i++) a.style.fontSize = ssize;
462 for (i = 0; (a = document.getElementsByTagName ("option")[i]); i++) a.style.fontSize = ssize;
463 for (i = 0; (a = document.getElementsByTagName ("li")[i]); i++) a.style.fontSize = ssize;
464 for (i = 0; (a = document.getElementsByTagName ("span")[i]); i++) a.style.fontSize = ssize;
465 for (i = 0; (a = document.getElementsByTagName ("select")[i]); i++) a.style.fontSize = ssize;
466 for (i = 0; (a = document.getElementsByTagName ("a")[i]); i++) a.style.fontSize = size2;
470 function sortWordsIgnoreCase(a, b) {
479 function getSelectedList(sel) {
482 for( var i = 0; i != sel.options.length; i++ ) {
483 if(sel.options[i].selected)
484 vals.push(sel.options[i].value);
490 function setEnterFunc(node, func) {
491 if(!(node && func)) return;
492 node.onkeydown = function(evt) {
493 if( userPressedEnter(evt)) func();
497 function iterate( arr, callback ) {
498 for( var i = 0; arr && i < arr.length; i++ )
505 /* taken directly from the JSAN util.date library */
506 /* but changed from the util.date.interval_to_seconds invocation,
507 because JSAN will assume the whole library is already loaded if
508 it sees that, and the staff client uses both this file and the
510 function interval_to_seconds( $interval ) {
512 $interval = $interval.replace( /and/, ',' );
513 $interval = $interval.replace( /,/, ' ' );
516 var results = $interval.match( /\s*\+?\s*(\d+)\s*(\w{1})\w*\s*/g);
517 for( var i = 0; i < results.length; i++ ) {
518 if(!results[i]) continue;
519 var result = results[i].match( /\s*\+?\s*(\d+)\s*(\w{1})\w*\s*/ );
520 if (result[2] == 's') $amount += result[1] ;
521 if (result[2] == 'm') $amount += 60 * result[1] ;
522 if (result[2] == 'h') $amount += 60 * 60 * result[1] ;
523 if (result[2] == 'd') $amount += 60 * 60 * 24 * result[1] ;
524 if (result[2] == 'w') $amount += 60 * 60 * 24 * 7 * result[1] ;
525 if (result[2] == 'M') $amount += ((60 * 60 * 24 * 365)/12) * result[1] ;
526 if (result[2] == 'y') $amount += 60 * 60 * 24 * 365 * result[1] ;
532 function openWindow( data ) {
534 var data = window.escape(
535 '<html><head><title></title></head><body>' + data + '</body></html>');
538 'data:text/html,' + data,
540 'chrome,resizable,width=700,height=500');
543 win = window.open('','', 'resizable,width=700,height=500,scrollbars=1');
544 win.document.body.innerHTML = data;
549 /* alerts the innerhtml of the node with the given id */
550 function alertId(id) {
552 if(node) alert(node.innerHTML);
555 function confirmId(id) {
557 if(node) return confirm(node.innerHTML);
561 function goBack() { history.back(); }
562 function goForward() { history.forward(); }
565 function uniquify(arr) {
568 for( var i = 0; i < arr.length; i++ ) {
570 if( ! grep( newarr, function(x) {return (x == item);}))
576 function contains(arr, item) {
577 for( var i = 0; i < arr.length; i++ )
578 if( arr[i] == item ) return true;
583 return (i && !(i+'').match(/f/i) );
587 /* builds a JS date object with the given info. The given data
588 has to be valid (e.g. months == 30 is not valid). Returns NULL on
590 Months are 1-12 (unlike the JS date object)
593 function buildDate( year, month, day, hours, minutes, seconds ) {
596 if(!month) month = 1;
598 if(!hours) hours = 0;
599 if(!minutes) minutes = 0;
600 if(!seconds) seconds = 0;
602 var d = new Date(year, month - 1, day, hours, minutes, seconds);
604 _debug('created date with ' +
605 (d.getYear() + 1900) +'-'+
606 (d.getMonth() + 1) +'-'+
614 (d.getYear() + 1900) == year &&
615 d.getMonth() == (month - 1) &&
616 d.getDate() == new Number(day) &&
617 d.getHours() == new Number(hours) &&
618 d.getMinutes() == new Number(minutes) &&
619 d.getSeconds() == new Number(seconds) ) {
626 function mkYearMonDay(date) {
627 if(!date) date = new Date();
628 var y = date.getYear() + 1900;
629 var m = (date.getMonth() + 1)+'';
630 var d = date.getDate()+'';
631 if(m.length == 1) m = '0'+m;
632 if(d.length == 1) d = '0'+d;
633 return y+'-'+m+'-'+d;