
function spamCh(nb, ch)
{
	var str='';
	for (var i=0; i<nb; i++)
		str +=ch
	
	return str;
}






/********************************************************************/
//** Objeto FBselector
//*  id		-> id del objeto; tiene que coincidir con la variable que se asigna
//*  width	-> ancho de la persiana del objeto
//*  rows	-> filas que se ven
//*  caption	-> titulo de la persiana
//*  btnurl	-> url del onclick del boton inferior
//*  btncaption -> caption del boton inferior
//*  btntext	-> true|false muestra|esconde la caja de texto inferior
//*  limit	-> limite de lineas que se aceptan
//*  header	-> cabecera formateada de la persiana
//*  tabs	-> anchura de las zonas a las que se formatea cada linea
//*  form	-> formulario donde se crea el objeto. Si se omite cada objeto se crea en un formulario
//*                con de nombre FORMFBsel_id
//*		   Si se incluye en un formulario ya existente, su nombre no puede empezar por FORMFB...
/********************************************************************/

function FBselector( selectorparams )
{
	
	
	this.parameters		= selectorparams;
	
	this.getParameter	= __getParameter;
	this.addTool		= __addTool;
	this.addData 		= __addData;
	this.addtextData 	= __addtextData;
	this.gettextData 	= __gettextData;
	this.getOptions 	= __getOptions;
	this.deleteData		= __deleteData;
	this.deleteSelectedData	= __deleteSelectedData;
	this.deleteallData	= __deleteallData;
	this.getData		= __getData;
	this.getText		= __getText;
	this.getSelectedData	= __getSelectedData;
	this.getSelectedText	= __getSelectedText;
	this.sortData		= __sortData;
	this.createSelector	= __createSelector;
	this.spamTabs		= __spamTabs;
	this.initSelector	= __initSelector;



	
	this.id			= this.getParameter('ID');		//id de la persiana
	this.FBid		= 'FBsel_' + this.getParameter('ID');		//id de la persiana
	this.FBform		= this.getParameter('form');
		this.FBform	= this.FBform== '' ? 'FORM' + this.FBid	: this.FBform	// nombre del formulario donde se crea
	this.width		= 1*this.getParameter('width');		//ancho de la persiana
	this.rows		= 1*this.getParameter('rows');	//alto de la persiana
	this.caption		= this.getParameter('caption');		//caption del boton de la persiana
	this.btnurl		= this.getParameter('btnurl');		//url que llama el boton de la persiana
	this.btnonclick		= this.btnurl!='' ? this.btnurl : this.getParameter('btnonclick');		//javascript que llama el boton de la persiana.Es igual que btnurl
	this.btncaption		= this.getParameter('btncaption');		//caption del boton de la persiana
	this.btntext		= this.getParameter('btntext');		//caption del boton de la persiana
	this.limit		= 1*this.getParameter('limit');		//limite de propiedades que caben en la persiana
	this.header		= this.getParameter('header');		//cabecera
	this.TABS		= this.getParameter('tabs');		//tabulaciones 
	  this.TABS 		= this.TABS!='' ? this.TABS.split(',') : null;
	
	this.oplength		=0;
	
	this.data		= new Array();				//lista de propiedades que se incluyen en la persiana
	this.ndata		=0;					//numero de propiedades que actualmente hay en la persiana
	
	this.frame		="";					//marco visible de la persiana
	this.toolbox		="";					//zona de herramientas
	
	
	this.initSelector();
	
}





function __getParameter ( idparam )
{
	
	var params = this.parameters.toLowerCase();
	var ix = params.indexOf ( idparam.toLowerCase() );
	var value = '';
	var chend = ' ';
	
	if ( ix!=-1 )
	{
		ix += idparam.length + 1;
		if ( this.parameters.charAt(ix)=='"') { chend='"'; ix++; }
		if ( this.parameters.charAt(ix)=='`') { chend='`'; ix++; }
			
		while ( ix<params.length && this.parameters.charAt(ix) != chend )
			value += this.parameters.charAt (ix++);
	}

if ( chend == '`' )
	value = eval ( value );
	
return value;	
	
}



