var iEstatica = 0;			// Utilizada por CambiaGraf()
var ventana;

// Función utilizada para los botones activos
function CambiaGraf(Boton, Imagen_1, Imagen_2) {
	iEstatica = iEstatica == 0 ? iEstatica = 1 : iEstatica = 0;
	if (iEstatica == 0)	{
					Boton.src = Imagen_1;
				}
	else			{
					Boton.src = Imagen_2;
				}
}

function Dale(Boton, Imagen) {
		Boton.src = "images/" + Imagen;
}

function Enviar(formulario, pagina) {
	formulario.action = pagina;
	formulario.submit();
}

function CompruebaInt(caja,mensaje) {
 	
	if (isNaN(caja.value)) {
		caja.value = caja.value.substring(0, caja.value.lenght-1);
		alert(mensaje);	
	} 
	if (caja.value.length != 0) {
		caja.value = Math.abs(caja.value);
	}
}

function CompruebaNumRC(caja,mensaje) {
 	
	if (isNaN(caja.value)) {
		caja.value = caja.value.substring(0, caja.value.lenght-1);
		alert(mensaje);	
	} 
}	

function CompruebaIntRango(caja,mensaje,inicio,fin) {
 	if (isNaN(caja.value)) {
		caja.select();
		alert(mensaje);
		caja.value = caja.value.substring(0, caja.value.lenght-1);	
		return false;
	} 
	if (caja.value.length != 0) {
		if(caja.value<inicio || caja.value>fin){
			caja.select();
			alert(mensaje);
			caja.value="";
			return false;
		}
		else{
			//caja.value = Math.abs(caja.value);
			return true;
		}
	}
}	

function CompruebaTinyInt(caja,mensaje) {
 	
	if (isNaN(caja.value)) {
		caja.value = caja.value.substring(0, caja.value.lenght-1);
		alert(mensaje);	
	} else {
		if (caja.value >= 0 && caja.value < 256) {
			if (caja.value.length != 0) {
				caja.value = Math.abs(caja.value);
			}
		} else {
			caja.value="";
			alert(mensaje);
		}
	}
}	

function CompruebaFloat(caja,mensaje) {
	var r;
	r = ','; 
	caja.value = caja.value.replace(r,".");
		
	if (isNaN(caja.value)) {
		caja.value = '';
		alert(mensaje);	
	} else {
		r = '.'; 
		caja.value=caja.value.replace(r,",");
	} 
}	

function CompruebaImporte(caja,mensaje,mensaje2,fin) {
	var r;
	r = ',';
	var valor;
	valor=caja.value.replace(r,"."); 
	caja.value = caja.value.replace(r,".");
	
	if (isNaN(caja.value)) {
		caja.value = '';
		alert(mensaje);	
	} else {
		r = '.'; 
		caja.value=caja.value.replace(r,",");
	} 
	
	if (caja.value.length != 0) {
		if(parseFloat(valor)>parseFloat(fin)){
			caja.select();
			alert(mensaje2);
			caja.value="";
			return false;
		}
		else{
			return true;
		}
	}
}

function CompruebaImporte2(caja,mensaje,mensaje2,fin) {
	var r;
	r = ',';
	var valor;
	valor=caja.value.replace(r,"."); 
	caja.value = caja.value.replace(r,".");
	
	if (isNaN(caja.value)) {
		caja.value = '';
		alert(mensaje);	
	} else {
		r = '.'; 
		caja.value=caja.value.replace(r,",");
	} 
	
	if (caja.value.length != 0) {
		if(parseFloat(valor)>parseFloat(fin)){
			alert(mensaje2);
			return false;
		}
		else{
			return true;
		}
	}
}

function CompruebaPorcentaje(caja) {
	var r;
	r = ','; 
	caja.value = caja.value.replace(r,".");
		
	if (isNaN(caja.value)) {
		caja.value = '';
		alert("El valor insertado no es número.");	
	} else {
		if (caja.value > 100 || caja.value < 0) {
			caja.value = '';
			alert("El valor debe ser un porcentaje.");
		} else {
			r = '.'; 
			caja.value=caja.value.replace(r,",");
		}
	} 
}	

