/*
############################################################################
#
# BC Chiropractic Association site programming
# Copyright (C) 2001-2009 Damon Harper <usrbin@usrbin.ca>
# usrbin design + programming: http://usrbin.ca/
#
# This program is for internal use by the BC Chiropractic Association
# only, and may not be distributed in any way, shape or form without
# the express written permission of the copyright holder.
#
############################################################################
*/

function isDef(o) {
  return typeof(o)!='undefined';
}
function isStr(o) {
  return typeof(o)=='string';
}
function isBool(o) {
  return typeof(o)=='boolean';
}

function getById(id) {
  if(!isStr(id))
    return id;
  if(document.getElementById)
    return document.getElementById(id);
  if(document.all)
    return document.all[id];
  return null;
}

function getByClass(classname, node, tag) {
  if(node==null)
    node=document;
  if(tag==null)
    tag='*';
  var elements=new Array();
  var idx=0;
  var e=node.getElementsByTagName(tag);
  var re=new RegExp("(^|\\s)"+classname+"(\\s|$)");
  for(var i=0; i<e.length; i++) {
    if(re.test(e[i].className)) {
      elements[idx]=e[i];
      idx++;
    }
  }
  return elements;
}

function getByTagName(tag, node) {
  if(node==null)
    node=document;
  return node.getElementsByTagName(tag);
}

function hasClass(node, classname) {
  var re=new RegExp("(^|\\s)"+classname+"(\\s|$)");
  return re.test(node.className);
}

function addClass(node, classname) {
  var re=new RegExp("(^|\\s)"+classname+"(\\s|$)");
  if(!re.test(node.className))
    node.className+=(node.className ? ' ' : '')+classname;
}

function delClass(node, classname) {
  var re=new RegExp("(^|\\s)"+classname+"(\\s|$)", 'g');
  if(node.className)
    node.className=node.className.replace(re, ' ').replace(/^\s+/, '').replace(/\s+$/, '').replace(/\s{2,}/, ' ');
}

var _timeout_countchars=false;
function timeout_countchars(input, output, maxchars) {
  if(!_timeout_countchars) {
    function _timeout_countchars_function() {
      if(_timeout_countchars) {
        _timeout_countchars=false;
        return countchars(input, output, maxchars);
      }
    }
    _timeout_countchars=setTimeout(_timeout_countchars_function, 500);
  }
}

function countchars(input, output, maxchars) {
  _timeout_countchars=false;
  if(input=document.getElementById(input)) {
    if(output)
      output=document.getElementById(output);
    var value=input.value;
    value=value.replace(/^\s+/, '');
    value=value.replace(/\s+$/, '');
    value=value.replace(/\s+/, ' ');
    var chars=value.length;
    if(output) {
      var prefix=chars>maxchars ? '<span style="color: #ff0000;">' : '';
      var suffix=chars>maxchars ? '</span>' : '';
      output.innerHTML=prefix+chars+suffix;
    }
    return chars<=maxchars;
  }
  return false;
}

function toggle_blocks() {
  var message='';
  var matchclass=new RegExp("^(show|hide)if(not)?-([^=\\s]+)=(\\S*)$");
  var blocks=getByClass("(show|hide)if(not)?-\\S+");
  for(var i=0; i<blocks.length; i++) {
    var show='undef';
    var classes=blocks[i].className.split(/\s+/);
    for(var j=0; j<classes.length; j++) {
      var matches=matchclass.exec(classes[j]);
      if(matches) {
        var type=matches[1];
        var want=matches[2] ? false : true;
        var control=matches[3];
        var value=matches[4];
        message+=control+' '+type+' '+value+'\n';
        if(show=='undef')
          show=type=='hide';
        var obj=getById(control);
        var button;
        if(obj && obj.tagName=='SELECT') { // workaround for IE
          if((obj.options[obj.selectedIndex].value==value)==want)
            show=type!='hide';
        } else if(button=getById(control+'='+value)) {
          if((button && button.checked)==want)
            show=type!='hide';
        } else if(obj) {
          if((obj.value==value)==want)
            show=type!='hide';
        }
      }
    }
    if(show && show!='undef') {
      var tag=blocks[i].tagName;
      var display;
      if(tag=='SPAN')
        display='inline';
      else if(tag=='TR')
        display='table-row';
      else if(tag=='TD' || tag=='TH')
        display='table-cell';
      if(!display)
        display='block';
      try {
        blocks[i].style.display=display;
      } catch(e) {
        blocks[i].style.display='block'; // IE doesn't understand table-*
      }
    } else
      blocks[i].style.display='none';
  }
}
