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) {
87 if(typeof child =='string') child = text(child);
89 node.appendChild(child);
93 function instanceOf(object, constructorFunction) {
96 while (object != null) {
97 if (object == constructorFunction.prototype)
99 object = object.__proto__;
102 while(object != null) {
103 if( object instanceof constructorFunction )
105 object = object.__proto__;
112 /* ------------------------------------------------------------------------------------------- */
113 /* detect my browser */
114 var isMac, NS, NS4, NS6, IE, IE4, IEmac, IE4plus, IE5, IE5plus, IE6, IEMajor, ver4;
115 function detect_browser() {
117 isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
118 NS = (navigator.appName == "Netscape") ? true : false;
119 NS4 = (document.layers) ? true : false;
120 IE = (navigator.appName == "Microsoft Internet Explorer") ? true : false;
121 IEmac = ((document.all)&&(isMac)) ? true : false;
122 IE4plus = (document.all) ? true : false;
123 IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
124 IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
125 IE6 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 6.")!=-1)) ? true : false;
126 ver4 = (NS4 || IE4plus) ? true : false;
127 NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape')!=-1)?true:false;
129 IE5plus = IE5 || IE6;
133 var start = navigator.appVersion.indexOf("MSIE");
134 var end = navigator.appVersion.indexOf(".",start);
135 IEMajor = parseInt(navigator.appVersion.substring(start+5,end));
136 IE5plus = (IEMajor>=5) ? true : false;
140 /* ------------------------------------------------------------------------------------------- */
144 if(t == null) t = "";
145 return document.createTextNode(t);
148 function elem(name, attrs, txt) {
149 var e = document.createElement(name);
152 if( key == 'id') e.id = attrs[key];
153 else e.setAttribute(key, attrs[key]);
156 if (txt) e.appendChild(text(txt));
161 /* sel is the selector object, sets selected on the
162 option with the given value. case does not matter...*/
163 function setSelector( sel, value ) {
164 if(sel && value != null) {
165 for( var i = 0; i!= sel.options.length; i++ ) {
166 if( sel.options[i] ) {
167 var val = sel.options[i].value;
168 if( val == null || val == "" ) /* for IE */
169 val = sel.options[i].innerHTML;
170 value += ""; /* in case of number */
171 if( val && val.toLowerCase() == value.toLowerCase() ) {
172 sel.selectedIndex = i;
173 sel.options[i].selected = true;
182 function setSelectorRegex( sel, regex ) {
183 if(sel && regex != null) {
184 for( var i = 0; i!= sel.options.length; i++ ) {
185 if( sel.options[i] ) {
186 var val = sel.options[i].value;
187 if( val == null || val == "" ) /* for IE */
188 val = sel.options[i].innerHTML;
189 value += ""; /* in case of number */
190 if( val && val.match(regex) ) {
191 sel.selectedIndex = i;
192 sel.options[i].selected = true;
201 function getSelectorVal( sel ) {
202 if(!sel) return null;
203 var idx = sel.selectedIndex;
204 if( idx < 0 ) return null;
205 var o = sel.options[idx];
207 if(v == null) v = o.innerHTML;
211 function getSelectorName( sel ) {
212 var o = sel.options[sel.selectedIndex];
214 if(v == null || v == undefined || v == "") v = o.innerHTML;
218 function setSelectorByName( sel, name ) {
219 for( var o in sel.options ) {
220 var opt = sel.options[o];
221 if( opt.name == name || opt.innerHTML == name ) {
222 sel.selectedIndex = o;
228 function findSelectorOptByValue( sel, val ) {
229 for( var i = 0; i < sel.options.length; i++ ) {
230 var opt = sel.options[i];
231 if( opt.value == val ) return opt;
236 function debugSelector(sel) {
237 var s = 'Selector\n';
238 for( var i = 0; i != sel.options.length; i++ ) {
239 var o = sel.options[i];
240 s += "\t" + o.innerHTML + "\n";
245 function findParentByNodeName(node, name) {
246 while( ( node = node.parentNode) )
247 if (node.nodeName == name) return node;
251 /* returns only elements in nodes childNodes list, not sub-children */
252 function getElementsByTagNameFlat( node, name ) {
254 for( var e in node.childNodes ) {
255 var n = node.childNodes[e];
256 if( n && n.nodeName == name ) elements.push(n);
261 /* expects a tree with a id() method on each node and a
262 children() method to get to each node */
263 function findTreeItemById( tree, id ) {
264 if( tree.id() == id ) return tree;
265 for( var c in tree.children() ) {
266 var found = findTreeItemById( tree.children()[c], id );
267 if(found) return found;
272 /* returns null if none of the tests are true. returns sub-array of
273 matching array items otherwise */
274 function grep( arr, func ) {
276 if(!arr) return null;
277 if( arr.constructor == Array ) {
278 for( var i = 0; i < arr.length; i++ ) {
280 results.push(arr[i]);
283 for( var i in arr ) {
285 results.push(arr[i]);
288 if(results.length > 0) return results;
292 function ogrep( obj, func ) {
295 for( var i in obj ) {
301 if(found) return results;
305 function doSelectorActions(sel) {
307 sel.onchange = function() {
308 var o = sel.options[sel.selectedIndex];
309 if(o && o.onclick) o.onclick()
314 /* if index < 0, the item is pushed onto the end */
315 function insertSelectorVal( selector, index, name, value, action, indent ) {
316 if( index < 0 ) index = selector.options.length;
318 for( var i = selector.options.length; i != index; i-- )
319 a[i] = selector.options[i-1];
321 var opt = setSelectorVal( selector, index, name, value, action, indent );
323 for( var i = index + 1; i < a.length; i++ )
324 selector.options[i] = a[i];
329 /* changes the value of the option at the specified index */
330 function setSelectorVal( selector, index, name, value, action, indent ) {
331 if(!indent || indent < 0) indent = 0;
332 indent = parseInt(indent);
337 var pre = elem("pre");
338 for( var i = 0; i != indent; i++ )
339 pre.appendChild(text(" "));
341 pre.appendChild(text(name));
342 option = new Option("", value);
343 selector.options[index] = option;
344 option.appendChild(pre);
347 indent = indent * 14;
348 option= new Option(name, value);
349 option.setAttribute("style", "padding-left: "+indent+'px;');
350 selector.options[index] = option;
351 if(action) option.onclick = action;
354 if(action) option.onclick = action;
359 /* split on spaces. capitalize the first /\w/ character in
361 function normalize(val) {
362 return val; /* disable me for now */
367 try {val = val.split(' ');} catch(E) {return val;}
370 for( var c = 0; c < val.length; c++) {
374 for(var x = 0; x != string.length; x++) {
377 var ch = string.charAt(x);
378 if(reg.exec(ch + "")) {
379 newVal += string.charAt(x).toUpperCase();
385 newVal += string.charAt(x).toLowerCase();
387 if(c < (val.length-1)) newVal += " ";
390 newVal = newVal.replace(/\s*\.\s*$/,'');
391 newVal = newVal.replace(/\s*\/\s*\/\s*$/,' / ');
392 newVal = newVal.replace(/\s*\/\s*$/,'');
398 /* returns true if n is null or stringifies to 'undefined' */
400 if( n == null || n == undefined || n.toString().toLowerCase() == "undefined"
401 || n.toString().toLowerCase() == "null" )
407 /* find nodes with an attribute of 'name' that equals nodeName */
409 function $n( root, nodeName ) { return findNodeByName(root,nodeName); }
411 function findNodeByName(root, nodeName) {
412 if( !root || !nodeName) return null;
414 if(root.nodeType != 1) return null;
416 if(root.getAttribute("name") == nodeName || root.name == nodeName )
419 var children = root.childNodes;
421 for( var i = 0; i != children.length; i++ ) {
422 var n = findNodeByName(children[i], nodeName);
430 /* truncates the string at 'size' characters and appends a '...' to the end */
431 function truncate(string, size) {
432 if(string && size != null &&
433 size > -1 && string.length > size)
434 return string.substr(0, size) + "... ";
439 /* style sheets must have a 'name' attribute for these functions to work */
440 function setActivateStyleSheet(name) {
442 for (i = 0; (a = document.getElementsByTagName ("link")[i]); i++) {
443 if (a.getAttribute ("rel").indexOf ("style") != -1 && a.getAttribute ("name")) {
445 if (a.getAttribute ("name").indexOf(name) != -1)
452 /* ----------------------------------------------------- */
454 function scaleFonts(type) {
461 if(!currentFontSize) currentFontSize = 'regular';
462 if(currentFontSize == 'regular' && type == 'regular' ) return;
463 if( currentFontSize == type ) return;
464 currentFontSize = type;
467 case "large": /* these are arbitrary.. but they seem to work ok in FF/IE */
474 document.getElementsByTagName('body')[0].style.fontSize = size;
475 for (i = 0; (a = document.getElementsByTagName ("td")[i]); i++) a.style.fontSize = size;;
476 for (i = 0; (a = document.getElementsByTagName ("div")[i]); i++) a.style.fontSize = ssize;
477 for (i = 0; (a = document.getElementsByTagName ("option")[i]); i++) a.style.fontSize = ssize;
478 for (i = 0; (a = document.getElementsByTagName ("li")[i]); i++) a.style.fontSize = ssize;
479 for (i = 0; (a = document.getElementsByTagName ("span")[i]); i++) a.style.fontSize = ssize;
480 for (i = 0; (a = document.getElementsByTagName ("select")[i]); i++) a.style.fontSize = ssize;
481 for (i = 0; (a = document.getElementsByTagName ("a")[i]); i++) a.style.fontSize = size2;
485 function sortWordsIgnoreCase(a, b) {
494 function getSelectedList(sel) {
497 for( var i = 0; i != sel.options.length; i++ ) {
498 if(sel.options[i].selected)
499 vals.push(sel.options[i].value);
505 function setEnterFunc(node, func) {
506 if(!(node && func)) return;
507 node.onkeydown = function(evt) {
508 if( userPressedEnter(evt)) func();
512 function iterate( arr, callback ) {
513 for( var i = 0; arr && i < arr.length; i++ )
520 /* taken directly from the JSAN util.date library */
521 /* but changed from the util.date.interval_to_seconds invocation,
522 because JSAN will assume the whole library is already loaded if
523 it sees that, and the staff client uses both this file and the
525 function interval_to_seconds( $interval ) {
527 $interval = $interval.replace( /and/, ',' );
528 $interval = $interval.replace( /,/, ' ' );
531 var results = $interval.match( /\s*\+?\s*(\d+)\s*(\w{1})\w*\s*/g);
532 for( var i = 0; i < results.length; i++ ) {
533 if(!results[i]) continue;
534 var result = results[i].match( /\s*\+?\s*(\d+)\s*(\w{1})\w*\s*/ );
535 if (result[2] == 's') $amount += result[1] ;
536 if (result[2] == 'm') $amount += 60 * result[1] ;
537 if (result[2] == 'h') $amount += 60 * 60 * result[1] ;
538 if (result[2] == 'd') $amount += 60 * 60 * 24 * result[1] ;
539 if (result[2] == 'w') $amount += 60 * 60 * 24 * 7 * result[1] ;
540 if (result[2] == 'M') $amount += ((60 * 60 * 24 * 365)/12) * result[1] ;
541 if (result[2] == 'y') $amount += 60 * 60 * 24 * 365 * result[1] ;
547 function openWindow( data ) {
549 var data = window.escape(
550 '<html><head><title></title></head><body>' + data + '</body></html>');
553 'data:text/html,' + data,
555 'chrome,resizable,width=700,height=500');
558 win = window.open('','', 'resizable,width=700,height=500,scrollbars=1');
559 win.document.body.innerHTML = data;
564 /* alerts the innerhtml of the node with the given id */
565 function alertId(id) {
567 if(node) alert(node.innerHTML);
570 function alertIdText(id, text) {
574 alert(text + '\n\n' + node.innerHTML);
576 alert(node.innerHTML);
579 function confirmId(id) {
581 if(node) return confirm(node.innerHTML);
585 function goBack() { history.back(); }
586 function goForward() { history.forward(); }
589 function uniquify(arr) {
592 for( var i = 0; i < arr.length; i++ ) {
594 if( ! grep( newarr, function(x) {return (x == item);}))
600 function contains(arr, item) {
601 for( var i = 0; i < arr.length; i++ )
602 if( arr[i] == item ) return true;
607 return (i && !(i+'').match(/f/i) );
611 /* builds a JS date object with the given info. The given data
612 has to be valid (e.g. months == 30 is not valid). Returns NULL on
614 Months are 1-12 (unlike the JS date object)
617 function buildDate( year, month, day, hours, minutes, seconds ) {
620 if(!month) month = 1;
622 if(!hours) hours = 0;
623 if(!minutes) minutes = 0;
624 if(!seconds) seconds = 0;
626 var d = new Date(year, month - 1, day, hours, minutes, seconds);
628 _debug('created date with ' +
629 (d.getYear() + 1900) +'-'+
630 (d.getMonth() + 1) +'-'+
638 (d.getYear() + 1900) == year &&
639 d.getMonth() == (month - 1) &&
640 d.getDate() == new Number(day) &&
641 d.getHours() == new Number(hours) &&
642 d.getMinutes() == new Number(minutes) &&
643 d.getSeconds() == new Number(seconds) ) {
650 function mkYearMonDay(date) {
651 if(!date) date = new Date();
652 var y = date.getYear() + 1900;
653 var m = (date.getMonth() + 1)+'';
654 var d = date.getDate()+'';
655 if(m.length == 1) m = '0'+m;
656 if(d.length == 1) d = '0'+d;
657 return y+'-'+m+'-'+d;
661 function debugFMObject(obj) {
662 if(typeof obj != 'object' ) return obj;
663 _debug("---------------------");
664 var keys = fmclasses[obj.classname];
665 if(!keys) { _debug(formatJSON(js2JSON(obj))); return; }
668 for( var i = 0; i < keys.length; i++ ) {
670 while( key.length < 12 ) key += ' ';
671 var val = obj[keys[i]]();
672 if( typeof val == 'object' ) {
676 _debug(key+' = ' +val);
680 _debug("---------------------");
684 function getTableRows(tbody) {
686 if(!tbody) return rows;
688 var children = tbody.childNodes;
689 if(!children) return rows;
691 for(var i = 0; i < children.length; i++) {
692 var child = children[i];
693 if(child.nodeName.match(/^tr$/i))
699 function getObjectKeys(obj) {