﻿/*
CREATED: Shenouda Andrawis 16/07/08
    
Amended: Matt Brunsdon - 16/12/09
*/

var _map = null; // global variable to hold map instance

function load(lat, lng) {
    // create a MDSMap instance
    _map = new MDSMap($get('mymap'));
    // pass in the key for the session
    _map.UserIDSet($get('UserID').value);
    // load the map - use centre provided
    _map.LoadMap(lat, lng, 565, 440, 665, 540);
}

function unload() {
    MDSUnload(); // call to MDSUnload function to destroy everything
}

function original() {
    resetDetails();
    _map.Markers.PopupHideAll();
    _map.OriginalView();
    document.getElementById('data').style.display = "";

}

function zoomToXY(X, Y, zoom) {
    _map.ZoomToPoint(Y, X, zoom);
    _map.Markers.PopupHideAll(); //hides map balloons
}

function zoomToPoint(X, Y, zoom, RadialDistance, poiTypeName, poiSelectionTypeName, name) {
    //alert(RadialDistance)
    _map.ZoomToPoint(Y, X, zoom);
    _map.Markers.PopupHideAll(); //hides map balloons
    //save to hidden variables
    //Log Call
    generateLog(X, Y, RadialDistance, poiTypeName, poiSelectionTypeName, name);
}

function getDetails2(number, X) {
    alert(number + X);
}

//Function gets the shows the details for a spacific postoffice (0 to 4)
function getDetails(number, X, Y, zoom, RadialDistance, poiTypeName, poiSelectionTypeName, name) {
    try {
        document.getElementById('AppointmentListBtn').style.display = "inline-block";
    }
    catch (e) {
    }

    _map.ZoomToPoint(parseFloat(Y), parseFloat(X), parseInt(zoom));
    _map.Markers.PopupHideAll(); //hides map balloons - not implemented in the MDSmap object
    document.getElementById('data').style.display = "none";

    // first clear all details incase there is one already showing
    resetDetails();

    document.getElementById('X').value = X;
    document.getElementById('Y').value = Y;
    document.getElementById('Zoom').value = zoom;

    // show only the spacfic number   
    document.getElementById(number).style.display = "";

    // selectedDetailsNumber is used to store which postbox has been selected
    document.getElementById("selectedDetailsNumber").value = number;
    //alert(document.getElementById('data').display = 'none')

    //Log Call
    generateLog(X, Y, RadialDistance, poiTypeName, poiSelectionTypeName, name);
}

function backToList(number) {
    document.getElementById(number).style.display = "none";
    document.getElementById('data').style.display = "";
    try {
        document.getElementById('Appointment').style.display = "none";
    } catch (e) {
    }
    original();  //go back to original view
}

//Uses Webservice to call a BI Log 
function generateLog(X, Y, RadialDistance, poiTypeName, poiSelectionTypeName, name) {
    //var poiSelectionTypeName = "MapSelection"; 
    //var poiSelectionTypeName = "RecordSelection";
    X = parseFloat(X);
    Y = parseFloat(Y);
    RadialDistance = parseFloat(RadialDistance);
    AnalyticsWS.LogFeature(X, Y, RadialDistance, poiTypeName, poiSelectionTypeName, name, isFinished);
}

function resetDetails() {
    if (document.getElementById('0') != null) { document.getElementById('0').style.display = "none"; }
    if (document.getElementById('1') != null) { document.getElementById('1').style.display = "none"; }
    if (document.getElementById('2') != null) { document.getElementById('2').style.display = "none"; }
    if (document.getElementById('3') != null) { document.getElementById('3').style.display = "none"; }
    if (document.getElementById('4') != null) { document.getElementById('4').style.display = "none"; }
}

function isFinished(result) {
    //alert(result.toString())
}

function getUrlVar(urlVarName) {
    //divide the URL in half at the '?'
    var urlHalves = String(document.location).split('?');
    var urlVarValue = '';
    if (urlHalves[1]) {
        //load all the name/value pairs into an array
        var urlVars = urlHalves[1].split('&');
        //loop over the list, and find the specified url variable
        for (i = 0; i <= (urlVars.length); i++) {
            if (urlVars[i]) {
                //load the name/value pair into an array
                var urlVarPair = urlVars[i].split('=');
                if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
                    //I found a variable that matches, load it's value into the return variable
                    urlVarValue = urlVarPair[1];
                }
            }
        }
    }
    return urlVarValue;
}


