var proj;

function generalWarning() {
  alert("Changes will be lost");
}

function saveChanges(map_id) {
    saveChanges = confirm("Would you like to save changes?");

    if (saveChanges == true) {
        change("SAVE_MAP", "");
    }

    unlockProject(map_id);
}

function unlockProject(map_id) {
    change("PROJECT_UNLOCK", map_id + "," + document.view.MAP.selectedIndex);
}

function saveLayerChanges(layer) {
    saveChanges = confirm("Would you like to save changes?");

    if (saveChanges == true) {
        change("SAVE_MAP", "");
    }
// change("LOAD_LAYER","");
}

function saveDatasetChanges(dataset) {
    saveChanges = confirm("Would you like to save changes?");

    if (saveChanges == true) {
        change("SAVE_PROJECT", "");
    }

    change("LOAD_LAYER" + "document.view.LAYER.options[document.view.LAYER.selectedIndex].value)");
}

function sureDelete(del_idx, layername) {
    if (layername == null) {
        sureDel = confirm("Are you sure to delete this layer?");
    } else {
        sureDel = confirm("Are you sure to delete '" + layername + "' Layer?");
    }

    if (sureDel == true)
        change("DELETE_LAYER", del_idx);
    else
        _contextHideAllMenus();
}

function sureDeleteGroup(layername) {
    if (layername == null) {
        sureDel = confirm("Are you sure to delete this layer?");
    } else {
        sureDel = confirm("Are you sure to delete '" + layername + "' Layer?");
    }

    if (sureDel == true)
        change("DELETE_GROUP_LAYER", layername);
    else
        _contextHideAllMenus();
}

function sureDeleteAttr(del_idx, attrname) {
    if (attrname == null) {
        sureDel = confirm("Are you sure to delete this dataset?");
    } else {
        sureDel = confirm("Are you sure to delete '" + attrname + "' Dataset?");
    }

    if (sureDel == true)
        change("DELETE_DATASET", del_idx);
    else
        _contextHideAllMenus();
}

function openChangePwd() {
    openDialog('change_password.jspx', 300, 160);
}

function openNewUser() {
    openDialog('new_user.jspx?init=true', 300, 260);
}

function openDeleteColumn(dci) {
    openDialog('delete_matrix_column.jspx?dci=' + dci, 340, 150);
}

function openAddColumn() {
    openDialog('add_matrix_column.jspx', 340, 150);
}

function openAddRow() {
    openDialog('add_matrix_row.jspx', 340, 150);
}

function openAddExtentEntry() {
    openDialog('AddExtentEntry.jsp', 340, 150);
}

function openLegendEditor(idx) {
    openDialog('legend_editor.jspx?init=true&idx=' + idx, 580, 535); //attrs);
}

function addRasterLegend() {
    openDialog('raster_legend_editor.jspx?init=true&raster=true&idx=-1', 585, 560); //attrs);
}

function openRasterLegend() {
    openDialog('raster_legend_editor.jspx?init=true&raster=true&idx=0', 585, 560); //attrs);
}

function delRasterLegend() {
    sureDel = confirm("Are you sure to delete this legend?");

    if (sureDel == true)
        change("DELETE_RASTER_LEGEND");
}

function backToLayersList(isEditted) {
    if (isEditted) {
    //     saveFirst=confirm("Would you like to save changes?")
    //    if(saveFirst== true) {
    //         change("SAVE_MAP","");
    //    } if(saveFirst==false) {
    //         change("IGNORE_CHANGES","");
    //    }
        }

    change('SET_EDITPROPS_IDX', '0');
}

function logout(isEditted) {
    if (isEditted) {
        var savebeforeOut = confirm(
                                "Map properties had been editted.  Would you like to save changes first before logging out?")

        if (savebeforeOut == true) {
            change("SAVE_MAP", "");
        }
    }

    change('LOG', 'OUT');
}

function openReorderProjects(cnt) {
    cnt = 16;
    initsize = 155;

    if (cnt > 3)
        initsize += ((cnt - 3) * 15.5);

    openDialog('project_reorder.jspx', 420, initsize, 'scroll');
}

function openReorderLayers(group, cnt) {
    initsize = 155;

    if (cnt > 3)
        initsize += ((cnt - 3) * 18.5);

    openDialog('reorder_layers.jspx?group=' + group, 420, initsize, 'scroll');
}

function openRegroupLayers(group) {
    openDialog('layer_regroup.jspx?group=' + group, 350, 200, 'scroll');
}

function openReorderGroupLayers(group) {
    initsize = 305;
    cnt = 5;

    if (cnt > 3)
        initsize += ((cnt - 3) * 30.5);

    openDialog('group_layer_reorder.jspx?group=' + group, 450, initsize, 'scroll');
}

