function updateTabs() {
    callToServer(ctxPath + "/updateTabs.html", "tabIFrame");
    document.getElementById("locatorMap").src = ctxPath + '/drawLocatorMap.image?ForceRefresh=' + new Date();
}

function promptQuestion(message)
{
    return prompt(message, "");
}

function searchMapState() {
    callToServer(ctxPath + "/searchMapState.html", "mapStateTabIFrame");
}


//DIDB40337 log the user action
function loadUserMapState(id) {
      logUserAction("Public Module", "Load User Map State");

    if (id != null)
    {
        callToServer(ctxPath + "/loadUserMapState.html?Id=" + id, "mapStateLoadIFrame");
    }
}

//DIDB40337 log the user action
function loadMapState(id) {
      logUserAction("Public Module", "Load Map State");
      
    if (id != null)
    {

        callToServer(ctxPath + "/loadMapState.html?Id=" + id, "mapStateLoadIFrame");
    }
}

//DIDB40337 log the user action
function saveMapState(name) {
      logUserAction("Public Module", "Save Map State");

    if (name != null && name.length>0)
    {
        callToServer(ctxPath + "/saveUserMapState.html?name=" + name, "mapStateTabIFrame");
    }
}


//DIDB40337 log the user action
//DIDB41410 Prompt the user to confirm a delete before deleting a map state.
function deleteMapState(mapStateId) {     
	
	var msg;
	if(langId == 3){
      		msg="\u00bfEst\u00e1 seguro que quiere borrar este mapa r\u00e1pido?";
      	}
      	else if(langId == 2){
      		msg="Etes vous certain de vouloir supprimer cette carte rapide";
      	}
      	else{//English - default
      		msg="Are you sure you want to delete this quick map?";
      	}    
	
	if(confirm(msg)){
		logUserAction("Public Module", "Delete Map State");
   		callToServer(ctxPath + "/deleteUserMapState.html?Id=" + mapStateId, "mapStateTabIFrame");
	}

}

//DIDB40337 log the user action
function renameMapState(mapStateId, newName) {
      logUserAction("Public Module", "Rename Map State");

    if (newName != null && newName.length>0)
    {
        callToServer(ctxPath + "/updateMapState.html?Id=" + mapStateId + "&name=" + newName, "mapStateTabIFrame");
    }
}

function keyPopUp() {
    openWindow(ctxPath + '/keyPopUp.jsp', 'keyPopUp', 'width=250,height=250');
}

function checkImage(img) {
    w = img.width;
    h = img.height;

    if ((w < 2) || (h < 2)) {
        img.src = ctxPath + '/images/vessel-not-found.jpg';
    } else if ((img.complete != null) && (!img.complete)) {
        img.src = ctxPath + '/images/vessel-not-found.jpg';
    } else {
           // good
    }
}

function updateToolbar() {
    callToServer(ctxPath + "/updateToolbar.html", "toolbarIFrame");
}

function updateDisplayPanel() {
    callToServer(ctxPath + "/updateDisplayPanel.html", "displayPanelIFrame");
}

function onSelectionLayerChange(radio) {
    if(radio) {
        setSelectionLayer(radio.value);
    }
}

function setSelectionLayer(sl) {
    callToServer(ctxPath + "/setSelectionLayer.html?selectionLayer=" + sl, "displayPanelIFrame");
    changeAGTab(sl);
    m.selectionLayer=sl;    
}

function getSelectValues(element) {
    var str = "";
    for (var j = 0; j < element.length ; j++) {
        if (element.options[j].selected == true) {
            if(element.options[j].value != '') {
                if(str.length > 0) {
                    str += ",";
                }
                str += element.options[j].value;
            }
        }
    }
    return str;
}

//DIDB40337 log the user action
function clearAction() {
    logUserAction("Public Module", "Clear Selection");
    m.clearSelection();
}

//DIDB40337 log the user action
function centerSelectionAction() {
      logUserAction("Public Module", "Center On Selection");
    m.centerOnSelection();
}
//DIDB40337 log the user action
function centerOnPoint(x,y,srs,scale) {
      logUserAction("Public Module", "Center On Point");
    m.centerOnPoint(x,y,srs,scale);
}

//DIDB40337 log the user action
//DIDB44057 Print output doesn't have markers
function printAction() {
    logUserAction("Public Module", "Print");
    callToServer(ctxPath + "/exportAllAttributesAndImage.html?displayRedline=" + m.displayRedline, "mapIFrame");
}

//DIDB40337 log the user action
function overviewAction() {
      logUserAction("Public Module", "Overview");
    
    m.overview();
}


