function tecla(evt) {

	var charCode = ( evt.which ) ? evt.which : evt.keyCode
	
	if ( charCode == 13 ) {
	
			if( document.getElementById('referencia').value != "" )
			{
				//alert('fitxa.php?ref='+document.getElementById('referencia').value);
				window.location.href='ficha.php?referencia='+document.getElementById('referencia').value;
			}
			else
			{
				//alert("busc");
				busc();	
			}
	
	} 

}


function busc()
{
	var parametros_url = "";
	var localidad = document.getElementById('localidad').value;
		if (localidad){ parametros_url += "&loc="+localidad; }
	var tipo = document.getElementById('tipo').value;
		if (tipo){ parametros_url += "&tip="+tipo; }
	var op = document.getElementById('operacion').value;
		if (op){ parametros_url += "&op="+op; }
	var precio_max = document.getElementById('precio_max').value;
		if (precio_max){ parametros_url += "&pmax="+precio_max; }
	document.location.href = 'index.php?pag=1'+parametros_url;
}

function visu_img(foto){
	var con_foto = new XHConn();
	var fin_foto= function (oXML) { document.getElementById('imagen').innerHTML = oXML.responseText; };	
	con_foto.connect("dinamic/visu_foto.php", "GET", "foto="+foto , fin_foto);
}

var con_filtro_tipo = new XHConn();
var fin_filtro_tipo = function (oXML) { document.getElementById('div_busc_tipo').innerHTML = oXML.responseText; };
function filtro_tipo (localidad){
	if(localidad != "0"){
	document.getElementById('div_busc_tipo').innerHTML = "<br /><img src=\"img/cargando_filtro_tipo.gif\"width=\"16\" height=\"16\" >";
	window.setTimeout("con_filtro_tipo.connect('dinamic/filtro_tipo.php', 'GET', 'loc="+localidad+"', fin_filtro_tipo)", 1000);
	}
}

function enviar_contacto(origen){
	if (document.getElementById('nom').value != ""){
		if (document.getElementById('telefon').value != "" || document.getElementById('email').value != ""){
			
			if(origen == "ficha"){
				var referencia = document.getElementById('referencia').value;	
				var localidad = document.getElementById('nombre_localidad').value;	
				var nombre = document.getElementById('nom').value;
				//var apellidos = document.getElementById('cognom').value;
				//var direccion = document.getElementById('direccio').value;
				var comentaris = document.getElementById('comentaris').value;
				var poblacion = document.getElementById('poblacio').value;
				var telefono = document.getElementById('telefon').value;
				var email = document.getElementById('email').value;
				
			document.getElementById('contacto').innerHTML = '<div style="display:block;float:left;width:573px;height:215px;font-family:Verdana, Arial, Helvetica, sans-serif;font-size:14px; color:#597B00; font-weight:bold;text-align:center;"><br><br>Enviant la petici&oacute;...</div>';
			var myConn_enviar = new XHConn();	
			var fin_enviar = function (oXML) { document.getElementById('contacto').innerHTML = oXML.responseText; };
			myConn_enviar.connect("mailform/mailform_contacto.php", "POST","referencia="+referencia+"&localidad="+localidad+"&nombre="+nombre+"&apellidos="+apellidos+"&direccion="+direccion+"&poblacion="+poblacion+"&telefono="+telefono+"&email="+email+"&origen="+origen+"&comentaris="+comentaris, fin_enviar);			

			}
			
			if(origen == "contacto"){
				var nombre = document.getElementById('nom').value;	
				var apellidos = document.getElementById('apellidos').value;	
				var empresa = document.getElementById('empresa').value;
				var direccion = document.getElementById('direccion').value;
				var poblacion = document.getElementById('poblacion').value;
				var cp = document.getElementById('cp').value;
				var telefono = document.getElementById('telefon').value;
				var fax = document.getElementById('fax').value;
				var email = document.getElementById('email').value;
				var comentario = document.getElementById('comentario').value;
				
			document.getElementById('contacto').innerHTML = '<div style="display:block;float:left;padding-left:100px;width:437;height:215px;font-family:Verdana, Arial, Helvetica, sans-serif;font-size:14px; color:#597B00; font-weight:bold;text-align:center;"><br><br>Enviant la petici&oacute;...</div>';
			var myConn_enviar = new XHConn();	
			var fin_enviar = function (oXML) { document.getElementById('contacto').innerHTML = oXML.responseText; };
			myConn_enviar.connect("mailform/mailform_contacto.php", "POST","nombre="+nombre+"&apellidos="+apellidos+"&empresa="+empresa+"&direccion="+direccion+"&poblacion="+poblacion+"&cp="+cp+"&telefono="+telefono+"&email="+email+"&fax="+fax+"&comentario="+comentario+"&origen="+origen, fin_enviar);			

			}
			
			
			
		} else {
			alert("Si us plau, introdueixi un telèfon o e-mail de contacte");	
		}
	} else {
		alert("Si us plau, introdueixi una persona de contacte");		
	}
}
/*----------------------------------------Funciones mapa------------------------------------------------*/


