/***** ROLLOVER SENCILLO QE TE CAGAS *****/
//en la imagen --> onmouseover="this.src=Imagenes[0].src;" onmouseout="this.src=Imagenes[1].src;"
//en onLoad de <body> -->onLoad="Precargar('rutarelativa/imagen-ON', 'rutarelativa/imagen-OFF')"
Imagenes = new Array();
function Precargar() {
	
  for (var i=0; i<Precargar.arguments.length; i++) {    
    Imagenes[i]=new Image; 
    Imagenes[i].src=Precargar.arguments[i];
  }
}



/***** Captura de la posición del ratón *****/
function Browser() {
this.isIE = false; // Internet Explorer
this.isNS = false; // Netscape
this.isOpera = false; // Opera
if (navigator.userAgent.indexOf("Netscape6/") >= 0) {
this.isNS = true;
return;
}
if (navigator.userAgent.indexOf("Gecko") >= 0) {
this.isNS = true;
return;
}
if (navigator.userAgent.indexOf("MSIE") >=0 && navigator.userAgent.indexOf("Opera") <0) {
this.isIE = true;
return;
}
if (navigator.userAgent.indexOf("Opera") >=0) {
this.isOpera = true;
return;
}
}

var browser = new Browser();

function initMovimiento(event){
cancelar();
if (browser.isIE || browser.isOpera) {
document.attachEvent("onmousemove", getposicion);
window.event.cancelBubble = true;
window.event.returnValue = false;
}
if (browser.isNS) {
document.addEventListener("mousemove", getposicion, true);
event.preventDefault();
}
}
function initPulsar(event){
cancelar();
if (browser.isIE || browser.isOpera) {
document.attachEvent("onclick", getposicion);
window.event.cancelBubble = true;
window.event.returnValue = false;
}
if (browser.isNS) {
document.addEventListener("click", getposicion, true);
event.preventDefault();
}
}
function getposicion(event){
if (browser.isIE) {
x = window.event.clientX + document.documentElement.scrollLeft
+ document.body.scrollLeft;
y = window.event.clientY + document.documentElement.scrollTop
+ document.body.scrollTop;
}
if (browser.isNS) {
x = event.clientX + window.scrollX;
y = event.clientY + window.scrollY;
}
if (browser.isOpera) {
x = window.event.clientX + document.documentElement.scrollLeft;
y = window.event.clientY + document.documentElement.scrollTop;
}
document.getElementById("posicion").style.visibility = "visible";
document.getElementById("posicion").style.left= (x - 55) +"px";
document.getElementById("posicion").style.top = y + 20 +"px";
document.getElementById("posicion").innerHTML = "Coordenada x: " + x + "<br>Coordenada y: " + y;
}

function cancelar() {
if (browser.isIE || browser.isOpera) {
document.detachEvent("onmousemove", getposicion);
document.detachEvent("onclick", getposicion);
}
if (browser.isNS ) {
document.removeEventListener("mousemove", getposicion, true);
document.removeEventListener("click", getposicion, true);
}
document.getElementById("posicion").style.visibility= "hidden";
}

//-----------------------------------------

// popups de imágenes de la galería
function popup(ruta_base, ancho, id_imagen)
{
	var features = "width=" + ancho + ", height=600";
	
	nueva = window.open(ruta_base + "admin/galeria/ver_imagen.php?id=" + id_imagen, "imagen", features);
	nueva.focus();
}

// funciones para mostrar/ocultar capas
function mostrar_capa(event, capa)
{
	var flagstyle = document.getElementById('flagstyle');
	if (flagstyle.style.display == 'none') {
		document.getElementById(capa).style.display = "block";
		document.getElementById(capa).style.position = "absolute";

		if (browser.isIE) {
			x = window.event.clientX + document.documentElement.scrollLeft
			+ document.body.scrollLeft;
			y = window.event.clientY + document.documentElement.scrollTop
			+ document.body.scrollTop;
		}
		if (browser.isNS) {
			x = event.clientX + window.scrollX;
			y = event.clientY + window.scrollY;
		}
		if (browser.isOpera) {
			x = window.event.clientX + document.documentElement.scrollLeft;
			y = window.event.clientY + document.documentElement.scrollTop;
		}

		document.getElementById(capa).style.left= (x - (document.getElementById(capa).offsetWidth/2)) +"px";
		document.getElementById(capa).style.top = (y + 20) +"px";
	}
}
function ocultar_capa(capa)
{
	var flagstyle = document.getElementById('flagstyle');
	if (flagstyle.style.display == 'none') {
		document.getElementById(capa).style.display = "none";
		document.getElementById(capa).style.position = "absolute";

		if (browser.isIE || browser.isOpera) {
			document.detachEvent("onmousemove", getposicion);
			document.detachEvent("onclick", getposicion);
		}
		
		if (browser.isNS ) {
			document.removeEventListener("mousemove", getposicion, true);
			document.removeEventListener("click", getposicion, true);
		}
	}
}

function ampliar_imagen(ruta, ancho, alto)
{
	var features = "width=" + ancho + ",height="+ alto;
	
	nueva = window.open(ruta , "Imagen", features);
	nueva.focus();
}

function validar_contacto(fContacto)
{
	var datos = "";
			
	if (document.fContacto.nombre.value == "")
		var datos = datos + "\n>> Nombre incompleto";
	
	if (document.fContacto.tmovil.value == "")
		var datos=datos + "\n>> Movil incompleto";

	if (document.fContacto.comentarios.value == "")
		var datos=datos + "\n>> Comentarios incompleto";
		
	if (document.fContacto.email.value == "")
		var datos=datos + "\n>> Email incompleto";
	else
	{
			if (!isValidEmail(document.fContacto.email.value))
				var datos = datos + "\n>> Email incorrecto";
	}
	
	if (datos != "")
	{
		alert(datos);
		return (false);
	}
	else
	{
		return (true);
	}
}

function validar_pedido(fPedido)
{
	var datos = "";
			
	if (document.fPedido.nombre.value == "")
		var datos = datos + "\n>> Nombre incompleto";
	
	if (document.fPedido.apellidos.value == "")
		var datos = datos + "\n>> Apellidos incompleto";
	
	if (document.fPedido.direccion.value == "")
		var datos = datos + "\n>> Dirección incompleto";
		
	if (document.fPedido.cp.value == "")
		var datos = datos + "\n>> CP incompleto";
	
	if (document.fPedido.tmovil.value == "")
		var datos=datos + "\n>> Movil incompleto";
	
	if (document.fPedido.email.value == "")
		var datos=datos + "\n>> Email incompleto";
	else
	{
			if (!isValidEmail(document.fPedido.email.value))
				var datos = datos + "\n>> Email incorrecto";
	}
	
	if (datos != "")
	{
		alert(datos);
		return (false);
	}
	else
	{
		return (true);
	}
}
	
//Funcion para validar un email
function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    
    email = trim(email);
    
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

function ltrim(cadena) { 
    return cadena.replace(/^\s+/, ""); 
} 
 
function rtrim(cadena) { 
    return cadena.replace(/\s+$/, ""); 
} 
 
function trim(cadena) { 
    return rtrim(ltrim(cadena)); 
}