// ////////////
// MultiSelects
// ////////////

// Multiselect: seleccionar todas las opciones
function mselectTodo(objSelect)
{
  for (var i=0; i < objSelect.length ; i++)
    objSelect.options[i].selected = true;
    
}

// Multiselect: deseleccionar todas las opciones
function mselectNada(objSelect)
{
  for (var i=0; i < objSelect.length ; i++)
    objSelect.options[i].selected = false;

}

// Select/Multi: eliminar opciones seleccionadas
function eliminarOpcionesSeleccionadas(objSelect)
{
  for (var i=(objSelect.length-1); i >= 0 ; i--)
    if ( objSelect.options[i].selected == true) objSelect.options[i] = null;
}

// Select/Multi: devuelve el primer TEXTO seleccionado
// si no hay ninguno seleccionado devuelve null
function getPrimerTextoSeleccionado(objSelect)
{
  for (var i=0; i < objSelect.length ; i++)
  {
    if ( objSelect.options[i].selected == true) 
      return objSelect.options[i].text;
  };
  return null;
}



// Select/Multi: devuelve el numero de opciones seleccionadas
function numeroOpcionesSeleccionadas(objSelect)
{
  var _x=0;
  var _F=objSelect.options;
  for(var i=0;i<_F.length;i++)
    _x+=(_F[i].selected)?1:0;
  return(_x);
}  

// Select/Multi: mira si ya existe una opcion en la select que tenga el "valor" dado (ojo: value, no text!)
function existeValorEnSelect(objSelect, valor)
{
  //Compruebo que una opcion no exista ya en un SELECT.
  x = new Boolean(false);
  for (var i=0; i < objSelect.length ; i++)
  {
    if (objSelect.options[i].value == valor) x= true;
  }
  return x;
}

// Select/Multi: si existe el valor, lo pone como seleccionado
function seleccionaValorEnSelect(objSelect, valor)
{
  for (var i=0; i < objSelect.length ; i++)
  {
    if (objSelect.options[i].value == valor) objSelect.options[i].selected = true;
  };
}

// Select/Multi: añadir una nueva opcion a la select
function nuevaOpcionSelect(objSelect, value , text)
{
   var nOpt = new Option(text,value);
   objSelect.options[objSelect.length]= nOpt;
   objSelect.selectedIndex = objSelect.length-1;
}

// Select/Multi: elimino todas las opciones (vaciado de select)
function eliminarOpcionesTodas(objSelect)
{
  // eliminamos todo (con vileza)
  for (var i=(objSelect.length-1); i >= 0 ; i--)
    objSelect.options[i] = null;
}

// Select/Multi: copia items seleccionados desde objSelectFrom a objSelectTo
// (solo copia en caso de que en objSelectTo no exista ya un item con el valor)
function copiarSeleccionadas(objSelectFrom,objSelectTo)
{
  for (var i=(objSelectFrom.length-1); i >= 0 ; i--)
  {
    if ( objSelectFrom.options[i].selected == true) // copiar a objSelectTo
    {
      valor = objSelectFrom.options[i].value;
      if ( existeValorEnSelect(objSelectTo,valor) == false ) 
        nuevaOpcionSelect(objSelectTo,valor,objSelectFrom.options[i].text)
    }
  }
}

// //////////////////////
// Checks y Radio buttons
// //////////////////////

// checkbox normal/multiple/radio: contar seleccionados
// (checkboxes de cero elems generados dinamicamente: devuelve 0)
function numCheckeds(formName, checkRadioName)
{
  var cuenta = 0;
  if ( ! document.forms[formName].elements[checkRadioName] ) return cuenta;
  checkRadioObj = document.forms[formName].elements[checkRadioName];
  if ( checkRadioObj.length == undefined ) // no es multi!
  {  	
    if ( checkRadioObj.checked == true ) return 1;
    else return 0;
  };
  for (i=0; i < checkRadioObj.length; i++)
  {    
    if ( checkRadioObj[i].checked == true ) cuenta = cuenta + 1;
  }
  return cuenta;
}


