 /* ModStylesheet Version 1.0 - Copyright 2009 Chris Beall */
var quietmode = false;   /* Global flag - Suppress error messages? 
                             quietmode: true or false. If true, most error messages are suppressed.
                          */

function MSShelp() {     /* Display invocation data as part of error message */
  return("\n\nInvocation requirements:\nSet global variable 'quietmode' to true or false.\nSet Parameters:\n\thitsheet:\t'all' - Modify all stylesheets.\n\t\t\t\t'enabled' - modify all enabled stylesheets.\n\t\t\t\t0 - n - Modify only stylesheet n (zero origin), regardles of disabled state.\n\tselector:  Single CSS selector to be modified.\n\tnewdata:   CSS property to be inserted at specified selector.\n"); 
}

function MSSBugAlert(text) {  /* Display error message unless suppressed */
  if (!quietmode) {
    alert(text);
    return;
  }
}

function MSSmodify(sheet,selector,property,newdata) {
      /* Adjust for non-standard browser terminology */       
  var theRules = new Array();
  if (document.styleSheets[sheet].cssRules) {
    theRules = document.styleSheets[sheet].cssRules;
  }
  else if (document.styleSheets[sheet].rules) {
    theRules = document.styleSheets[sheet].rules;
  }
    else {
      alert("Neither styleSheets([n].cssRules nor styleSheets[n].rules exists!");
      return;
    }
      /* For each matching selector, make the requested change */
  if (theRules.length > 0) {
    for (j=0;j < theRules.length;j++) {
      if (selector == theRules[j].selectorText) {
      theRules[j].style[property] = newdata;  
      }
    }
  }
}

      /* ModStylesheet invocation <--------------*********************** */   
function ModStylesheet(hitsheet,selector,property,newdata) {
      /* Parameters:
            hitsheet:  'all' - Modify all stylesheets.             
                       'enabled' - modify all enabled stylesheets. 
                       0 - n - Modify only stylesheet n (zero origin), regardles of disabled state.
            selector:  Single CSS selector to be modified.
            newdata:   CSS property to be inserted at specified selector.
         Restrictions:
          1. Does not work for grouped selectors in either the invocation or the DOM.
          2. Selector parameter must exactly match DOM selectorText, which may not be what appears in the style sheet.   
       */
      /* Do nothing if required UA support not present. */
  if (!document.implementation.hasFeature("CSS","2.0")) return; 
  if (!document.styleSheets) return;
      /* Test input parameters */
  if ((quietmode != true) & (quietmode != false)) {
    alert("ModStylesheet reports:\n Global variable 'quietmode'; must be set true or false."); 
    return;
  }
  if (typeof(hitsheet) == "undefined") {
    MSSBugAlert("ModStylesheet reports:\n Parameter 'hitsheet' is missing."+MSShelp());
    return;
  }
  if ((isNaN(hitsheet)) & (hitsheet != "all") & (hitsheet != "enabled")) {
    MSSBugAlert("ModStylesheet reports:\n Invalid parameter 'hitsheet' ("+hitsheet+"); must be a positive integer, 'all', or 'enabled'.");
    return;
  }
  if (hitsheet > (document.styleSheets.length-1) | (hitsheet < 0)) {
    MSSBugAlert("ModStylesheet reports:\n Invalid numeric parameter 'hitsheet' ("+hitsheet+"); must be 0 - "+(document.styleSheets.length-1)+",\nwhich is the current range of stylesheet indexes.");
    return;
  }
  if (typeof(selector) == "undefined") {
    MSSBugAlert("ModStylesheet reports:\n Parameter 'selector' is missing."+MSShelp());
    return;
  }
  if (selector.match(/,/)) {
    MSSBugAlert("ModStylesheet reports:\n Invalid character in parameter 'selector' ("+selector+"); must not contain comma.");
    return;
  }
  if (typeof(property) == "undefined") {
    MSSBugAlert("ModStylesheet reports:\n Parameter 'property' is missing."+MSShelp());
    return;
  }
  if (typeof(newdata) == "undefined") {
    MSSBugAlert("ModStylesheet reports:\n Parameter 'newdata' is missing."+MSShelp());
    return;
  }
       /* Loop through all stylesheets */
  if ((hitsheet == "all") | (hitsheet == "enabled")) {
    for (i=0;i <= document.styleSheets.length-1;i++) {
      /* Skip this sheet if it is disabled and we only want enabled ones */
      if ((hitsheet == 'enabled') & (document.styleSheets[i].disabled == true)) continue;
      /* Modify the current sheet */
      MSSmodify(i,selector,property,newdata);
    }
  } 
       /* or just do the one specified */
  else MSSmodify(hitsheet,selector,property,newdata);
}