var map; //hacemos global la variable para poder interactuar con otras funciones
function carga_mapa(idlocalidad,zoom) {
	var urlstr="http://www.industrialcat.com/dinamic/xml_centro_mapa.php?idlocalidad="+idlocalidad;
	var request = GXmlHttp.create();
	request.open("GET", urlstr, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
		var xmlDoc = request.responseXML;
		var markers = xmlDoc.documentElement.getElementsByTagName("marker");
			for (var i = 0; i < markers.length; i++) {
			longitud_centro = parseFloat(markers[i].getAttribute("lng"));
			latitud_centro = parseFloat(markers[i].getAttribute("lat"));
				//filtro para evitar que se cargue el google maps sin coordenadas
				if (latitud_centro != 0 && longitud_centro != 0){
				inicia_mapa(longitud_centro,latitud_centro,idlocalidad,zoom);
				}
			}
    	 }
	}
	request.send(null);

function inicia_mapa(longitud_centro,latitud_centro,idlocalidad,zoom){
	document.getElementById('google_maps').style.width = '877px';
	document.getElementById('google_maps').style.height = '200px';

	map = new GMap2(document.getElementById("google_maps"));
	map.addControl(new GSmallMapControl());
	map.setCenter(new GLatLng(latitud_centro, longitud_centro), zoom);
	map.setMapType(G_NORMAL_MAP);

	function createInfoMarker(point,idlocalidad,localidad,localidad_filtro,producto) {
//	var marker = new GMarker(point, icon);	
	var marker = new GMarker(point);	
	//definimos como variables aquellos campos que contengan alguna obra nueva
	//seteamos las variables a vacio
	var producto_text = "";
	
	//mas de un elemento PLURALES
	if (producto > 0){
	producto_text = "<b>"+producto+"</b> Immobles<br />";
	}


	//un elemento SINGULARES
	if (producto == 1){
	producto_text = "<b>"+producto+"</b> Immoble<br />";
	}
	
	
	var detalle_marca = '<table width="200" border="0" cellpadding="5" cellspacing="0" bgcolor="#FFFFFF"" style="border: 1px solid #000000;">';
	detalle_marca += '<tr><td bgcolor="#2A7686" align="center" class="titulo_marca" height="20"><strong>'+localidad+'</strong></td></tr>';
	detalle_marca += '<tr><td height="5"></td></tr>';
	detalle_marca += '<tr><td height="12" align="center" class="texto_marca">'+producto_text+'</td></tr>';
	detalle_marca += '<tr><td height="5"></td></tr></table>';
	
		
	// cargamos el contenido del detalle
	GEvent.addListener(marker, "mouseover", function() { ddrivetip(detalle_marca);} );
	GEvent.addListener(marker, "mouseout", function() {  hideddrivetip();} );
	GEvent.addListener(marker, "click", function() { hideddrivetip(); idlocalidad_global=idlocalidad; pagina_global=1; document.location.href='Naus-i-terrenys-'+localidad_filtro+'.html';});
	return marker;
	}
	
	var urlstr="http://www.industrialcat.com/dinamic/xml_marcas_mapa.php?idlocalidad="+idlocalidad;
	var request = GXmlHttp.create();
	request.open("GET", urlstr, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
		var xmlDoc = request.responseXML;
		var markers = xmlDoc.documentElement.getElementsByTagName("marker");
			for (var i = 0; i < markers.length; i++) {
			var point = new GPoint(parseFloat(markers[i].getAttribute("lng")), parseFloat(markers[i].getAttribute("lat")));
			var marker = createInfoMarker(point,parseFloat(markers[i].getAttribute("idlocalidad")),markers[i].getAttribute("localidad"),markers[i].getAttribute("localidad_filtro"),parseFloat(markers[i].getAttribute("producto")));
			map.addOverlay(marker);
       		}
    	 }
	}
 	 request.send(null);
} // fin funcion inicia mapa

} // fin carga_mapa


