function toggleall(e,sube)
{
  if (e.checked) {
    selAll(sube);
  }
  else {
    deselAll(sube);
  }
}

//funciona igual que toggleall pero no es guia per un checkbox concret sino per si estan tots activats en el camp
//  - si estan tots activats, desactiva
//  - si no estan tots activats, activa els que faltin
function togglelite(field)
{
  if (!AllChecked(field)) {
    selAll(field);
  }
  else {
    deselAll(field);
  }
}

function toggle(sube,supere)
{
  supere.checked = AllChecked(sube);
}

function AllChecked(field)
{
  // Problems with a single checkbox
  if (field.type == 'checkbox') {return field.checked;}
  for (var i=0; i<field.length; i++) {
    if (!field[i].checked) {return false;}
  }
  return true;
}

function selAll(field)
{
  // Problems with a single checkbox
  if (field.type == 'checkbox') {
    field.checked = true
  }
  else {
    for (var i=0; i<field.length; i++){field[i].checked = true}
  }
}

function deselAll(field)
{
  // Problems with a single checkbox
  if (field.type == 'checkbox') {
    field.checked = false
  }
  else {
    for (var i=0; i<field.length; i++){ field[i].checked = false}
  }
}

function invSel(field)
{
  for (var i=0; i<field.length; i++){ field[i].checked = !field[i].checked}
}

/* BEGIN: Funcions per poder tenir la capçalera de cerca (resultsBegin) duplicada al final de la pàgina */
function getResBegTopOrBottom(quintinc){
  if (quintinc=='_top'){
    return '_bottom';
  }else if (quintinc=='_bottom'){
    return '_top';
  }else{
    return '';
  }
}
function switchSearchOpts(elemento){
  var idchk = elemento.id;
  var nftemp = elemento.form.name; var nomform = 0;
  if (nftemp!='') {nomform = nftemp;}
  var patt = /(lab_)([^_]{5})([^_]{3})(_bottom|_top|$)/;
  var arrMatches = idchk.match(patt);
  if (arrMatches!=null){
    toggle(document.forms[nomform].elements['ck_'+arrMatches[2]],document.forms[nomform].elements['ck_' +arrMatches[2]+ 'all']);
    if (arrMatches[4]!=''){
      var esteTopBottom = arrMatches[4];
      var otroTopBottom = getResBegTopOrBottom(esteTopBottom);
      var otrochk = document.getElementById(arrMatches[1]+arrMatches[2]+arrMatches[3]+otroTopBottom);
      otrochk.checked = elemento.checked;
      toggle(document.forms[otrochk.form.name].elements['ck_' +arrMatches[2]],document.forms[otrochk.form.name].elements['ck_' +arrMatches[2]+ 'all']);
    }
  }
}
function switchSearchOptsAll(elemento){
  var idchk = elemento.id;
  var nftemp = elemento.form.name; var nomform = 0;
  if (nftemp!='') {nomform = nftemp;}
  var patt = /(lab_)([^_]{5})([^_]{3})(_bottom|_top|$)/;
  var arrMatches = idchk.match(patt);
  if (arrMatches!=null){
    toggleall(elemento,document.forms[nomform].elements['ck_'+arrMatches[2]]);
    if (arrMatches[4]!=''){
      var esteTopBottom = arrMatches[4];
      var otroTopBottom = getResBegTopOrBottom(esteTopBottom);
      var otrochk = document.getElementById(arrMatches[1]+arrMatches[2]+arrMatches[3]+otroTopBottom);
      otrochk.checked = elemento.checked;
      toggleall(elemento,document.forms[otrochk.form.name].elements['ck_'+arrMatches[2]]);
    }
  }
}
function switchSearchOptsSingleCheck(elemento){
  var idchk = elemento.id;
  var patt = /(lab_)([^_]+)(_bottom|_top|$)/;
  var arrMatches = idchk.match(patt);
  if (arrMatches!=null){
    if (arrMatches[3]!=''){
      var esteTopBottom = arrMatches[3];
      var otroTopBottom = getResBegTopOrBottom(esteTopBottom);
      var otrochk = document.getElementById(arrMatches[1]+arrMatches[2]+otroTopBottom);
      otrochk.checked = elemento.checked;
    }
  }
}
/* END: Funcions per poder tenir la capçalera de cerca (resultsBegin) duplicada al final de la pàgina */