// JS Utilities by Robert P. Levy, Copyright 2008 Robert P. Levy, see GNU Public License var browserType; if (document.layers) {browserType = "nn4"} if (document.all) {browserType = "ie"} if (window.navigator.userAgent.toLowerCase().match("gecko")) { browserType= "gecko" } function getById(id,doc) { if (!doc) doc = document; if (browserType == "gecko" ) el = doc.getElementById(id); else if (browserType == "ie") el = doc.getElementById(id); else el = doc.all[id]; return el; } function modById(id,fn,change) { el = getById(id); if (change) el=fn(el); else return fn(el); } // show and hide function hideVis(id) { modById(id, function (e) { e.style.visibility = "hidden"; }, true ) } function showVis(id) { modById(id, function (e) { e.style.visibility = "visible"; }, true ) } function hideDis(id) { modById(id, function (e) { e.style.display = "none"; }, true ) } function showDis(id) { modById(id, function (e) { e.style.display = "inline"; }, true ) } // assumes two two divs with a shared base name and the following suffixes var textDivSuf = "TextDiv"; var edDivSuf = "EdDiv"; function switchTextToEdit(id) { hideDis( id + textDivSuf ); showDis( id + edDivSuf ) } function switchEditToText(id) { hideDis( id + edDivSuf ); showDis( id + textDivSuf ) } // list processing function cdr(l) {return l.slice(1);} // String formatting function removeExtraSpaces(s) { s = s.replace(/\s+/,' '); return s;} function titleCase(s) { s = removeExtraSpaces(s.toLowerCase()); s = s.substr(0,1).toUpperCase()+s.substr(1); pos = s.indexOf(' ',1); while ( pos > 0 ) { s = s.substr(0,pos)+' '+s.substr(pos+1,1).toUpperCase()+s.substr(pos+2); pos = s.indexOf(' ',pos+1);} return s;} // form and DOM processing functions function checkBoxData(chkbx) { chkbxary=Array(); for (var i=0; i < chkbx.length; i++) if ( chkbx[i].checked ) chkbxary.push(chkbx[i].value); return chkbxary } function optionBoxData(opbx) { opbxary=Array(); for (var i=0; i < opbx.length; i++) if ( opbx[i].selected ) opbxary.push(opbx[i].value); return opbxary } function getSelected(opbx) { return opbx[opbx.selectedIndex].value } // inner html functions // note: refactor these 3 functions to share some of the same code // also apply this idea from http://domscripting.com/blog/display/99: //First of all, use the DOM to create a block level element—such as a div—using createElement. Then update the innerHTML property of this newly-created element. Finally, insert the updated element into the document using a DOM method like appendChild or insertBefore: //var newdiv = document.createElement("div"); //newdiv.innerHTML = xhr.responseText; //var container = document.getElementById("container"); //container.appendChild(newdiv); function getContent(id) { if (document.getElementById || document.all) { var el = document.getElementById? document.getElementById(id): document.all[id]; if (el && typeof el.innerHTML != "undefined") return el.innerHTML}} // change inner html function changeContent(id,shtml) { if (document.getElementById || document.all) { var el = document.getElementById? document.getElementById(id): document.all[id]; if (el && typeof el.innerHTML != "undefined") el.innerHTML = shtml;}} // change inner html function appendContent(id,shtml) { if (document.getElementById || document.all) { var el = document.getElementById? document.getElementById(id): document.all[id]; if (el && typeof el.innerHTML != "undefined") el.innerHTML = el.innerHTML + shtml;}} // AJAX tools // generate a request object function genRequest() { if (window.XMLHttpRequest) { req = new XMLHttpRequest(); } else if (window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP");} return req;} // AJAX request (HOF): make a request and then apply a function on the result function makeRequest(url,fun) { req=genRequest(); req.onreadystatechange=function() { if (req.readyState==4) { fun(req);}} req.open('get', url, true); req.send(null);} // pluralizer of the above -- make one cascading sequence of requests /* function makeRequests(urlList,funList) { makeRequest(urlList[0], function (req) { funList[0](req); // funList[0] is a closure if (urlList.length > 2) { // urlList is a closure too makeRequests(cdr(urlList),cdr(funList));} else { makeRequest(urlList[1],funList[1]);}});} */ /* alternate approach function makeRequests(urlList,funList) { req=genRequest(); req.onreadystatechange=function() { if (req.readyState==4) { funList[0](req); if (urlList.length > 2) { makeRequests(cdr(urlList),cdr(funList));} else { makeRequest(urlList[1],funList[1]);}}} req.open('get', urlList[0], true); req.send(null);} */ // simplified request function that applies the text directly // add later: report error as an alert, with optional div id for error report function requestChange(url,id) { makeRequest(url,function (r) { changeContent(id,r.responseText);});} // url loader using cookie instead of query string so as to maintain state consistently function loadUrl(url,variable,value,cookiepath) { createCookie(variable,value,30,cookiepath); window.location.href=url;} // various function nothing() {} // cookies // copied from some website, slightly modified function createCookie(name,value,days,path) { if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); } else var expires = ""; document.cookie = name+"="+value+expires+"; path="+path; } function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; } function eraseCookie(name) { createCookie(name,"",-1); }