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 redirect(url) {
  document.location=url;
}

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((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 exclude_packages(obj) {
  if(obj.checked) {
    var checkboxes=getByClass('event-package', null, 'input');
    for(var i=0; i<checkboxes.length; i++) {
      if(checkboxes[i].name!=obj.name)
        checkboxes[i].checked=false;
    }
  }
}

function exclude_checkboxes(obj, ids) {
  if(obj.checked) {
    var cb;
    ids=ids.split(' ');
    for(var i in ids)
      if(ids[i].length && (cb=getById('event'+ids[i]+'=registered')))
        cb.checked=false;
  }
}

function include_checkboxes(obj, ids) {
  if(obj.checked) {
    var cb;
    ids=ids.split(' ');
    for(var i in ids)
      if(ids[i].length && (cb=getById('event'+ids[i]+'=registered')))
        cb.checked=true;
  }
}

function init_menu() {
  var menu;
  if(document.all && document.getElementById && (menu=getById('menu'))) {
    for(var i=0; i<menu.childNodes.length; i++) {
      var node=menu.childNodes[i];
      if(node.nodeName=="LI") {
        node.onmouseover=function() {
          this.className+=" over";
        };
        node.onmouseout=function() {
          this.className=this.className.replace(" over", "");
        };
      }
    }
  }
}

function _countwords(input, output, maxwords, style) {
  var words=input.value.replace(/^\s+/, '').replace(/\s+$/, '');
  if(words.length)
    words=words.split(/\s+/).length;
  else
    words=0;
  if(words>maxwords && style)
    words='<span style="'+style+';">'+words+'</span>';
  output.innerHTML=words;
}

function countwords(input, output, maxwords, style) {
  input=getById(input);
  output=getById(output);
  if(input && output) {
    _countwords(input, output, maxwords, style);
    var _timeout_countwords=false;
    input.onchange=function() {
      _countwords(input, output, maxwords, style);
    };
    function _timeout_countwords_func() {
      if(_timeout_countwords) {
        _countwords(input, output, maxwords, style);
        _timeout_countwords=false;
      }
    }
    input.onkeyup=function() {
      if(!_timeout_countwords)
        _timeout_countwords=setTimeout(_timeout_countwords_func, 500);
    };
  }
}

function getform(form) {
  if(form && (typeof form)=='object')
    return form;
  if(!form)
    form='default';
  return document.forms[form];
}

function team_input(form) {
  form=getform(form);
  var team_change=form.elements['team_change'];
  if(!form || !team_change)
    alert('Error in team_input function!');
  team_change.value=1;
  form.submit();
  return false;
}

function please_wait() {
  if($('#please-wait').length) {
    window.setTimeout(function() {
      if(typeof document.body.style.maxHeight==="undefined") { //IE6
        $('body','html').css({height: '100%', width: '100%'});
        $('html').css('overflow', 'hidden');
        $('body').append('<iframe id="blackout-iefix"></iframe>');
      }
      $('body').append('<div id="blackout-mask"></div>');
      $('#please-wait').css({display: 'block'});
    }, 1500);
  }
  return true;
}

function setup_menu() {
  $('.menu').each(function() {
    var title=$(this).find('.title');
    var body=$(this).find('.body');
    var body_collapsed=$(this).find('.body-collapsed');
    if(title.length && body.length) {
      title.click(function() {
        if(body.is(':visible')) {
          body.slideUp('fast', function() { body_collapsed.show(); });
          title.removeClass('displayed');
        } else {
          body_collapsed.hide();
          body.slideDown('normal');
          title.addClass('displayed');
        }
        return false;
      });
    }
  });
}

$(document).ready(function() {
  toggle_blocks();
  setup_menu();
});