function openReorderDefinitions(cnt) {
    cnt = 16;
    initsize = 155;

    if (cnt > 3)
        initsize += ((cnt - 3) * 15.5);

    openDialog('reorder_definitions.jspx', 420, initsize);
}

function editLayer(idx) {
    //change("SET_EDIT_PROPS_IDX", "2," + idx);
    change('SET_EDITOR_MAP_PANEL', '3');
}

function editRaster(idx) {
    //    change("SET_EDIT_PROPS_IDX", "4," + idx);
    change('SET_EDITOR_MAP_PANEL', '5');
}

function editDataset(idx) {
    //    change("SET_EDIT_PROPS_IDX", "3," + idx);
    change('SET_EDITOR_MAP_PANEL', '4');
}

function setDefaLayer(layer_name) {
    change("LOAD_LAYER", layer_name);
}

function setDefaAttr(idx) {
    change("LOAD_ATTRIBUTE", idx);
}

function editPO_old(which_po, label, isOutline) {
    if (which_po == "MAP_BACKGROUND") {
        openDialog('pattern_outline_editor.jspx?which=' + which_po + '&init=true', 400, 420);
    } else if (which_po.indexOf("FEATURE") != - 1) {
        if (isOutline == "0")
            openDialog(
                'pattern_outline_editor.jspx?which=' + which_po + '&init=true&label=' + label + '&isOutline='
                      + isOutline,
                400,
                420);
        else
            openDialog(
                'pattern_outline_editor.jspx?which=' + which_po + '&init=true&label=' + label + '&isOutline='
                     + isOutline,
                400,
                280);
    } else if (which_po == "ONE_SYMBOL") {
        if (isOutline == "0")
        openDialog(
            'pattern_outline_editor.jspx?which=' + which_po + '&init=true&label=' + label + '&isOutline=' + isOutline,
            400,
            420);
        else
            openDialog(
                'pattern_outline_editor.jspx?which=' + which_po + '&init=true&label=' + label + '&isOutline=' + isOutline,
                400,
                280);
    }
}
//pwg - 20/1/6
function editPO(which_po, label, isOutline, isPoint) 
{
	if (which_po == "MAP_BACKGROUND")
	{
		openDialog('pattern_outline_editor.jspx?which=' + which_po + '&init=true', 400, 420);
	} 
	else if (which_po.indexOf("FEATURE") != - 1 || which_po == "ONE_SYMBOL") 
	{
		openDialog('pattern_outline_editor.jspx?which=' + which_po + '&init=true&label=' + label + '&isOutline=' + isOutline + '&isPoint=' + isPoint, 400, 500);
	}
}

function showFontChooser(which_po) {
    if (which_po.indexOf("FEATURE_LABEL") != - 1) {
        openDialog('font_editor.jspx?init=&which=' + which_po, 400, 300);
    } else if (which_po == "MAP_LABEL") {
        openDialog('font_editor.jspx?init=&which=MAP_LABEL', 400, 260);   //400,400);
    }
}

function changeCoord(coordsys) {
    openDialog('coordsys_editor.jspx?which=' + coordsys + '&init=' + coordsys, 410, 160);
}

function showImportLayer(parent_composite) {
 openDialog('import_layer.jspx?init=true&parent_composite=' + parent_composite, 620, 485, 'scroll'); //340
}

function showImportDataset() {
    //alert("showImportDataset("+fromImportLayer+")");
    //note: update also importDataset.js uploadFile()
    //      openDialog('ImportDatasetWizard.jsp?init=true',600,340);
    //openDialog('import_dataset.jspx?init=import', 780, 580, 'scroll');
   openDialog('import_dataset_wizard.jspx?init=import', 780, 580,'scroll');
}

function updateDataset() {
    openDialog('import_dataset_wizard.jspx?init=update', 780, 580, 'scroll');
}

function showImportDatasetFrImportLayer() {
    if (dialogWin.win != null)
        dialogWin.win.close();

    dialogWin = new Object();
    document.view.submit();
    //      openDialog('ImportDatasetWizard.jsp?fromImportLayer=true',600,340);
    openDialog('import_dataset_wizard.jspx?fromImportLayer=true&init=import', 780, 580, 'scroll');
}

function openLegendEditorFromImportDataset() {
    if (dialogWin.win != null)
        dialogWin.win.close();

    dialogWin = new Object();
    document.view.submit();
    openLegendEditor(0);
}

