function TbOverOut(elId, status){
    var but = document.getElementById(elId);

    if (but.className != 'TOOLBARTD_ON') {

        if (status == 'on') {
            but.className='TOOLBARTD_OVER';
        } else {
            but.className='TOOLBARTD';
        }
    }
}

function TbDownUp(elId, status){
    var but = document.getElementById(elId);
    if (status == 'd') {
        but.className='TOOLBARTD_ON';
    } else {
        but.className='TOOLBARTD';
    }
}

function zoomfullext() {
    //alert("zoomfultext");
    showloading();
    if(document.all){            
        document.frames['visualizzatoreMappa'].document.getMapForm.maction.value = 'zoomfultext';
        document.frames['visualizzatoreMappa'].document.getMapForm.submit();        
        //alert("explorer")
    }else{
        document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.maction.value = 'zoomfultext';
        document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.submit();
        //alert("mozilla")
    }
/*    var mapurl = 'map.phtml?'+SID+'&mode=map&zoom_type=zoomfull';
    parent.mapFrame.location = mapurl;*/
}

function goback() {
    showloading();
    if(document.all){      
        document.frames['visualizzatoreMappa'].document.getMapForm.maction.value = 'goback';
        document.frames['visualizzatoreMappa'].document.getMapForm.submit();
    }else{
        document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.maction.value = 'goback';
        document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.submit();
    }
}

function domouseclick(button) {
    chiudiMisura();
    if (button == 'zoomin') {
        if(document.all){            
            document.frames['visualizzatoreMappa'].document.getMapForm.mode.value = 'map' 
            document.frames['visualizzatoreMappa'].document.getMapForm.zoomType.value = 'zoomrect'
            document.frames['visualizzatoreMappa'].document.getMapForm.maction.value = 'box'
        }else{
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.mode.value = 'map' 
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.zoomType.value = 'zoomrect'
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.maction.value = 'box'
        }
    } else if (button == 'zoomout') {
        if(document.all){            
            document.frames['visualizzatoreMappa'].document.getMapForm.mode.value = 'map' 
            document.frames['visualizzatoreMappa'].document.getMapForm.zoomType.value = 'zoomout'
            document.frames['visualizzatoreMappa'].document.getMapForm.maction.value = 'click'
        }else{
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.mode.value = 'map' 
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.zoomType.value = 'zoomout'
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.maction.value = 'click'
        }    
    } else if (button == 'pan') {
        if(document.all){            
            document.frames['visualizzatoreMappa'].document.getMapForm.mode.value = 'map' 
            document.frames['visualizzatoreMappa'].document.getMapForm.zoomType.value = 'zoompoint'
            document.frames['visualizzatoreMappa'].document.getMapForm.maction.value = 'pan'   
        }else{
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.mode.value = 'map' 
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.zoomType.value = 'zoompoint'
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.maction.value = 'pan'            
        }        
    } else if (button == 'measure') {
        if(document.all){                        
            document.frames['visualizzatoreMappa'].document.getMapForm.maction.value = 'measure'
            //document.frames['visualizzatoreMappa'].document.removeMeasurePoints(true);
        }else{           
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.maction.value = 'measure'           
            //document.getElementById('visualizzatoreMappa').contentDocument.stu();
        }
        document.getElementById('datiMappa').style.display = 'none';
        document.getElementById('datiMisura').style.display = 'block';

    }else if (button == 'identify') {      
        if(document.all){            
            document.frames['visualizzatoreMappa'].document.getMapForm.mode.value = 'query'             
            document.frames['visualizzatoreMappa'].document.getMapForm.maction.value = 'click' 
            document.frames['visualizzatoreMappa'].document.getMapForm.zoomType.value = 'identify'           
        }else{
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.mode.value = 'query' 
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.maction.value = 'click'            
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.zoomType.value = 'identify'            
        }
    }else if (button == 'add') {      
        if(document.all){            
            document.frames['visualizzatoreMappa'].document.getMapForm.mode.value = 'query_add'             
            document.frames['visualizzatoreMappa'].document.getMapForm.maction.value = 'click' 
            document.frames['visualizzatoreMappa'].document.getMapForm.zoomType.value = 'add'           
        }else{
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.mode.value = 'query_add' 
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.maction.value = 'click'            
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.zoomType.value = 'add'                        
        }
    }else if (button == 'sidentify') {
        if(document.all){            
            document.frames['visualizzatoreMappa'].document.getMapForm.mode.value = 'query'             
            document.frames['visualizzatoreMappa'].document.getMapForm.maction.value = 'click' 
            document.frames['visualizzatoreMappa'].document.getMapForm.zoomType.value = 'sidentify'                       
        }else{
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.mode.value = 'query' 
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.maction.value = 'click'            
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.zoomType.value = 'sidentify'             
        }
    }else if (button == 'prgidentify') {
        if(document.all){            
            document.frames['visualizzatoreMappa'].document.getMapForm.mode.value = 'query_prg'             
            document.frames['visualizzatoreMappa'].document.getMapForm.maction.value = 'click' 
            document.frames['visualizzatoreMappa'].document.getMapForm.zoomType.value = 'prgidentify'                       
        }else{
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.mode.value = 'query_prg' 
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.maction.value = 'click'            
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.zoomType.value = 'prgidentify'             
        }
    }else if (button == 'pgtidentify') {
        if(document.all){
            document.frames['visualizzatoreMappa'].document.getMapForm.mode.value = 'query_pgt'
            document.frames['visualizzatoreMappa'].document.getMapForm.maction.value = 'click'
            document.frames['visualizzatoreMappa'].document.getMapForm.zoomType.value = 'pgtidentify'
        }else{
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.mode.value = 'query_pgt'
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.maction.value = 'click'
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.zoomType.value = 'pgtidentify'
        }
    }else if (button == 'select') {
        if(document.all){            
            document.frames['visualizzatoreMappa'].document.getMapForm.mode.value = 'nquery'             
            document.frames['visualizzatoreMappa'].document.getMapForm.maction.value = 'box' 
            document.frames['visualizzatoreMappa'].document.getMapForm.zoomType.value = 'nidentify'                       
        }else{
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.mode.value = 'nquery' 
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.maction.value = 'box'            
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.zoomType.value = 'identify'             
        }
    }else if (button == 'attuale') {
        //alert("attuale")
        if(document.all){
            document.frames['visualizzatoreMappa'].document.getMapForm.mode.value = 'estrai_attuale'
            document.frames['visualizzatoreMappa'].document.getMapForm.zoomType.value = 'zoomrect'
            document.frames['visualizzatoreMappa'].document.getMapForm.maction.value = 'box'
        }else{
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.mode.value = 'estrai_attuale'
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.zoomType.value = 'zoomrect'
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.maction.value = 'box'
        }
    }

    setCursor(button);
}