//DIDB40337 log the user action
function panAction() {
      logUserAction("Public Module", "Pan"); 
      
    callToServer(ctxPath + "/setMapTool.html?mapTool=2", "toolbarIFrame");
    
      
}

//DIDB40337 log the user action
function previousCoverAction() {
      logUserAction("Public Module", "Previous Cover");

    callToServer(ctxPath + "/previousCover.html", "toolbarIFrame");
}


//DIDB40337 log the user action
function pointSelectAction() {
      logUserAction("Public Module", "Point Select"); 

    callToServer(ctxPath + "/setMapTool.html?mapTool=3", "toolbarIFrame");
   
    
}

//DIDB40337 log the user action
function rangeSelectAction() {
      logUserAction("Public Module", "Range Select"); 

    callToServer(ctxPath + "/setMapTool.html?mapTool=4", "toolbarIFrame");
}


//DIDB33771 set buffer selection tool
function bufferSelectAction() {
      logUserAction("Public Module", "Buffer Select"); 

    callToServer(ctxPath + "/setMapTool.html?mapTool=10", "toolbarIFrame");
}

//DIDB40087 added this method
//This method sets the distance measurement tool as the current map tool
//DIDB40337 log the user action
function distanceMeasurementAction() {
      logUserAction("Public Module", "Distance Measurement"); 
    callToServer(ctxPath + "/setMapTool.html?mapTool=7", "toolbarIFrame");
}

//DIDB40088 added this method
//This method sets the area measurement tool as the current map tool
//DIDB40337 log the user action
function areaMeasurementAction() {
      logUserAction("Public Module", "Area Measurement"); 
    callToServer(ctxPath + "/setMapTool.html?mapTool=8", "toolbarIFrame");
}

//DIDB40144 added this method
//This method clear the area and distance measurement
//DIDB40337 log the user action
function clearMeasurementAction() {
      logUserAction("Public Module", "Clear Measurement"); 
    m.clearMeasurement();
    callToServer(ctxPath + "/clearMeasurement.html", "toolbarIFrame");    
}

//DIDB40644 added this method
//This methods set the redline tool as the current map tool
function editRedLineAction() {
       m.showRedline();
    callToServer(ctxPath + "/setMapTool.html?mapTool=9", "toolbarIFrame");
}


//DIDB40644 added this method
//This methods show/hide the redline image
function hideRedLineAction() {
    m.hideRedline();
    if (m.mapTool == m.redline) {
          callToServer(ctxPath + "/setMapTool.html?mapTool=1", "toolbarIFrame");
          var editRedLineToolBarImage = document.getElementById('editRedLineToolBarImage');
          toolBarImageOff(editRedLineToolBarImage);
    }
    
}

//DIDB46227 added this method
//This method brings another user's redlines to the map and hide the current user's redlines
function shareRedLineAction() {
	m.showRedline();
	var msg;
	if(langId == 3){
   		msg="Entre por favor el nombre del usuario, del usuario cuyos marcadores ser\u00e1n exhibidos.";
   	}
   	else if(langId == 2){
   		msg="Veuillez entrer le nom d\u2019utilisateur de l'utilisateur dont les marqueurs seront montr\u00e9s.";
   	}
   	else{//English - default
   		msg="Please input the username of the user whose markers will be displayed.";
   	}   
	var username = promptQuestion(msg);
	if (username != null && username.length>0)
    	callToServer(ctxPath + "/importOtherUserRedline.html?userName=" + username, "toolbarIFrame");	
}


//DIDB40337 log the user action
function redrawAction() {
      logUserAction("Public Module", "Redraw"); 
    m.redraw();
}

//DIDB40337 log the user action
function panDirection(d) {
      logUserAction("Public Module", "Pan Direction"); 
    callToServer(ctxPath + "/panDirection.html?direction=" + d, "mapIFrame");
}



function returnToHomeAction() {
    window.location=ctxPath + '/returnToHome.html';
}

function setMapSize(w, h) {
    m.setSize(w,h);
    
    var mapWin = document.getElementById("mapWindow");
    if(mapWin) {

    } else {

    } 
}

function panelViewAction() {
    callToServer(ctxPath + '/togglePanelView.html', 'toolbarIFrame');
}