function selectTheme(theme, feat_idx, isOutline) {
    if (theme == 5) {        //5 hard-coded; index of "One Color"
        editPO('ONE_SYMBOL', 'Common Color', isOutline);
    } else if (theme > 0 && theme < 5) { //map colors theme
        var objname = "document.forms[0].ThemeLegend";
        var obj = eval(objname);
        change('SET_COLOR_THEME', feat_idx+','+obj.value);

    } else if(theme == 6){//fade
        var objname = "document.forms[0].ThemeLegend";
        var obj = eval(objname);
        var theme = obj.value;
        var first, last;

        var objname = "document.forms[0].last";
        var obj = eval(objname);

        objname = "document.forms[0].size";
        obj = eval(objname);
        var size = obj.value;

        objname = "document.forms[0].start";
        obj = eval(objname);
        var start_idx = obj.value;

        var last_idx = size;
        obj = eval("document.forms[0].img1");
        if(obj!=null)
            first = obj.value;

        obj = eval("document.forms[0].outline1");
        if(obj!=null)
            first_outline = obj.value;

        obj = eval("document.forms[0].img"+last_idx);
        if(obj!=null)
            last = obj.value;

        obj = eval("document.forms[0].outline"+last_idx);
        if(obj!=null)
            last_outline = obj.value;

        theme = 10;//FADE

        change('SET_COLOR_THEME', feat_idx+','+
            theme+","+
            first+","+last+","+
            first_outline+","+last_outline);

    } else if(theme == 7){//reverse color
    }
}

//pwg - following code shoule be removed and modal.js included instead
var Nav4 = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) == 4))
var dialogWin = new Object()

// Generate a modal dialog.
function openDialog(url, width, height, scrolls) {
    delay();
    if (! dialogWin.win || (dialogWin.win && dialogWin.win.closed)) {
        dialogWin.url = url
        dialogWin.width = width
        dialogWin.height = height
        // Keep name unique so Navigator doesn't overwrite an existing dialog.
        dialogWin.name = ( new Date()).getSeconds().toString()

        // Assemble window attributes and try to center the dialog.
        if (Nav4) {
            dialogWin.left = window.screenX + ((window.outerWidth - dialogWin.width) / 2)
            dialogWin.top = window.screenY + ((window.outerHeight - dialogWin.height) / 2)
            var attr = "screenX=" + dialogWin.left + ",screenY=" + dialogWin.top + ",resizable=no,scrollbar=yes,width="
                    + dialogWin.width + ",height=" + dialogWin.height
        } else {
            dialogWin.left = (screen.width - dialogWin.width) / 2
            dialogWin.top = (screen.height - dialogWin.height) / 2
            var attr = "left=" + dialogWin.left + ",top=" + dialogWin.top + ",width=" + dialogWin.width
                    + ",height=" + dialogWin.height;

            if (scrolls != null)
                attr += ",scrollbars,resizable=yes"
            else
                attr += "resizable=no"
        }

        //     if(attrs!=null)
        //dialogWin.win=window.open(dialogWin.url,dialogWin.name,attrs)
        // ',resizable,scrollbars'
        //else
        dialogWin.win = window.open(dialogWin.url, dialogWin.name, attr)
    }

    dialogWin.win.focus()
}

// Event handler to inhibit Navigator form element and IE link activity when dialog window is active.
function deadend() {
    if (dialogWin.win && ! dialogWin.win.closed) {
        dialogWin.win.focus()
        return false
    }
}

// Since links in IE4 cannot be disabled, preserve IE link onclick event handlers while they're "disabled."
// Restore when re-enabling the main window.
var IELinkClicks

// Disable form elements and links for IE.
function disableForms() {
    IELinkClicks = new Array()

    for (var h = 0; h < frames.length; h++) {
        for (var i = 0; i < frames[h].document.forms.length; i++) {
            for (var j = 0; j < frames[h].document.forms[i].elements.length; j++) {
                frames[h].document.forms[i].elements[j].disabled = true
            }
        }

        IELinkClicks[h] = new Array()

        for (i = 0; i < frames[h].document.links.length; i++) {
            IELinkClicks[h][i] = frames[h].document.links[i].onclick
            frames[h].document.links[i].onclick = deadend
        }
    }
}

// Restore IE form elements and links to normal behavior.
function enableForms() {
    for (var h = 0; h < frames.length; h++) {
        for (var i = 0; i < frames[h].document.forms.length; i++) {
            for (var j = 0; j < frames[h].document.forms[i].elements.length; j++) {
                frames[h].document.forms[i].elements[j].disabled = false
            }
        }

        for (i = 0; i < frames[h].document.links.length; i++) {
            frames[h].document.links[i].onclick = IELinkClicks[h][i]
        }
    }
}

