/*
############################################################################
#
# Part of the Partnerships BC site code
# Copyright (C) 2008 Damon Harper
# usrbin design + programming - http://usrbin.ca/
#
# This program is governed by the agreed upon license between Damon Harper
# d.b.a. usrbin design + programming, and AMG Media Inc.  This program may
# be modified for internal use but may not be sold or otherwise distributed
# without express written permission from 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=trim_inside(node.className.replace(re, ' '));
}

function trim(string) {
  return string.replace(/^\s+/, '').replace(/\s+$/, '');
}

function trim_inside(string) {
  return trim(string).replace(/\s{2,}/, ' ');
}

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((trim(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';
  }
}

function setup_attribute_other(id) {
  $(document).ready(function() {
    var checkbox=$('#attribute-other-checkbox-'+id);
    var input=$('#attribute-other-'+id);
    if(checkbox && input) {
      var toggle_attribute=function(event) {
        if(checkbox.attr('checked')) {
          input.show();
          if(event)
            input.get(0).focus();
        } else {
          input.hide();
          input.attr('value', '');
        }
      }
      toggle_attribute();
      checkbox.click(toggle_attribute);
    }
  });
}

function setup_attribute_all(attribute) {
  $(document).ready(function() {
    $('#attribute-all-'+attribute).click(function() {
      $('.attribute-'+attribute).attr('checked', this.checked)
    });
    $('.attribute-'+attribute).click(function() {
      if(!this.checked)
        $('#attribute-all-'+attribute).attr('checked', false);
    });
    var checked=true;
    $('.attribute-'+attribute).each(function() {
      if(!$(this).attr('checked'))
        checked=false;
    });
    if(checked)
      $('#attribute-all-'+attribute).attr('checked', true);
  });
}
