// JavaScript Document
// Utilidades para el sitio web
var FotoIni=1;
var FotoFin=5;


function popup(pagina,nombre,ancho,alto,parameters) {
  var w=screen.width;
  var h=screen.height;
  var x=Math.round((w-ancho+1)/2);
  var y=Math.round((h-alto+1)/2);
  var wp ='width='+ancho+',height='+alto+',hotkeys=NO,screenX='+x+',screenY='+y+',left='+x+',top='+y+parameters;
  var pp = window.open(pagina,nombre,wp);
  pp.focus();
}


function Pagina(cual)
{	var p,di;
	i=1;
	infe=0; supe=0;
	// Mostrar la pgina correspondiente
	window.status="Un momento por favor...";
	while (p=document.getElementById("Pagina"+i))
	{	window.status="Un momento por favor...";
		infe=i * 4-3;
		supe=infe+3;
		if (cual!='all')
		{	p.style.display="none";
		}
		else
		{	p.style.display="";
		}
		while (di=document.getElementById("Layer"+infe))
		{	di.style.display=p.style.display;
			//if (typeof colocarlayer=="function") colocarlayer(infe);
			infe++;
			if (infe>supe) break;
		}
		i++;
	}
	if (cual!="all")
	{	p=document.getElementById("Pagina"+cual);
		if (p)	
		{	p.style.display="";
			infe=cual * 4-3;
			supe=infe+3;
			while (di=document.getElementById("Layer"+infe))
			{	di.style.display=p.style.display;
				//if (typeof colocarlayer=="function") colocarlayer(infe);
				infe++;
				if (infe>supe) break;				
			}	
		}
	}
	if (typeof colocarlayers=="function")	colocarlayers();
	window.status="Listo"	;
}

function Ficha(cual)
{	var p;
	var f;
	i=1;
	// Mostrar la ficha correspondiente
	while (p=document.getElementById("Pagina"+i))
	{	f=document.getElementById("Ficha"+i);
		if (!f) break;
		r=document.getElementById("rl_tipospropiedades");
		switch(parseInt(r.value))
		{	default:
				f.src="images/villas_0"+i+".gif";
				break;
			case 4:
				if (i==2)	f.src="images/colindancia_0"+i+".gif";
				else f.src="images/villas_0"+i+".gif";
				break;			
		}
		if (cual!='all')
		{	p.style.display="none";
		}
		else
		{	p.style.display="";
		}
		i++;
	}
	if (cual!="all")
	{	p=document.getElementById("Pagina"+cual);
		if (p)	p.style.display="";
		f=document.getElementById("Ficha"+cual);
		if (f) 
		{	f.src="images/villas1_0"+cual+".gif";
			r=document.getElementById("rl_tipospropiedades");
			switch(parseInt(r.value))
			{	default:
					f.src="images/villas1_0"+cual+".gif";
					break;
				case 4:
					if (cual==2)	f.src="images/colindancia1_0"+cual+".gif";
					else f.src="images/villas1_0"+cual+".gif";
					break;			
			}	
		}
	}
}

function PonerFoto(nombre)
{	var f, PDf, i, j;
	f=document.getElementById("foto");
	if ( f )	f.src=nombre;
	i=1; j=FotoIni;
	while (fotos[i]!=nombre && i<fotos.length)
	{	i++;
	}
	PDf=document.getElementById("pdf");
	PDf.href=document.f.ligaoriginal.value+"&foto="+i;
}

function FotoIzq(Foto)
{	if (fotos)
	{	if (FotoIni>1)
		{	FotoIni--;
			FotoFin--;
			Galeria(Foto);
		}
	}
}

function FotoDer(Foto)
{	if (fotos)
	{	if (FotoFin<fotos.length-1)
		{	FotoIni++;
			FotoFin++;
			Galeria(Foto)
		}
	}
}

function Galeria(Foto)
{	var foto=Foto;
	var i=1;
	var fo,lp;
	var I,D;
	I=document.getElementById("izq");
	D=document.getElementById("der");
	for (i=1; i<=5; i++)
	{	fo=document.getElementById("f"+i);
		fo.innerHTML='<img src="images/imagen_fondo.gif" width="80" height="80" alt="" border="0" id="foto'+i+'">';
	}
	i=1; j=FotoIni;
	while (fo=document.getElementById("f"+i))
	{	if (foto[j])
		{	fo.innerHTML='<a href="javascript:PonerFoto(fotos['+j+']);"><img src="'+foto[j]+'" width="80" height="80" alt="" border="0" id="foto'+i+'"></a>';
			lp=document.getElementById("lpf"+i);
			if (lp)
			{	ant=lp.href;
				lp.href='javascript:PonerFoto(fotos['+j+'])';
			}
		}
		else
		{	break;
		}
		i++;
		j++;
		if (j>FotoFin) break;
	}
}


function Submit(valor)
{	if (document.f.accion)
	{	if (Validar())
		{	document.f.accion.value=valor.toLowerCase();
			document.f.submit();
		}
	}
}