// Grab all Navigator events that might get through to form elements while dialog is open. For IE, disable form elements.
function blockEvents() {
    if (Nav4) {
        window.captureEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS)
        window.onclick = deadend
    } else {
        disableForms()
    }

    window.onfocus = checkModal
}

// As dialog closes, restore the main window's original event mechanisms.
function unblockEvents() {
    if (Nav4) {
        window.releaseEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS)
        window.onclick = null
        window.onfocus = null
    } else {
        enableForms()
    }
}

function checkModal() {
    setTimeout("finishChecking()", 50)
    return true
}

function finishChecking() {
    if (dialogWin.win && ! dialogWin.win.closed) {
        dialogWin.win.focus()
    }
}

//pwg - following context menu code should be removed and contextmenu.js included instead!!
var contextMenus = new Array(); // Used to track all menus.
var contextMenuParameters = new Array();

function _contextNewMenu(menu_name) {
    var index = contextMenus.length;
    contextMenus[index] = menu_name;
    contextMenuParameters[index] = - 1;
    return false;
}

function _contextFindMenuIndex(menu_name) {
    for (i = 0; i < this.contextMenus.length; i++) {
        if (contextMenus[i] == menu_name) {
            return (i);
        }
    }

    return (- 1);
}

function _contextShowMenu(menu_name) {
    _contextHideAllMenus();
    var index = _contextFindMenuIndex(menu_name); //see if menu exists first

    if (index == - 1) {
        _contextNewMenu(menu_name);               //create it if it doesn't exist
    }

    var ie5menu = null;
    //ie5menu = document.all[menu_name];
    ie5menu =  document.getElementById(menu_name);

    var rightedge = document.body.clientWidth - event.clientX;
    var bottomedge = document.body.clientHeight - event.clientY;

    if (rightedge < ie5menu.offsetWidth)
        ie5menu.style.left = document.body.scrollLeft + event.clientX - ie5menu.offsetWidth;
    else
        ie5menu.style.left = document.body.scrollLeft + event.clientX;

    if (bottomedge < ie5menu.offsetHeight)
        ie5menu.style.top = document.body.scrollTop + event.clientY - ie5menu.offsetHeight;
    else
        ie5menu.style.top = document.body.scrollTop + event.clientY;

    ie5menu.style.visibility = "visible";
    //ie5menu.style.display = "block";

    event.cancelBubble = true; //trap the event
    event.returnValue = false;
    return false;
}

function _contextTrapContext() {
    _contextHideAllMenus();

    event.cancelBubble = true; //trap the event
    event.returnValue = false;
    return false;
}

function _contextHideMenu(menu_name) {
    var ie5menu = document.all[menu_name];
    ie5menu.style.visibility = "hidden";
    return false;
}

function _contextHideAllMenus() {
    for (i = 0; i < this.contextMenus.length; i++) {
        //alert("context hide : " + i +  " , " + contextMenus[i]);
        _contextHideMenu(contextMenus[i]);
    }

    return false;
}

function _contextHighlight() {
    if (event.srcElement.className == "menuitems") {
        event.srcElement.style.backgroundColor = "skyblue";
    }
}

function _contextLowlight() {
    if (event.srcElement.className == "menuitems") {
        event.srcElement.style.backgroundColor = "";
    }
}

function context(menu_name, parameter) {
    _contextShowMenu(menu_name);
    var index = _contextFindMenuIndex(menu_name);
    contextMenuParameters[index] = parameter;
    var p1 = contextMenuParameters[index];
    //alert ("set parameter (" + menu_name + "," + index + ") = " + parameter + "," + p1);
    return false;
}

function _contextGetParameter(menu_name) {
    var index = _contextFindMenuIndex(menu_name);
    var parameter = contextMenuParameters[index];
    //alert ("get parameter (" + menu_name + "," + index + ") = " + parameter);
    return parameter;
}

function clickLayer(idx) {
    //obj.onmousedown=catchClick;
    var args = click.arguments;

    for (i = 0; i < (args.length); i++) {
        //alert(i+"th arg:"+args[i]);
        if (args[i] != null)
            idx = args[i];
    }

    if (is.ns) {
        window.captureEvents(Event.MOUSEDOWN);
        window.onmousedown = click;
    }

    if (is.ns) {
        //    alert("ns btn:"+event.button);
        if (event.which >= 2) {
            alert("right-click disabled! :)" + idx);
            context('layerlistcontext', idx);
            return false;
        }
    } else {
        if (event.button > 1) {
            alert("right-click disabled! :)");
        }
    }
}

function openTools() {
    openDialog("Tools.html");
}

