﻿// Get the URL parameter
function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null) return "";
    else return results[1];
}
function clearFD() {
    document.getElementById('fdback').innerHTML = '';
}
function makeCoord(instr) {
    var coord = 0.0;
    if (instr.match(/^\s*\d{1,3}\s+\d{1,2}\s+[\d\.]+\s*$/)) {
        var res = instr.match(/(\d+)\s+(\d+)\s+([\d\.]+)/);
        coord = parseFloat(res[1]) + (parseFloat(res[2]) / 60.0) + (parseFloat(res[3]) / 3600.0);
    }
    if (instr.match(/^\s*\d{1,3}\s+[\d\.]+\s*$/)) {
        var res = instr.match(/(\d+)\s+([\d\.]+)/);
        coord = parseFloat(res[1]) + (parseFloat(res[2]) / 60.0);
    }
    if (instr.match(/^\s*[\d{1,3}\.\d*]+\s*$/)) {
        var res = instr.match(/([\d\.]+)/);
        coord = parseFloat(res[1]);
    }
    return coord;
}
function testCoord(instr) {
    if (instr.match(/^\s*\d{1,3}\s+\d{1,2}\s+[\d\.]+\s*$/)) {
        return true;
    }
    if (instr.match(/^\s*\d{1,3}\s+[\d\.]+\s*$/)) {
        return true;
    }
    if (instr.match(/^\s*[\d{1,3}\.\d*]+\s*$/)) {
        return true;
    }
    return false;
}
function gotoLL() {
    var lat = 0.0;
    var lon = 0.0;

    if (testCoord(document.getElementById('lattext').value.replace(/[-\x22\x2c\x60\x27]/g, ' '))) {
        lat = makeCoord(document.getElementById('lattext').value.replace(/[-\x22\x2c\x60\x27]/g, ' '));
        if ((lat < -90.0) || (lat > 90.0)) {
            document.getElementById('fdback').innerHTML = 'Latitude out of range -90 to 90 ' + document.getElementById('lattext').value;
            return false;
        }
        else {
            var mylist = document.getElementById('latdir');
            var latdir = mylist.options[mylist.selectedIndex].text;
            if (latdir == 'S') {
                lat = lat * -1.0;
            }
        }
    }
    else {
        document.getElementById('fdback').innerHTML = 'Invalid latitude ' + document.getElementById('lattext').value;
        return false;
    }

    if (testCoord(document.getElementById('lontext').value.replace(/[-\x22\x2c\x60\x27]/g, ' '))) {
        lon = makeCoord(document.getElementById('lontext').value.replace(/[-\x22\x2c\x60\x27]/g, ' '));
        if ((lon < -180.0) || (lon > 180.0)) {
            document.getElementById('fdback').innerHTML = 'Longitude out of range -180 to 180 ' + document.getElementById('lattext').value;
            return false;
        }
        else {
            var mylist = document.getElementById('londir');
            var londir = mylist.options[mylist.selectedIndex].text;
            if (londir == 'W') {
                lon = lon * -1.0;
            }
        }
    }
    else {
        document.getElementById('fdback').innerHTML = 'Invalid longitude ' + document.getElementById('lontext').value;
        return false;
    }
    processClick(new GLatLng(parseFloat(lat), parseFloat(lon)), true);
    return true;
}
// Display a user feedback string
function setFeedback(str) {
    if (str.length > 0) {
        document.getElementById('feedback').innerHTML = str;
    }
    else {
        document.getElementById('feedback').innerHTML = '&nbsp;';
    }
}
// isLocalSearch property is 1 if currently in a LocalSearch selection
//  or 0 for a polygon selection
function getLocalSearch() {
    return isLocalSearch;
}

function setLocalSearch(nval) {
    isLocalSearch = nval;
}

function getMyIdle() {
    return myidle;
}

