function buscarCoordenadas() {
    var direccion = document.getElementById('direccion').value;
    var localidad = document.getElementById('localidad').value;
    var provincia = document.getElementById('provincia').value;
    var cp = document.getElementById('cp').value;

    var conn = new XHConn();
    if (!conn) {
        alert("XMLHTTP not available");
        return false;
    }
    var fnWhenDone = function (xmlhttp) {
        insertarDirecciones(xmlhttp);
    };
    var sVars = "direccion="+direccion+"&localidad="+localidad+"&provincia="+provincia+"&cp="+cp;
    conn.connect(_geoURL, "POST", sVars, fnWhenDone);
}

function insertarDirecciones(xmlhttp) {
    if (xmlhttp.readyState == 4) { // Complete
        if (xmlhttp.status == 200) { // OK response
            div = document.getElementById('direcciones');
            div.innerHTML = "";
            div.innerHTML = xmlhttp.responseText;
            objLatitud     = document.getElementById('latitud');
            objLongitud    = document.getElementById('longitud');
            objDireccion   = document.getElementById('direccioncompleta');
            objUdDireccion = document.getElementById('uddireccion');
            objUdLatitud   = document.getElementById('udlatitud');
            objUdLongitud  = document.getElementById('udlongitud');
            if (objUdDireccion && objUdLatitud && objUdLongitud) {
                objLatitud.value   = objUdLatitud.value;
                objLongitud.value  = objUdLongitud.value;
                objDireccion.value = objUdDireccion.value;
            } else {
                objLatitud.value   = "";
                objLongitud.value  = "";
                objDireccion.value = "";
            }
        }
    }
}

function setCoordenadas(latitud, longitud, direccion) {
    objLatitud   = document.getElementById('latitud');
    objLongitud  = document.getElementById('longitud');
    objDireccion = document.getElementById('direccioncompleta');
    objLatitud.value   = latitud;
    objLongitud.value  = longitud;
    objDireccion.value = direccion;
}

function mostrarMapa() {
    var Latitud   = document.getElementById('latitud').value;
    var Longitud  = document.getElementById('longitud').value;
    var Direccion = document.getElementById('direccioncompleta').value;
    var Mapa = window.open('', 'Mapa', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=800,height=600');
    if (Mapa) {
        Mapa.focus();
        Mapa.document.open();
        Mapa.document.write('<form name="f" action="'+_mapURL+'" method="POST">'+
                            '<input type="hidden" name="latitud" value="'+Latitud+'" />'+
                            '<input type="hidden" name="longitud" value="'+Longitud+'" />'+
                            '<input type="hidden" name="direccion" value="'+Direccion+'" />'+
                            '</form>');
        Mapa.document.close();
        Mapa.document.f.submit();
    }
}

function load(latitud, longitud, direccion) {
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        var center = new GLatLng(longitud, latitud);
        var marker = new GMarker(center, {draggable: false});
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(center, 13);
        map.addOverlay(marker);
        GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(direccion); });
        fit();
    }
}

function fit() {
    b = document.getElementsByTagName("body").item(0);
    b.style.margin  = "0px";
    b.style.padding = "0px";
    mapa = document.getElementById("map");

    // Netscape
    if (self.innerWidth) {
        iWidth  = self.innerWidth;
        iHeight = self.innerHeight;
    // Explorer 6 if you use a DOCTYPE
    } else if (document.documentElement && document.documentElement.clientWidth) {
        iWidth  = document.documentElement.clientWidth;
        iHeight = document.documentElement.clientHeight;
    // Explorer in all other cases
    } else if (document.body) {
        iWidth  = document.body.clientWidth;
        iHeight = document.body.clientHeight;
    }

    // Netscape 4 has a bug in its implementation of resizeTo
    if (document.layers) {
        tmp1 = parent.outerWidth - parent.innerWidth;
        tmp2 = parent.outerHeight - parent.innerHeight;
        iWidth  -= tmp1;
        iHeight -= tmp2;
    }

    iWidth  = parseInt(mapa.style.width) - iWidth;
    iHeight = parseInt(mapa.style.height) - iHeight - 30;
    window.resizeBy(iWidth, iHeight);
}