// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


function toggle_check_all(element) {
  list_form = $(element).up("form");
  //list_form = $(list_form).up();
  //while(typeof list_form != 'undefined' && list_form.tagName != 'FORM') {
  //  list_form = $(list_form).up();
  //}
  Form.getInputs(list_form, "checkbox","other_id[]").each(function(e) {
    
    e.checked=$(element).checked;
    select_related_user_checkbox(e);
  });
}

function uncheck_all(element) {
  list_form = $(element).up("form");
  //list_form  = $(list_form).up();
  //while(typeof list_form != 'undefined' && list_form.tagName != 'FORM') {
  //  list_form = $(list_form).up();
  //}
  Form.getInputs(list_form, "checkbox","other_id[]").each(function(e) {
    e.checked=false;
    select_related_user_checkbox(e);
  });

}

function uncheck_all_checkboxes_complete_site(){
	for(a=0;a<self.document.forms.length;a++){
		list_form = self.document.forms[a];
	  //list_form  = $(list_form).up();
	  //while(typeof list_form != 'undefined' && list_form.tagName != 'FORM') {
	  //  list_form = $(list_form).up();
	  //}
	  Form.getInputs(list_form, "checkbox","other_id[]").each(function(e) {
	    e.checked=false;
	    select_related_user_checkbox(e);
	  });
		
	}
	
	
	
}



function get_user_id_string(element) {
  list_form = $(element).up("form");
  //while(typeof list_form != 'undefined' && list_form.tagName != 'FORM') {
  //  list_form = $(list_form).up();
  //}

  ret =  Form.getInputs(list_form, "checkbox","id[]").collect(function(e){ return Form.Element.serialize(e); }).without('id[]=undefined').without(undefined);
	//uncheck_all(element);
	
  //console.log(ret);
  return ret.join("&");

}

function get_and_set_user_id_string(element, target) {
  target = document.getElementById(target);
	list_form = $(element).up("form");
  //while(typeof list_form != 'undefined' && list_form.tagName != 'FORM') {
  //  list_form = $(list_form).up();
  //}

  ret = Form.getInputs(list_form, "checkbox","id[]");
	ret = ret.collect(function(e){ return Form.Element.serialize(e); });
	ret = ret.without('id[]=undefined').without(undefined);
	//uncheck_all(element);
	
  //console.log(ret);
  target.value= ret.join("&");

}


function get_other_id_string(element) {
  list_form = $(element).up("form");
  //while(typeof list_form != 'undefined' && list_form.tagName != 'FORM') {
  //  list_form = $(list_form).up();
  //}
  ret = Form.getInputs(list_form, "checkbox","other_id[]").collect(function(e){ return "id[]="+ Form.Element.getValue(e);} ).without('id[]=undefined').without(undefined);
  //console.log(ret);
  return ret.join("&");
}


function select_related_user_checkbox(element) {
  element = $(element);
  if(element.tagName != "INPUT")
    return false;
  next_td = element.up().next();
	prev_tr = element.up().up();
	if (element.checked) {
		prev_tr.style.backgroundColor = "#ffcc00";
	} else {
		prev_tr.style.backgroundColor = "#ffffff";
	}
	
  check_box = $A(next_td.childNodes).find(function(value,index){ return (value.tagName == "INPUT" && value.type=="checkbox") });
  check_box.checked = element.checked;
}

// global AJAX Handlers 
// currently only to show the spinner for any Prototype-AJAX Request
var globalAjaxHandler = {
  onCreate: function() {
     Element.show('ajax_loading_message');
     if(navigator.appVersion.match(/\bMSIE\b/)) {
         $('ajax_loading_message').style.top= document.documentElement.scrollTop + "px";
     }
  },
  onComplete: function()  {
      if(Ajax.activeRequestCount == 0) {
          Element.hide('ajax_loading_message');
       }
  },
  onFailure: function() {
      alert('OOPS, es ist etwas komisches passiert. Bitte nochmal versuchen... ;)');
      Element.hide('ajax_loading_message');
  }
};
Ajax.Responders.register(globalAjaxHandler);
// end AJAX handlers