// JavaScript Document
function ventanaSecundaria (URL, ancho, alto, barra_desplazamiento){
//   window.open(URL,"ventana1","width=120,height=300,scrollbars=yes")
   window.open(URL,"ventana1","width="+ancho+",height="+alto+",scrollbars="+barra_desplazamiento)
}

// ajax
var xmlHttp;
function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	 {
	 // Firefox, Opera 8.0+, Safari
	 xmlHttp=new XMLHttpRequest();
	 }
	catch (e)
	 {
	 //Internet Explorer
	 try
	  {
	  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	 catch (e)
	  {
	  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	 }
	return xmlHttp;
}

function comprobarPropietario (formulario)
{
	if (formulario.email_propietario.value == "") 
	{		
		document.getElementById("txtHint").innerHTML="<b><img src='imagenes/plantilla/vineta_1.gif' />&nbsp;Debe escribir su email</b>";
		return;
	} 
	else if (formulario.password_propietario.value == "") 
	{
		document.getElementById("txtHint").innerHTML="<b><img src='imagenes/plantilla/vineta_1.gif' />&nbsp;Debe escribir su password</b>";		
		return;
	}
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	 {
	 	alert ("Lo sentimos, pero su navegador no soportar peticiones HTTP");
	 	return;
	 }	
	var url="acciones/propietarios_comprobacion_ueiis.php";
	url=url+"?email="+formulario.email_propietario.value;
	url=url+"&pass="+formulario.password_propietario.value;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
	 if (xmlHttp.responseText=="ok") {	
	 	location.href="propietarios_area_ueiis.php";	 
	 }
	 else 
	 {	
	 	//alert(xmlHttp.responseText);
		//document.getElementById("email_propietario").select();
		document.getElementById("password_propietario").value = "";		
	 	document.getElementById("txtHint").innerHTML="<img src='imagenes/plantilla/vineta_1.gif' />&nbsp;<b>"+xmlHttp.responseText+"</b>"; 
	 }
	}
}

function limpiar_aviso() {
		document.getElementById("txtHint").innerHTML="";
}


function validarNuevoPropietario (formulario)
{
	if( formulario.nombre_propietario.value == "" )
         {
			document.getElementById("txtHint").innerHTML="<b><img src='imagenes/plantilla/vineta_1.gif' />&nbsp;Debe escribir su nombre</b>";
			return;
         }
	if( formulario.email_propietario.value == "" )
         {
			document.getElementById("txtHint").innerHTML="<b><img src='imagenes/plantilla/vineta_1.gif' />&nbsp;Debe escribir su email</b>";
			return;
         }
	if( formulario.password_propietario.value == "" )
         {
			document.getElementById("txtHint").innerHTML="<b><img src='imagenes/plantilla/vineta_1.gif' />&nbsp;Debe escribir una contraseña</b>";
			return;
         }
	 xmlHttp=GetXmlHttpObject();
	 if (xmlHttp==null)
	  {
		 alert ("Lo sentimos, pero su navegador no soportar peticiones HTTP");
		 return;
	  }	
	 if (formulario.tipo_propietario[0].checked) {
		 tmp_chk = 1; 
	 } else {
		 tmp_chk = 2; 
	 }
	 var url="acciones/propietarios_comprobacion_nuevo_ueiis.php";
	 url=url+"?email="+formulario.email_propietario.value;
	 url=url+"&nombre="+formulario.nombre_propietario.value;	 
	 url=url+"&pass="+formulario.password_propietario.value;
 	 url=url+"&tipo="+tmp_chk;
	 url=url+"&sid="+Math.random();
	 xmlHttp.onreadystatechange=estadoComprobacionNuevo;
	 xmlHttp.open("GET",url,true);
	 xmlHttp.send(null);
}

function estadoComprobacionNuevo() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
	 if (xmlHttp.responseText=="ok") {	
	 	location.href="propietarios_acceso_ueiis.php?op=1";	 
	 }
	 else 
	 {	
	 	//alert(xmlHttp.responseText);
		//document.getElementById("email_propietario").select();
		document.getElementById("password_propietario").value = "";		
	 	document.getElementById("txtHint").innerHTML="<img src='imagenes/plantilla/vineta_1.gif' />&nbsp;<b>"+xmlHttp.responseText+"</b>"; 
	 }
	}
}