function ComprobarCamposVacios(Formu,mensaje) {
	for(i=1; i < ComprobarCamposVacios.arguments.length; i++) {
		if (ComprobarCamposVacios.arguments[i].length == 0) {
			alert(mensaje);
			//ComprobarCamposVacios.arguments[i].focus();
			return;
		}
	}
	Formu.submit();
}

function ComprobarCamposVaciosConvo(Formu,mensaje,mensajeConfirmacion) {
	for(i=1; i < ComprobarCamposVaciosConvo.arguments.length; i++) {
		if (ComprobarCamposVaciosConvo.arguments[i].length == 0) {
			alert(mensaje);
			//ComprobarCamposVaciosConvo.arguments[i].focus();
			return;
		}
	}
	if (!Formu.activa.checked){
		if (confirm(mensajeConfirmacion))
			Formu.submit();
	}else{
		Formu.submit();
	}
}

function ComprobarCamposVaciosPublico(Formu,mensaje) {
	for(i=1; i < ComprobarCamposVaciosPublico.arguments.length; i++) {
		if (ComprobarCamposVaciosPublico.arguments[i].length == 0) {
			alert(mensaje);
			//ComprobarCamposVaciosPublico.arguments[i].focus();
			return;
		}
	}
	Formu.action="FichaInscripcionPublico.asp"
	Formu.submit();
}

function ComprobarCamposVaciosReservaPublico(Formu,mensaje) {
	for(i=1; i < ComprobarCamposVaciosReservaPublico.arguments.length; i++) {
		if (ComprobarCamposVaciosReservaPublico.arguments[i].length == 0) {
			alert(mensaje);
			//ComprobarCamposVaciosReservaPublico.arguments[i].focus();
			return;
		}
	}
	Formu.action="FichaReservaPublico.asp"
	Formu.submit();
}

function EstaVacio(Formu,mensaje) {
	for(i=1; i < EstaVacio.arguments.length; i++) {
		if (EstaVacio.arguments[i].length == 0) {
			return true;
		}
	}
	return false;
}

function Seleccionar(Tabla, CampoId, CampoRep, Titulo, Valor, bId, Where) {
	var sCampos = new String();
	var i;
	var ventana;

	for(i=7; i < Seleccionar.arguments.length; i++) {
		if (i == Seleccionar.arguments.length - 1) {
			sCampos += Seleccionar.arguments[i];
		} else {
			sCampos += Seleccionar.arguments[i] + ",";
		}
	}
	window.open("Seleccionar.asp?Tabla=" + Tabla + "&Consulta=" + sCampos + "&CampoId=" + CampoId + "&CampoRep=" + CampoRep + "&Titulo=" + Titulo + "&Valor=" + Valor + "&bId=" + bId + "&Where=" + Where, "", "status=yes; scrollbars=yes");
}

function BorrarCajas() {
	for(i=0; i < BorrarCajas.arguments.length; i++) {
		BorrarCajas.arguments[i].value = '';
	}
}

function ComprobarNumeroEntorno(caja, minimo, maximo) {
	if (isNaN(caja.value)) {
		caja.value = caja.value.substring(0, caja.value.lenght-1);
		alert("El valor insertado no es número.");	
	} else {
		if (caja.value < minimo && caja.value > maximo) {
			caja.value = "";
		}
	}
}

function SeleccionarCodigo(Valor) {
	window.open("Seleccionar_CodigoPostal.asp?Valor=" + Valor, "", "status=yes; scrollbars=yes");
}

function CambiarColor(campo,campoFormato) {
	window.open("colores.asp?campo="+campo+"&campoFormato="+campoFormato,"","scrollbars=yes,menubar=no,width=650,height=420");
	//Funcion que llama a la página donde se seleccionan los colores.
}

function muestraMenu(direcc,ven,param){
	var ventana;
	if (ventana!=null) ventana.close();
	ventana=window.open(direcc, ven, param);
	ventana.resizeTo(210,490);
	//ventana.moveTo(1,1);
	ventana.moveTo(1,window.screen.height-window.screen.availHeight);
	ventana.focus();
}

