// Funciones genéricas

function CopiaSelect (select_origen, select_destino)
{
	var valor = select_destino.value;
	var indice_bueno = 0

	while (select_destino.length > 0)
		select_destino.remove(0);

	for (var i=0; i<select_origen.length; i++)
	{
		if (select_origen.options[i].value == valor)
			indice_bueno = i
		select_destino.options[i] = new Option (select_origen.options[i].text,select_origen.options[i].value,false,false)
	}

	select_destino.selectedIndex = indice_bueno
}
// -------------------------------------------------------


// Function array_index
// Busca y devuelve el indice de un valor determinado dentro de un array, si no existe dicho valor devuelve -1.
function array_index (array,valor)
{
	var a=0;
	
	while (a<array.length)
	{
		if (array[a]==valor)
		{
			return a;
		}

		a++
	}
	
	return -1;
}
// ----------------------------------------------------

function CreaCampoTexto (nombre_form, nombre_campo, clase, estilo, valor, max_caracteres, on_blur, campo_float)
// Crea un campo de texto de los de hacer click para editar en este formulario de presupuesto o factura
// max_caracteres es -1 si no hay máximo para este input
// campo_float es un booleano que indica si es un campo tipo float o no. Por defecto considero que vale false
{
	salida  = '<DIV id="div_'+nombre_form+'_input_'+nombre_campo+'" style="display: none;">'
	salida += '   <input type=text id='+nombre_form+'_input_'+nombre_campo+' name="'+nombre_campo+'" value="'+valor+'" class="'+clase+'" style="'+estilo+'" '
	if ( (max_caracteres != null) && (max_caracteres != -1) )
		salida += ' maxlength=' + max_caracteres
	if (campo_float == true)
		salida += '	  onBlur=restaurar_input("'+nombre_form+'","'+nombre_campo+'","'+nombre_campo+'",true);'+on_blur+'>'
	else
		salida += '	  onBlur=restaurar_input("'+nombre_form+'","'+nombre_campo+'","'+nombre_campo+'");'+on_blur+'>'
	salida += '</DIV>'
	salida += '<DIV id="div_'+nombre_form+'_'+nombre_campo+'"" class='+clase+' style="width:100%;height:20;'+estilo+'" ondblclick=modificar_input("'+nombre_form+'","'+nombre_campo+'","'+nombre_campo+'")>'
	if (campo_float == true)
		salida += number_format(valor,"2",",",".")
	else
		salida += valor
	salida += '</DIV>'

	return salida
}
// ----------------------------------------------------

function number_format(a, b, c, d)
{
	var i,j

	a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 	e = a + '';
 	f = e.split('.');

	if (!f[0])
		f[0] = '0';

	if (!f[1])
		f[1] = '';

	if (f[1].length < b)
	{
  		g = f[1];
		for (i=f[1].length + 1; i <= b; i++)
   			g += '0';

  		f[1] = g;
 	}
 
	if(d != '' && f[0].length > 3)
	{
  		h = f[0];
		f[0] = '';
  		for(j = 3; j < h.length; j+=3)
		{
   			i = h.slice(h.length - j, h.length - j + 3);
			f[0] = d + i +  f[0] + '';
  		}
  		j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  		f[0] = j + f[0];
	}
 	
	c = (b <= 0) ? '' : c;

	return f[0] + c + f[1];
}
// ----------------------------------------------------

function checkEnter(e)
{ //e is event object passed from function invocation

	var characterCode; //literal character code will be stored in this variable

	if (e && e.which)
	{ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else
	{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}

	if (characterCode == 13)
	{ //if generated character code is equal to ascii 13 (if enter key)
		login.submit();
		return true;
	}
	else
	{
		return false;
	}

}