function Validar()
{	var n=document.getElementById("nombre");
	var e=document.getElementById("email");
	var p =document.getElementById("propiedad");
	//var c =document.getElementById("contacto");
	var o =document.getElementById("comentario");
	var oe =document.getElementById("comentarios_en");
	var os =document.getElementById("comentarios_es");
	var d =document.getElementById("destino");	
	var m=document.getElementById("emaild");
	var ok=true;
	if (n) if (n.value.length==0)	{	alert("Please enter your name"); n.focus(); return false}
	if (e) if (e.value.length==0)	{	alert("Please enter your email") ; e.focus(); return false}
	if (e) if (!isEmailAddress(e))	{	alert("The email address appears to be incorrect. Please check the email address."); e.focus();	return false; }	
	//if (c) if (c.value.length==0)	{	alert("Please enter your contact info"); c.focus(); return false}
	
	if (d) if (d.value.length==0)	{	alert("Please enter a name"); d.focus(); return false}
	if (m) if (m.value.length==0)	{	alert("Please enter your email") ; m.focus(); return false}
	if (m) if (!isEmailAddress(m))	{	alert("The email address appears to be incorrect. Please check the email address."); m.focus();	return false; }
	if (o) if (o.value.length==0)	{	alert("Please enter a comment"); o.focus(); return false}
	if (oe) if (oe.value.length==0)	{	alert("Please enter a comment"); oe.focus(); return false}
	if (os) if (os.value.length==0)	{	alert("Please enter a comment"); os.focus(); return false}
	return ok;
}


function isEmailAddress(theElement, nombre_del_elemento )
{	var s = theElement.value;
	var filter=/^[A-Za-z_].[A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	var filter=/^[_A-Za-z0-9-]+(.[_A-Za-z0-9-]+)*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	if (s.length == 0 ) return true;
	if (filter.test(s))
	return true;
	else
	alert("La direccin de correo no parece ser correcta. Ingrese una direccion vlida");
	theElement.focus();
	return false;
}


function MoreInfo(IDX)
{	document.f.action="moreinfo.php";
	document.f.idx.value=IDX;
	//alert(document.f.idx.value+"\n"+	document.f.rl_tipospropiedades.value)
	document.f.submit();
}

// Una funcion de manejo de eventos
function Props()
{	var rl_tiposprop	;	rl_tiposprop=document.getElementById("rl_tipospropiedades");
	var rl_recams		;	rl_recams	=document.getElementById("rl_recamaras");
	if (rl_tiposprop)
	{	if (rl_recams)
		{	rl_tiposprop.onchange= Recs;
		}
	}
}

function Recs(event)
{	var rl_recams		;	rl_recams	=document.getElementById("rl_recamaras");
	rl_recams.disabled=((this.value!=5) && (this.value!=2));
}

function Reset()
{	if (confirm("Are you sure to reset this form?"))
	{	document.f.reset();
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function Buscar()
{	var t;
	t=document.getElementById("texto");
	if (t)
	{	if (t.value!="")
		{	loc="search.php?q="+t.value;
			//alert(loc);
			document.busca.submit();
			//location.href=loc;
			//alert("Texto: "+t.value);
		}
		else
		{	alert("Please enter a text");
			t.focus();
		}
	}
}

function Buscar1()
{	var T,t;
	T=document.getElementById("texto1");
	t=document.getElementById("texto");
	if (t && T)
	{	if (T.value!="")
		{	t.value=T.value;
			
			//Buscar();
			//alert("Texto: "+t.value);
		}
		else
		{	alert("Please enter a text");
			T.focus();
		}
	}
}

function Idioma(cual)
{	var n=-1, p, l, d;
	n=checkForCharacters(location.href,"216.69.180.120");
	if (n<=-1)	n=checkForCharacters(location.href,"ronlavender.com");
	p=location.pathname;
	l=location.host;
	d=location.href.split("?");
	// Cambio de idiomas inglés-español
	// En sitio web remoto
	if (n>-1)
	{	p=p.split("/");
		switch (cual)
		{	case "en":
				//alert("0: "+p[0]+"\n1: "+p[1]+"\n2: "+p[2]);
				location.href="http://"+l+"/"+p[2]+(d.length>1?"?"+d[1]:"");
				break;
			case "es":
				location.href="http://"+l+"/"+p[0]+"spanish/"+p[1]+(d.length>1?"?"+d[1]:"");
				break;
		}
	}
	// En sitio web local
	else
	{	p=p.split("/");
		switch (cual)
		{	case "en":
				location.href="http://"+l+"/"+p[1]+"/"+p[3]+(d.length>1?"?"+d[1]:"");
				break;
			case "es":
				location.href="http://"+l+"/"+p[1]+"/spanish/"+p[2]+(d.length>1?"?"+d[1]:"");
				break;
		}
	}
}

function checkForCharacters(inputString, checkString, startingIndex)
{
  if (!startingIndex) startingIndex = 0;
  return inputString.indexOf(checkString);
}



//-------------------- Ejemplo ------------------------------------
/*
function test() {
  var elemento = document.getElementById('miboton');
  var pos = getAbsoluteElementPosition(elemento)
  
  alert("top="+pos.top+" left="+pos.left)
}
*/


var newwindow;
function poptastic(url)
{ 
newwindow=window.open(url,'name','height=500,width=400,left=20,top=20,toolbar=no,menubar=no,directories=no,location=no,scrollbars=yes,status=no,resizable=yes,fullscreen=no');
if (window.focus) {newwindow.focus()}
}