function validarModificarDatosPropietario (formulario)
{
	if (formulario.tipo_propietario[1].checked) 
	{
		if( formulario.empresa.value == "" )
			 {
					 alert("El campo empresa es obligatorio.");
					 return false;
			 }
		if( formulario.direccion.value == "" )
			 {
					 alert("El campo dirección es obligatorio.");
					 return false;
			 }
	}
	if (formulario.nombre_propietario.value == "")
		{
			     alert("El campo nombre es obligatorio.");
                 return false;
		}
	if (formulario.email_propietario.value== "")
		{
			     alert("El campo email es obligatorio.");
                 return false;
		}
	if (formulario.password_propietario.value== "")
		{
			     alert("El campo password es obligatorio.");
                 return false;
		}
	if (formulario.telefono1.value== "" ||formulario.telefono1.value==0)
	{
			 alert("Debe introducir al menos un teléfono.");
			 return false;
	}
	return true;
}

function cambiar_empresa_particular (tipo) {
	if (tipo == 1) {
			document.forms.form_datos_propietario.empresa.value = ""; 
			document.forms.form_datos_propietario.direccion.value = ""; 
			document.getElementById('coll_empresa').className = "fila_oculta";

	} else if (tipo == 2) {	
			document.getElementById('coll_empresa').className = "fila_vista";
	}
}

function VenderAlquilar (formulario) {
	if(formulario.id_modo.options[0].selected) {		
		document.getElementById("coll_precio_alquiler").className = "fila_oculta";
		document.getElementById("coll_precio_venta").className = "fila_vista";		
	} else {
		document.getElementById("coll_precio_venta").className = "fila_oculta";
		document.getElementById("coll_precio_alquiler").className = "fila_vista";				
	}
	formulario.precio_venta.value = 0;
	formulario.precio_alquiler.value = 0;
	
}
function validarNuevaPropiedad (formulario) 
{

	if( formulario.titulo.value == "" )
         {
                 alert("El campo título es obligatorio.");
                 return false;
         }
	if (formulario.id_provincia.options[0].selected)
		{
			     alert("Debe seleccionar alguna provincia.");
                 return false;
		}
	if (formulario.id_provincia.options[0].selected)
		{
			     alert("Debe seleccionar alguna provincia.");
                 return false;
		}
	if(formulario.id_modo.options[0].selected) {
		$tmp_precio = formulario.precio_venta.value;
		if ($tmp_precio.indexOf(",")!=-1 || $tmp_precio.indexOf(".")!=-1)
		{	
			alert("Precio no válido. El precio debe ser escrito sin punto ni comas.");
			return false;
		} else {
			if (isNaN(parseInt(formulario.precio_venta.value))) {
			   alert("Precio no válido.");
			   return false;
			} else {
				if ((parseInt(formulario.precio_venta.value))<=0) {
					alert("El precio de venta la propiedad deber ser mayor que cero.");
					return false;
				}
					
			}			
			
		}
	} else {
		$tmp_precio = formulario.precio_alquiler.value;
		if ($tmp_precio.indexOf(",")!=-1 || $tmp_precio.indexOf(".")!=-1)
		{	
			alert("Precio no válido. El precio debe ser escrito sin punto ni comas.");
			return false;
		} else {		
			if (isNaN(parseInt(formulario.precio_alquiler.value))) {
			   alert("Precio no válido.");
			   return false;
			} else {
				if ((parseInt(formulario.precio_alquiler.value))<=0) {
					alert("El precio del alquiler propiedad deber ser mayor que cero.");
					return false;
				}
					
			}
		}
	}
	return true;

}

function validarBorrarPropiedad (cod, ind) 
{
	var agree=confirm("¿Elminar la propiedad "+ind+"?");
	if (agree) 
		{	
				document.location='acciones/propietarios_acciones_propiedad_ueiis.php?op=3&cod_propietario='+cod+'&cod_propiedad='+ind;
		}
}


// Ranges for the rent and sale search

var ventaR = new Array();
var alquilerR = new Array();

alquilerR[0] = 0;
alquilerR[1] = 5000;
alquilerR[2] = 10000;
alquilerR[3] = 15000;
alquilerR[4] = 20000;
alquilerR[5] = 30000;
alquilerR[6] = 50000;
alquilerR[7] = 75000;
alquilerR[8] = 100000;
alquilerR[9] = 150000;
alquilerR[10] = 200000;
alquilerR[11] = 300000;

ventaR[0] = 0;
ventaR[1] = 500000;
ventaR[2] = 1000000;
ventaR[3] = 1500000;
ventaR[4] = 2000000;
ventaR[5] = 3000000;
ventaR[6] = 5000000;
ventaR[7] = 7000000;
ventaR[8] = 10000000;
ventaR[9] = 15000000;
ventaR[10] = 25000000;
ventaR[11] = 50000000;

