// JavaScript Document

function crearXHR() {
	var xhrObject=false;
	try {
		xhrObject = new ActiveXObject("MSXML2.XMLHTTP");
	} catch (exception1) {
		try {
		      xhrObject = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(exception2) {  xhrObject = false;  }
	}
	if (!xhrObject && window.XMLHttpRequest) {
		xhrObject = new XMLHttpRequest();
	}
	return xhrObject; // Devolve false se non se puido crear
}

var actualizando=false;

function cargarGaleria(carpeta) {
	document.getElementById('galeria').innerHTML="<table style='border-bottom: 1px solid #000; background-color: #FFCC99' width='666' height='20' cellpading='0' cellspacing='0' border='0'><tr><td align='left'><strong>&nbsp;"+carpeta+"/</strong></td><td align=\"right\"><strong><a href=\"javascript:cerrar('galeria');\">Cerrar</a></strong>&nbsp;</td></tr></table><iframe src='EasyGallery2/EremoGallery.php?carpeta="+carpeta+"' width='666' height='440' marginwidth='0' marginheight='0' frameborder='0' scrolling='no'>";		
	document.getElementById('galeria').style.visibility='visible';
}

function enviarPresupuesto() {
	if (!actualizando) {
		var xhr=crearXHR();
		if (xhr) {
			actualizando=true;
			xhr.open("POST","mailform/mailform.php",true);
			xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); // Enviamos por POST
			//xhr.setRequestHeader("Content-Type","multipart/form-data");
			xhr.onreadystatechange = function() {
				var mensaje="";
				if (xhr.readyState==4 && xhr.status==200) {
					if (xhr.responseText.indexOf("Debe cubrir el campo")>=0) mensaje="<div align='center'><strong><font color='red'>Por favor, rellene todos los campos obligatorios.</font></strong><br/><br/>Los campos obligatorios est&aacute;n marcados con  '*'.</div>";
					else if (xhr.responseText.indexOf("Correo electronico incorrecto")>=0) mensaje="<div align='center'><strong><font color='red'>Por favor, introduzca un correo electr&oacute;nico v&aacute;lido.</font></strong></div>";
					else if (xhr.responseText.indexOf("Gracias")>=0) {
						mensaje="<div align='center'><strong>Formulario enviado correctamente.</strong><br/>En breve se le notificar&aacute; su presupuesto.<br/><br/>Gracias por su confianza</div>";
						document.presupuesto.reset();
					} else mensaje="<div align='center'><strong><font color='red'>Ocurri&oacute; un error enviando el formulario.</font></strong><br/><br/>Por favor, int&eacute;ntelo m&aacute;s tarde.</font></div>";
					document.getElementById('mensaje').innerHTML="<table style='border-bottom: 1px solid #000; background-color: #FFCC99' width='400' height='20' cellpading='0' cellspacing='0' border='0'><tr><td align='left'><strong>&nbsp;Solicitud de presupuesto</strong></td><td align=\"right\"><strong><a href=\"javascript:cerrar('mensaje');\">Cerrar</a></strong>&nbsp;</td></tr></table><br/>"+mensaje;	
					document.getElementById('mensaje').style.visibility='visible';
					actualizando=false;					
				}else if (xhr.readyState==4 && xhr.status!=200) {
					mensaje="<div align='center'><strong><font color='red'>Ocurri&oacute; un error enviando el formulario.</font></strong></font><br/><br/>Por favor, int&eacute;ntelo m&aacute;s tarde.</div>";
					document.getElementById('mensaje').innerHTML="<table style='border-bottom: 1px solid #000; background-color: #FFCC99' width='400' height='20' cellpading='0' cellspacing='0' border='0'><tr><td align='left'><strong>&nbsp;Solicitud de presupuesto</strong></td><td align=\"right\"><strong><a href=\"javascript:cerrar('mensaje');\">Cerrar</a></strong>&nbsp;</td></tr></table><br/>"+mensaje;	
					document.getElementById('mensaje').style.visibility='visible';
					actualizando=false;
				}				
			}
			//Recopilamos los parametros a enviar
			var parametros="submitform=Submit";
			parametros+="&sort=order:NombreApellidos,Direccion,CodigoPostal,Ciudad,Telefono,Email,TipoVivienda,OtroTipoVivienda,Ubicacion,M2,ACS,Personas,Banos,Jacuzzi,Comentarios";
			parametros+="&required=NombreApellidos,Email,TipoVivienda,Ubicacion,M2,ACS";
			parametros+="&recipient=info@eremo.es";
			parametros+="&subject=Solicitud web de presupuesto";
			parametros+="&email="+escape(document.presupuesto.Email.value);
			//parametros+="&realname="+escape(document.presupuesto.NombreApellidos.value);
		    parametros+="&title=Resultado del envio";
		    parametros+="&env_report=REMOTE_HOST,HTTP_USER_AGENT,REMOTTE_ADDR";
			parametros+="&NombreApellidos="+escape(document.presupuesto.NombreApellidos.value);
			parametros+="&Direccion="+escape(document.presupuesto.Direccion.value);
			parametros+="&CodigoPostal="+escape(document.presupuesto.CodigoPostal.value);
			parametros+="&Ciudad="+escape(document.presupuesto.Ciudad.value);
			parametros+="&Telefono="+escape(document.presupuesto.Telefono.value);
			parametros+="&Email="+escape(document.presupuesto.Email.value);
			parametros+="&TipoVivienda="+escape(document.presupuesto.TipoVivienda.value);
			parametros+="&OtroTipoVivienda="+escape(document.presupuesto.OtroTipoVivienda.value);
			parametros+="&Ubicacion="+escape(document.presupuesto.Ubicacion.value);
			parametros+="&M2="+escape(document.presupuesto.M2.value);
			parametros+="&ACS="+(document.presupuesto.ACS[0].checked?"Si":"No");
			parametros+="&Personas="+escape(document.presupuesto.Personas.value);
			parametros+="&Banos="+escape(document.presupuesto.Banos.value);
			parametros+="&Jacuzzi="+(document.presupuesto.Jacuzzi[0].checked?"Si":"No");
			parametros+="&Comentarios="+escape(document.presupuesto.Comentarios.value);
			xhr.send(parametros+"&t="+new Date().getTime()); // Enviamos datos POST
		}
	}
}

