function MostrarOcultar(elem, boton) {
	if (document.getElementById(elem).className == 'on')
	{
	document.getElementById(elem).className = 'off';
	document.getElementById(boton).className= 'ocultar';
	}
	else{
	document.getElementById(elem).className = 'on';
	document.getElementById(boton).className= 'mostrar';
	}
}
/*MODIFICADO PARA QUE CIERRE TODOS*/
function CerrarResto() {
	divs = document.getElementsByTagName('div');
		for(var i=0; i<divs.length; i++){
		if (divs[i].className =="on")
		{
			divs[i].className="off";
		}
		if (divs[i].className =="mostrar")
		{
			divs[i].className="ocultar";
		}				
	}
}
/**/
function Mostrar(elem) {
	document.getElementById(elem).className = 'on';
	}
	
function MostrarOcultarCerrar(elem, boton) {
	if (document.getElementById(elem).className == 'on')
	{
	document.getElementById(elem).className = 'off';
	document.getElementById(boton).className= 'ocultar';
	}
	else{
	CerrarResto();
	document.getElementById(elem).className = 'on';
	document.getElementById(boton).className= 'mostrar';
	}
}
/*  /MODIFICADO PARA QUE CIERRE TODOS*/
function EstaVacio(cadena)
{
	var i;
	var c;
	var longitud;

	longitud = cadena.length;

		// Se buscan caracteres que no sean espacios
	for (i=0 ; i<longitud ; i++)
		{
		c = cadena.charAt(i);
		if ( c != " " )
			return false;
		}

	return true;
}

function EsNumeroEnteroSinSigno(cadena)
{
	var	numero = /^\d+$/

	return numero.test(cadena)
}

function FechaCorrecta(fecha)
{
      // 0123456789
      // Se espera que fecha sea una cadena en formato "DD-MM-AAAA"
      var   dd, mm, aaaa;
      var dias;
	  var sep1, sep2;

      if ( EstaVacio(fecha) )
            return true;

      if ( fecha.length != 10 )
            return false;
      
      dias = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
      
      dd = fecha.substring(0, 2);
	  sep1 = fecha.substring(2,3);
      mm = fecha.substring(3, 5);
  	  sep2 = fecha.substring(5,6);
      aaaa = fecha.substring(6, 10);

	  if( sep1 != "/" )
	  		return false
	  if( sep2 != "/" )
	  		return false
      if ( !EsNumeroEnteroSinSigno(dd) )
            return false;
      if ( !EsNumeroEnteroSinSigno(mm) )
            return false;
      if ( !EsNumeroEnteroSinSigno(aaaa) )
            return false;
            
      if ( mm < 1 || mm > 12 )
            return false;
            
      mm--;
      
      if ( (aaaa % 4) == 0 )
            dias[1] = 29;
            
      if ( dd < 1 || dd > dias[mm] )
            return false;
            
      return true;
}


var tituloPagina = "Grupo Obras Especiales"; 
var ventanaNueva;
var cont=0;

function Nventana(imagen,titulo)
{
if(cont==1){ventanaNueva.close();ventanaNueva=null}
ventanaNueva=window.open('','ventana','resizable=yes,scrollbars=no')
if(titulo==null){titulo=tituloPagina}
ventanaNueva.document.write('<html><head><title>' + titulo + '</title></head><body style="overflow:hidden" marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" scroll="no" onUnload="opener.cont=0" onclick="window.close()"><img src="' + imagen + '" onLoad="opener.ajuste(this.width, this.height+15)">');
ventanaNueva.document.close();
cont++;
}