function __spamTabs(str, nb)
{
	lix = 0;
	ix =0;
	
	
	while ( (ix=str.indexOf('\t'))!=-1 )
	{
		nexttab = typeof nb=='undefined' ? this.TABS[lix] : lix*nb;
		str=str.replace(/\t/, spamCh(nexttab-ix,' ') );
		lix++;
	}
	

	return str;
}




function __addTool ( toolimg, toolurl )
{
	
       toolurl = toolurl.replace (/"/g,"\'");
       
       this.toolbox +=  ' <a href=# onclick="' + toolurl + ';return false;" ><img src="' + toolimg + '" border=0></a><br>&nbsp;<br>  \n';
	
}


//añade el texto desde el campo de texto a la lista
function __addtextData ()
{
	
	obj = document.forms[this.FBform];
	
	var val = obj[this.FBid+'_txt'].value.replace(/\\t/g,'\t');
       
	this.addData( val, obj[this.FBid+'_txt'].value )	;
}


// Obtiene el texto del campo de texto 
function __gettextData ()
{
	
	obj = document.forms[this.FBform];
	
	return obj[this.FBid+'_txt'].value;
       
}


// Obtiene el numero de opciones disponibles en la lista
function __getOptions ()
{
	
	obj = document.forms[this.FBform][this.FBid];
	
	return ( obj.options )
       
}





// Añade un dato a la lista
function __addData (  data , value, option)
{
	
	obj = document.forms[this.FBform][this.FBid];
	
	if ( obj.options.length>=this.limit)
	{
		alert( 'Esta lista no acepta más valores.' );
	}
	else
	{
		
		var op = typeof option=='undefined' ? obj.options.length : option;
		var dt = data.length>this.width ? data.slice(0,this.width-3)+'...' : data;
		obj.options[op]= new Option(  this.spamTabs(dt) , value );
		this.oplength = obj.options.length;
		
	}
}


// Borra de la lista la opcion indicada y si no se indica
// borra todos los seleccionados
function __deleteData ( option )
{
	
	obj = document.forms[this.FBform][this.FBid];

	if ( typeof option=='undefined' )
	{

		var st = this.header!='' ? 2 : 1;
		for ( var i=st; i<obj.options.length; i++)
			if ( obj.options[i].selected )
			{
				obj.options[i] = null;
				i=st-1;
			}
	}
	else
		obj.options[option] = null;
		
		
	this.oplength = obj.options.length;
}




/// Borra de la lista todos los seleccionados
function __deleteSelectedData()
{
	this.deleteData();
}



function __deleteallData (  )
{
	
	obj = document.forms[this.FBform][this.FBid];

	var st = this.header!='' ? 2 : 1;
	for ( var i=st; i<obj.options.length; i++)
	{
		obj.options[i] = null;
		i=st-1;
	}
}


//Obtiene todos los valores de la lista separdos por comas
function __getData()
{
	
	var val = "";

	obj = document.forms[this.FBform][this.FBid];
	
	var st = this.header!='' ? 2 : 1;
	for ( var i=st; i<obj.options.length; i++)
	{
		val += obj.options[i].value + ',' ;
		
	}

	return val.slice(0,-1);
	
}






//Obtiene todos los textos de la lista separdos por comas
function __getText()
{
	
	var val = "";
	
	obj = document.forms[this.FBform][this.FBid];

	var st = this.header!='' ? 2 : 1;
	for ( var i=st; i<obj.options.length; i++)
	{
		val += obj.options[i].text + ',' ;
		
	}

	return val.slice(0,-1);
	
}





// Obtiene un array con  los valores seleccionados de la lista
function __getSelectedData ( )
{
	
	obj = document.forms[this.FBform][this.FBid];
	
	var retdata = new Array();

	var st = this.header!='' ? 2 : 1;
	for ( var i=st; i<obj.options.length; i++)
		if ( obj.options[i].selected )
		{
			retdata.push(obj.options[i].value);
		}


return retdata;

}


// Obtiene un array los textos seleccionados de la lista
function __getSelectedText ( )
{
	
	obj = document.forms[this.FBform][this.FBid];
	
	var retdata = new Array();

	var st = this.header!='' ? 2 : 1;
	for ( var i=st; i<obj.options.length; i++)
		if ( obj.options[i].selected )
		{
			retdata.push(obj.options[i].text);
		}


return retdata;

}




//Ordena los datos de la lista
function __sortData()
{
	
	obj = document.forms[this.FBform][this.FBid];

	var data = this.getData().split(',');
	var text = this.getText().split(',');
	
	var tmp = new Array();
	
	for ( var i=0; i<data.length; i++)
		tmp.push(data[i] + '·' + text[i]);

	
	tmp.sort();	
	
	this.deleteallData();
	
	for ( var i=0; i<tmp.length; i++)
	{
		opt = tmp[i].split('·');
		
		this.addData(opt[0],opt[1]);
		
	}
	
	
}




function __initSelector (  )
{
	
	

       if ( this.FBform.slice(0,6)=='FORMFB' )
       		this.toolbox +=  ' <FORM NAME="' + this.FBform + '"  action="" >  \n';
	
       this.toolbox +=  ' <TABLE BORDER=2 width=' + this.width + ' >  \n';
       this.toolbox +=  '  <!-- ------------------ -->  \n';

       this.toolbox +=  ' <tr>  \n';
       this.toolbox +=  ' <td  colspan=4 class="titfbselector">  \n';
       this.toolbox +=  ( this.caption );
       this.toolbox +=  ' </td>  \n';
       this.toolbox +=  '  </tr>  \n';
       this.toolbox +=  ' <!-- ----------        -------- -->  \n';

       this.toolbox +=  ' <tr>  \n';
       this.toolbox +=  ' <td  class="fbselector" valign=top align=center>  \n';

       this.frame +=  ' </td>  \n';
       this.frame +=  ' <td  class="bodyfbselector" >  \n';
       this.frame +=  ' <select multiple name=' + this.FBid + ' size=' + this.rows + ' id=bodyfbselector  >  \n';
    if ( this.header!='')
       this.frame +=  ' <option  value=0>' + this.spamTabs(this.header).replace(/ /g,'&nbsp;') + '</option>  \n';
       this.frame +=  ' <option  value=0>' + spamCh(this.width,"=") + '</option>  \n';
       this.frame +=  ' </select>  \n';
       this.frame +=  ' </td>  \n';
       this.frame +=  ' <td  class="fbselector" valign=top>  \n';

       this.frame +=  ' </td>  \n';
       this.frame +=  ' <td  class="bodyfbselector" >  \n';

       this.frame +=  ' </td>  \n';
       this.frame +=  ' </tr>  \n';

       this.frame +=  ' <!-- ----------  -------- -->  \n';
       this.frame +=  ' <tr>  \n';
       this.frame +=  ' <td colspan=4 class="fbselector" align=center bgcolor=#d0d0d0 >  \n';
     if ( this.btncaption!='' )
       this.frame +=  ' <input type="button" id="fbselector"  value="' + this.btncaption + '" onclick="' + this.btnonclick + '" >  \n';
     if ( this.btntext=='true' )
       this.frame +=  ' <input type="text" name="' + this.FBid + '_txt" id="fbselector"  value=""  >  \n';
       this.frame +=  ' </td>  \n';
       this.frame +=  ' </tr>  \n';

       this.frame +=  ' </TABLE>  \n';
       
       
       if ( this.FBform.slice(0,6)=='FORMFB' )
       		this.frame +=  ' </FORM >  \n';
	
	
       
}





function __createSelector()
{
	
	document.write( this.toolbox + this.frame );
	
	obj = document.forms[this.FBform][this.FBid];
	this.oplength = obj.options.length;

}