function activarACS(activar) {
	document.presupuesto.Personas.disabled = !activar;
	document.presupuesto.Banos.disabled = !activar;
	document.presupuesto.Jacuzzi[0].disabled = !activar;
	document.presupuesto.Jacuzzi[1].disabled = !activar;
	if (!activar) {
		document.presupuesto.Personas.value="";
		document.presupuesto.Banos.value="";
		document.presupuesto.Jacuzzi[1].checked = true;
	}
}

function activarOtroTipoVivienda() {
	var activar=false;
	if (document.presupuesto.TipoVivienda.selectedIndex==document.presupuesto.TipoVivienda.length-1) activar=true;
	document.presupuesto.OtroTipoVivienda.disabled = !activar;
	if (!activar) document.presupuesto.OtroTipoVivienda.value="";
}

function cargarGaleriaAJAX(carpeta) {
	if (!actualizando) {
		var xhr=crearXHR();
		if (xhr) {
			actualizando=true;
			xhr.open("POST","EremoGallery.php",true);
			xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); // Enviamos por POST
			xhr.onreadystatechange = function() {
				if (xhr.readyState==4 && xhr.status==200) {
					document.getElementById('galeria').innerHTML=xhr.responseText;	
					document.getElementById('galeria').style.visibility='visible';
					actualizando=false;					
				}else if (xhr.readyState==4 && xhr.status!=200) {
					actualizando=false;
				}				
			}	
			xhr.send("carpeta="+carpeta+"&t="+new Date().getTime()); // Enviamos datos POST
		}
	}
}

function cerrar(objName) {
	document.getElementById(objName).style.visibility='hidden';
}

var zoom = 4;
var speed = 4;
var real = 0;
var intervalIn;
var divs = document.getElementsByTagName('div');
for (var i=0; i<divs.length; i++)
{
  if (divs[i].id == 'livethumbnail')
  {
	var myimg = divs[i].getElementsByTagName('img')[0];	
	myimg.smallSrc = myimg.getAttribute('src');
	myimg.smallWidth = parseInt(myimg.getAttribute('width'));
	myimg.smallHeight = parseInt(myimg.getAttribute('height'));
	divs[i].onmouseover = scaleIn;
	divs[i].onmouseout = scaleOut;
	if (!myimg.smallWidth)
    {
    /*<?php
    if (isset($image)){
	  if ($image[0] > $image[1])
	  {
        echo "myimg.smallWidth = $image[0];\n";
        echo "myimg.smallHeight = $image[1];\n";
      }
      else
	  {
        echo "myimg.smallWidth = $image[1];\n";
 	    echo "myimg.smallHeight = $image[0];\n";
      }
	}
    ?>*/
      real = 0;
    }
    else
    {
  	  real = 1;
    }
  }
}