function over_effect(e, state) {
    if (document.all)
        source4 = event.srcElement

    else if (document.getElementById)
        source4 = e.target

    if (source4.className == "menulines")
        source4.style.borderStyle = state
    else {
        while (source4.tagName != "TABLE") {
            source4 = document.getElementById ? source4.parentNode : source4.parentElement

            if (source4.className == "menulines")
                source4.style.borderStyle = state
        }
    }
}

function delLegend() {
    ans = confirm("Are you sure to delete the legend definition?");

    if (ans == true)
        change('DELETE_LEGEND', document.view.DEFINITION.selectedIndex)
}

function openRegister() {
    //alert();
    if (dialogWin.win != null)
        dialogWin.win.close();

    dialogWin = new Object();
    //document.view.submit();
     openRegPage();
}

function openRegPage() {
    openDialog('register.jspx', 500, 290);
}

function openChartEditor(idx) {
    // openDialog('ChartEditor.jsp?init=true&idx='+idx,580,560,'scroll');
    openDialog('chart_definition_editor.jspx?init=true&idx=' + idx, 600, 475, 'scroll');
}

function editChartEditor() {
    idx = document.view.CHART_DEFINITION.selectedIndex;
    // openDialog('ChartEditor.jsp?init=true&idx='+idx,580,560,'scroll');
    openDialog('chart_definition_editor.jspx?init=true&idx=' + idx, 600, 475, 'scroll');
}

function sureDelLegend(idx) {
    sureDel = confirm("Are you sure to delete this legend definition?");

    if (sureDel == true) {
        change('DELETE_LEGEND', idx);
    }
}

function sureDelAllLegend() {
    sureDel = confirm("Are you sure to delete all legend definitions?");

    if (sureDel == true) {
        change('DELETE_ALL_LEGEND', '-1');
    }
}

function sureDelPrec(idx) {
    sureDel = confirm("Are you sure to delete this definition?");

    if (sureDel == true) {
        change('DELETE_PRECISION', idx);
    }
}

function sureDelUnit(idx) {
    sureDel = confirm("Are you sure to delete this unit definition?");

    if (sureDel == true) {
        change('DELETE_UNIT', idx);
    }
}

function sureDelChart(idx) {
    sureDel = confirm("Are you sure to delete this chart definition?");

    if (sureDel == true) {
        change('DELETE_CHART', idx);
    }
}

function openAddKeyedExtentEditor(cnt) {
    icnt = 110;
    initheight = 400;

    if (cnt > 3)
        initheight += ((cnt - 3) * icnt);

    openDialog('keyed_extent_entry_editor.jspx?init=true', 700, initheight, 'scroll');
}

function openEditKeyedExtentEditor(idx, cnt) {
    icnt = 16;
    initheight = 400;

    if (cnt > 3)
        initheight += ((cnt - 3) * icnt);

    openDialog('keyed_extent_entry_editor.jspx?init=true&idx=' + idx, 600, initheight, 'scroll');
}

function openPrecUnit() {
   openDialog('set_precision_and_unit.jspx?init=true', 300, 150);
}

function editCurrentColor() {
    openDialog('legend_editor.jspx?init=true&contextColor=true', 585, 560);
}

//function openRasterLegend() {
//  openDialog('legend_editor.jspx?init=true&raster=a',585,560);
//}
function showLegendEditorFrImportDataset() {
    if (dialogWin.win != null)
        dialogWin.win.close();

    dialogWin = new Object();
    document.view.submit();
    //      openDialog('import_dataset_wizard.jspx?fromImportLayer=true',600,340);
    openDialog('legend_editor.jspx?init=true&idx=0', 585, 560, 'scroll');
}

function delKeyedExtentEntry(idx) {
    sureDel = confirm("Are you sure to delete this key?");

    if (sureDel == true) {
        change('DELETE_KEYED_EXTENT_ENTRY', idx);
    }
}

function delKeyedExtentEntries() {
    sureDel = confirm("Are you sure to delete these keys?");

    if (sureDel == true) {
        idxes = "";
        var ml = document.view;
        var len = ml.elements.length;

        for (var i = 0; i < len; i++) {
            var e = ml.elements[i];

            if (e.name == "e" && e.checked)
                idxes += e.value + ",";
        }

        idxes = idxes.substring(0, idxes.length - 1);
        //sort from highest to lowest
        inputNumbers = idxes.split(",");

        for (var i = 0; i < inputNumbers.length; i++) {
            inputNumbers[i] = parseInt(inputNumbers[i], 10);
        }

        inputNumbers = bubbleSort(inputNumbers, 0, inputNumbers.length - 1);

        change('DELETE_KEYED_EXTENT_ENTRIES', inputNumbers);
    }
}