function borrarLista(laLista) {
	var i = 0;
	var select_url = laLista.options;
	for (i = select_url.length; i >= 0; --i) {
		select_url[i] = null;
	}
}

function selectAddOption(laLista, txt, val) {
	var select_url = laLista.options;
	var mLen;
	if (select_url.length < 0) { 
        	mLen = 0;
	} else {
        	mLen = select_url.length;
	}
	select_url[mLen] = new Option(txt, val);
}

function formatNum(intNumber) {
	var strint = intNumber.toString();
	
	if(strint.length < 4) {
		return strint;
	}
	
	if(strint.length > 3 && strint.length < 7) {
		strint = strint.substring(0, strint.length-3) + ',' + strint.substring(strint.length-3, strint.length)
		return strint;
	}
	
	if(strint.length > 6 && strint.length < 10) {
		strint = strint.substring(0, strint.length-6) + ',' + strint.substring(strint.length-6, strint.length-3) + ',' + strint.substring(strint.length-3, strint.length)
		return strint;
	}
	
}

function cambiarRango(whichType) {
	var i = 0;
	var price1 = document.forms.form_busqueda.precio_desde;
	var price2 = document.forms.form_busqueda.precio_hasta;

	borrarLista(price1);
	borrarLista(price2);
	
	
	if(whichType == 'v') {

		for(i = 0; i < ventaR.length;i++) {
			if(i+1 != ventaR.length) {			
				selectAddOption(price1, 'RD$ ' + formatNum(ventaR[i]), ventaR[i]);			
			}
			
			if(i != 0) {			
				if(i+1 == ventaR.length) {
					selectAddOption(price2, 'RD$ +' + formatNum(ventaR[i]), 999999999999);
				} else {
					selectAddOption(price2, 'RD$ ' + formatNum(ventaR[i]), ventaR[i]);			
				}
			}
			
		}
		
		price1.selectedIndex = 0;
		price2.selectedIndex = 10;		
		
	} else {

		for(i = 0; i < alquilerR.length;i++) {
			if(i+1 != alquilerR.length) {
				selectAddOption(price1, 'RD$ ' + formatNum(alquilerR[i]) + ' / Mes', alquilerR[i]);
			}
			
			if(i != 0) {
				if(i+1 == alquilerR.length) {
					selectAddOption(price2, 'RD$ +' + formatNum(alquilerR[i]) + ' / Mes', 999999999999);
				} else {
					selectAddOption(price2, 'RD$ ' + formatNum(alquilerR[i]) + ' / Mes', alquilerR[i]);		
				}	
			}
			
		}
		
		price1.selectedIndex = 0;
		price2.selectedIndex = 10;		
		
	}
		
}

//-->

// consulta inicial
function actualizar_consulta (formulario) {
	//document.getElementById("resultado_provisional").innerHTML="hola";
	document.getElementById("resultado_provisional").innerHTML="<img src='imagenes/plantilla/img_espera.gif' />"
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	 {
	 	alert ("Lo sentimos, pero su navegador no soportar peticiones HTTP");
	 	return;
	 }
	temp_chk = "";
	if (formulario.id_tipo.options[0].selected) {temp_chk+="1";} else {temp_chk+="0";}
	if (formulario.id_tipo.options[1].selected) {temp_chk+="1";} else {temp_chk+="0";}
	if (formulario.id_tipo.options[2].selected) {temp_chk+="1";} else {temp_chk+="0";}
	if (formulario.id_tipo.options[3].selected) {temp_chk+="1";} else {temp_chk+="0";}
	if (formulario.id_tipo.options[4].selected) {temp_chk+="1";} else {temp_chk+="0";}
	if (formulario.id_tipo.options[5].selected) {temp_chk+="1";} else {temp_chk+="0";}
	if (formulario.id_tipo.options[6].selected) {temp_chk+="1";} else {temp_chk+="0";}
	if (formulario.id_tipo.options[7].selected) {temp_chk+="1";} else {temp_chk+="0";}
	var i 
    for (i=0;i<formulario.opciones_modo.length;i++){ 
       if (formulario.opciones_modo[i].checked) 
          break; 
    } 
    temp_mod = formulario.opciones_modo[i].value;
	var url="acciones/consulta_inicial.php";
	url=url+"?chk="+temp_chk;
	url=url+"&mod="+temp_mod;
	url=url+"&prov="+formulario.id_provincia.value;
	url=url+"&p_f="+formulario.precio_desde.value;
	url=url+"&p_t="+formulario.precio_hasta.value;
	url=url+"&sid="+Math.random();
	//alert(url);
	xmlHttp.onreadystatechange=stateChanged3;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged3() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
	 //if (xmlHttp.responseText!="ok") {	
	 	document.getElementById("resultado_provisional").innerHTML=xmlHttp.responseText;
		document.getElementById("n_result_busqueda").value= xmlHttp.responseText;
	 //}
	}
}