function getSelPnt() {
    return selpnt;
}
function idleLS() {
    myidle = 1;
    LSearch.goIdle();
    myidle = 0;
    setLocalSearch(0);
}
// create the LocalSearch control
function createLS() {
    var LS = null;
    /* Metal Mode */
    // set up pins, use the metalset
    var pins = new Array();
    pins["kml"] = "metalblue";
    pins["local"] = "metalred";
    var labels = new Array();
    labels["kml"] = "metalblue";
    labels["local"] = "metalred";
    var options = {
        listingTypes: GlocalSearch.TYPE_BLENDED_RESULTS,
        Xpins: pins,
        Xlabels: labels,
        //                    onGenerateMarkerHtmlCallback: function(marker, html, result) {
        //                        showDBG(html.innerHTML);
        //                    },
        onIdleCallback: function() {
            var inls = getLocalSearch();
            if (getMyIdle() != 1) {
                if (getLocalSearch() == 1) {
                    processClick(getSelPnt(), true);
                }
            }
        },
        onMarkersSetCallback: function(markers) {
            if (markers.length > 0) {
                processClick(markers[0].marker.getLatLng(), false);
            }
            else {
                processClick(map.getCenter(), true);
            }
            setLocalSearch(1);
        }
    }
    var topLeft = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(100, 5));
    LS = new google.maps.LocalSearch(options);
    map.addControl(LS, topLeft);
    return LS;
}
// Main executable starts here.......................
var i = 0;
var polys = [];
var reslist = [];
var visreslist = [];
var jsonData = [];
var selectedId = null;
var ptmarker = null;
var dbgon = 0; // 1 enables debug messages to go to page
var Icon = new GIcon();
Icon.image = 'data/red_MarkerA.png';
Icon.iconSize = new GSize(20, 34);
Icon.iconAnchor = new GPoint(10, 34);
Icon.infoWindowAnchor = new GPoint(2, 30);
var isLocalSearch = 0;
var selpnt = null;
var myidle = 0;
var map = null;
var LSearch = null;