function bubbleSort(inputArray, start, rest) {
    for (var i = rest - 1; i >= start; i--) {
        for (var j = start; j <= i; j++) {
            if (inputArray[j + 1] > inputArray[j]) {
                var tempValue = inputArray[j];
                inputArray[j] = inputArray[j + 1];
                inputArray[j + 1] = tempValue;
            }
        }
    }

    return inputArray;
}

function editPrec(idx) {
   // openDialog('precision_definition_edit.jspx', 300, 150);

openDialog('precision_definition_edit.jspx?init=true&idx=' + idx, 300, 150);

}

function addPrec() {
    //openDialog('precision_definition_new.jspx', 300, 150);
    openDialog('precision_definition_new.jspx?init=true&idx=-1', 300, 150);

}

function editUnit(idx) {
    //openDialog('unit_definition_edit.jspx', 300, 150);
    openDialog('unit_definition_edit.jspx?init=true&idx=' + idx, 300, 150);
}


function addUnit() {
    //openDialog('unit_definition_new.jspx', 300, 150);
    openDialog('unit_definition_new.jspx?init=true&idx=-1', 300, 150);
}

function changeChartDef() {
    str = document.view.CHART_DEFINITION.options[document.view.CHART_DEFINITION.selectedIndex].value;
				//document.view.CHART_DEFINITION.selectedIndex;// + ","
              //+ document.view.CHART_DEFINITION.options[document.view.CHART_DEFINITION.selectedIndex].value;
    change('LOAD_CHART', str);
}

function filter() {
    openDialog('FilterDefinitions.jsp?init=true', 600, 500, 'scroll');
}

function addNote() {
    openDialog('edit_note_definition.jspx?init=true&idx=-1', 500, 345,'scroll');
}

function addSource() {
    openDialog('edit_source_definition.jspx?init=true&idx=-1', 585, 490,'scroll');
}

function addSourceDB() {
    openDialog('EditSourceDefinitionDB.jsp?init=true&idx=-1', 630, 435, 'scroll');
}

function editSourceDB(idx) {
    openDialog('EditSourceDefinitionDB.jsp?init=true&idx=' + idx, 630, 435, 'scroll');
}

function addLink() {
    openDialog('EditLinkDefinition.jsp?init=true&idx=-1', 500, 145);
}

function editNote(idx) {
    openDialog('edit_note_definition.jspx?init=true&idx=' + idx, 500, 345,'scroll');
}

function editSource(idx) {
    openDialog('edit_source_definition.jspx?init=true&idx=' + idx, 585, 490,'scroll');
}

function editLink(idx) {
    openDialog('EditLinkDefinition.jsp?init=true&idx=' + idx, 500, 145);
}

function sureDelLink(idx) {
    sureDel = confirm("Are you sure to delete this link definition?");

    if (sureDel == true) {
        change('DELETE_LINK', idx);
    }
}

function sureDelNote(idx) {
    sureDel = confirm("Are you sure to delete this note definition?");

    if (sureDel == true) {
        change('DELETE_NOTE', idx);
    }
}

function sureDelSource(idx) {
    sureDel = confirm("Are you sure to delete this source definition?");

    if (sureDel == true) {
        change('DELETE_SOURCE', idx);
    }
}

function openEditorInMain() {
    if (dialogWin.win != null)
        dialogWin.win.close();

    dialogWin = new Object();
    document.view.submit();
}

function newProject() {
    openDialog('project_new.jspx', 300, 150);
}

function renameProject() {
    openDialog('project_rename.jspx', 300, 150);
}

function deleteProject() {
    idxes = "";
    var ml = document.view;
    var len = ml.elements.length;

    for (var i = 0; i < len; i++) {
        var e = ml.elements[i];

        if (e.name == "project" && e.checked)
            idxes += e.value + ",";
    }
    if(idxes != "") {
        idxes = idxes.substring(0, idxes.length - 1);
        //sort from highest to lowest
        inputNumbers = idxes.split(",");

        for (var i = 0; i < inputNumbers.length; i++) {
            inputNumbers[i] = parseInt(inputNumbers[i], 10);
        }
        sureDel = confirm("Are you sure to delete these projects?");

        if (sureDel == true) {
            inputNumbers = bubbleSort(inputNumbers, 0, inputNumbers.length - 1);

            change('DELETE_PROJECT', inputNumbers);
        }
    } else {
        sureDel = confirm("Are you sure to delete the current project?");

        if (sureDel == true) {
            change("DELETE_PROJECT", "");
        }
    }
}

function copyLayer() {
    change('COPY_LAYER', "");
}