// fin de consulta inicial
// pestañaas
function MostrarEsconder(id) {
	if (id == "id_observaciones") {
		document.getElementById('pestana_1').className = "pestana_desact";
		document.getElementById('pestana_2').className = "pestana_act_ancha";
		document.getElementById('pestana_4').className = "pestana_desact_super_ancha";		
        document.getElementById("id_observaciones").style["display"] = "block";
        document.getElementById("id_fotos").style["display"] = "none";
		document.getElementById("id_contactar").style["display"] = "none";
	}  else if (id == "id_contactar")  {
		document.getElementById('pestana_1').className = "pestana_desact";		
		document.getElementById('pestana_2').className = "pestana_desact_ancha";
		document.getElementById('pestana_4').className = "pestana_act_super_ancha";			
		document.getElementById("id_fotos").style["display"] = "none";
        document.getElementById("id_observaciones").style["display"] = "none";
		document.getElementById("id_contactar").style["display"] = "block";
	} else {
		document.getElementById('pestana_2').className = "pestana_desact_ancha";
		document.getElementById('pestana_1').className = "pestana_act";					
		document.getElementById("id_fotos").style["display"] = "block";
        document.getElementById("id_observaciones").style["display"] = "none";
		document.getElementById("id_contactar").style["display"] = "none";		
	}
}
function Redirigir(url) {
	document.location.href=url;
}
function cambiar_pestana (pestana){
	if (pestana == 'comprar') {
		document.forms.form_busqueda.opciones_modo[0].checked=true;
		cambiarRango('v');
		document.forms.form_busqueda.submit();
	} else if (pestana == 'alquilar') {
		document.forms.form_busqueda.opciones_modo[1].checked=true;
		cambiarRango('a');
		document.forms.form_busqueda.submit();

	}
}

function validarBorrarPropiedadGuardada (cod, ind) 
{
	var agree=confirm("¿Elminar la propiedad guardada: "+ind+"?");
	if (agree) 
		{	
				document.location='propietarios_propiedades_guardadas_ueiis.php?op=3&cod_propietario='+cod+'&cod_propiedad='+ind;
		}
}

function validarCorreoContactoPropietario (formulario, cod_propiedad)
{
	if (formulario.nombre.value== "")
		{
			     alert("El campo nombre es obligatorio.");
                 return false;
		}	
	if (formulario.email.value== "")
		{
			     alert("El campo email es obligatorio.");
                 return false;
		}
	document.getElementById("enviado_correo").innerHTML="<img src='imagenes/plantilla/vineta_1.gif'> Enviando correo ..."
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	 {
	 	alert ("Lo sentimos, pero su navegador no soportar peticiones HTTP");
	 	return;
	 }		
	 var url="acciones/envio_a_propietario.php";
	 url=url+"?cod_propiedad="+cod_propiedad;
	 url=url+"&email="+formulario.email.value;	 
	 url=url+"&nombre="+formulario.nombre.value;	 
	 url=url+"&telefono="+formulario.telefono.value;
 	 url=url+"&comentario="+formulario.comentario.value;
	 url=url+"&sid="+Math.random();
	 xmlHttp.onreadystatechange=estadoEnvio;
	 xmlHttp.open("GET",url,true);
	 xmlHttp.send(null);
}

function estadoEnvio() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
	 if (xmlHttp.responseText=="ok") {	
	 	document.getElementById("enviado_correo").innerHTML="<img src='imagenes/plantilla/vineta_1.gif'> El correo ha sido enviado correctamente."
	 }
	 else 
	 {	
	 	//alert(xmlHttp.responseText);
		//document.getElementById("email_propietario").select();
		document.getElementById("enviado_correo").innerHTML="<img src='imagenes/plantilla/vineta_1.gif'> No se ha podido enviar el correo. Por favor, int&eacute;ntelo m&aacute;s tarde."
	 }
	}

}