function reloadMap() {
    var layers = getLayers();
    /*if(layers.length == 0){
        alert("Selezionare almeno un livello informativo");
        return;
    }*/
    //if(layers.length > 0){
        showloading();
        if(document.all){      
            document.frames['visualizzatoreMappa'].document.getMapForm.layers.value = layers;
            document.frames['visualizzatoreMappa'].document.getMapForm.submit();
        }else{
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.layers.value = layers;
            document.getElementById('visualizzatoreMappa').contentDocument.getMapForm.submit();
        }
    //}
/*
    //top.location.reload(true);
    //if (parent.varFrame.document.varform.zoomselected.value == '1') {
        //showloading();
        parent.varFrame.document.varform.zoomselected.value = '0';
        var mapurl = 'map.phtml?'+SID+'&zoom_type=zoompoint&resultlayer=remove';
        parent.mapFrame.location = mapurl;
    //}
*/
}

function openPrintDlg() {
    //alert("openPrintDlg")
    //window.open("servizi/mappe/printGenericMap.jsp");
    window.open("VisualizzaMappa?print=yes");
}

function setTbTDButton(button) {
    var tdarr = document.getElementsByTagName('td');
    for (var i = 0; i < tdarr.length; i++) {
        var tdid = tdarr[i].id;
        if (tdid != 'tsep' && tdid != '' && tdid != 'posizione') {
            if (tdid != button) {
                rmHighlTD(tdid)
            } else {
                highlTD(tdid);
            }
        }
    }
}

function highlTD(elId) {
    var but = document.getElementById(elId);
    but.className='TOOLBARTD_ON';
}


function rmHighlTD(elId) {
    var but = document.getElementById(elId);
    but.className='TOOLBARTD';    
}

function showloading(){
    if(document.all){            
        document.frames['visualizzatoreMappa'].document.getElementById("loading").style.visibility = "visible";
        //alert("explorer")
    }else{
        document.getElementById('visualizzatoreMappa').contentDocument.getElementById("loading").style.visibility = "visible";
        //alert("mozilla")
    }
}

function getLayers() {
    var layerstring = "";

    if(document.layerList.layer != undefined){

    if(document.layerList.layer.value != undefined){
        //Nel caso in cui ho un solo check
            if(document.layerList.layer.checked){
                 layerstring += document.layerList.layer.value +',';
            }
        }else{
        //Nel caso di una lista di check
            for(i=0; i<document.layerList.layer.length;i++){
                if(document.layerList.layer[i].checked)
                    layerstring += document.layerList.layer[i].value +',';
            }            
        } 
/*    for (var i=0; i<document.layerList.layer.length; i++) {    
        alert("document.layerList.layer[i].checked " + document.layerList.layer[i].checked)
           if (document.layerList.layer[i].checked == true) {
                layerstring += document.layerList.layer[i].value +',';
    	}
    }*/
    }else{
        //alert('Attenzione non ci sono livelli informativi selezionabili');
        return "";
    }
    if(layerstring.length > 0)
        layerstring = layerstring.substr(0, layerstring.length - 1);    
    //else
    //    alert('Attenzione non ci sono livelli informativi selezionati');
    return layerstring;
}

