﻿function checkSave() {
    var stateId = jsel.getSelVal(document.getElementById('statemenu'));
    
    if (stateId.value == 0) {
        dijit.byId('saveMapDialog').show();
    } else {
        saveMap(dojo.toJson(getMapState(map)));
    }
}

function saveMap(mapData) {
    //alert("map: "+mapData);
    var stateId = jsel.getSelVal(document.getElementById('statemenu'));
    var stateName = "";
    if (stateId.value == 0) {
        //stateName = prompt("Enter a name for the map:");
        stateName = dojo.byId("savename").value;
        //alert("save name: " + dojo.byId("savename").value);
    }
    if (stateId.value != 0 || (stateName != null && stateName != "")) {
        try {
            //console.log( "loading view: "+evt.target.value );
            esri.request({
                url: stateServiceUrl,
                content: {
                    f: "json",
                    action: "save",
                    name: stateName,
                    state: mapData,
                    id: stateId.value,
		    lu: currentUserLogin
                },
                callbackParamName: "callback",
                load: function(viewList) {
                    //alert("saved: "+viewList.stateid); 
                    jsel.loadStateData(document.getElementById('statemenu'), viewList.stateid);
                    alert("Map Saved.");
                },
                error: function(err) {
                    console.error("Unable to get view information" + err);
                }
            });
        }
        catch (e) {
            console.error(e);
        }
    } else {
        alert("You must enter a name for your saved map.");
    }
}

function loadMap() {
    //alert("map: "+mapData);
    //var stateId = prompt("Enter a stateId:");
    var stateId = jsel.getSelVal(document.getElementById('statemenu'));
    if (stateId.value != 0) {
        try {
            console.log("loading state: " + stateId.value);
            esri.request({
                url: stateServiceUrl,
                content: {
                    f: "json",
                    action: "load",
                    id: stateId.value,
                    lu: currentUserLogin
                },
                callbackParamName: "callback",
                load: function(viewList) {
                    //alert("extent2: "+viewList.extent.xmin); 
                    var tmpExtent = new esri.geometry.Extent(viewList.extent);
                    //alert("extent3: "+tmpExtent.xmin); 
                    //
                    drawingLayer.clear();
                    //alert("graphics: "+viewList.graphics); 
                    dojo.forEach(viewList.graphics, function(graphic) {
                        drawingLayer.add(new esri.Graphic(graphic));
                    });
                    //loadView({target:{value:viewList.view}});
                    jsel.loadData(document.getElementById('viewmenu'), viewList.view);
                    try {
                        map.setExtent(tmpExtent);
                    } catch (e) {
                        console.error("Error setting extent: " + e);
                    }
                },
                error: function(err) {
                    console.error("Unable to get view information" + err);
                }
            });
        }
        catch (e) {
            console.error(e);
        }
    }
}

function deleteMap() {
    //alert("map: "+mapData);
    //var stateId = prompt("Enter a stateId:");
    var stateId = jsel.getSelVal(document.getElementById('statemenu'));
    try {
        //alert( "loading state: "+stateId.value );
        esri.request({
            url: stateServiceUrl,
            content: {
                f: "json",
                action: "delete",
                id: stateId.value,
                lu: currentUserLogin
            },
            callbackParamName: "callback",
            load: function(viewList) {
                jsel.loadStateData(document.getElementById('statemenu'), "0");
            },
            error: function(err) {
                console.error("Unable to get view information" + err);
            }
        });
    }
    catch (e) {
        console.error(e);
    }
}