function scaleIn()
{
  var myimg = this.getElementsByTagName('img')[0];
  this.style.zIndex = 20;
  myimg.src = myimg.smallSrc;
  var count = 0;
  var real = 0;
  intervalIn = window.setInterval(scaleStepIn, 1);
  return false;
  
  function scaleStepIn()
  {
	var widthIn = parseInt(myimg.style['width']);
	var heightIn = parseInt(myimg.style['height']);
	var topIn = parseInt(myimg.style['top']);
	var leftIn = parseInt(myimg.style['left']);
	if(widthIn >= heightIn) {
	  widthIn += speed;
	  heightIn += Math.floor(speed * (3/4));
	  topIn -= (Math.floor(speed * (3/8)));
	  leftIn -= (speed/2);
	}
	else
	{
	  widthIn += Math.floor(speed * (3/4));
	  heightIn += speed;
	  topIn -= (speed/2);
	  leftIn -= (Math.floor(speed * (3/8)));
	}
	myimg.style['width'] = widthIn;
	myimg.style['height'] = heightIn;
	myimg.style['top'] = topIn;
	myimg.style['left'] = leftIn;
	count++;
	if (count >= zoom)
	  window.clearInterval(intervalIn);
  }			
}
function scaleOut()
{
  window.clearInterval(intervalIn);
  var myimg = this.getElementsByTagName('img')[0];
  myimg.src = myimg.smallSrc;
  this.style.zIndex = 10;
  var mydiv = this;
  var interval = window.setInterval(scaleStepOut, 1);
  return false;

  function scaleStepOut()
  {
	var width = parseInt(myimg.style['width']);
	var height = parseInt(myimg.style['height']);
	var top = parseInt(myimg.style['top']);
	var left = parseInt(myimg.style['left']);
	if(width >= height) {
	  width -= speed;
	  height -= Math.floor(speed * (3/4));
  	  if(width < myimg.smallWidth + 4) {
	    myimg.style['width'] = myimg.smallWidth;
	    myimg.style['height'] = myimg.smallHeight;
	    myimg.style['top'] = 0;
	    myimg.style['left'] = 0;
		mydiv.style['zIndex'] = 1;
		window.clearInterval(interval);
	  }
	  else{
	    myimg.style['width'] = width;
	    myimg.style['height'] = height;
	    myimg.style['left'] = left + (speed/2);
	    myimg.style['top'] = top + (Math.floor(speed * (3/8)));
	  }
	}
	else
	{
	  width -= Math.floor(speed * (3/4));
	  height -= speed;
	  if(real==1)
	  {
	    if(width < myimg.smallWidth + 4)
	    {
	      myimg.style['width'] = myimg.smallWidth;
	      myimg.style['height'] = myimg.smallHeight;
		  myimg.style['top'] = 0;
	      myimg.style['left'] = 0;
		  mydiv.style['zIndex'] = 1;
	      window.clearInterval(interval);
		}
		else{
		  myimg.style['width'] = width;
	      myimg.style['height'] = height;
	      myimg.style['top'] = top + (speed/2);
	      myimg.style['left'] = left + (Math.floor(speed * (3/8)));
		}
	  }
	  else
	  {
	  	if(height < myimg.smallWidth + 4)
	    {
	      myimg.style['width'] = myimg.smallHeight;
	      myimg.style['height'] = myimg.smallWidth;
		  myimg.style['top'] = 0;
	      myimg.style['left'] = 0;
		  mydiv.style['zIndex'] = 1;
	      window.clearInterval(interval);
		}
		else{
		  myimg.style['width'] = width;
	      myimg.style['height'] = height;
	      myimg.style['top'] = top + (speed/2);
	      myimg.style['left'] = left + (Math.floor(speed * (3/8)));
		}
	  }
	}	  
  }
}