function renameLayer(layer_name) {
    openDialog('layer_rename.jspx?layer_name=' + layer_name, 300, 150);
}

function renameGroupLayer(layer_name) {
    openDialog('group_layer_rename.jspx?layer_name=' + layer_name, 300, 150);
}

function renameThisLayer(composite) {
    change('RENAME_LAYER', composite + "," + document.view.layer_name.value);
}

function ToggleAll(e) {
    if (e.checked)
        CheckAll();
    else
        ClearAll();
}

function Check(e) {
    e.checked = true;
}

function Clear(e) {
    e.checked = false;
}

function CheckAll() {
    var ml = document.view;
    var len = ml.elements.length;

    for (var i = 0; i < len; i++) {
        var e = ml.elements[i];

        if (e.name == "e")
            Check(e);
    }
}

function ClearAll() {
    var ml = document.view;
    var len = ml.elements.length;

    for (var i = 0; i < len; i++) {
        var e = ml.elements[i];

        if (e.name == "e")
            Clear(e);
    }
}

function Toggle(e) {
    document.view.toggle.checked = false;
}

function openLegendBuilder() {
    openLegendBuilderWindow();

    if (dialogWin.win != null)
        dialogWin.win.close();

    dialogWin = new Object();
    document.view.submit();
    openLegendBuilderWindow();
}

function openLegendBuilderWindow() {
    openDialog('LegendBuilder.jsp?init=true', 860, 430); //attrs);
}

function openAddNewUser() {
    //    alert("Add User Function is under construction!");
    openDialog('AddNewUser.jsp', 500, 290);
}

function deleteUser(id) {
    id = document.view.userlist.options[document.view.userlist.selectedIndex].value
    del = confirm("Would you like to delete user '" + id + "'?");

    if (del == true) {
        alert("Delete User Function is under construction!");
    }
}

function renameDataset(idx) {
    openDialog('dataset_rename.jspx?idx=' + idx, 300, 150);
}

function removeExtent(path) {
    change("REMOVE_EXTENT", path);
}

function importProject() {
    // alert("under construction!");
    openDialog('import_projects.jspx?init=true', 620, 400, 'scroll'); //340
}

function exportProject() {
    // alert("under construction!");
    openDialog('export_projects.jspx?init=true', 620, 400, 'scroll'); //340
}

function publishProject() {
    /*  doIt=confirm("This would update the FFC online. Are you sure to publish the projects?");
      if(doIt == true) {
         openDialog('Publish.jsp',10,10);
      }*/
    //alert("Under Construction!");
    change("PublishProject");
}

function reorderDatasets(size) {
    cnt = 16;
    initsize = 155;

    if (cnt > 3)
        initsize += ((cnt - 3) * 15.5);

    openDialog('reorder_datasets.jspx', 420, initsize, 'scroll');
}

function addGroupLayer(parent_layer) {
    //alert("Under Construction!\nAdd Group Layer "+parentLayer);
    openDialog('group_layer_new.jspx?parent_layer=' + parent_layer, 300, 150);
}

function openCSVDownloadDialog() {
    openDialog(proj+'/servlet/CSVDownloadServlet', 1, 1);
}

function delExtentEntries(extent_idx) {
    sureDel = confirm("Are you sure to delete these entries?");

    if (sureDel == true) {
        idxes = "";
        var ml = document.view;
        var len = ml.elements.length;

        //alert("view elements:"+len);
        for (var i = 0; i < len; i++) {
            var e = ml.elements[i];

            if (e.name == "entry" && e.checked)
                idxes += e.value + ",";
        }

        idxes = idxes.substring(0, idxes.length - 1);
        //alert("idxes = "+idxes);
        //sort from highest to lowest
        inputNumbers = idxes.split(",");

        for (var i = 0; i < inputNumbers.length; i++) {
            inputNumbers[i] = parseInt(inputNumbers[i], 10);
        //alert(inputNumbers[i]);
        }

        inputNumbers = bubbleSort(inputNumbers, 0, inputNumbers.length - 1);

        change('DELETE_EXTENT_ENTRY', (extent_idx - 1) + "," + inputNumbers);
    }
}