function ajuste(ancho, alto)
{
ventanaNueva.resizeTo(ancho,alto);
ventanaNueva.moveTo((screen.width-ancho)/2,(screen.height-alto)/2);
}
/*validar busqueda formulario proyectos*/
	function optionValida(value){
		if ( value=="NO"){
			alert("Su selección no se trata de un tipo de obra válido, elija una opción de segundo nivel.")
			document.FormBuscador.id_t_obra.options[0].selected = true;
			document.FormBuscador.id_t_obra.focus();
		}
	}
	
	function seleccionarOpt(value){
		var i = 0;
		for ( i; i<document.FormBuscador.id_comunidad.length; i++){
			if (document.FormBuscador.id_comunidad.options[i].value==value){
				document.FormBuscador.id_comunidad.options[i].selected = true
			}
		}
		document.FormBuscador.id_comunidad.focus();
	}
	
	
	function validar (){
		/*
		with (document.FormBuscador){
			if (( nom_proyecto.value=="" || nom_proyecto.value=="Introduzca nombre de obra" || nom_proyecto.value=="Obraren izena") & id_empresa.selectedIndex==0 & id_t_obra.selectedIndex==0 & id_cliente.selectedIndex==0 & id_comunidad.selectedIndex==0 & (f_desde.value=="" || f_desde.value=="dd/mm/aaaa") & (f_hasta.value=="" || f_hasta.value=="dd/mm/aaaa")){
				alert("Elija al menos un criterio de búsqueda.");
				return false;
			}else if ( (f_desde.value !="" && f_desde.value != "dd/mm/aaaa") && !FechaCorrecta(f_desde.value)){
				alert("La fecha desde no es correcta.");
				return false;
			}else if ( (f_hasta.value !="" && f_hasta.value != "dd/mm/aaaa") && !FechaCorrecta(f_hasta.value)){
				alert("La fecha hasta no es correcta.");
				return false;
			}
		}*/
		return true;
	}
	
	function validar_eu (){
		/*
		with (document.FormBuscador){
			if (( nom_proyecto.value=="" || nom_proyecto.value=="Obraren izena") & id_empresa.selectedIndex==0 & id_t_obra.selectedIndex==0 & id_cliente.selectedIndex==0 & id_comunidad.selectedIndex==0 & (f_desde.value=="" || f_desde.value=="ee/hh/uuuu") & (f_hasta.value=="" || f_hasta.value=="ee/hh/uuuu")){
				alert("Hauta ezazu bilaketa irizpide bat gutxienez");
				return false;
			}else if ( (f_desde.value !="" && f_desde.value != "ee/hh/uuuu") && !FechaCorrecta(f_desde.value)){
				alert("Akatsen bat gertatu da");
				return false;
			}else if ( (f_hasta.value !="" && f_hasta.value != "ee/hh/uuuu") && !FechaCorrecta(f_hasta.value)){
				alert("Akatsen bat gertatu da");
				return false;
			}
		}*/
		return true;
	}
	function validar_en (){
		return true;
	}	
/*  /validar busqueda formulario proyectos*/

/**************************************************************************************************
Funcion para validar formularios GENERICO
	Campo Obligatorio: class="obligatorio"
	Campo mail: class="obligatorio mail"
	Nota: Primero te comprueba los campos de tipo input y despues los campos de tipo textarea
**************************************************************************************************/