//DIDB40087 added the distance measurement tool and removed the unuseful resetMapToolImages 
//DIDB40088 added the area measurement tool. Add a null pointer check. 
//DIDB40644 added the redline tool. 
//DIDB33771 added the buffer selection tool
function resetMapToolImages() {
    toolBarImageOff(document.getElementById('zoomToolBarImage'));        
    toolBarImageOff(document.getElementById('panToolBarImage'));        
    toolBarImageOff(document.getElementById('rangeSelectToolBarImage'));
    toolBarImageOff(document.getElementById('pointSelectToolBarImage'));  
    toolBarImageOff(document.getElementById('bufferSelectToolBarImage'));  
    toolBarImageOff(document.getElementById('editRedLineToolBarImage'));    
    if (document.getElementById('distanceMeasurementToolBarImage')!=null)
          toolBarImageOff(document.getElementById('distanceMeasurementToolBarImage'));  
    if (document.getElementById('areaMeasurementToolBarImage')!=null)
          toolBarImageOff(document.getElementById('areaMeasurementToolBarImage'));                              
}

function mapSizeAction() {
    callToServer(ctxPath + '/toggleMapSize.html', 'toolbarIFrame');
}

function portStatusAction() {
    var mf = document.mapForm;
    var bfs = '';
    var boundForPortIds = mf.boundForPortIds;
    if(boundForPortIds.length) {
        for(var i = 0; i < boundForPortIds.length; i++) {
            if(bfs.length > 0) {
                bfs += ',';
            }
            bfs = boundForPortIds[i].value;
        }
    } else {
        bfs = boundForPortIds.value;    
    }
    
    var cas = '';
    var currentlyAtPortIds = mf.currentlyAtPortIds;
    if(currentlyAtPortIds.length) {
        for(var i = 0; i < currentlyAtPortIds.length; i++) {
            if(cas.length > 0) {
                cas += ',';
            }
            cas = currentlyAtPortIds[i].value;
        }
    } else {
        cas = currentlyAtPortIds.value;    
    }    
    
    callToServer(ctxPath + '/portStatus.html?boundForPortIds=' + bfs + '&currentlyAtPortIds=' + cas, 'toolbarIFrame');
}

function showVesselsBoundFor(idno) {
    callToServer(ctxPath + '/vesselsBoundFor.html?idno=' + idno, 'mapIFrame');
}

function showVRM(llpno) {
    callToServer(ctxPath + '/showVRM.html?llpno=' + llpno, 'mapIFrame');
}

//DIDB40337 log the user action
function zoomInAction() {
      logUserAction("Public Module", "Zoom In");
    m.zoomIn();
}

//DIDB40337 log the user action
function zoomOutAction() {
      logUserAction("Public Module", "Zoom Out");
    m.zoomOut();
}

//DIDB40337 log the user action
function changeScale() {
      logUserAction("Public Module", "Change Scale");
    var scaleTextBox = document.getElementById("scaleValue");
    if(scaleTextBox) {
        m.changeScale(scaleTextBox.value);
    }
}

//DIDB40337 log the user action
function setZoomFactor(zf) {
      
      logUserAction("Public Module", "Set Zoom Factor"); 
    callToServer(ctxPath + "/setZoomFactor.html?zoomFactor=" + zf, "toolbarIFrame");
}

//DIDB40337 log the user action
function zoomAction() {

      logUserAction("Public Module", "Zoom"); 
    callToServer(ctxPath + "/setMapTool.html?mapTool=1", "toolbarIFrame");
}

function regionAction() {
    callToServer(ctxPath + "/setMapTool.html?mapTool=5", "toolbarIFrame");
}

//DIDB44473 log the user action
function toggleLayerGroup(id) {
	logUserAction("Public Module", "toggleLayerGroup"); 
    callToServer(ctxPath + "/toggleLayerGroup.html?mapLayerGroupId=" + id, "tabIFrame");
}

//DIDB44473 log the user action
function toggleLayerGroupLayer(id) {
	logUserAction("Public Module", "toggleLayer"); 
    callToServer(ctxPath + "/toggleLayerGroupLayer.html?mapLayerGroupLayerId=" + id, "tabIFrame");
}


function setSelectionLayer(layerName) {
    callToServer(ctxPath + "/setSelectionLayer.html?selectionLayer=" + layerName, "tabIFrame");
}

var currentToolBarImgClass='';
function toolBarImageOver(tbi) {
    currentToolBarImgClass=tbi.className;
    tbi.className='toolBarImgOut';
}

//DIDB40644 set the classname for hideRedLineToolBarImage icon. 
//Since we automatically switch the map tool to zoom box when the current 
//map tool is redline and the user clicks the hide button, 
//the icon's classname is set to 'toolBarImagOn' without this fix.  
function toolBarImageOut(tbi) {
      if (tbi.id != 'hideRedLineToolBarImage')
          tbi.className=currentToolBarImgClass; 
    else 
          tbi.className='toolBarImg';
    currentToolBarImgClass=''
}