function callMe() {
    if (GBrowserIsCompatible()) {
        // Display the map, with some controls
        showDBG('start me');
        setFeedback('Loading...');
        map = new GMap(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.addControl(new GScaleControl());
        map.setCenter(new GLatLng(52.3, -110.00), 2);
        // look for URL parameters ll is center, z is zoom level, spn is display area span
        var c = gup('ll');
        var z = gup('z');
        var spn = gup('spn');
        if (c.length > 0) {
            var y = Number(c.substr(0, c.indexOf(',')));
            var x = Number(c.substr(c.indexOf(',') + 1));
            map.setCenter(new GLatLng(y, x));

            if (spn.length > 0) {
                dy = Number(spn.substr(0, spn.indexOf(',')));
                dy = dy / 2;
                dx = Number(spn.substr(spn.indexOf(',') + 1));
                dx = dx / 2;
                var sw = new GLatLng(Number(y - dy), Number(x - dx));
                var ne = new GLatLng(Number(y + dy), Number(x + dx));
                var bnds = new GLatLngBounds(sw, ne);
                map.setZoom(map.getBoundsZoomLevel(bnds));
            }
            else if (z.length > 0) {
                var zl = Number(z);
                map.setZoom(zl);
            }
        }
        showDBG("make LS");
        // create the LocalSearch control
        LSearch = createLS();
        map.enableScrollWheelZoom();
        // === Define the function thats going to process the JSON file ===
        process_it = function(doc) {
            // === Parse the JSON document ===
            // filter points to change ^n to new GLatLgn(
            doc = doc.replace(/\$n/g, 'new GLatLng(');
            doc = doc.replace(/\$t/g, "<table width='100%'><tr><td width='22%'>");
            jsonData = eval('(' + doc + ')');
            // === Null the polylines ===
            for (var i = 0; i < jsonData.lines.length; i++) {
                polys[i] = null;
            }
            // draw the polygongs for this view
            setPolyVis(map.getZoom());
            var c = gup('ll');
            if (c.length > 0) {
                var y = Number(c.substr(0, c.indexOf(',')));
                var x = Number(c.substr(c.indexOf(',') + 1));
                processClick(new GLatLng(y, x), true);
            }
        }
        // === Fetch the JSON data file ====
        showDBG("fetch file");
        if (file.length > 0) {
            GDownloadUrl(file, process_it);
            showDBG("end file proc");
            GEvent.addListener(map, "zoomend", function(oz, nz) {
                setPolyVis(nz);
                showResList(document.getElementById('side_bar'));
                setFeedback('');
            });
            GEvent.addListener(map, "movestart", function() {
                setFeedback('Moving...');
            });
            GEvent.addListener(map, "move", function() {
                setFeedback('Moving...');
            });
            GEvent.addListener(map, "moveend", function() {
                setPolyVis(map.getZoom());
                showResList(document.getElementById('side_bar'));
                setFeedback('');
            });
            GEvent.addListener(map, "click", function(overl, ll, overlll) {
                if (overl == undefined) {
                    if (getLocalSearch() == 1) {
                        idleLS();
                    }
                    processClick(ll, true);
                }
                else {
                    if (overl instanceof GPolygon) {
                        if (getLocalSearch() == 1) {
                            idleLS();
                        }
                        processClick(overlll, true);
                    }
                }
            });
        }
        else {
            alert('Unknown area');
        }
    }
    else {
        alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}
// does the given set of points appear in the current view
function onScreen(points, mapbnds, dateline) {
    var minlng = 99999;
    var maxlng = -99999;
    var minlat = 99999;
    var maxlat = -99999;
    for (var i = 0; i < points.length; i++) {
        if (mapbnds.containsLatLng(points[i])) {
            return true;
        }
        if (dateline == '1') {
            if (points[i].lng() < 0) {
                maxlng = Math.max(maxlng, points[i].lng());
                minlng = Math.min(minlng, points[i].lng());
            }
            maxlat = Math.max(maxlat, points[i].lat());
            minlat = Math.min(minlat, points[i].lat());
        }
        else {
            maxlng = Math.max(maxlng, points[i].lng());
            minlng = Math.min(minlng, points[i].lng());
            maxlat = Math.max(maxlat, points[i].lat());
            minlat = Math.min(minlat, points[i].lat());
        }
    }
    var b = new GLatLngBounds(new GLatLng(minlat, minlng), new GLatLng(maxlat, maxlng));
    return mapbnds.intersects(b);
}
// display only the polygons for current view, remove polygons outside of view, create new as needed
function setPolyVis(mapzoom) {
    var showcbox = document.getElementById('showAllCheck');
    setFeedback('Redrawing...');
    var mbnds = map.getBounds();
    var showall = document.getElementById('showAllCheck').checked;
    var jl = jsonData.lines;
    var myonScreen = onScreen;
    var mypolys = polys;

    for (var i = 0; i < mypolys.length; i++) {
        // abbrev minzoom maxzoom
        if (((mapzoom >= jl[i].mn) && (mapzoom <= jl[i].mx)) || showall) {
            // abrev dateline
            if (myonScreen(jl[i].points, mbnds, jl[i].dl)) {
                if (mypolys[i] == null) {
                    var poly = null;
                    // abbrev colour
                    poly = new GPolygon(jl[i].points, jl[i].cl, 2, 0.5, "#CC0000", 0.0);
                    map.addOverlay(poly);
                    mypolys[i] = poly;
                }
            }
            else {
                if (mypolys[i] != null) {
                    map.removeOverlay(polys[i]);
                    mypolys[i] = null;
                }
            }
        }
        else {
            if (mypolys[i] != null) {
                map.removeOverlay(polys[i]);
                mypolys[i] = null;
            }
        }
    }
    if (selectedId != null) {
        if (mypolys[selectedId] != null) {
            showSelectedPoly(selectedId);
        }
    }
    setFeedback('');
}
// display a table data item
function addDataItem(tdiv, item, tstyle) {
    var it1 = document.createElement('td');
    it1.appendChild(item);
    it1.style.cssText = tstyle;
    tdiv.appendChild(it1);
}
// display selected charts in side bar area and select the first one
function showResList(resdiv) {
    for (var i = resdiv.childNodes.length - 1; i >= 0; i--) {
        resdiv.removeChild(resdiv.childNodes[i]);
    }
    visreslist = [];
    if (reslist.length > 0) {
        var tbl = document.createElement('tbody');
        var trow = document.createElement('tr');
        addDataItem(trow, document.createTextNode('Chart'), 'font-weight:bold;text-align:center;');
        addDataItem(trow, document.createTextNode('Panel'), 'font-weight:bold;text-align:center;');
        addDataItem(trow, document.createTextNode('Scale'), 'font-weight:bold;text-align:center;');
        tbl.appendChild(trow);
        for (i = 0; i < reslist.length; i++) {
            var newa = document.createElement('a');
            newa.id = 'an' + reslist[i];
            if (polys[reslist[i]] == null) {
                dbgon = 1;
                // abbrev dateline
                onScreen(jsonData.lines[reslist[i]].points, map.getBounds(), jsonData.lines[reslist[i]].dl);
                dbgon = 0;
            }
            else {
                visreslist.push(reslist[i]);
                newa.href = '#';
                newa.onclick = function() {
                    var myId = this.id.substring(2, this.id.length);
                    displaySelect(myId);
                };
            }
            var t = document.createTextNode(jsonData.lines[reslist[i]].cnum);
            newa.appendChild(t);
            trow = document.createElement('tr');
            addDataItem(trow, newa, 'text-align:left;');
            addDataItem(trow, document.createTextNode(jsonData.lines[reslist[i]].kapp), 'text-align:right;');
            // abbrev scale
            addDataItem(trow, document.createTextNode(jsonData.lines[reslist[i]].scale), 'text-align:right;');
            tbl.appendChild(trow);
        }
        if ((prodid != 'state') && (prodid != 'cgd') && (prodid != 'cp')) {
            var ttbl = document.createElement('table');
            ttbl.appendChild(tbl);
            resdiv.appendChild(ttbl);
        }

        if (visreslist.length > 0) {
            displaySelect(visreslist[0]);
        }
        else {
            clearSelectedText();
        }
    }
    else {
        clearSelectedText();
        resdiv.appendChild(document.createTextNode('Click in a polyon on the map to see product information'));
    }
}
function processClick(llval, isPoly) {
    // clear last selection
    if (selectedId != null) {
        if (polys[selectedId] != null) {
            ns = { color: "#FF0000", weight: 2, opacity: 0.0 };
            polys[selectedId].setFillStyle(ns);
            if (prodid == 'cgd') {
                if (selectedId == 3 && polys[4] != null) {
                    polys[4].setFillStyle(ns);
                }
                if (selectedId == 4 && polys[3] != null) {
                    polys[3].setFillStyle(ns);
                }
            }
        }
    }
    map.panTo(llval);
    if (ptmarker != null) {
        map.removeOverlay(ptmarker);
        ptmarker = null;
        document.getElementById('pointinfo').innerHTML = '';
    }
    if (isPoly) {
        ptmarker = new GMarker(llval, { icon: Icon, title: 'Selected' });
        map.addOverlay(ptmarker);
    }
    showMapLocation(llval);
    // clear any previous results
    resdiv = document.getElementById('side_bar');
    reslist = [];
    visreslist = [];
    selectedId = null;
    reslinks = document.getElementById('chartlinksdiv');
    for (var k = reslinks.childNodes.length - 1; k >= 0; k--) {
        reslinks.removeChild(reslinks.childNodes[k]);
    }
    // find polys at this point
    var jl = jsonData.lines;
    var mypolys = polys
    var myisInside = isInside;
    for (i = 0; i < mypolys.length; i++) {
        // abbrev dateline
        if (myisInside(llval, jl[i].points, jl[i].dl)) {
            reslist.push(i);
        }
    }
    showResList(resdiv);
}
function polyClick(llval) {
    if (getLocalSearch() == 1) {
        myidle = 1;
        LSearch.goIdle();
        myidle = 0;
        setLocalSearch(0);
    }
    processClick(llval, true);
}
// create a polygon click event handler
function createPolyClick(poly, myId) {
    GEvent.addListener(poly, "click", polyClick);
}
// create the hyperlinks to other pages for the selected chart
function showLinks(pid) {
    var tdiv = document.getElementById('chartlinksdiv');
    var temp = '';
    for (var k = tdiv.childNodes.length - 1; k >= 0; k--) {
        tdiv.removeChild(tdiv.childNodes[k]);
    }
    tdiv.appendChild(document.createElement('br'));
    temp = prodstr + ' {0}';
    tdiv.appendChild(document.createTextNode(temp.replace('{0}', jsonData.lines[pid].cnum)));
    tdiv.appendChild(document.createElement('br'));
    //http://www.charts.noaa.gov/OnLineViewer/{0}.shtml
    if (link1.length > 0) {
        var newa = document.createElement('a');
        temp = link1;
        //        temp = 'http://www.charts.noaa.gov/OnLineViewer/{0}.shtml';
        if ((prodid == 'state') || (prodid == 'cgd')) {
            newa.href = temp.replace('{0}', jsonData.lines[pid].abbrev);
        }

        else if (prodid == 'cp') {
        newa.href = jsonData.lines[pid].dlink;

        }
        else {
            newa.href = temp.replace('{0}', jsonData.lines[pid].cnum);
        }
        newa.target = '_blank';
        newa.appendChild(document.createTextNode(link1t));
        tdiv.appendChild(newa);
        tdiv.appendChild(document.createElement('br'));
    }
    //http://ocsdata.ncd.noaa.gov/nm/Listing.asp?Chart={0}
    if (link2.length > 0) {
        newa = document.createElement('a');
        temp = link2;
        if ((prodid == 'state') || (prodid == 'cgd')) {
            newa.href = temp.replace('{0}', jsonData.lines[pid].abbrev);
        }
        else {
            newa.href = temp.replace('{0}', jsonData.lines[pid].cnum);
        }
        newa.target = '_blank';
        newa.appendChild(document.createTextNode(link2t));
        tdiv.appendChild(newa);
        tdiv.appendChild(document.createElement('br'));
    }
    //http://www.charts.noaa.gov/RNCs/Agreement.shtml?{0}
    if (link3.length > 0) {
        newa = document.createElement('a');
        temp = link3;
        if ((prodid == 'state') || (prodid == 'cgd')) {
            newa.href = temp.replace('{0}', jsonData.lines[pid].abbrev);
        }
        else {
            newa.href = temp.replace('{0}', jsonData.lines[pid].cnum);
        }
        newa.target = '_blank';
        newa.appendChild(document.createTextNode(link3t));
        tdiv.appendChild(newa);
        tdiv.appendChild(document.createElement('br'));
    }
    //http://www.charts.noaa.gov/RNCs/Agreement.shtml?{0}
    if (link4.length > 0) {
        newa = document.createElement('a');
        temp = link4;
        if ((prodid == 'state') || (prodid == 'cgd')) {
            newa.href = temp.replace('{0}', jsonData.lines[pid].abbrev);
        }
        else {
            newa.href = temp.replace('{0}', jsonData.lines[pid].cnum);
        }
        newa.target = '_blank';
        newa.appendChild(document.createTextNode(link4t));
        tdiv.appendChild(newa);
        tdiv.appendChild(document.createElement('br'));
    }
}

// clear old selected polygon and highlight the new one
function displaySelect(theid) {
    if (selectedId != null) {
        ns = { color: "#FF0000", weight: 2, opacity: 0.0 };
        if (polys[selectedId] != null) {
            polys[selectedId].setFillStyle(ns);
            if (prodid == 'cgd') {
                if (selectedId == 3 && polys[4] != null) {
                    polys[4].setFillStyle(ns);
                }
                if (selectedId == 4 && polys[3] != null) {
                    polys[3].setFillStyle(ns);
                }
            }
        }
        if ((prodid != 'state') && (prodid != 'cgd') && (prodid != 'cp')) {
            var anch = document.getElementById('an' + selectedId);
            anch.style.backgroundColor = "";
        }
    }
    showSelectedPoly(theid);
    return true;
}

// highlight the given polygon
function showSelectedPoly(theid) {
    //                    showDBG('turn on highlight ' + 'an' + theid);
    selectedId = theid;
    if ((prodid != 'state') && (prodid != 'cgd') && (prodid != 'cp')) {
        document.getElementById('an' + theid).style.backgroundColor = "yellow";
    }
    ns = { color: "#FFFF00", weight: 2, opacity: 0.5 };
    if (polys[selectedId] == null) {
        clearSelectedText();
    }
    else {
        document.getElementById('infodiv').innerHTML = jsonData.lines[theid].infotext;
        showLinks(theid);
        polys[selectedId].setFillStyle(ns);
        if (prodid == 'cgd') {
            if (selectedId == 3 && polys[4] != null) {
                polys[4].setFillStyle(ns);
            }
            if (selectedId == 4 && polys[3] != null) {
                polys[3].setFillStyle(ns);
            }
        }
    }
}
function clearSelectedText() {
    document.getElementById('infodiv').innerHTML = '';
    var tdiv = document.getElementById('chartlinksdiv');
    for (var k = tdiv.childNodes.length - 1; k >= 0; k--) {
        tdiv.removeChild(tdiv.childNodes[k]);
    }
}
// convert clicked location to DMS and display
function showMapLocation(llval) {
    with (Math) {
        var flat = abs(llval.lat());
        var lat = truncNumber(flat);

        var latmin = truncNumber((flat - lat) * 60.0);
        var latsec = (flat - (lat + latmin / 60.0)) * 3600.0;

        if (latsec > 59.999999) {
            latmin = latmin + 1;
            latsec = 0.0;
        }

        if (latmin > 59.999999) {
            lat = lat + 1;
            latmin = 0;
        }

        var latstr = lat.toString();
        if (lat < 10) {
            latstr = '0' + latstr;
        }
        var latminstr = latmin.toString();
        if (latmin < 10) {
            latminstr = '0' + latminstr;
        }
        var latsecstr = latsec.toFixed(2).toString();
        if (latsec < 10) {
            latsecstr = '0' + latsecstr;
        }

        latstr += '&nbsp;&nbsp;' + latminstr + "'&nbsp;&nbsp;" + latsecstr + "''";
        if (llval.lat() > 0) {
            latstr += ' N';
        }
        else {
            latstr += ' S';
        }

        var flon = abs(llval.lng());
        var lon = truncNumber(flon);
        var lonmin = truncNumber((flon - lon) * 60.0);
        var lonsec = (flon - (lon + lonmin / 60.0)) * 3600.0;


        if (lonsec > 59.999999) {
            lonmin = lonmin + 1;
            lonsec = 0.0;
        }

        if (lonmin > 59.999999) {
            lon = lon + 1;
            lonmin = 0;
        }

        var lonstr = lon.toString();
        if (lon < 100) {
            lonstr = '0' + lonstr;
        }
        if (lon < 10) {
            lonstr = '0' + lonstr;
        }
        var lonminstr = lonmin.toString();
        if (lonmin < 10) {
            lonminstr = '0' + lonminstr;
        }
        var lonsecstr = lonsec.toFixed(2).toString();
        if (lonsec < 10) {
            lonsecstr = '0' + lonsecstr;
        }

        lonstr += '&nbsp;&nbsp;' + lonminstr + "'&nbsp;&nbsp;" + lonsecstr + "''";
        if (llval.lng() > 0) {
            lonstr += ' E';
        }
        else {
            lonstr += ' W';
        }

        var ptdiv = document.getElementById('pointinfo');
        selpnt = llval;
        ptdiv.innerHTML = '<img src="data/red_MarkerA.png" /><a href="#" onClick="map.panTo(selpnt);">Selected Point:</a><br />&nbsp;&nbsp;' + latstr + '<br />' + lonstr;
    }
    return true;
}
// the 'Show all charts' check box was clicked
function showAllChange() {
    setPolyVis(map.getZoom());
    showResList(document.getElementById('side_bar'));
}
// do the right kind of integer truncation
function truncNumber(num) {
    if (num < 0) {
        return Math.ceil(num);
    }
    else {
        return Math.floor(num);
    }
}
// calc slope of line for given points
function slope(ax, ay, bx, by) {
    return ((by - ay) / (bx - ax));
}
// find the b value in y = mx + b equation
function bconst(m, ax, ay) {
    if (Math.abs(m) == Infinity) {
        return (ax);
    }
    else {
        return (ay - (m * ax));
    }
}
// is x between a1 and a2
function between(x, a1, a2) {
    return ((Math.abs(x - a1) < Math.abs(a2 - a1)) && (Math.abs(x - a2) < Math.abs(a2 - a1)));
}
// does the clickat location fall inside the points array
function isInside(clickat, points, dateline) {
    var intcnt = 0;
    for (var i = 0; i < points.length - 1; i++) {
        var pt1lng = points[i].lng();
        var pt2lng = points[i + 1].lng();
        if (dateline == '1') {
            if (clickat.lng() > 0) {
                if (pt1lng < 0) {
                    pt1lng += 360;
                }
                if (pt2lng < 0) {
                    pt2lng += 360;
                }
            }
            else {
                if (pt1lng > 0) {
                    pt1lng -= 360;
                }
                if (pt2lng > 0) {
                    pt2lng -= 360;
                }
            }
        }
        if (between(clickat.lat(), points[i].lat(), points[i + 1].lat())) {
            var m = slope(pt1lng, points[i].lat(), pt2lng, points[i + 1].lat());
            if (Math.abs(m) == Infinity) {
                if (pt1lng >= clickat.lng()) {
                    intcnt++;
                }
            }
            else {
                var b = bconst(m, pt1lng, points[i].lat());
                var x = (clickat.lat() - b) / m;
                if (x >= clickat.lng()) {
                    intcnt++;
                }
            }
        }
    }

    if ((intcnt % 2) == 1) {
        return true;
    }
    else {
        return false;
    }
}
function showDBG(msg) {
    if (dbgon == 1) {
        var thediv = document.getElementById('dbg');
        thediv.innerHTML = thediv.innerHTML + msg + '<br />';
    }
}
// This Javascript is based on code provided by the
// Blackpool Community Church Javascript Team
// http://www.commchurch.freeserve.co.uk/   
// http://econym.googlepages.com/index.htm
