/**
 * Change the CSS class name of an element from fromClass to toClass
 * @param element html id, if string; or can be element reference
 */
function changeClass(element, fromClass, toClass) {
   var target;

   if (!document.getElementById) {
       return false;
   }

   if ((typeof element) == 'object') {
       target = element;
   }
   else {
       target = document.getElementById(element);
   }

   if(target == null) {
       return false;
   }

   //  word boundary or a punctuation set
   var bound = "(\\b|[:_-])";
   var regexp = new RegExp(bound + fromClass + bound);

   target.className = target.className.replace(regexp, toClass);   
}

/**
 * Element has one of their classes in className switched from classA
 * to classB (if classname contained classA), or classB to classA if
 * className contained classB
 *  
 * @param element html id, if string; or can be element reference
 */
function toggleClass(element, classA, classB) {
   var targetElement;

   if (!document.getElementById) {
       return false;
   }

   if ((typeof element) == 'object') {
       targetElement = element;
   }
   else {
       targetElement = document.getElementById(element);
   }

   if (targetElement == null) { 
       return false;
   }

   //  word boundary or a punctuation set
   var bound = "(\\b|[:_-])";
   var regexp = new RegExp(bound + classA + bound);

   // classname has classA in it; so switch it with B
   if (targetElement.className.match(regexp) != null) { 
       changeClass(targetElement, classA, classB);
   }
   // otherwise, switch instance of classB with classA
   else {
       changeClass(targetElement, classB, classA);
   }

}
