
function deleteBlankRowIfNotEmpty(toList)
{
   var idx = -1;
   var val = "";
// find a blank row in table
   for (i = 0; i < toList.length; i++){
        val = toList.options[i].value;
        if (val == "") {
           idx = i;
           break;
        }
   }
   if (idx >= 0 && (toList.length > 1))
      toList.options[idx] = null;
}

function selectAll(fromList)
{
  if (fromList.length==1 && fromList.options[0].value=='') return true;
  for ( i = 0; i <= fromList.length-1; i++ )
    fromList.options[i].selected = true;
  return true;
}

function unSelectAll(fromList)
{
  for ( i = 0; i <= fromList.length-1; i++ )
    fromList.options[i].selected = false;
  return true;
}

function clearList(fromList)
{
  fromList.length = 0;
}

function copyToList(fromList, toList, direction, p_formname) {

  for ( i = 0; i <= fromList.length-1;) {
    if (fromList.options[i].selected) {
        txt = fromList.options[i].text;
        val = fromList.options[i].value;
        if ( val != "" ) {
           // check if value is a spacer
           if ( val == "spacer" ) {
                if ( direction == "left" ) {
                     // remove from right and do not add on left
                     fromList.options[i]= null;
                }
                else {
                    // add to right but do not remove from left
                    fromList.options[i].selected = false;
                    toList.options[toList.length] = new Option( txt, val, false, true );
                    toList.options[toList.selectedIndex].selected = false;
                }
           }
           else {  //only increment when not moving and deleting
             // create a new row
             toList.options[toList.length] = new Option( txt, val, false, true );
             // added these lines
             // removes from fromList and unselects item in toList
             fromList.options[i]= null;
             toList.options[toList.selectedIndex].selected = false;
           }  //only increment when not moving and deleting
        }
    }
    else i++;  //only increment when not moving and deleting
  }
  deleteBlankRowIfNotEmpty(fromList);
  deleteBlankRowIfNotEmpty(toList);
}

function copyAll(fromList, toList, direction, p_formname) {
     indexofspacer = -1;
        spacerval = "";
        spacertxt = "";
        indexofitem = toList.length;
     for ( i = 0; i <= fromList.length-1; i++ ) {
         txt = fromList.options[i].text;
         val = fromList.options[i].value;
         if ( val != "" ) {
             // check if we need to copy the spacer too
             if ( val != "spacer") {
                  toList.options[indexofitem] = new Option( txt, val, false, true );
                  toList.options[indexofitem].selected = false;
                     indexofitem++;
                }
                else { // found a spacer
                    indexofspacer = i;
                    spacerval = val;
                    spacertxt = txt;
                }
            }
     }
        if (indexofspacer != -1 && direction == "right" ) // let the spacer be on the from list
            fromList.length = 1;
        else
         clearList(fromList);
     deleteBlankRowIfNotEmpty(toList);
     unSelectAll(toList);
}

function copyAllToRight (fromList, toList) {
     indexofspacer = -1;
     spacerval = "";
     spacertxt = "";
     indexofitem = toList.length;
     for ( i = 0; i <= fromList.length-1; i++ ) {
         txt = fromList.options[i].text;
         for (j=0;j<txt.length;j++)
           if (txt.charCodeAt(j)!=160) break;
         txt=txt.substring(j);
         val = fromList.options[i].value;
         if ( val != "" ) {
             // check if we need to copy the spacer too
             if ( val != "spacer") {
                  existe=false;
                  for ( j = 0; j <= toList.length-1;j++)
                    if (toList.options[j].value==val) {
                      existe=true;
                      break;
                    }
                  if (!existe) {
                    toList.options[indexofitem] = new Option( txt, val, false, true );
                    toList.options[indexofitem].selected = false;
                    indexofitem++;
                  }
                  else {
                    alert('Valor ya añadido: '+txt);
                    break;
                  }
             }
             else { // found a spacer
                    indexofspacer = i;
                    spacerval = val;
                    spacertxt = txt;
             }
         }
     }
     if (indexofspacer != -1 && direction == "right" ) // let the spacer be on the from list
       fromList.length = 1;
     deleteBlankRowIfNotEmpty(toList);
     unSelectAll(toList);
}


function copyToRight (fromList, toList) {
  for ( i = 0; i <= fromList.length-1;i++) {
    if (fromList.options[i].selected) {
        txt = fromList.options[i].text;
        for (j=0;j<txt.length;j++)
          if (txt.charCodeAt(j)!=160) break;
        txt=txt.substring(j);
        val = fromList.options[i].value;
        if ( val != "" ) {
          existe=false;
          for ( j = 0; j <= toList.length-1;j++)
            if (toList.options[j].value==val) {
              existe=true;
              break;
            }
          if (!existe) {
             // removes from fromList
             fromList.options[i].selected = false;
             // create a new row
             toList.options[toList.length] = new Option( txt, val, false, true );
             // added these lines unselects item in toList
             toList.options[toList.selectedIndex].selected = false;
          }
          else {
             alert('Valor ya añadido: '+txt);
             break;
          }
        }
    }
  }
  deleteBlankRowIfNotEmpty(fromList);
  deleteBlankRowIfNotEmpty(toList);
}


function copyToLeft (fromList) {
  for (i=fromList.length-1; i>=0; i--) {
    if (fromList.options[i].selected) fromList.options[i] = null;
  }
  deleteBlankRowIfNotEmpty(fromList);
}


function copyToLeft_OLD (fromList, toList) {
  unSelectAll(toList);
  for ( i = 0; i <= fromList.length-1;i++) {
    if (fromList.options[i].selected) {
      for ( j = 0; j <= toList.length-1;j++) {
        if (toList.options[j].value==fromList.options[i].value)
          toList.options[j].selected = true;
      }
    }
  }
  for ( i = 0; i <= toList.length-1;i++) {
    if (toList.options[i].selected) {
      for ( j = 0; j <= fromList.length-1;j++) {
        if (fromList.options[j].value==toList.options[i].value)
          fromList.options[j] = null;
      }
    }
  }
  deleteBlankRowIfNotEmpty(fromList);
  unSelectAll(toList);
}

var _rnp = "redir_next_page";

function copyForm(nombOrg, nombDst)
{
  // Comprobar si los formularios están definidos
  org = eval('document.forms["'+nombOrg+'"]');
  dst = eval('document.forms["'+nombDst+'"]');
  if (!org || !dst) return;

  // Para cada campo del formulario de búsqueda
  for (i = 0; i < dst.length; i++) {
    var objDst = dst.elements[i];   // Campo destino (buscador)
    var objOrg = org[objDst.name];  // Campo origen (oculto en el listado)
    if (objOrg && objOrg.name && objOrg.name != 'pr' && // "pr" no se copia
        objOrg.name.substr(0, _rnp.length) != _rnp) {   // "redir_next_page" tampoco
      if (objDst.options) { // Select => Buscar opción activa
        for (j = 0; j < objDst.length; j++) {
          if (objDst.options[j].value == objOrg.value) {
            objDst.selectedIndex = j;
            break;
          }
        }
      } else if (objDst.type == 'checkbox') {   // Checkbox => Activar si definido
        objDst.checked = (objOrg.value != null && objOrg.value != '' && objOrg.value != 0);
      } else if (objDst.type == 'radio') {  // Es un RadioButton => Activar si definido
        if (objDst.value == objOrg.value) {
          objDst.checked = true;
        }
      } else {
        objDst.value = objOrg.value;
      }
    }
  }
}