// Gerenador de etiquetas ALT--------------------------------------------

var offsetfromcursorX=12 //Customize x offset of tooltip
var offsetfromcursorY=10 //Customize y offset of tooltip

var offsetdivfrompointerX=10 //Customize x offset of tooltip DIV relative to pointer image
var offsetdivfrompointerY=14 //Customize y offset of tooltip DIV relative to pointer image. Tip: Set it to (height_of_pointer_image-1).

document.write('<div id="dhtmltooltip"></div>') //write out tooltip DIV
document.write('<img id="dhtmlpointer" src="http://www.industrialcat.com/img/arrow2.gif" style="visibility: hidden;">') //write out pointer image

var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false
if (ie||ns6)
var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""

var pointerobj=document.all? document.all["dhtmlpointer"] : document.getElementById? document.getElementById("dhtmlpointer") : ""

function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function ddrivetip(thetext, thewidth, thecolor){
if (ns6||ie){
if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
tipobj.innerHTML=thetext
enabletip=true
return false
}
}

function positiontip(e){
if (enabletip){
var nondefaultpos=false
var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
//Find out how close the mouse is to the corner of the window
var winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20
var winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight-20

var rightedge=ie&&!window.opera? winwidth-event.clientX-offsetfromcursorX : winwidth-e.clientX-offsetfromcursorX
var bottomedge=ie&&!window.opera? winheight-event.clientY-offsetfromcursorY : winheight-e.clientY-offsetfromcursorY

var leftedge=(offsetfromcursorX<0)? offsetfromcursorX*(-1) : -1000

//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<tipobj.offsetWidth){
//move the horizontal position of the menu to the left by it's width
tipobj.style.left=curX-tipobj.offsetWidth+"px"
nondefaultpos=true
}
else if (curX<leftedge)
tipobj.style.left="5px"
else{
//position the horizontal position of the menu where the mouse is positioned
tipobj.style.left=curX+offsetfromcursorX-offsetdivfrompointerX+"px"
pointerobj.style.left=curX+offsetfromcursorX+"px"
}

//same concept with the vertical position
if (bottomedge<tipobj.offsetHeight){
tipobj.style.top=curY-tipobj.offsetHeight-offsetfromcursorY+"px"
nondefaultpos=true
}
else{
tipobj.style.top=curY+offsetfromcursorY+offsetdivfrompointerY+"px"
pointerobj.style.top=curY+offsetfromcursorY+"px"
}
tipobj.style.visibility="visible"
if (!nondefaultpos)
pointerobj.style.visibility="visible"
else
pointerobj.style.visibility="hidden"
}
}

function hideddrivetip(){
if (ns6||ie){
enabletip=false
tipobj.style.visibility="hidden"
pointerobj.style.visibility="hidden"
tipobj.style.left="-1000px"
tipobj.style.backgroundColor=''
tipobj.style.width=''
}
}

document.onmousemove=positiontip