function validarCorreoEnvioAmigo (formulario, cod_propiedad)
{
	if (formulario.nombre.value== "")
		{
			     alert("El campo nombre es obligatorio.");
                 return false;
		}	
	if (formulario.email.value== "")
		{
			     alert("El campo email es obligatorio.");
                 return false;
		}
	document.getElementById("enviado_correo").innerHTML="<img src='imagenes/plantilla/vineta_1.gif'> Enviando correo ..."
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	 {
	 	alert ("Lo sentimos, pero su navegador no soportar peticiones HTTP");
	 	return;
	 }		
	 var url="acciones/envio_a_amigo.php";
	 url=url+"?cod_propiedad="+cod_propiedad;
	 url=url+"&email="+formulario.email.value;	 
	 url=url+"&nombre="+formulario.nombre.value;	 
	 url=url+"&email_amigo="+formulario.email_amigo.value;
 	 url=url+"&comentario="+formulario.comentario.value;
	 url=url+"&sid="+Math.random();
	 xmlHttp.onreadystatechange=estadoEnvioAmigo;
	 xmlHttp.open("GET",url,true);
	 xmlHttp.send(null);
}

function estadoEnvioAmigo() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
	 if (xmlHttp.responseText=="ok") {	
	 	document.getElementById("enviado_correo").innerHTML="<img src='imagenes/plantilla/vineta_1.gif'> El correo ha sido enviado correctamente."
	 }
	 else 
	 {	
	 	//alert(xmlHttp.responseText);
		//document.getElementById("email_propietario").select();
		document.getElementById("enviado_correo").innerHTML="<img src='imagenes/plantilla/vineta_1.gif'> No se ha podido enviar el correo. Por favor, int&eacute;ntelo m&aacute;s tarde."
	 }
	}

}
function validarCorreoContacto (formulario)
{
	if (formulario.nombre.value== "")
		{
			     alert("El campo nombre es obligatorio.");
                 return false;
		}	
	if (formulario.email.value== "")
		{
			     alert("El campo email es obligatorio.");
                 return false;
		}
	if (formulario.telefono.value== "")
		{
			     alert("El campo telefono es obligatorio.");
                 return false;
		}
	if (formulario.comentario.value== "")
		{
			     alert("Debe escribir algún comentario.");
                 return false;
		}		
	document.getElementById("enviado_correo").innerHTML="<img src='imagenes/plantilla/vineta_1.gif'> Enviando correo ..."
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	 {
	 	alert ("Lo sentimos, pero su navegador no soportar peticiones HTTP");
	 	return;
	 }		
	 var url="acciones/envio_contacto.php";
	 url=url+"?email="+formulario.email.value;	 
	 url=url+"&nombre="+formulario.nombre.value;	 
	 url=url+"&telefono="+formulario.telefono.value;
 	 url=url+"&comentario="+formulario.comentario.value;
	 url=url+"&sid="+Math.random();
	 xmlHttp.onreadystatechange=estadoEnvioContacto;
	 xmlHttp.open("GET",url,true);
	 xmlHttp.send(null);
}

function estadoEnvioContacto() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
	 if (xmlHttp.responseText=="ok") {	
	 	document.getElementById("enviado_correo").innerHTML="<img src='imagenes/plantilla/vineta_1.gif'> El correo ha sido enviado correctamente."
		document.forms.form_contacto.reset();
	 }
	 else 
	 {	
	 	//alert(xmlHttp.responseText);
		//document.getElementById("email_propietario").select();
		document.getElementById("enviado_correo").innerHTML="<img src='imagenes/plantilla/vineta_1.gif'> No se ha podido enviar el correo. Por favor, int&eacute;ntelo m&aacute;s tarde."
	 }
	}

}

// recuperación de contraseña
function reestableceClave (formulario)
{
	if (formulario.email_propietario.value== "")
		{
			     alert("El campo email es obligatorio.");
                 return false;
		}		
	document.getElementById("txtHint").innerHTML="<img src='imagenes/plantilla/vineta_1.gif'> Enviando correo ..."
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	 {
	 	alert ("Lo sentimos, pero su navegador no soportar peticiones HTTP");
	 	return;
	 }		
	 var url="acciones/envio_nueva_clave.php";
	 url=url+"?email="+formulario.email_propietario.value;	 
	 url=url+"&sid="+Math.random();
	 xmlHttp.onreadystatechange=estadoReestableceClave;
	 xmlHttp.open("GET",url,true);
	 xmlHttp.send(null);
}

function estadoReestableceClave() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
	 if (xmlHttp.responseText=="ok") {	
	 	document.getElementById("txtHint").innerHTML="<img src='imagenes/plantilla/vineta_1.gif'> El correo con la nueva contraseña ha sido enviado correctamente."
		document.forms.form_contacto.reset();
	 }
	 else 
	 {	
	 	//alert(xmlHttp.responseText);
		//document.getElementById("email_propietario").select();
		document.getElementById("txtHint").innerHTML="<img src='imagenes/plantilla/vineta_1.gif'> "+xmlHttp.responseText;
	 }
	}

}


