function MapaToponimos() {
	activateWMS(map, 'http://www.idee.es/wms/MTN-Raster/MTN-Raster?REQUEST=GetMap&VERSION=1.1.0&Service=WMS&FORMAT=JPEG&reaspect=false&&LAYERS=mtn_rasterizado', 21, 'EPSG:4326', 'IDEE-IGN', 'http://www.idee.es');
}

function MapaSigPac() {
	activateWMS(map, 'http://195.235.91.59/wms/wms.aspx?REQUEST=GetMap&VERSION=1.1.0&Service=WMS&STYLES=&FORMAT=image/png&BGCOLOR=0xFFFFFF&TRANSPARENT=TRUE&EXCEPTIONS=XML&reaspect=true&&LAYERS=ORTOFOTOS', 12, 'EPSG:23030', 'SIGPAC-Fondo Español de Garantía Agraria', 'http://www.mapa.es/es/sig/pags/sigpac/intro.htm');
}

function MapaPNOA() {
	activateWMS(map, 'http://www.idee.es/wms/PNOA/PNOA?REQUEST=GetMap&VERSION=1.1.1&Service=WMS&FORMAT=JPEG&reaspect=false&&LAYERS=pnoa', 22, 'EPSG:4326', 'IDEE-IGN', 'http://www.idee.es');
}




var DEBUG_MODE = false;
var pi = 3.14159265358979;
var sm_a = 6378137;
var sm_b = 6356752.314;
var sm_EccSquared = 0.00669437999013;
var UTMScaleFactor = 0.9996;

function activateWMS(g, c, f, d, b, h) {
    var a = buildWMSLayer(c, "WMS", d, b, h);
    var e = g.getMapTypes()[5];
    if (e) {
        g.getMapTypes().pop()
    }
    g.addMapType(a);
    g.setMapType(a)
}


function buildWMSLayer(b, j, e, c, h) {
    var f = new GCopyrightCollection("Imagery &copy;");
    var d = new GCopyright(99, new GLatLngBounds(new GLatLng(-90, -180), new GLatLng(90, 180)), 7, c);
    f.addCopyright(d);
    var g = [new GTileLayer(f, 7, 19), G_HYBRID_MAP.getTileLayers()[1]];
    g[0].wmsUrl = b;
    g[0].wmsSrs = e;
    g[0].getTileUrl = function (m, u) {
        var p = 256;
        var l = new GPoint(m.x * p, m.y * p);
        var q = new GPoint((m.x + 1) * p, (m.y + 1) * p);
        var s = G_SATELLITE_MAP.getProjection().fromPixelToLatLng(l, u);
        var r = G_SATELLITE_MAP.getProjection().fromPixelToLatLng(q, u);
        var t;
        if (this.wmsSrs.indexOf("EPSG:230") != -1) {
            var v = new Array(2);
            var n = new Array(2);
            var o = Math.floor((s.x + 180) / 6) + 1;
            o = LatLonToUTMXY(DegToRad(s.y), DegToRad(s.x), o, v);
            o = LatLonToUTMXY(DegToRad(r.y), DegToRad(r.x), o, n);
            switch (this.wmsSrs) {
            case "EPSG:23031":
                v[0] += 95;
                v[1] += 205;
                n[0] += 95;
                n[1] += 205;
                break;
            case "EPSG:23030":
                v[0] += 104;
                v[1] += 205;
                n[0] += 104;
                n[1] += 205;
                if (this.wmsUrl.indexOf("andaluciajunta") != -1) {
                    v[0] += 8;
                    v[1] += 8;
                    n[0] += 8;
                    n[1] += 8
                } else {
                    this.wmsSrs = "EPSG:230" + o
                }
                break
            }
            t = v[0] + "," + n[1] + "," + n[0] + "," + v[1]
        } else { if (this.wmsUrl.indexOf("http://ideg.xunta") != -1) {
                t = (s.x + 0.0004) + "," + (r.y - 0.00014) + "," + (r.x + 0.0004) + "," + (s.y - 0.00014)
            } else {
                t = s.x + "," + r.y + "," + r.x + "," + s.y
            }
        }
        var k = this.wmsUrl + "&SRS=" + this.wmsSrs + "&BBOX=" + t + "&WIDTH=" + p + "&HEIGHT=" + p;
        if (DEBUG_MODE) {
            GLog.writeHtml("<div class='debugInfo'>" + k + "</div>")
        }
        return k
    };
    var a = new GMapType(g, G_SATELLITE_MAP.getProjection(), j, {
        errorMessage: "We're sorry but the WMS server you have selected couldn't serve the map for this request"
    });
    return a
}