function addExtentEntry(extent_idx) {
    var str = prompt("Please enter the name of entry you want to add", "");

    if (str != null && str.length == 0) {
        alert("Please enter a name!");
        return false;
    } else if(str != null){
        change('ADD_EXTENT_ENTRY', (extent_idx - 1) + "," + str);
    }
}
function delay() {
   setTimeout("nothing();", 100);
}
function nothing() {
   var i=0;
   i++;
}
function enterMatrixCell(obj, next_row_id) {
  if(event.keyCode==13) {
    current_obj_name = obj.name;
    column_id = current_obj_name.substring(current_obj_name.indexOf('_')+1);
    next_row = "c"+next_row_id + "_" + column_id;
    next_obj = eval('document.view.'+next_row);
    if(next_obj !=null) {
       next_obj.focus();
       next_obj.select();
  }
}
}
function moveLayer(layer_name) {
    openDialog('MoveLayer.jspx?NO_XSL=true&layer_name=' + layer_name, 450, 350,'scroll');
}
function exportToTable() {
    openDialog(proj+'/servlet/ExportMatrixServlet', 1, 1);
}
function imageSwap(img_name, img1, img2) {
//alert("imageSwap("+img_name+", "+img1+", "+img2+")");
  var objname = "document.forms[0]."+img_name;
  obj = eval(objname);
  if(obj != null) {
     obj.src = "Images/V_icons/"+img2;
  } else
     alert("Unable to find "+objname);
}

function restore(){
  var i, x, a = document.MM_sr;
		for(i=0; a && i < a.length && (x=a[i]) && x.oSrc; i++)
		   x.src = x.oSrc;
}
function swap(){
   var i, j=0, x, a=swap.arguments;
			document.MM_sr = new Array;
			for(i=0;i<(a.length-2);i+=3)
			   if ((x=find(a[i]))!=null){
						   document.MM_sr[j++]=x;
									tmp = x.src.substring(0, x.src.lastIndexOf("/")+1);
									//alert(tmp);
									if(!x.oSrc)
									   x.oSrc=x.src;
									x.src = tmp + a[i+2];
									//alert(a[i+2]);
						}
}
function find(n, d){
var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=find(n,d.layers[i].document); return x;
}

function openLayerBrowser(cnt) {
    initsize = 110;

    if (cnt > 3)
        initsize += ((cnt - 3) * 13.5);

    openDialog('layer_browser.jspx', 420, initsize);
}

function openLogIn() {
    openDialog('login.jspx?init=true', 320, 170);
}



function modifiedCell() {
    modified = true;
}

function submitSimplePopup(what) {
    value = document.forms['form'].field1.value;
    hidden_value = document.forms['form'].hidden_value.value;

    if (value == '') {
        alert("Please do not leave the field blank!");
    } else {
        if(hidden_value!="NULL" && hidden_value!='')
           opener.change(what, hidden_value+","+value);
        else
            opener.change(what, value);
       self.close();
    }
}

function editLayerPopUp(idx) {
    if(idx.indexOf("2") == 0) {//layer
    //  javascript:windowHandle=window.open('build_map_editor_panel.jspx?what=SET_EDITOR_MAP_PANEL&setting=' + idx,'newwin','height=470,width=538,resizable=yes,scrollbars=yes');windowHandle.focus();_contextHideAllMenus();
      _contextHideAllMenus();
      openDialog('build_map_editor_panel.jspx?what=SET_EDITOR_MAP_PANEL&setting=' + idx, 545, 455,'scrolls');
    } else if(idx.indexOf("3") == 0) {// dataset
    //javascript:windowHandle=window.open('build_map_editor_panel.jspx?what=SET_EDITOR_MAP_PANEL&setting=' + idx,'newwin','height=510,width=525,resizable=yes,scrollbars=yes');windowHandle.focus();_contextHideAllMenus();
      _contextHideAllMenus();
      openDialog('build_map_editor_panel.jspx?what=SET_EDITOR_MAP_PANEL&setting=' + idx, 538, 485,'scrolls');
    } else {
    //  javascript:windowHandle=window.open('build_map_editor_panel.jspx?what=SET_EDITOR_MAP_PANEL&setting=' + idx,'newwin','height=385,width=517,resizable=yes,scrollbars=yes');windowHandle.focus();_contextHideAllMenus();
      _contextHideAllMenus();
      openDialog('build_map_editor_panel.jspx?what=SET_EDITOR_MAP_PANEL&setting=' + idx, 530, 360,'scrolls');
    }
}

function editRasterPopUp(idx) {
  javascript:windowHandle=window.open('build_map_editor_panel.jspx?what=SET_EDITOR_MAP_PANEL&setting=' + idx,'newwin','height=470,width=538,resizable=yes,scrollbars=yes');windowHandle.focus();_contextHideAllMenus();
}


function hideDatasets() {
    change('LOAD_ATTRIBUTE', -1);
}

function editProject() {
    change('SET_EDITOR_MAP_PANEL', '1');

}

function openExportMap() {
       javascript:windowHandle=window.open('./servlet/MapExportServlet','newwin','height=500,width=600,resizable=yes,scrollbars=yes');windowHandle.focus();
}
