// Global Ajax indicators
// Registers handlers for a global ajax indicator
//------------------------------------------------------------
Ajax.Responders.register({ 
 onCreate: function() { 
   if (Ajax.activeRequestCount > 0) 
     Element.show('ajax_indicator'); 
 }, 
 onComplete: function() { 
   if (Ajax.activeRequestCount == 0) 
     Element.hide('ajax_indicator'); 
 } 
}); 

// inspired by article http://joyeur.com/2006/04/07/joyful-javascript
//   checkbox_all_items('select_all', 'input.selectable[type=checkbox] ')
//------------------------------------------------------------
function check_checkboxes(item_id, class_name) {
  obj = document.getElementById('select_all')
  state = obj.checked
  if (state == true) {
    $$('input.selectable[type=checkbox] ').each(function(input){
      input.checked = true;
    });
  } else {
    $$('input.selectable[type=checkbox] ').each(function(input){
      input.checked = false;
    });    
  }
}

// disable all items with class_name if the checkbox is checked
//   disable_inputs('my_check_box_id', '.class_name')
//------------------------------------------------------------
function disable_inputs(item_id, class_name) {
  obj = document.getElementById(item_id)
  state = obj.checked
  if (state == true) {
    $$(class_name).each(function(input){
      input.disabled = false;
    });
  } else {
    $$(class_name).each(function(input){
      input.disabled = true;
    });    
  }
}

// disable all items with class_name if the checkbox is checked
//   disable_inputs('my_check_box_id', '.class_name')
//------------------------------------------------------------
function hide_inputs(item_id, class_name) {
  obj = document.getElementById(item_id)
  state = obj.checked
  if (state == true) {
    $$(class_name).each(function(input){
      input.show();
    });
  } else {
    $$(class_name).each(function(input){
      input.hide();
    });    
  }
}


// Resize the textarea down as text is typed in.  You need to 
// add onkeyup="size_textarea(this);" to the textarea tag.
// If you want it resized when it gets focus via the mouse add
// onclick="size_textarea(this);"
// Reference: http://www.felgall.com/jstip45.htm
//------------------------------------------------------------
function size_textarea(t) {
  a = t.value.split('\n');
  b = 1;
  for ( x= 0; x < a.length; x++) {
    if (a[x].length >= t.cols) b+= Math.floor(a[x].length/t.cols);
  }
  b += a.length;
  if (b > t.rows) t.rows = b;
}

//------------------------------------------------------------
var ExpandView = {
	toggle: function(update_id) {
		icon = $(update_id + '_expand_icon');

	  if (icon.className == 'expand') {
	    icon.className = 'expand_activated';
		} else if (icon.className == 'expand_activated') {
	    icon.className = 'expand';
	  }
	}
}
