// JavaScript Document

// variable para ID del timeOut de fade in de fotos
var fadein;

// genera un enlace a una dirección email de tipo <a class="email" href="mailto:nombre@dominio">nombre@dominio</a>
function protectMail(nombre,dominio) {
	document.write('<a class="email" href="mailto:' + nombre + '@' + dominio + '">' + nombre + '@' + dominio + '</a>');
}

// inicia 1 formulario
function iniciar() {
	document.forms[0].onsubmit = comprobar;
}

// inicia formulario largo
function iniciarLong() {
	document.forms[0].onsubmit = comprobarcontact;
}

// comprobacion de formulario
function comprobar() {
	// Recoge los valores del campo email
	var email = document.forms[0].elements.email.value;
	// comprobacion de email válido
	if(!isEmail(email)) {
		alert("Introduzca una dirección email válida");
		return false;
	}
	else {
		// no hay errores. Se envía el formulario
		return true;
	}
}

// BROWSER DETECTION

// Browser Detection Javascript
// copyright 1 February 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function whichBrs() {
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("opera") != -1) return 'Opera';
	if (agt.indexOf("staroffice") != -1) return 'Star Office';
	if (agt.indexOf("beonex") != -1) return 'Beonex';
	if (agt.indexOf("chimera") != -1) return 'Chimera';
	if (agt.indexOf("netpositive") != -1) return 'NetPositive';
	if (agt.indexOf("phoenix") != -1) return 'Phoenix';
	if (agt.indexOf("firefox") != -1) return 'Firefox';
	if (agt.indexOf("safari") != -1) return 'Safari';
	if (agt.indexOf("skipstone") != -1) return 'SkipStone';
	if (agt.indexOf("msie") != -1) return 'Internet Explorer';
	if (agt.indexOf("netscape") != -1) return 'Netscape';
	if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
	if (agt.indexOf('\/') != -1) {
	if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
	return navigator.userAgent.substr(0,agt.indexOf('\/'));}
	else return 'Netscape';} else if (agt.indexOf(' ') != -1)
	return navigator.userAgent.substr(0,agt.indexOf(' '));
	else return navigator.userAgent;
}

//************** COMIENZO - PÁGINA FOTOS *************************

// función que cambia la imagen ampliada
function ampliar(imagen,imagenGnd,ancho,alto) {
	if(document.getElementById){
		// poner opacidad a 0 y parar fadein activo
		imageId = 'bigview';
		image = document.getElementById(imageId);
		window.clearTimeout(fadein); // interrumpe el fadein activo, si hay
		setOpacity(image, 0);
		// cambiar imagen
		var imgGrande = document.getElementById("bigview");
		imgGrande.anchoAmpli=ancho;
		imgGrande.altoAmpli=alto;
		imgGrande.src = imagen;
		imgGrande.name = imagenGnd;
		// esperar un tiempo antes de iniciar fade in
 		window.setTimeout('initImage()',2000);
	}
}
// función que abre imagen grande en otra ventana
function abrir() {
	var imgGrande = document.getElementById("bigview");
	urlimagen = imgGrande.name;
	var tamanoDefecto = 785;
	if (imgGrande.anchoAmpli==undefined) {
		width = height = tamanoDefecto;
	}
	else {
		width = Number(imgGrande.anchoAmpli);
		height = Number(imgGrande.altoAmpli);
	}
	// ampliamos para incorporar margenes (distintos valores en Firefox que en IE)
	if (whichBrs() == "Firefox") {
		width += 16;
		height += 16;
	}
	else {
		width += 37;
		height += 30;
	}
	window.open(urlimagen,'ampliada','width='+width+',height='+height+',scrollbars=1,resizable=yes,top=50,left=40,dependent=yes,modal=yes');
}
// función que inicializa fade-in de imagen
function initImage() {
 	imageId = 'bigview';
  image = document.getElementById(imageId);
	setOpacity(image, 0);
  image.style.visibility = 'visible';
	fadeIn(imageId,0);
}
// función que pone un objeto a una determinada opacidad -multibrowser-
function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}
// función que efectúa un fade in mediante la función setOpacity
function fadeIn(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
      setOpacity(obj, opacity);
      opacity += 3;
      fadein = window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 1);
    }
  }
}



//************** FIN- PÁGINA FOTOS *************************

// comprobacion de formulario de contacto
function comprobarcontact() {
	// Recoge los valores del campo email
	var email = document.forms[0].elements.email.value;
	// comprobacion de email válido
	if(!isEmail(email)) {
		alert("Introduzca una dirección email válida");
		return false;
	}
	else {
		// no hay errores. Se envía el formulario
		return true;
	}
}

function isEmail(email) {
	var posArroba = email.indexOf("@");
	var posArrobaFin = email.lastIndexOf("@");
	var posPunto = email.lastIndexOf(".");
	var longitud = email.length;
	
	// no ha introducido nada
	if(longitud == 0) {
		alert
		return false;
	}
	// no hay @
	else if(posArroba == -1) {
		return false;
	}
	// hay algun espacio
	else if(email.indexOf(" ") != -1) {
		return false;
	}
	// hay mas de una @
	else if(posArroba != posArrobaFin) {
		return false;
	}
	// nada antes de la @
	else if(posArroba == 0) {
		return false;
	}
	// no hay punto
	else if(posPunto == -1) {
		return false;
	}
	// hay menos de 2 caracteres despues del punto
	else if(longitud < posPunto+3) {
		return false;
	}
	// no hay nada entre el punto y la arroba
	else if(posPunto <= posArroba+2) {
		return false;
	}
	// dirección es correcta
	else {
		return true;
	}
}

// vacía campo texto la primera vez que se pincha sobre él
function limpiar() {
	if(document.getElementById){
		campotext = document.getElementById('email');
		campotext.value = "";
		campotext.onclick = null;
	}
}