/*************************************************

  www.emsudbury.ca :: Sudbury Emergency Medicine
  000.common/000-02.scripts/default.js
  common utilities scripts
  2003 :: v030124, rev030201, rev030222
  2005 :: v050607
  2009 :: v090827

 *************************************************/




//.....  AUTORUN/ONLOAD SCRIPT:
//.....  autoRun() executes all functions contained in autorunList$, UPON LOAD;
//.....  add functions by appending them to autorunList$ [ie autorunList$ += 'newFunction();'];
//.....  window.onload is over-ridden by body.onload, therefor if you use body.onload,
//.....     include your functions as arguments to runthis()
//.....       example: <body onload="runthis('self.focus();');">
//.....       example will execute self.focus(), followed by everything
//.....       in the autoRun list;

  // the autoRun script:
    function autoRun()
      {
      eval(autorunList$);
      }

  // set autoRun to run on load:
    window.onload = autoRun;
    var autorunList$ = (autorunList$)? autorunList$ : '';

  // alternate script for page-level inclusion [use: onload="runthis(funct1(); funct2(); ...;)" ]:
    function runthis(function$)
      {
      eval(function$);
      autoRun();
      }


//.....  FOCUS:
//.....  adds focus() to the autorunlist$;
//.....  if (for what reason(?)) some page should NOT get
//.....   focus on load, then self.focus(); needs to be removed
//.....   from autorunList$ by page-specific onload function;


  // check if autorunList$ exists and has value:
    var autorunList$ = (autorunList$)? autorunList$ : '';
  // add self.focus() to autorunList$ for autoRun() [see defaults.js]:
    autorunList$ += 'self.focus();';




//.....  DE-CLOAKING SCRIPT:
//.....  script runs onload;
//.....  searches document for span.ecloak nodes;
//.....  replaces span.ecloak.innerHTML with standard html markup <a href>;
//.....  script is invisible to non-scripted browsers;
//.....  if script fails or is ignored, then hard-coded human-readable address remains:

  // check if autorunList$ exists and has value:
    var autorunList$ = (autorunList$)? autorunList$ : '';
  // add cloaker() to autorunList$ for autoRun() [see defaults.js]:
    autorunList$ += 'cloaker();';

  function cloaker()
    {
    var spanslist$$ = document.getElementsByTagName('span');
      var regex$$ = new Array(/<!--[^>]*-->/gi,/[ ]*\[at\][ ]*/gi,/[ ]*\[dot\][ ]*/gi);
      var swap$$ = new Array('','@','.');
      var whitespace = /[ ]/g;

    for(var i=0; i<spanslist$$.length; i++)
      {
      if(spanslist$$[i].className.indexOf('ecloak')>(-1) && spanslist$$[i].innerHTML.indexOf('[at]') > (-1))
        {

      // capture cloaked textnode:
        var oldcode = spanslist$$[i].innerHTML;

      // split out query$ to preserve:
        var querysplit$$ = oldcode.split('(');
        var query$ = (querysplit$$[1])? querysplit$$[1].replace(/[")]/g,'') : '';   // " (dquote added for benefit of Textpad)
        query$ = query$.replace(/[ ]*=[ ]*/g,'=');

if(spanslist$$[i].parentNode.id == 'disclosure')
  {
  var uri = document.location.href;
  var underscore$ = '%0A[2] (please leave these lines in your message)%0A%0A%0A';
  var body$ = 'body=[1] mssgRefUri = ' + uri + underscore$;
  query$ += (query$)? '&' + body$ + '&subject=':'?' + body$;
  }

        query$ = escape(query$);


      // remove comments && swap out placeholder tokens:
        for(var ii=0; ii<regex$$.length; ii++)
          {
          querysplit$$[0] = querysplit$$[0].replace(regex$$[ii],swap$$[ii]);
          }

      // split out link$:
        var linksplit$$ = querysplit$$[0].split(':');
        var thelink = (linksplit$$[1])? linksplit$$[0] : linksplit$$[0].replace(whitespace,'');

      // assemble href:
        var theaddr = (linksplit$$[1])? linksplit$$[1].replace(whitespace,'') : linksplit$$[0].replace(whitespace,'');
        if(query$) theaddr += ('?' + query$);


      // assemble replacement node:
        var newcode = '<a h';
          newcode += 'ref="mai';
          newcode += 'lto:' + theaddr + '">' + thelink + '<\/';
          newcode += 'a>';
        var newclass = spanslist$$[i].className.replace('ecloak','uncloaked');

      // update textnode:
        spanslist$$[i].innerHTML = newcode;

      // update class:
        spanslist$$[i].className = newclass;

        }
      }
    }


//***** general functions:  ***********************


//... for dating revisions, based on server's lastModified value:

function wasRevised()
   {
   var thisdate = new Date(document.lastModified);
   var thismonth = thisdate.getMonth();
   var thisyear = thisdate.getYear();
   var arrMonths = new Array('Jan','Feb','Mar','Apr','May','June','July','Aug','Sept','Oct','Nov','Dec');
   if (thisyear < 1000) thisyear += 1900;
   document.write('revised ' +thisdate.getDate()+ '-' +arrMonths[thismonth]+ '-' +thisyear);
   }



//... for [more] precisely sizing containing element to window height:

function setheight(thiselement,windowreduction)
  {
  if(!windowreduction || windowreduction != 0) var windowreduction = 150;
    /* ----------------------------------------------
    |  windowreduction is the size (in px) to crop   |
    |  from the browser's current window height in   |
    |  order to allow for template, etc.             |
     ---------------------------------------------- */
  var htmlObj = document.getElementsByTagName('html')[0];
  var thisObj = document.getElementById(thiselement);
  thisObj.style.minHeight = (htmlObj.clientHeight - windowreduction)+'px';
  if(navigator.appName.indexOf('Microsoft')>-1) thisObj.style.height = (htmlObj.clientHeight - windowreduction)+'px';
  }


/* **************** deprecated:
  function setheight(thiselement,windowreduction)
    {
    if(!windowreduction && windowreduction != 0) var windowreduction = 115;

    var htmlObj = document.getElementsByTagName('html')[0];
    var thisObj = document.getElementById(thiselement);
    thisObj.style.height = (htmlObj.clientHeight - windowreduction)+'px';
    }
**************** */


//... standard 600px popup window:

var newwin = false;
function popup650(url,name)
   {
   newwin = window.open (url,name,'width=650,height=575,resizable,scrollbars');
   newwin.focus();
   }


// .......... Soft Dog WebWorks : webmanager.emsudbury[at]thesoftdog[dot]com .......... //