function chiudiMisura(){
    window.visualizzatoreMappa.resetMeasure();
    window.visualizzatoreMappa.document.getMapForm.maction.value = '';

    //window.visualizzatoreMisure.document.getElementById('parziale').innerHTML = '';
    //window.visualizzatoreMisure.document.getElementById('totale').innerHTML = '';        
    document.getElementById('datiMappa').style.display = 'block';
    document.getElementById('datiMisura').style.display = 'none';
    
}

function azzeraMisura(){
    window.visualizzatoreMappa.resetMeasure();  
}

function getRootPath() {
    var theLoc = document.location.href;
    var theLastPos = theLoc.lastIndexOf('/');
    var RootPath = theLoc.substr(0,theLastPos) + '/';
    return RootPath;
}

function setCursor(toolType) {
/*    if (parent.varFrame.document.varform) {
        var toolType = parent.varFrame.document.varform.tool.value;
    } else {
        var toolType = 'zoomin';
    }
*/
    
    /* Define settings for cursor to be used for tools
       set to true if you want to use the same cursors for all browsers (incl. IE) */
    var internalCursor = ((navigator.version < 6) || (navigator.appName == 'Netscape'));
    //internalCursor = true; 
    
    var rootPath = getRootPath();
    var usedCursor = (internalCursor) ? toolType : "url(" +rootPath + "images/cursors/zoomin.cur)";
    
    document.getElementById('visualizzatoreMappa').style.cursor = usedCursor;
    
    switch (toolType) {
        case "zoomin" :                        
            var usedCursor = (internalCursor) ? 'crosshair' : 'url(' +rootPath + 'images/cursors/zoomin.cur)';	
            break;        
        case "zoomout" :
            var usedCursor = (internalCursor) ? 'e-resize' : 'url(' +rootPath + 'images/cursors/zoomout.cur)';	
            break;
        case "identify" :            
            var usedCursor = (internalCursor) ? 'help' : 'url(' +rootPath + 'images/cursors/select.cur)';
            break;
        case "pan" :
            var usedCursor = 'move';
            break;            
        case "select" :
            var usedCursor = (internalCursor) ? 'help' : 'url(' +rootPath + 'images/cursors/select.cur)';	
            break;            
        case "measure" :
            var usedCursor = (internalCursor) ? 'crosshair' : 'url(' +rootPath + 'images/cursors/measure.cur)';	
            break;
        case "sidentify" :
            var usedCursor = (internalCursor) ? 'help' : 'url(' +rootPath + 'images/cursors/select.cur)';	
            break;   
        case "prgidentify" :
            var usedCursor = (internalCursor) ? 'help' : 'url(' +rootPath + 'images/cursors/select.cur)';	
            break;         
        default:
            var usedCursor = 'default';
            
    }

    if(document.all){    
        document.frames['visualizzatoreMappa'].document.body.style.cursor = usedCursor;
/*        var divarr = document.frames['visualizzatoreMappa'].document.getElementsByTagName('div');    
        for (var i = 0; i < divarr.length; i++) {        
            var divid = divarr[i].id;                   
            if(divid == "mapimg" || divid == "mapimg_sel" ){
                alert(divid + " a " + usedCursor)
                document.frames['visualizzatoreMappa'].document.getElementById(divid).style.cursor = usedCursor;
            }
        }*/
//        document.frames['visualizzatoreMappa'].document.getElementById('mapimg').style.cursor = usedCursor;          
    }else{
        document.getElementById('visualizzatoreMappa').contentDocument.body.style.cursor = usedCursor;         
/*        var divarr = document.getElementById('visualizzatoreMappa').contentDocument.getElementsByTagName('div');    
        for (var i = 0; i < divarr.length; i++) {        
            var divid = divarr[i].id;                   
            if(divid == "mapimg" || divid == "mapimg_sel" ){
                alert(divid + " b " + usedCursor)
                document.getElementById('visualizzatoreMappa').contentDocument.getElementById(divid).style.cursor = usedCursor;
            }
        }*/
//        document.getElementById('visualizzatoreMappa').contentDocument.getElementById('mapimg').style.cursor = usedCursor;         
    }

    //parent.mapFrame.document.getElementById('maplayer').style.cursor = usedCursor;
    //document.getElementById('visualizzatoreMappa').style.cursor = usedCursor;
    
}

function ripristinaCursore(){    
    //var tdarr = document.getElementsByName('tbtd');
    var tdarr = document.getElementsByTagName('td');    
    for (var i = 0; i < tdarr.length; i++) {        
        var tdid = tdarr[i].id;       
        if(tdid != ''){
            var but = document.getElementById(tdid);              
            var classe = '';
            classe = but.className;
            if(classe == 'TOOLBARTD_ON'){                 
               setCursor(tdid);
            }
        }
    }
    
    /*for (var i = 0; i < tdarr.length; i++) {        
        var tdid = tdarr[i].id;       
        var but = document.getElementById(tdid);              
        var classe = '';
        classe = but.className;
        if(classe == 'TOOLBARTD_ON'){           
            setCursor(tdid);
        }
    }*/
}
