//////////////////////////////////////////////////////
//Variable que indica si se ha tocado algún campo del formulario
//////////////////////////////////////////////////////
var LogCambios;
top.LogCambios=false;
//alert(top.LogCambios)

// variable que indica si en los formularios esta deshabilitada la vuelta atras (ver validaciones.js)
//var LogNoVueltaAtras;
//LogNoVueltaAtras=true;


function cambio()
{
	top.LogCambios = true;
}

//Sólo funciona en Explorer 4+
function salirEducadamente()
{
	if (LogCambios)	
		window.event.returnValue = "Si sale de la página sin guardar los datos se perderán las modificaciones."; 
}

function comprobarCambios()
{
	if (top.LogCambios && estoyEnFormulario)
	{
		if (!confirm("Si no pulsa el botón \"Guardar\" antes de abandonar la pantalla actual \nse perderán los cambios que haya realizado. ¿Desea continuar?"))
			return false;
	}
	return 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;
var estoyEnFormulario=false;

function _enviar(formulario){
	if (formularioEnviado==0){
		formularioEnviado = 1;
		formulario.submit();
		//window.document.body.innerHTML = "<table width=\"100%\" style=\"height:100%\" border=\"0\"><tr><td class=\"txtDes\" style=\"text-align:center;vertical-align:middle\">Cargando la página...<br /><br />Por favor espere</td></tr></table>";
		window.document.body.innerHTML = "<div style=\"width:100%;height:100%;text-align:center;vertical-align:middle\"><table style=\"margin-top:25%;border:1px solid #666666;background-color:#ffffff\" cellpadding=\"8\"><tr><td style=\"text-align:center;color:#006699;font-size:10pt;font-family:Arial\">Cargando datos</td></tr><tr><td><img src=\"img/cargando.gif\" /></td></tr></table></div>";
	}
}

function _eliminar(StrMensaje, StrDestino)
{	
	if (confirm(StrMensaje))
	{
		var formulario = window.document.forms["form1"];	
		formulario.action = StrDestino;
		formulario.submit();
	}														
}	

function _restaurar(StrMensaje, StrDestino)
{	
	if (!LogCambios)
		alert("El formulario contiene los valores iniciales");
	else if (confirm(StrMensaje))
		window.document.location.href=StrDestino																
}

function _volver(StrDestino)
{	
	if (!LogCambios)
		window.document.location.href=StrDestino;																
	else if (confirm("¿Realmente quiere abandonar esta página sin guardar los datos?"))
		window.document.location.href=StrDestino;	
}

function _nuevo(StrDestino)
{		
	window.document.location.href=StrDestino;																
}

function _seleccionar(destino, IntAncho, IntAlto, formulario)
{
	
	self.name = "lanzador";

	if (IntAncho=="")
		IntAncho=500;

	if (IntAlto=="")
		IntAlto=500;

	var PosX = (screen.availWidth - IntAncho)/2;
	var PosY = 100;

	window.open("", "VentanaSeleccion","scrollbars=1,height=" + IntAlto + ",width=" + IntAncho + ",left=" + PosX + ",top=" +PosY);
	formulario.target = "VentanaSeleccion";
	formulario.action = destino;
	formulario.submit();
}

function _seleccionarproyecto(IntAncho, IntAlto, formulario)
{
	
	self.name = "VentanaSeleccion";

	if (IntAncho=="")
		IntAncho=500;

	if (IntAlto=="")
		IntAlto=500;

	var PosX = (screen.availWidth - IntAncho)/2;
	var PosY = 100;

	window.open("", "VentanaSeleccionProyecto","scrollbars=1,height=" + IntAlto + ",width=" + IntAncho + ",left=" + PosX + ",top=" +PosY);
	formulario.target = "VentanaSeleccionProyecto";
	formulario.action = "seleccion.proyecto.asp";
	formulario.submit();
}

function _avisar(IdAviso, IntAncho, IntAlto, formulario)
{
	
	self.name = "lanzador";

	if (IntAncho=="")
		IntAncho=500;

	if (IntAlto=="")
		IntAlto=400;

	var PosX = (screen.availWidth - IntAncho)/2;
	var PosY = (screen.availHeight - IntAlto)/2;

	window.open("", "VentanaAviso","height=" + IntAlto + ",width=" + IntAncho + ",left=" + PosX + ",top=" +PosY);
	formulario.target = "VentanaAviso";
	formulario.action = "aviso.asp?id=" + IdAviso;
	formulario.submit();
}


function ventana(StrUrl, IntAncho, IntAlto, StrOtros)
{
	
	if (IntAncho=="")
		IntAncho=500;

	if (IntAlto=="")
		IntAlto=500;

	  var PosX = (screen.availWidth - IntAncho)/2;
	  var PosY = (screen.availHeight - IntAlto)/2;

	return(window.open(StrUrl, "","height=" + IntAlto + ",width=" + IntAncho + ",left=" + PosX + ",top=" +PosY + "," + StrOtros));
}

// ---------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------
//Abre una página nueva con nombre centrada en la página desde la que se ha abierto
// ---------------------------------------------------------------------------------------------------------------
function ventana2(StrUrl,StrNombre,IntAncho, IntAlto, StrOtros)
{

	if (IntAncho=="")
		IntAncho=750;

	if (IntAlto=="")
		IntAlto=500;

	var PosX = (screen.availWidth - IntAncho)/2;
	var PosY = (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 mostrarCalendario(caja)
{
	var calendar
	var miHeight;
	var miWidth;
	
	if (navigator.appName == "Netscape")
	{
		miHeight = 280;
		miWidth = 220;
	} else {
		miHeight = 225;
		miWidth = 220;
	}
	
	window.dateField = eval("document.form1."+caja);
	//calendar = window.open("./Inc/calendario.htm","cal","WIDTH="+miWidth+",HEIGHT="+miHeight+",SCROLLBARS=NO,top=200,left=200");

	calendar = ventana("./Inc/calendario.htm", miWidth, miHeight, "scrollbars=1")

	return false;
}

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;
} 

//--------------------------------------------------------------------------------------------------------------------------------------------------
//	Editor HTML para textareas
//
//
//--------------------------------------------------------------------------------------------------------------------------------------------------

function EditorHTML(campo) {
 miVent = ventana("inc/editor/editor.asp?campo="+campo,"800","450","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=auto,resizable=0")
// miVent = ventana("Inc/editor/editor.asp?campo="+campo,"660","320","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=auto,resizable=0")
 miVent.creator = window
}


// ---------------------------------------------------------------------------------------------------------------
//Autoajusta el alto de un iframe a su contenido
// ---------------------------------------------------------------------------------------------------------------

 function resizeIframe() {
	var i;
	// Must launched on the body onload event handler for IE
	// Use document.documentElement if you are in Compat mode
	i = parent.document.getElementById(window.name)
	iHeight = document.body.scrollHeight
//	if (iHeight>1)
		i.style.height = iHeight + 5 + "px"
}


// ---------------------------------------------------------------------------------------------------------------
//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);		
}


function enConstruccion()
{
	alert('En construcción. En breve dispondrá de acceso a esta sección.');
}

// ---------------------------------------------------------------------------------------------------------------
// Marca/desmarca todos los elementos de un check list
// ---------------------------------------------------------------------------------------------------------------
function MarcarTodosCheck(campo, valor)
{
var i;
	
	if (eval(campo[0]))
	{
		for (i = 0; i < campo.length; i++) 
			campo[i].checked = valor ; 
	}
	else
	{		
	   campo.checked = valor ; 
	}
}

