/* popup centrat */
function popup(url,windowname,width,height,features) { 
width=(width)?width:screen.width/2; 
height=(height)?height:screen.height/2; 
var screenX = (screen.width/2 - width/2); 
var screenY = (screen.height/2 - height/2); 
if (features) {
var features= "width=" + width + ",height=" + height + "," + features; 	
} 
else {
var features= "width=" + width + ",height=" + height +",scrollbars=yes,status=no,resizable=no"; 
}
features += ",screenX=" + screenX + ",left=" + screenX; 
features += ",screenY=" + screenY + ",top=" + screenY; 

var mywin=window.open(url, windowname, features); 
if (mywin) 
mywin.focus(); 
return mywin; 
} 
function checkear_extension(fichero)	{
	function prever() {
		return;
	}

	function no_prever() {
		alert("Formato de foto no compatible");
		limpiar();
		
	}

	(/\.(jpg|jpeg|gif)$/i.test(fichero.value)) ? prever() : no_prever();
}

function limpiar()	{
	
	foto = document.getElementById("foto1");	
	nouFile = document.createElement("input");
	nouFile.id = foto.id;
	nouFile.type = "file";
	nouFile.name = "foto1";
	nouFile.className = "camp_foto_pujar";
	nouFile.value = "";
	nouFile.onchange = foto.onchange;
	nodoPadre = foto.parentNode;
	nodoSiguiente = foto.nextSibling;
	nodoPadre.removeChild(foto);
	(nodoSiguiente == null) ? nodoPadre.appendChild(nouFile):nodoPadre.insertBefore(nouFile, nodoSiguiente);
}

function comprueba_extension(formulario, archivo) {
	
		extensiones_permitidas = new Array(".gif", ".jpg");
		mierror = "";
		if (!archivo) {
			//Si no tengo archivo, es que no se ha seleccionado un archivo en el formulario
			formulario.submit();
			return 1;
		}else{
			//recupero la extensión de este nombre de archivo
			extension = (archivo.substring(archivo.lastIndexOf("."))).toLowerCase();
			//alert (extension);
			//compruebo si la extensión está entre las permitidas
			permitida = false;
			for (var i = 0; i < extensiones_permitidas.length; i++) {
				if (extensiones_permitidas[i] == extension) { 
					permitida = true;
					break;
				}
			}
			if (!permitida) {
				mierror = "Comprueba la extensión de los archivos a subir. \nSólo se pueden subir archivos con extensiones: " + extensiones_permitidas.join();
			}else{
				//submito!
				formulario.submit();
				return 1;
			}
		}
		
		//si estoy aqui es que no se ha podido submitir
		alert (mierror);
		return false;
	
}
function validar() {
	txt_candado = document.getElementById("txt_candado").value;
	
	candado1  = document.getElementById("candado1").checked;
	candado2  = document.getElementById("candado2").checked;
	candado3  = document.getElementById("candado3").checked;
		
	id_zona = document.getElementById("id_zona").value;		
			
			
	if (txt_candado!="" && id_zona>"0" && (candado1==true || candado2==true || candado3==true )){	
		frm_candado.submit();
	}else{
		alert("escoge un candado, un lugar y escribe un texto para el candado");
	}
}


function validar3() {
	txt_candado = document.getElementById("txt_candado").value;	
	
	
	candado1  = document.getElementById("candado1").checked;
	candado2  = document.getElementById("candado2").checked;
	candado3  = document.getElementById("candado3").checked;			
	if (txt_candado!="" && (candado1==true || candado2==true || candado3==true )){	
		frm_candado.submit();
	}else{
		alert("escoge un candado y escribe un texto para el candado");
	}
}


function validar2() {
	
	id_provincia = document.getElementById("id_provincia").value;	
	v_nom = document.getElementById("v_nom").value;			
	v_nom_amic = document.getElementById("v_nom_amic").value;	
	v_mail = document.getElementById("v_mail").value;
	
	texto = v_mail;
	
	var mailres = true;            
    var cadena = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890@._-";
    
    var arroba = texto.indexOf("@",0);
    if ((texto.lastIndexOf("@")) != arroba) arroba = -1;
    
    var punto = texto.lastIndexOf(".");
                
		for (var contador = 0 ; contador < texto.length ; contador++){
			if (cadena.indexOf(texto.substr(contador, 1),0) == -1){
				mailres = false;
				break;
		}
    }

    if ((arroba > 1) && (arroba + 1 < punto) && (punto + 1 < (texto.length)) && (mailres == true) && (texto.indexOf("..",0) == -1))
    	mailres = true;
    else
    	mailres = false;
	
	
	if (id_provincia>"0" && v_nom!="" && v_nom_amic!=""){	
		if (mailres==true){
			frm_enviar.submit();
		}else{
			alert("error en el E-mail");	
		}
	}else{
		alert("escoge una provincia y los datos de envio");
	}
	
}

/* AJAX BUSCAR */
// JavaScript Document
function nuevoAjax(){
	var xmlhttp=false;
	try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(E){
			xmlhttp = false;
		}
	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	
	return xmlhttp;
}

function buscarDato(){
	resul = document.getElementById('resultado');
	
	bus=document.frmbusqueda.dato.value;
	
	ajax=nuevoAjax();
	ajax.open("POST", "busqueda.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			resul.innerHTML = ajax.responseText
		}
	}
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax.send("busqueda="+bus)

}