function abreventana(direcc, ven, param){
    ventana=window.open(direcc, ven, param);
    ventana.moveTo(170,100);
	ventana.focus();
}

function abreventanaTema(direcc, ven, param){
    ventana=window.open(direcc, ven, param);
    //ventana.moveTo(170,100);
	ventana.focus();
}

function abreventanaPosicion(direcc, ven, param, x, y){
    ventana=window.open(direcc, ven, param);
    ventana.moveTo(x,y);
	ventana.focus();
}

function abreventanaMaterial(direcc, ven, param){
    ventana=window.open(direcc, ven, param);
	ventana.focus();
}

function abreVentanaModal(direcc, ven, param){
    window.showModalDialog(direcc, ven, param);
}

function entrarCampus(formu,mensaje){
	if (window.event.keyCode ==13){
		for(i=1; i < entrarCampus.arguments.length; i++) {
			if (entrarCampus.arguments[i].length == 0) {
				alert(mensaje);
				return;
			}
		}
		window.event.returnValue = false;
		//formu.action = "EntradaPrivada.asp";
		formu.method = "Post";
		formu.submit();
	}
}

function compruebaNIF(nif,mensaje){
	var valor,valorDado,valorCorrecto,dni,sdado,scorrecto;
	valorDado=nif.toUpperCase();
	dni=nif.substring(0,nif.length-1);
	if(isNaN(dni)){
		alert(mensaje);
	}else{
		valorCorrecto=dni+"TRWAGMYFPDXBNJZSQVHLCKE".substring(dni%23,dni%23+1);
		if (valorCorrecto!=valorDado){
			alert(mensaje);
		}
	}	
}

function foco(){
 /*
 if(document.frmTomarFoco.fin!=null)
 {
  //alert('paso');
  document.frmTomarFoco.fin.focus();
 }
 else{	
	//alert('NO paso');
 */

	if (document.forms.length>0){
		var i;
		for(i=0;i<document.forms.length;i++){
			if (document.forms[i].elements.length>0){
				var j;
				for(j=0;j<document.forms[i].elements.length;j++){
					tipo=document.forms[i].elements[j].type;
					if ((tipo=="text"||tipo=="file")&& document.forms[i].elements[j].style.visibility!='hidden' ){
						//alert(document.forms[i].elements[j].style.visibility);
						try{
							document.forms[i].elements[j].focus();
						}catch(ex){}
						return true;
					}
				}
			}
		}
	} 
 }

/*
}
*/


function validaFecha(objName,mensaje) {
	var datefield = objName;
	objName.value=objName.value.replace(".","");
	if (chkdate(objName) == false) {
		datefield.select();
		alert(mensaje);
		datefield.focus();
		datefield.value=""
		return false;
	}
	else {
		return true;
	}
}

function validaMesAnyo(formu,mes,anyo,mensaje){
	if (isNaN(mes)||isNaN(anyo)) {
			alert(mensaje);
			formu.mes.focus();
			formu.mes.value="";
			formu.anyo.value="";
			return;
	}
	if (mes>12 || mes<1) {
			alert(mensaje);
			formu.mes.focus();
			formu.mes.value="";
			formu.anyo.value="";
			return;
	}
	formu.submit();
}

function chkdate(objName) {
	var strDatestyle = "EU";  //European date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var datefield = objName;
	var err = 0;
	strDate = datefield.value;
	if (strDate.length < 1) {
		return true;
	}
	strDateArray = strDate.split("/");
	strDay = strDateArray[0];
	strMonth = strDateArray[1];
	strYear = strDateArray[2];

	intday = parseInt(strDay, 10);
	if (isNaN(strDay)) {
		err = 2;
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(strMonth)) {
			err = 3;
			return false;
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(strYear)) {
		err = 4;
		return false;
	}
	if (intYear<1975){
		err = 5;
		return false;
	}
	if (intMonth>12 || intMonth<1) {
		err = 5;
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
		err = 6;
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
		err = 7;
		return false;
	}
	if (intMonth == 2) {
		if (intday < 1) {
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true) {
			if (intday > 29) {
				err = 9;
				return false;
			}
		}
	else {
		if (intday > 28) {
			err = 10;
			return false;
		}
	}
	}
	return true;
	}
	function LeapYear(intYear) {
	if (intYear % 100 == 0) {
	if (intYear % 400 == 0) { return true; }
	}
	else {
	if ((intYear % 4) == 0) { return true; }
	}
	return false;
}

