//////////////////////////////////////////////////////
//Variable que indica si se ha tocado algún campo del formulario
//////////////////////////////////////////////////////
var LogCambios;
LogCambios=false;


function cambio()
{
	LogCambios = true;
}


//--------------------------------------------------------------------------------------------------------------------------------------------------
//Hace el envío de un formulario y lo sustituye en la página por un mensaje de texto. Se evita así que el usuario pulse
//varias veces el botón de Guardar. 
//Si aún así da tiempo al usuario a pulsar el botón varias veces, el formulario sólo se envía la primera vez 
//Sólo debe usarse en formularios que guarden datos
//--------------------------------------------------------------------------------------------------------------------------------------------------

var formularioEnviado=0;

function _enviar(formulario)
{
	if (formularioEnviado==0)
	{
		formularioEnviado = 1;
		formulario.submit();
		window.document.body.innerHTML = "<table width='100%' height='100%' border='0'><tr><td class=txtDes><center><img src='Img/guardando.gif' /><br>Enviando datos...</center></td></tr></table>";
	}
}

function _enviarFtp(formulario)
{
	if (formularioEnviado==0)
	{
		formularioEnviado = 1;
		formulario.submit();
		window.document.body.innerHTML = "<table width='100%' height='100%' border='0'><tr><td align='center'><img border='0' src='Img/subiendo.gif' alt='Subiendo fichero al servidor'></td></tr></table>";
	}
}

function _recibirFtp(formulario)
{
	if (formularioEnviado==0)
	{
		formularioEnviado = 1;
		formulario.submit();
		window.document.body.innerHTML = "<table width='100%' height='100%' border='0'><tr><td align='center'><img border='0' src='Img/descargando.gif' alt='Descargando fichero del servidor'></td></tr></table>";
	}
}

//--------------------------------------------------------------------------------------------------------------------------------------------------
//Sustituye en la página por un mensaje de texto. Enviando...
//--------------------------------------------------------------------------------------------------------------------------------------------------
function _sendingMessage()
{
	window.document.body.innerHTML = "<table width='100%' height='100%' border='0'><tr><td class=txtDes><center><img src='Img/guardando.gif' /><br>Enviando datos...</center></td></tr></table>";
}

// ---------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------
//Abre una página nueva con nombre centrada en la página desde la que se ha abierto
// ---------------------------------------------------------------------------------------------------------------
function ventana(StrUrl,StrNombre,IntAncho, IntAlto, StrOtros)
{
	if (IntAncho=="")
		IntAncho=750;

	if (IntAlto=="")
		IntAlto=500;

	var PosX = parseInt((screen.availWidth - IntAncho)/2);
	var PosY = parseInt((screen.availHeight - IntAlto)/2);

	return(window.open(StrUrl, StrNombre,"height=" + IntAlto + ",width=" + IntAncho + ",left=" + PosX + ",top=" +PosY + "," + StrOtros));
}

//--------------------------------------------------------------------------------------------------------------------------------------------------
//selecciona el elemento de un combo cuyo calor coincide con la clave numérica que se recibe como parámetro
//--------------------------------------------------------------------------------------------------------------------------------------------------
function seleccionarComboPorClave(combo, intClave)
{
	 var i;
	 var logEncontrado;
	 
	 logEncontrado=false;
	 i=0;
	 while(i<combo.length && !logEncontrado)
	 {
		if(parseInt(combo.options[i].value)==parseInt(intClave))
		{
			combo.selectedIndex=i;
			logEncontrado = true;
		}
		i++;
	}
	
}

//--------------------------------------------------------------------------------------------------------------------------------------------------
//Indica el número de elementos seleccionados en un check múltiple
//--------------------------------------------------------------------------------------------------------------------------------------------------
function numeroSeleccionados(check, tamano)
{
	 var i = 0;
	 var cont = 0;	 

	if (check[1])
	{
		 while(i<tamano)
		 {
			if(check[i].checked)
			{
				cont++;
			}
			i++;
		 }
	}
	else
	{
		if(check.checked)
			cont++;
	}
	 return(cont);	
}

//--------------------------------------------------------------------------------------------------------------------------------------------------
//Controla la longitud del texto introducido en un <TEXTAREA>
//--------------------------------------------------------------------------------------------------------------------------------------------------

function longitud(StrCampo, IntLongitud)
{
	if (StrCampo.value.length>=IntLongitud)
	{
		alert('Se ha alcanzado la longitud máxima.');
		StrCampo.value=StrCampo.value.substring(0, IntLongitud-1);
	}
}

function radioSeleccionadoIndice(radio) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (radio[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<radio.length; i++) {
         if (radio[i].checked) {
            return i
         }
      }
   } else {
      if (radio.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} 


function radioSeleccionadoValor(radio) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = radioSeleccionadoIndice(radio);
   if (i == -1) {
      return "";
   } else {
      if (radio[i]) { // Make sure the button group is an array (not just one button)
         return radio[i].value;
      } else { // The button group is just the one button, and it is checked
         return radio.value;
      }
   }
} 


function checkboxSeleccionadoIndice(checkbox) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   
   for (var i=0; i<checkbox.length; i++) 
   {
	 if (checkbox[i].checked) 
	 {
		retArr.length = lastElement;
        retArr[lastElement] = i;
        lastElement++;
     }
   }
   return retArr;
} 

function checkboxSeleccionadoValor(checkbox) {
   // return an array of values selected in the check box group. if no boxes
   // were checked, returned array will be empty (length will be zero)
   var retArr = new Array(); // set up empty array for the return values
   var selectedItems = checkboxSeleccionadoIndice(checkbox);
   if (selectedItems.length != 0) { // if there was something selected
      retArr.length = selectedItems.length;
      for (var i=0; i<selectedItems.length; i++) {
         if (checkbox[selectedItems[i]]) { // Make sure it's an array
            retArr[i] = checkbox[selectedItems[i]].value;
         } else { // It's not an array (there's just one check box and it's selected)
            retArr[i] = checkbox.value;// return that value
         }
      }
   }
   return retArr;
} 

function mostrarCalendario(caja)
{
	var calendar
	var miHeight;
	var miWidth;

	miHeight = 225;
	miWidth = 220;
		
	window.dateField = eval("document.form1."+caja);
		
	calendar = ventana("./Inc/calendario.htm", "calendario", miWidth, miHeight, "scrollbars=1")

	return false;
}

// ---------------------------------------------------------------------------------------------------------------
//variables y funciones para hacer que un pop-up se cierre transcurridos 2sg si se cambia la página desde dónde se abrió
// ---------------------------------------------------------------------------------------------------------------

var PaginaPadreActual;
var PaginaPadreLlamada;
function CompruebaCambio()
{
	if (window.opener.closed)
		window.close()
	else
	{
		PaginaPadreActual=window.opener.location.toString();
		if (PaginaPadreLlamada!=PaginaPadreActual)
			window.close();
	}
}

function CompruebaCierre()
{
	PaginaPadreLlamada=window.opener.location.toString();
	setInterval('CompruebaCambio()',2000);		
}

//--------------------------------------------------------------------------------------------------------------------------------------------------
//	Editor HTML para textareas
//
//
//--------------------------------------------------------------------------------------------------------------------------------------------------
function EditorHTML(campo) 
{
	miVent = ventana("Inc/editor/editor.jsp?campo="+campo,"editorHTML", "800","450","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=auto,resizable=0")
	miVent.creator = window
}