function MapLatLonToXY(o, m, c, s) {
    var n, a, p, q, k, f;
    var j, g, e, d, b, r;
    var h;
    p = (Math.pow(sm_a, 2) - Math.pow(sm_b, 2)) / Math.pow(sm_b, 2);
    a = p * Math.pow(Math.cos(o), 2);
    n = Math.pow(sm_a, 2) / (sm_b * Math.sqrt(1 + a));
    q = Math.tan(o);
    k = q * q;
    h = (k * k * k) - Math.pow(q, 6);
    f = m - c;
    j = 1 - k + a;
    g = 5 - k + 9 * a + 4 * (a * a);
    e = 5 - 18 * k + (k * k) + 14 * a - 58 * k * a;
    d = 61 - 58 * k + (k * k) + 270 * a - 330 * k * a;
    b = 61 - 479 * k + 179 * (k * k) - (k * k * k);
    r = 1385 - 3111 * k + 543 * (k * k) - (k * k * k);
    s[0] = n * Math.cos(o) * f + (n / 6 * Math.pow(Math.cos(o), 3) * j * Math.pow(f, 3)) + (n / 120 * Math.pow(Math.cos(o), 5) * e * Math.pow(f, 5)) + (n / 5040 * Math.pow(Math.cos(o), 7) * b * Math.pow(f, 7));
    s[1] = ArcLengthOfMeridian(o) + (q / 2 * n * Math.pow(Math.cos(o), 2) * Math.pow(f, 2)) + (q / 24 * n * Math.pow(Math.cos(o), 4) * g * Math.pow(f, 4)) + (q / 720 * n * Math.pow(Math.cos(o), 6) * d * Math.pow(f, 6)) + (q / 40320 * n * Math.pow(Math.cos(o), 8) * r * Math.pow(f, 8));
    return
}


function MapXYToLatLon(l, k, b, o) {
    var z, h, p, u, D, f, A, w, q;
    var d, v, j, a, s, e, B, m;
    var C, n, c, t, g, E, r;
    z = FootpointLatitude(k);
    D = (Math.pow(sm_a, 2) - Math.pow(sm_b, 2)) / Math.pow(sm_b, 2);
    q = Math.cos(z);
    u = D * Math.pow(q, 2);
    h = Math.pow(sm_a, 2) / (sm_b * Math.sqrt(1 + u));
    p = h;
    f = Math.tan(z);
    A = f * f;
    w = A * A;
    d = 1 / (p * q);
    p *= h;
    v = f / (2 * p);
    p *= h;
    j = 1 / (6 * p * q);
    p *= h;
    a = f / (24 * p);
    p *= h;
    s = 1 / (120 * p * q);
    p *= h;
    e = f / (720 * p);
    p *= h;
    B = 1 / (5040 * p * q);
    p *= h;
    m = f / (40320 * p);
    C = -1 - u;
    n = -1 - 2 * A - u;
    c = 5 + 3 * A + 6 * u - 6 * A * u - 3 * (u * u) - 9 * A * (u * u);
    t = 5 + 28 * A + 24 * w + 6 * u + 8 * A * u;
    g = -61 - 90 * A - 45 * w - 107 * u + 162 * A * u;
    E = -61 - 662 * A - 1320 * w - 720 * (w * A);
    r = 1385 + 3633 * A + 4095 * w + 1575 * (w * A);
    o[0] = z + v * C * (l * l) + a * c * Math.pow(l, 4) + e * g * Math.pow(l, 6) + m * r * Math.pow(l, 8);
    o[1] = b + d * l + j * n * Math.pow(l, 3) + s * t * Math.pow(l, 5) + B * E * Math.pow(l, 7);
    return
}


function LatLonToUTMXY(c, d, a, b) {
    MapLatLonToXY(c, d, UTMCentralMeridian(a), b);
    b[0] = b[0] * UTMScaleFactor + 500000;
    b[1] = b[1] * UTMScaleFactor;
    if (b[1] < 0) {
        b[1] = b[1] + 10000000
    }
    return a
}