function entrar(fuente) {
  if (fuente.className == "filaNormal"){
	fuente.className = "filaSeleccionada";
  }else{
	if (fuente.className == "filaNormalForo"){
		fuente.className = "filaSeleccionadaForo";
	}
  }
}

function entrarDisponibles(fuente) {
  if (fuente.className == "filaNormalDisponibles"){
	fuente.className = "filaSeleccionadaDisponibles";
  }
}


function entrarCabecera(fuente) {
  if (fuente.className == "filaCabecera"){
	fuente.className = "filaSeleccionada";
  }else{
	if (fuente.className == "filaNormalForo"){
		fuente.className = "filaSeleccionadaForo";
	}
  }
}

function salirDisponibles(fuente) {
	if (fuente.className == "filaSeleccionadaDisponibles") {
		fuente.className = "filaNormalDisponibles";
	}	
}

function salir(fuente) {
	if (fuente.className == "filaSeleccionada") {
		fuente.className = "filaNormal";
	}else{
	  if (fuente.className == "filaSeleccionadaForo") {
		fuente.className = "filaNormalForo";
	  }
	}	
}

function salirCabecera(fuente) {
	if (fuente.className == "filaSeleccionada") {
		fuente.className = "filaCabecera";
	}else{
	  if (fuente.className == "filaSeleccionadaForo") {
		fuente.className = "filaNormalForo";
	  }
	}	
}


function desmarcar(chk){
	var elementos=document.getElementsByName(chk);
	for (i=0;i<elementos.length;i++){
		elementos[i].checked=false;
	}
}

function validarFechaInicioFin(fechaInicio, fechaFin, mensaje){
	var arrayFecha; 
	arrayFecha= fechaFin.value.split("/");
	var DiaFin; 
	DiaFin = parseInt(arrayFecha[0],10);
	var MesFin; 
	MesFin = parseInt(arrayFecha[1],10);
	var AnioFin; 
	AnioFin = parseInt(arrayFecha[2],10);
	arrayFecha= fechaInicio.value.split("/");
	var DiaInicio;
	DiaInicio = parseInt(arrayFecha[0],10);
	var MesInicio;
	MesInicio = parseInt(arrayFecha[1],10);
	var AnioInicio;
	AnioInicio = parseInt(arrayFecha[2],10);	
	

			if (AnioInicio > AnioFin){
				fechaInicio.select();
				alert(mensaje);
				fechaInicio.value="";
				return false;
			}else{
				if (AnioInicio == AnioFin){
					if (MesInicio > MesFin){
						fechaInicio.select();
						alert (mensaje);
						fechaInicio.value="";
						return false;
					}else{
						if (MesInicio == MesFin){
							if (DiaInicio > DiaFin){
								fechaInicio.select();
								alert (mensaje);
								fechaInicio.value="";
								return false;
							}
						}
					}
				}
			}
		return true;
}


function ComprobarCamposVaciosRespuestas(Formu,mensaje,respuestas) {
	if (respuestas.length>0){
		for(i=0; i < respuestas.length; i++) {
		
			if (respuestas[i].value.length == 0 && eval('Formu.rutagrafico' + eval(i + 1) + '.value.length') == 0) {
				alert(mensaje);
				return;
			}
		}
	}else{
		if(respuestas.value.length==0 && Formu.rutagrafico1.value.length==0){
			alert(mensaje);
			return;
		}
	}
	Formu.submit();
}

function ComprobarCamposVaciosRespuesta(Formu,mensaje,respuesta,ruta) {
	if (respuesta.length == 0 && ruta.length == 0) {
		alert(mensaje);
		return;
	}
	Formu.submit();
}