//DIDB40644 display the redline editting icons on the map 
function toolBarImageOn(tbi) {
    currentToolBarImgClass='toolBarImgIn';
    tbi.className='toolBarImgIn';
    if (tbi.id == 'editRedLineToolBarImage') {
          var redlineToolDiv = document.getElementById('redlineToolDiv');
          if (redlineToolDiv!=null) {
                redlineToolDiv.style.display='';
                redlineToolDiv.style.zindex=100;
                setRedLineTool(6);
          }
    }
}

//DIDB40644 hide the redline editting icons on the map and finish the current redline feature
function toolBarImageOff(tbi) {
    currentToolBarImgClass='toolBarImg';  
    tbi.className='toolBarImg';
    if (tbi.id == 'editRedLineToolBarImage') {        
          var redlineToolDiv = document.getElementById('redlineToolDiv');
          if (redlineToolDiv!=null) {
                redlineToolDiv.style.display='none';
          }      
    }    
}

function openSeaSearcher(url, winTitle) {
    if(window.opener) {
        window.opener.location=url;
        if(window.focus) {
            window.opener.focus();
        }
    } else {
        var screenWidth = parseInt(screen.availWidth);
        var screenHeight = parseInt(screen.availHeight);

        var winParams = "width=" + screenWidth + ",height=" + screenHeight;
            winParams += ",left=0,top=0,toolbar,scrollbars,resizable,status=yes";
    
        openWindow(url, 'seaSearcher', winParams);
    }
}

function openFilterCodes(target) {
    var winParams = "width=300,height=300";
        winParams += ",left=0,top=0,toolbar,scrollbars,resizable,status=yes";

    openWindow(ctxPath + "/filterCodes.html#" + target, 'filterCodes', winParams);
}

function gmlAction() {
    var url = ctxPath+"/map/loadWFSRequestForm.html"
    url += "?minX=" + m.cover.minX;
    url += "&minY=" + m.cover.minY;
    url += "&maxX=" + m.cover.maxX;
    url += "&maxY=" + m.cover.maxY;
    openWindow(url);
}

function toggleClassification(mapLayerId, classificationId)
{
    callToServer(ctxPath + "/toggleClassification.html?mapLayerId=" + mapLayerId + "&classificationId=" + classificationId, "layerTabClassifcationIFrame");
}

//DIDB40337 log the user action
function deletePOI(featureId, layerName) {
    // Confirm that the user really wishes to delete this POI.
    if(window.confirm("Are you sure you want to delete this POI?")) {
        
        logUserAction("Public Module", "Delete POI"); 
        
        // Delete the POI.
        callToServer(ctxPath + "/deletePOI.html?ident=" + featureId + "&LayerName=" + layerName, "poiTabIFrame");

        // Clear the POI Editor tab.
        window.document.getElementById('innerCreateForm').innerHTML = "";
        window.document.getElementById('innerUpdateForm').innerHTML = "";
        window.document.getElementById('poiError').style.display='none';
        window.document.getElementById('createForm').style.display='none';
        window.document.getElementById('updateForm').style.display='none';
        window.document.getElementById('POIEditor').style.display='none';
    }
}

//DIDB40337 log the user action
function editPOI(featureId, layerName) {
    window.document.getElementById('poiError').style.display='none';
    window.document.getElementById('selectPOIPositionPrompt').style.display='none';
    window.document.getElementById('createForm').style.display='none';
    window.document.getElementById('updateForm').style.display='none';
    window.document.getElementById('POIEditor').style.display='none';

      var tabNum = -1;

    for(i = 0; i < myDivs.length; i++) {
             if(myDivs[i].id == 'poiContents' || myDivs[i].id == 'visible' || myDivs[i].id == '') {
                  tabNum = i;
             }
    }

      logUserAction("Public Module", "Edit POI");
    callToServer(ctxPath + "/loadPOI.html?ident=" + featureId + "&LayerName=" + layerName, "poiTabIFrame");

    accordion(myDivs[tabNum]);

    window.document.getElementById('POIEditor').style.display='';
    window.document.getElementById('updateForm').style.display='';
}

//DIDB40337 log the user action
function deleteOnTheFlyClassification(mapLayerId, classificationId)
{
      logUserAction("Public Module", "Delete On The Fly Classification");
      callToServer(ctxPath + "/deleteOnTheFlyClassification.html?mapLayerId=" + mapLayerId + "&classificationId=" + classificationId, "layerTabClassifcationIFrame");
}

//DIDB40644 setRedLineTool 
function setRedLineTool(tool) {
      m.setCurRedlineTool(tool);
}

//DIDB40644 added this method
function deleteSelectedRedLine() {
      m.deleteSelectedRedline();
}