// checkbox simple/multiple: seleccionar todos
// (checkboxes de cero elems generados dinamicamente: ninguna acción)
function mcheckTodo(formName, checkRadioName)
{
  if ( ! document.forms[formName].elements[checkRadioName] ) return;
  checkRadioObj = document.forms[formName].elements[checkRadioName];
  if ( checkRadioObj.length == undefined ) // no es multi!
  {  	
    checkRadioObj.checked = true;
    return;
  };  
  for (i=0; i < checkRadioObj.length; i++)
  {    
    checkRadioObj[i].checked = true;
  }
  return;
}

// checkbox simple/multiple: deseleccionar todos
// (checkboxes de cero elems generados dinamicamente: ninguna acción)
function mcheckNada(formName, checkRadioName)
{
  if ( ! document.forms[formName].elements[checkRadioName] ) return;
  checkRadioObj = document.forms[formName].elements[checkRadioName];
  if ( checkRadioObj.length == undefined ) // no es multi!
  {  	
    checkRadioObj.checked = false;
    return;
  };    
  for (i=0; i < checkRadioObj.length; i++)
  {    
    checkRadioObj[i].checked = false;
  }
  return;
}




// checkbox simple: marcar el checkbox como seleccionado, dandole el valor
function setCheck(formName, checkName, value)
{
  checkObj = document.forms[formName].elements[checkName];
  setCheckObj(checkObj,valor);
}
function setCheckObj(checkObj,valor)
{
  checkObj.checked = true;
  checkObj.value = valor;
}


// checkbox simple: marcar el checkbox como deseleccionado
function setUnCheck(formName, checkName)
{
  checkObj = document.forms[formName].elements[checkRadioName];
  setUnCheckObj(checkObj,valor);
}
function setUnCheckObj(checkObj)
{
  checkObj.checked = false;
}


// //////////////////////
// Text y TextAreas
// //////////////////////


// entry -- texto donde se efectua el reemplazo
// out -- string con todos los caracteres prohibidos
// add -- string con los caracteres substitutos
function replaceChars(entry,out,add)
{
  temp = "" + entry; // temporary holder
  for ( i=0; i<out.length; i++ )
  {
    unCharNo = out.charAt(i);
    unCharSi = add.charAt(i);          
    while (temp.indexOf(unCharNo)>-1) 
    {
      pos= temp.indexOf(unCharNo);
      temp = "" + (temp.substring(0, pos) + unCharSi + 
      temp.substring((pos + 1), temp.length));
    };
  };
  //alert(entry + " ---> " + temp);
  return temp;
}


// devuelve "texto" con los caracteres de Windows que dan problemas
// en HTML substituidos
function filtraCharsWindows(texto)
{
  var prohibidos  = String.fromCharCode(8218)     // baseline single quote    
                  + String.fromCharCode(402 )     // florin                   
                  + String.fromCharCode(8222)     // baseline double quote    
                  + String.fromCharCode(8230)     // ellipsis                 
                  + String.fromCharCode(8224)     // dagger                   
                  + String.fromCharCode(8225)     // double dagger            
                  + String.fromCharCode(710 )     // circumflex accent        
                  + String.fromCharCode(8240)     // permile                  
                  + String.fromCharCode(352 )     // S Hacek                  
                  + String.fromCharCode(8249)     // left single guillemet    
                  + String.fromCharCode(338 )     // OE ligature              
                  + String.fromCharCode(8216)     // left single quote        
                  + String.fromCharCode(8217)     // right single quote       
                  + String.fromCharCode(8220)     // left double quote        
                  + String.fromCharCode(8221)     // right double quote       
                  + String.fromCharCode(8226)     // bullet                   
                  + String.fromCharCode(8211)     // endash                   
                  + String.fromCharCode(8212)     // emdash                   
                  + String.fromCharCode(732 )     // tilde accent             
                  + String.fromCharCode(8482)     // trademark ligature       
                  + String.fromCharCode(353 )     // s Hacek                  
                  + String.fromCharCode(8250)     // right single guillemet   
                  + String.fromCharCode(339 )     // oe ligature              
                  + String.fromCharCode(376 )     // Y Dieresis               
                  + String.fromCharCode(8364);    // Euro Symbol
  var substitutos = "'" + "f" + "\"" + "?" + "?" + "?" + "^" + "?" + "?" + "'" + "?" + "'" + "'" + "\"" + "\""
                  + "*" + "-" + "-" + "~" + "?" + "?" + "'" + "?" + "Y" + "e";
  //alert(prohibidos);
  //alert(substitutos);
  return replaceChars(texto,prohibidos,substitutos);
}