function ComprobarCamposVaciosPregunta(Formu,mensaje,textoBusqueda,pregunta,ruta) {
	if (textoBusqueda.length == 0) {
		alert(mensaje);
		return;
	}	
	if (pregunta.length == 0 && ruta.length == 0) {
		alert(mensaje);
		return;
	}
	Formu.submit();
}

function ComprobarCamposFechaRango(Formu,fechaInicio,fechaFin,mensajeFecha,mensajeVacio){
	if (validarFechaInicioFin(fechaInicio,fechaFin,mensajeFecha)){
		ComprobarCamposVacios(Formu,mensajeVacio,fechaInicio.value,fechaFin.value)
	}
}

function ComprobarHorasCalendario(horaInicio,minutosInicio,horaFin,minutosFin,mensajeRango,mensaje){
	var correcto=false;
	if(!CompruebaIntRango(horaInicio,mensajeRango,0,23) || !CompruebaIntRango(horaFin,mensajeRango,0,23) || !CompruebaIntRango(minutosInicio,mensajeRango,0,59) || !CompruebaIntRango(minutosFin,mensajeRango,0,59)){
		correcto=false;
	}else{
		correcto=true;
	}
	if(correcto){
		if (parseInt(horaInicio.value,10) > parseInt(horaFin.value,10)){
				horaInicio.select();
				alert(mensaje);
				horaInicio.value="";
				return false;
		}else{
				if (parseInt(horaInicio.value,10) == parseInt(horaFin.value,10)){
					if (parseInt(minutosInicio.value,10) > parseInt(minutosFin.value,10)){
						minutosInicio.select();
						alert (mensaje);
						minutosInicio.value="";
						return false;
					}else{
							return true;
					}
				}else{
					return true;
				}
		}
	}else{
		return false;
	}
}

function ComprobarHoraCorrecta(hora,mensaje)
{

	var correcto=false;
	if (!CompruebaIntRango(hora,mensaje,0,23))
	{
		correcto=false;
	}
	else
	{
		correcto=true;
	}
	return correcto;
}

function ComprobarMinutosCorrectos(minutos,mensaje)
{
	var correcto=false;
	if (!CompruebaIntRango(minutos,mensaje,0,59))
	{
		correcto=false;
	}
	else
	{
		correcto=true;
	}
	return correcto;
}
function abreventanaPosicion(direcc, ven, param, x, y){
    ventana=window.open(direcc, ven, param);
    ventana.moveTo(x,y);
	ventana.focus();
}

function abreventanaTutorias(direcc, venta, param, x, y){
	var ventanaTutoria;
	if(ventanaTutoria==null){
		try{
			ventanaTutoria=window.open(direcc, venta, param);
			ventanaTutoria.moveTo(x,y);
			ventanaTutoria.focus();
		}catch(ex){}
	}else{
		ventanaTutoria.close();
	}
}

function cierraMenu(direcc, ven, param){
	var ventana;
	ventana=window.open(direcc, ven, param);
	ventana.close();
}

function CompruebaAlfanumerico(campo,mensaje) {
	var valid = "abcdefghijklmnopqrstuvwxyzñÑABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	var ok = "yes";
	var temp;
	for (var i=0; i<campo.value.length; i++) {
		temp = "" + campo.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") {
		alert(mensaje);
		campo.focus();
   }
}

function ComprobarHorasConexion(horaInicio,minutosInicio,horaFin,minutosFin){

		if (parseInt(horaInicio,10) > parseInt(horaFin,10)){
				return false;
		}else{
				if (parseInt(horaInicio,10) == parseInt(horaFin,10)){
					if (parseInt(minutosInicio,10) > parseInt(minutosFin,10)){
						return false;
					}else{
							return true;
					}
				}else{
					return true;
				}
		}
}

function exit()
{
    var top;
    var closeNav = false;
    
    if (document.all) //ie
    {
    	top = self.screenTop;
		if (top > 9000)
			closeNav = true;
    }
    else
    {
    	top = window.outerWidth;
    	if (top == 0)
			closeNav = true;
	}

	if (closeNav)
		window.open('exit.asp','hastaPronto','width=250,height=150,top=100,left=100,toolbar=no,menubar=no,scrollbars=no,resizeable=no,location=no,status=no');
}