function validar_formulario(nombreformulario){
	var pattern = /^[a-zA-Z0-9\-\._]+@[a-zA-Z0-9\-_]+(\.?[a-zA-Z0-9\-_]*){2,3}\.[a-zA-Z]{2,3}$/;
	var class_obligatorio = "obligatorio";
	var class_mail = "mail";
	alert(nombreformulario);
	
	
	var obj = document.getElementById(nombreformulario).getElementsByTagName("fieldset");
	for (i=0; i<obj.length; i++){//Bucle para los fieldset
		for (var b=0; b<obj[i].childNodes.length; b++) {//Bucle para los hijos de cada fieldset
			var campo = obj[i].childNodes[b];

			if (existe_cadena(campo.className,class_obligatorio)){//Comprobar si es obligatorio
				//Si es un input
				if (campo.tagName == "INPUT") {
					if (campo.type == "text") {
						if(campo.value=='' || campo.value==campo.getAttribute("title")){
							alert(campo.getAttribute("title"));
							campo.focus();
							return false;
						}
						
						//Si el campo es un mail
						if (existe_cadena(campo.className,class_mail)){
							if (!pattern.test(campo.value) ){//Si el formato es incorrecto
								alert(campo.getAttribute("title"));
								campo.focus();
								return false;
							}
						}
						
					}
					if (campo.type == "checkbox") {
					   if (!campo.checked) {
						  alert(campo.getAttribute("title"));
						  return false;
						  
					   }
					}
		
				 }//Fin Si es input
				 
				 //Si es un textarea
				if (campo.tagName == "TEXTAREA") {
					var Str = campo.value;
						if (Str.length == 0 || campo.value==campo.getAttribute("title")){
							alert(campo.getAttribute("title"));
							campo.focus();
							return false;
						}
				}
				//Si es un select
				if (campo.tagName == "SELECT") {
					if(campo.options[campo.selectedIndex].text=='' || campo.options[campo.selectedIndex].text==campo.getAttribute("title")){
						alert(campo.getAttribute("title"));
						campo.focus();
						return false;
					}
				}
				

				 
			}//Fin si es obligatorio
			
		}
	}
	document.getElementsByTagName("form").submit();//Poner cuando no lleva boton submit	
}
function validar_formulario_Forms(nombreformulario){
	var pattern = /^[a-zA-Z0-9\-\._]+@[a-zA-Z0-9\-_]+(\.?[a-zA-Z0-9\-_]*){2,3}\.[a-zA-Z]{2,3}$/;
	var class_obligatorio = "obligatorio";
	var class_mail = "mail";
	
	
	var obj = document.getElementById(nombreformulario).getElementsByTagName("fieldset");
	for (i=0; i<obj.length; i++){//Bucle para los fieldset
		for (var b=0; b<obj[i].childNodes.length; b++) {//Bucle para los hijos de cada fieldset
			var campo = obj[i].childNodes[b];

			if (existe_cadena(campo.className,class_obligatorio)){//Comprobar si es obligatorio
				//Si es un input
				if (campo.tagName == "INPUT") {
					if (campo.type == "text") {
						if(campo.value=='' || campo.value==campo.getAttribute("title")){
							alert(campo.getAttribute("title"));
							campo.focus();
							return false;
						}
						
						//Si el campo es un mail
						if (existe_cadena(campo.className,class_mail)){
							if (!pattern.test(campo.value) ){//Si el formato es incorrecto
								alert(campo.getAttribute("title"));
								campo.focus();
								return false;
							}
						}
						
					}
					if (campo.type == "checkbox") {
					   if (!campo.checked) {
						  alert(campo.getAttribute("title"));
						  return false;
						  
					   }
					}
		
				 }//Fin Si es input
				 
				 //Si es un textarea
				if (campo.tagName == "TEXTAREA") {
					var Str = campo.value;
						if (Str.length == 0 || campo.value==campo.getAttribute("title")){
							alert(campo.getAttribute("title"));
							campo.focus();
							return false;
						}
				}
				//Si es un select
				if (campo.tagName == "SELECT") {
					if(campo.options[campo.selectedIndex].text=='' || campo.options[campo.selectedIndex].text==campo.getAttribute("title")){
						alert(campo.getAttribute("title"));
						campo.focus();
						return false;
					}
				}
				

				 
			}//Fin si es obligatorio
			
		}
	}
	document.getElementsByTagName("form").submit();//Poner cuando no lleva boton submit	
}


function existe_cadena(cadena1,cadena2){

	if(cadena1==null){
			return false;
	}
	if(cadena1.indexOf(cadena2)!=-1){
		return true;
	}else{
		return false;
	}
}

/********************  /Funcion para validar formularios ******************************************/
