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;
73 /* Using setTimeout in the following function means that goTo is threaded,
74 and multiple calls to it will be processed indeterminately. Since goTo
75 should effectively end the page, we will only honor the first call. */
76 var goToHasRun = false;
83 /* setTimeout because ie sux */
84 setTimeout( function(){ location.href = url; }, 0 );
88 function removeChildren(dom) {
90 while(dom.childNodes[0])
91 dom.removeChild(dom.childNodes[0]);
94 function appendClear(node, child) {
95 if(typeof child =='string') child = text(child);
97 node.appendChild(child);
101 function instanceOf(object, constructorFunction) {
104 while (object != null) {
105 if (object == constructorFunction.prototype)
107 object = object.__proto__;
110 while(object != null) {
111 if( object instanceof constructorFunction )
113 object = object.__proto__;
120 /* ------------------------------------------------------------------------------------------- */
121 /* detect my browser */
122 var isMac, NS, NS4, NS6, IE, IE4, IEmac, IE4plus, IE5, IE5plus, IE6, IEMajor, ver4, Safari;
123 function detect_browser() {
125 isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
126 NS = (navigator.appName == "Netscape") ? true : false;
127 NS4 = (document.layers) ? true : false;
128 IE = (navigator.appName == "Microsoft Internet Explorer") ? true : false;
129 IEmac = ((document.all)&&(isMac)) ? true : false;
130 IE4plus = (document.all) ? true : false;
131 IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
132 IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
133 IE6 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 6.")!=-1)) ? true : false;
134 ver4 = (NS4 || IE4plus) ? true : false;
135 NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape')!=-1)?true:false;
136 Safari = navigator.userAgent.match(/Safari/);
138 IE5plus = IE5 || IE6;
142 var start = navigator.appVersion.indexOf("MSIE");
143 var end = navigator.appVersion.indexOf(".",start);
144 IEMajor = parseInt(navigator.appVersion.substring(start+5,end));
145 IE5plus = (IEMajor>=5) ? true : false;
149 /* ------------------------------------------------------------------------------------------- */
153 if(t == null) t = "";
154 return document.createTextNode(t);
157 function elem(name, attrs, txt) {
158 var e = document.createElement(name);
161 if( key == 'id') e.id = attrs[key];
162 else e.setAttribute(key, attrs[key]);
165 if (txt) e.appendChild(text(txt));
170 /* sel is the selector object, sets selected on the
171 option with the given value. case does not matter...*/
172 function setSelector( sel, value ) {
173 if(sel && value != null) {
174 for( var i = 0; i!= sel.options.length; i++ ) {
175 if( sel.options[i] ) {
176 var val = sel.options[i].value;
177 if( val == null || val == "" ) /* for IE */
178 val = sel.options[i].innerHTML;
179 value += ""; /* in case of number */
180 if( val && val.toLowerCase() == value.toLowerCase() ) {
181 sel.selectedIndex = i;
182 sel.options[i].selected = true;
191 function setSelectorRegex( sel, regex ) {
192 if(sel && regex != null) {
193 for( var i = 0; i!= sel.options.length; i++ ) {
194 if( sel.options[i] ) {
195 var val = sel.options[i].value;
196 if( val == null || val == "" ) /* for IE */
197 val = sel.options[i].innerHTML;
198 value += ""; /* in case of number */
199 if( val && val.match(regex) ) {
200 sel.selectedIndex = i;
201 sel.options[i].selected = true;
210 function getSelectorVal( sel ) {
211 if(!sel) return null;
212 var idx = sel.selectedIndex;
213 if( idx < 0 ) return null;
214 var o = sel.options[idx];
216 if(v == null) v = o.innerHTML;
220 function getSelectorName( sel ) {
221 var o = sel.options[sel.selectedIndex];
223 if(v == null || v == undefined || v == "") v = o.innerHTML;
227 function setSelectorByName( sel, name ) {
228 for( var o in sel.options ) {
229 var opt = sel.options[o];
230 if( opt.name == name || opt.innerHTML == name ) {
231 sel.selectedIndex = o;
237 function findSelectorOptByValue( sel, val ) {
238 for( var i = 0; i < sel.options.length; i++ ) {
239 var opt = sel.options[i];
240 if( opt.value == val ) return opt;
245 function debugSelector(sel) {
246 var s = 'Selector\n';
247 for( var i = 0; i != sel.options.length; i++ ) {
248 var o = sel.options[i];
249 s += "\t" + o.innerHTML + "\n";
254 function findParentByNodeName(node, name) {
255 while( ( node = node.parentNode) )
256 if (node.nodeName == name) return node;
260 /* returns only elements in nodes childNodes list, not sub-children */
261 function getElementsByTagNameFlat( node, name ) {
263 for( var e in node.childNodes ) {
264 var n = node.childNodes[e];
265 if( n && n.nodeName == name ) elements.push(n);
270 /* expects a tree with a id() method on each node and a
271 children() method to get to each node */
272 function findTreeItemById( tree, id ) {
273 if( tree.id() == id ) return tree;
274 for( var c in tree.children() ) {
275 var found = findTreeItemById( tree.children()[c], id );
276 if(found) return found;
281 /* returns null if none of the tests are true. returns sub-array of
282 matching array items otherwise */
283 function grep( arr, func ) {
285 if(!arr) return null;
286 if( arr.constructor == Array ) {
287 for( var i = 0; i < arr.length; i++ ) {
289 results.push(arr[i]);
292 for( var i in arr ) {
294 results.push(arr[i]);
297 if(results.length > 0) return results;
301 function ogrep( obj, func ) {
304 for( var i in obj ) {
310 if(found) return results;
314 function doSelectorActions(sel) {
315 if((IE || Safari) && sel) {
316 sel.onchange = function() {
317 var o = sel.options[sel.selectedIndex];
318 if(o && o.onclick) o.onclick()
323 /* if index < 0, the item is pushed onto the end */
324 function insertSelectorVal( selector, index, name, value, action, indent ) {
325 if( index < 0 ) index = selector.options.length;
327 for( var i = selector.options.length; i != index; i-- )
328 a[i] = selector.options[i-1];
330 var opt = setSelectorVal( selector, index, name, value, action, indent );
332 for( var i = index + 1; i < a.length; i++ )
333 selector.options[i] = a[i];
338 /* changes the value of the option at the specified index */
339 function setSelectorVal( selector, index, name, value, action, indent ) {
340 if(!indent || indent < 0) indent = 0;
341 indent = parseInt(indent);
346 var pre = elem("pre");
347 for( var i = 0; i != indent; i++ )
348 pre.appendChild(text(" "));
350 pre.appendChild(text(name));
351 option = new Option("", value);
352 selector.options[index] = option;
353 option.appendChild(pre);
356 indent = indent * 14;
357 option= new Option(name, value);
358 option.setAttribute("style", "padding-left: "+indent+'px;');
359 selector.options[index] = option;
360 if(action) option.onclick = action;
363 if(action) option.onclick = action;
368 /* split on spaces. capitalize the first /\w/ character in
370 function normalize(val) {
371 return val; /* disable me for now */
376 try {val = val.split(' ');} catch(E) {return val;}
379 for( var c = 0; c < val.length; c++) {
383 for(var x = 0; x != string.length; x++) {
386 var ch = string.charAt(x);
387 if(reg.exec(ch + "")) {
388 newVal += string.charAt(x).toUpperCase();
394 newVal += string.charAt(x).toLowerCase();
396 if(c < (val.length-1)) newVal += " ";
399 newVal = newVal.replace(/\s*\.\s*$/,'');
400 newVal = newVal.replace(/\s*\/\s*\/\s*$/,' / ');
401 newVal = newVal.replace(/\s*\/\s*$/,'');
407 /* returns true if n is null or stringifies to 'undefined' */
409 if( n == null || n == undefined || n.toString().toLowerCase() == "undefined"
410 || n.toString().toLowerCase() == "null" )
416 /* find nodes with an attribute of 'name' that equals nodeName */
418 function $n( root, nodeName ) { return findNodeByName(root,nodeName); }
420 function findNodeByName(root, nodeName) {
421 if( !root || !nodeName) return null;
423 if(root.nodeType != 1) return null;
425 if(root.getAttribute("name") == nodeName || root.name == nodeName )
428 var children = root.childNodes;
430 for( var i = 0; i != children.length; i++ ) {
431 var n = findNodeByName(children[i], nodeName);
439 /* truncates the string at 'size' characters and appends a '...' to the end */
440 function truncate(string, size) {
441 if(string && size != null &&
442 size > -1 && string.length > size)
443 return string.substr(0, size) + "... ";
448 /* style sheets must have a 'name' attribute for these functions to work */
449 function setActivateStyleSheet(name) {
451 for (i = 0; (a = document.getElementsByTagName ("link")[i]); i++) {
452 if (a.getAttribute ("rel").indexOf ("style") != -1 && a.getAttribute ("name")) {
454 if (a.getAttribute ("name").indexOf(name) != -1)
461 /* ----------------------------------------------------- */
463 function scaleFonts(type) {
470 if(!currentFontSize) currentFontSize = 'regular';
471 if(currentFontSize == 'regular' && type == 'regular' ) return;
472 if( currentFontSize == type ) return;
473 currentFontSize = type;
476 case "large": /* these are arbitrary.. but they seem to work ok in FF/IE */
483 document.getElementsByTagName('body')[0].style.fontSize = size;
484 for (i = 0; (a = document.getElementsByTagName ("td")[i]); i++) a.style.fontSize = size;;
485 for (i = 0; (a = document.getElementsByTagName ("div")[i]); i++) a.style.fontSize = ssize;
486 for (i = 0; (a = document.getElementsByTagName ("option")[i]); i++) a.style.fontSize = ssize;
487 for (i = 0; (a = document.getElementsByTagName ("li")[i]); i++) a.style.fontSize = ssize;
488 for (i = 0; (a = document.getElementsByTagName ("span")[i]); i++) a.style.fontSize = ssize;
489 for (i = 0; (a = document.getElementsByTagName ("select")[i]); i++) a.style.fontSize = ssize;
490 for (i = 0; (a = document.getElementsByTagName ("a")[i]); i++) a.style.fontSize = size2;
494 function sortWordsIgnoreCase(a, b) {
503 function getSelectedList(sel) {
506 for( var i = 0; i != sel.options.length; i++ ) {
507 if(sel.options[i].selected)
508 vals.push(sel.options[i].value);
514 function setEnterFunc(node, func) {
515 if(!(node && func)) return;
516 node.onkeydown = function(evt) {
517 if( userPressedEnter(evt)) func();
521 function iterate( arr, callback ) {
522 for( var i = 0; arr && i < arr.length; i++ )
529 /* taken directly from the JSAN util.date library */
530 /* but changed from the util.date.interval_to_seconds invocation,
531 because JSAN will assume the whole library is already loaded if
532 it sees that, and the staff client uses both this file and the
534 function interval_to_seconds( $interval ) {
536 $interval = $interval.replace( /and/, ',' );
537 $interval = $interval.replace( /,/, ' ' );
540 var results = $interval.match( /\s*\+?\s*(\d+)\s*(\w{1})\w*\s*/g);
541 for( var i = 0; i < results.length; i++ ) {
542 if(!results[i]) continue;
543 var result = results[i].match( /\s*\+?\s*(\d+)\s*(\w{1})\w*\s*/ );
544 if (result[2] == 's') $amount += result[1] ;
545 if (result[2] == 'm') $amount += 60 * result[1] ;
546 if (result[2] == 'h') $amount += 60 * 60 * result[1] ;
547 if (result[2] == 'd') $amount += 60 * 60 * 24 * result[1] ;
548 if (result[2] == 'w') $amount += 60 * 60 * 24 * 7 * result[1] ;
549 if (result[2] == 'M') $amount += ((60 * 60 * 24 * 365)/12) * result[1] ;
550 if (result[2] == 'y') $amount += 60 * 60 * 24 * 365 * result[1] ;
556 function openWindow( data ) {
558 var data = window.encodeURIComponent(
559 '<html><head><title></title></head><body>' + data + '</body></html>');
562 'data:text/html;charset=UTF-8,' + data,
564 'chrome,resizable,width=700,height=500');
567 win = window.open('','', 'resizable,width=700,height=500,scrollbars=1,chrome');
568 win.document.body.innerHTML = data;
573 /* alerts the innerhtml of the node with the given id */
574 function alertId(id) {
576 if(node) alert(node.innerHTML);
579 function alertIdText(id, text) {
583 alert(text + '\n\n' + node.innerHTML);
585 alert(node.innerHTML);
588 function confirmId(id) {
590 if(node) return confirm(node.innerHTML);
594 function goBack() { history.back(); }
595 function goForward() { history.forward(); }
598 function uniquify(arr) {
601 for( var i = 0; i < arr.length; i++ ) {
603 if( ! grep( newarr, function(x) {return (x == item);}))
609 function contains(arr, item) {
610 for( var i = 0; i < arr.length; i++ )
611 if( arr[i] == item ) return true;
616 return (i && !(i+'').match(/f/i) );
620 /* builds a JS date object with the given info. The given data
621 has to be valid (e.g. months == 30 is not valid). Returns NULL on
623 Months are 1-12 (unlike the JS date object)
626 function buildDate( year, month, day, hours, minutes, seconds ) {
629 if(!month) month = 1;
631 if(!hours) hours = 0;
632 if(!minutes) minutes = 0;
633 if(!seconds) seconds = 0;
635 var d = new Date(year, month - 1, day, hours, minutes, seconds);
637 _debug('created date with ' +
638 (d.getYear() + 1900) +'-'+
639 (d.getMonth() + 1) +'-'+
647 (d.getYear() + 1900) == year &&
648 d.getMonth() == (month - 1) &&
649 d.getDate() == new Number(day) &&
650 d.getHours() == new Number(hours) &&
651 d.getMinutes() == new Number(minutes) &&
652 d.getSeconds() == new Number(seconds) ) {
659 function mkYearMonDay(date) {
660 if(!date) date = new Date();
661 var y = date.getYear() + 1900;
662 var m = (date.getMonth() + 1)+'';
663 var d = date.getDate()+'';
664 if(m.length == 1) m = '0'+m;
665 if(d.length == 1) d = '0'+d;
666 return y+'-'+m+'-'+d;
670 function debugFMObject(obj) {
671 if(typeof obj != 'object' ) return obj;
672 _debug("---------------------");
673 var keys = fmclasses[obj.classname];
674 if(!keys) { _debug(formatJSON(js2JSON(obj))); return; }
677 for( var i = 0; i < keys.length; i++ ) {
679 while( key.length < 12 ) key += ' ';
680 var val = obj[keys[i]]();
681 if( typeof val == 'object' ) {
685 _debug(key+' = ' +val);
689 _debug("---------------------");
693 function getTableRows(tbody) {
695 if(!tbody) return rows;
697 var children = tbody.childNodes;
698 if(!children) return rows;
700 for(var i = 0; i < children.length; i++) {
701 var child = children[i];
702 if(child.nodeName.match(/^tr$/i))
708 function getObjectKeys(obj) {