function UTMXYToLatLon(a, f, b, e, d) {
    var c;
    a -= 500000;
    a /= UTMScaleFactor;
    if (e) {
        f -= 10000000
    }
    f /= UTMScaleFactor;
    c = UTMCentralMeridian(b);
    MapXYToLatLon(a, f, c, d);
    return
}


function btnToUTM_OnClick() {
    var a = new Array(2);
    if (isNaN(parseFloat(document.frmConverter.txtLongitude.value))) {
        alert("Please enter a valid longitude in the lon field.");
        return false
    }
    lon = parseFloat(document.frmConverter.txtLongitude.value);
    if ((lon < -180) || (180 <= lon)) {
        alert("The longitude you entered is out of range.  Please enter a number in the range [-180, 180).");
        return false
    }
    if (isNaN(parseFloat(document.frmConverter.txtLatitude.value))) {
        alert("Please enter a valid latitude in the lat field.");
        return false
    }
    lat = parseFloat(document.frmConverter.txtLatitude.value);
    if ((lat < -90) || (90 < lat)) {
        alert("The latitude you entered is out of range.  Please enter a number in the range [-90, 90].");
        return false
    }
    zone = Math.floor((lon + 180) / 6) + 1;
    zone = LatLonToUTMXY(DegToRad(lat), DegToRad(lon), zone, a);
    document.frmConverter.txtX.value = a[0];
    document.frmConverter.txtY.value = a[1];
    document.frmConverter.txtZone.value = zone;
    if (lat < 0) {
        document.frmConverter.rbtnHemisphere[1].checked = true	
    } else {
        document.frmConverter.rbtnHemisphere[0].checked = true
    }
    return true
}


function btnToGeographic_OnClick() {
    latlon = new Array(2);
    var a, d, b, c;
    if (isNaN(parseFloat(document.frmConverter.txtX.value))) {
        alert("Please enter a valid easting in the x field.");
        return false
    }
    a = parseFloat(document.frmConverter.txtX.value);
    if (isNaN(parseFloat(document.frmConverter.txtY.value))) {
        alert("Please enter a valid northing in the y field.");
        return false
    }
    d = parseFloat(document.frmConverter.txtY.value);
    if (isNaN(parseInt(document.frmConverter.txtZone.value))) {
        alert("Please enter a valid UTM zone in the zone field.");
        return false
    }
    b = parseFloat(document.frmConverter.txtZone.value);
    if ((b < 1) || (60 < b)) {
        alert("The UTM zone you entered is out of range.  Please enter a number in the range [1, 60].");
        return false
    }
    if (document.frmConverter.rbtnHemisphere[1].checked == true) {
        c = true
    } else {
        c = false
    }
    UTMXYToLatLon(a, d, b, c, latlon);
    document.frmConverter.txtLongitude.value = RadToDeg(latlon[1]);
    document.frmConverter.txtLatitude.value = RadToDeg(latlon[0]);
    return true
};

function DegToRad(a) {
    return (a / 180 * pi)
}
function RadToDeg(a) {
    return (a / pi * 180)
}

function UTMCentralMeridian(a) {
    var b;
    b = DegToRad(-183 + (a * 6));
    return b
}

function ArcLengthOfMeridian(c) {
    var e, d, b, h, g, f;
    var a;
    f = (sm_a - sm_b) / (sm_a + sm_b);
    e = ((sm_a + sm_b) / 2) * (1 + (Math.pow(f, 2) / 4) + (Math.pow(f, 4) / 64));
    d = (-3 * f / 2) + (9 * Math.pow(f, 3) / 16) + (-3 * Math.pow(f, 5) / 32);
    b = (15 * Math.pow(f, 2) / 16) + (-15 * Math.pow(f, 4) / 32);
    h = (-35 * Math.pow(f, 3) / 48) + (105 * Math.pow(f, 5) / 256);
    g = (315 * Math.pow(f, 4) / 512);
    a = e * (c + (d * Math.sin(2 * c)) + (b * Math.sin(4 * c)) + (h * Math.sin(6 * c)) + (g * Math.sin(8 * c)));
    return a
}