//var mapaGuia="wxpcnig060/pnoa/spain1.ecw";
var mapaGuia="wxpcnig060/pnoa/spain1.ecw";
//var mapaGuia="wxpcnig060/pnoa/ORTOIMAGENES_pen_g.ecw";
/*
 ORTOIMAGENES_pen_g
 */
//var mapaPen="www.cnig.es:8081/images/mapaguia/peninsula_orto.ecw";var mapaCan="www.cnig.es:8081/images/mapaguia/canarias_orto.ecw";
//var mapaCan="wxpcnig060/pnoa/canarias_orto.ecw";

var iCajaWorldX,iCajaWorldY;    
var bArrastra=false;
var bLoaded=0;

var bBoxMode=false;
var bMasterOnOff=false;
var bPointerMode=false;

function cargaVisores(map){
    /*if(map=='PEN')mapaGuia="wxpcnig060/pnoa/ORTOIMAGENES_pen_g.ecw";
    else if(map=='CAN')mapaGuia="wxpcnig060/pnoa/ORTOIMAGENES_can_g.ecw";*/
        if(map=='PEN')
            mapaGuia="wxpcnig060/pnoa/spain1.ecw";
        else if(map=='CAN'){
            mapaGuia="wxpcnig060/pnoa/canarias_orto.ecw";            
        }    
        else{
            alert("mapa Guia NOT defined");
            return;
        }
        var visor1 =document.ECWView1;
        var visor2 =document.ECWView2;
        if(map=='CAN'){
            visor1.SetBackGroundColor("#FFFFFF");
        }    
        else
            visor1.SetBackGroundColor("#270192");
        visor2.SetBackGroundColor("#F2F2F2");
        visor1.SetPointerMode(PM_POINTER);
        visor2.SetPointerMode(PM_POINTER);
        
        visor1.DeleteAllLayers();
        visor2.DeleteAllLayers();
        if(visor2.AddLayer("ecw","ecwp://"+mapaGuia,"layer2","")<0){
            alert(""+visor2.GetLastErrorText());
        }
        if(visor1.AddLayer("ecw","ecwp://"+mapaGuia,"layer1","")<0){
            alert(""+visor1.GetLastErrorText());
        }
        if(map=='PEN')
            visor2.height="100px";
        else if(map=='CAN')
            visor2.height="80px";
            
        OverlayRect = new NCSOverlayRect(); 
        OverlayRect.Init(visor2,visor1);            
        
        var tamanioCaja=document.getElementById("tamanioCaja").value;
        var iBox=parseInt(tamanioCaja); 
        var TLX,BRY,BRX,TLY;        
        TLX =visor1.GetLayerImageTopLeftWorldCoordinateX("layer1"); 
	TLY =visor1.GetLayerImageTopLeftWorldCoordinateY("layer1"); 
	BRX =visor1.GetLayerImageBottomRightWorldCoordinateX("layer1"); 
	BRY =visor1.GetLayerImageBottomRightWorldCoordinateY("layer1");         
        var X0,Y0,X1,Y1,X2,Y2;
        X0=(TLX+BRX)/2;//X medio
        Y0=(TLY+BRY)/2;//Y medio           
        
        X1=X0-iBox/2;//X sup-left referido a la caja
        Y1=Y0+iBox/2;//Y sup_left            
        X2=X1+iBox;//X inf-right
        Y2=Y1-iBox;//Y inf_right
        X1=parseInt(X1);
        X2=parseInt(X2);
        Y1=parseInt(Y1);
        Y2=parseInt(Y2);
        //alert("X1="+X1+" Y1="+Y1+"   X2="+X2+"  Y2="+Y2+"\n"+"TLX="+TLX+" TLY="+TLY+" BRX="+BRX+" BRY="+BRY);
        OverlayRect.Create(X1, Y1, X2, Y2);
        var tlwcx = visor1.GetTopLeftWorldCoordinateX(); 
        var tlwcy = visor1.GetTopLeftWorldCoordinateY(); 
	var brwcx = visor1.GetBottomRightWorldCoordinateX(); 
	var brwcy = visor1.GetBottomRightWorldCoordinateY(); 
        OverlayRect.SetRect(tlwcx,tlwcy,brwcx,brwcy, false);  
        ECWVectorLayer = new NCSVectorLayer(document.ECWView1, "VectorOverlay");                
        if (document.ECWView1.AddLayer("simplevector", "", "VectorOverlay", ";") < 0) {
            alert(document.ECWView1.GetLastErrorText());
	}
        document.getElementById("capaMaster").style.visibility="visible";
        //capaHoja(document.ECWView1.GetViewWidth(),document.ECWView1.GetViewHeight());
        document.getElementById("imArea").src='img/toolBox.png';
        return;
    }

    
    function ECWUpdateOverview(dScreenX, dScreenY, dWorldX, dWorldY, bUpdateSlave,bBArrastra,iCajaX, iCajaY) { 
	OverlayRect.Move2(dWorldX, dWorldY, bUpdateSlave,bBArrastra,iCajaX, iCajaY);
        return;
    } 
    
    //function moveVisores(direccion){
    function moveVisores(event){
        var objRosaVientos=document.getElementById("imRoseWind"); 
        posicion=findPos(objRosaVientos);        
        var x = event.clientX;
        var y = event.clientY;        
        var dx=parseInt(x-posicion[0]);
        var dy=parseInt(y-posicion[1]);
        
        var visor= document.ECWView1;
        var tlx,tly,brx,bry; 
	var tlwcx,tlwcy,brwcx,brwcy; 
	var stepX,stepY; 
	tlwcx = visor.GetTopLeftWorldCoordinateX(); 
        tlwcy = visor.GetTopLeftWorldCoordinateY(); 
	brwcx = visor.GetBottomRightWorldCoordinateX(); 
	brwcy = visor.GetBottomRightWorldCoordinateY(); 
	stepX =brwcx - tlwcx; 
        stepY =tlwcy - brwcy; 
	stepX=parseInt(stepX/2);
	stepY=parseInt(stepY/2);
        if(dx>8 && dx<17 && dy<8){
                tly = tlwcy + stepY; 
                bry = brwcy + stepY; 
                tlx = tlwcx; 
                brx = brwcx;        
        }
        else if(dx>8 && dx<22 && dy>16){
                tly = tlwcy - stepY; 
                bry = brwcy - stepY; 
                tlx = tlwcx; 
                brx = brwcx;        
        }
        else if(dy>7&& dy<16 && dx<10){
                tlx = tlwcx - stepX; 
                brx = brwcx - stepX; 
                tly = tlwcy; 
                bry = brwcy;        
        }
        else if(dy>7&& dy<16 && dx>16){
                tlx = tlwcx + stepX; 
                brx = brwcx + stepX; 
                tly = tlwcy; 
                bry = brwcy; 
        }
        visor.SetExtents(tlx, tly, brx, bry);
        OverlayRect.SetRect(tlx, tly, brx, bry, false);
        return;
    }
    function overRoseWind(event){
        var objRosaVientos=document.getElementById("imRoseWind"); 
        posicion=findPos(objRosaVientos);        
        var x = event.clientX;
        var y = event.clientY;        
        var dx=parseInt(x-posicion[0]);
        var dy=parseInt(y-posicion[1]);
        
        if(dx>8 && dx<17 && dy<8){
                objRosaVientos.alt="N";
                objRosaVientos.title="N";
                objRosaVientos.src="img/roseWind_N.png";
        }
        else if(dx>8 && dx<17 && dy>16){
                objRosaVientos.alt="S";
                objRosaVientos.title="S";
                objRosaVientos.src="img/roseWind_S.png";
        }
        else if(dy>7&& dy<16 && dx<8){
                objRosaVientos.alt="O";
                objRosaVientos.title="O";
                objRosaVientos.src="img/roseWind_O.png";
        }
        else if(dy>7&& dy<16 && dx>16){
                objRosaVientos.alt="E";
                objRosaVientos.title="E";
                objRosaVientos.src="img/roseWind_E.png";
        }
        else{
            objRosaVientos.alt="";
            objRosaVientos.title="";
            objRosaVientos.src="img/roseWind.png";
        }
        return;
    }

    function Crea_Objeto(sHTML,sNOMBRE){ 
        if (Es.IE){
            document.body.insertAdjacentHTML('beforeEnd', sHTML);
	}
	else if (Es.DOM){
            document.getElementById("divMapa").insertAdjacentHTML('beforeEnd', sHTML);
        }
    }
         
    
    
    /* Al hacer click sobre el visor master(visor pequeño)
       se actualiza el visor slave(grande)
     */
    function MasterClick(buttonMask, screenx, screeny, worldx, worldy) { 
        iCajaWorldX=worldx; 
	iCajaWorldY=worldy;   
        ECWUpdateOverview(screenx, screeny, worldx, worldy, true,false,iCajaWorldX, iCajaWorldY);//true->actualiza slave,false->no arrastra
	return;
    }
    /*  Controla el movimiento con el cursor ROAM sobre el visor grande(slave).
        Según arrastra el layer actualiza el rectángulo de posición del 
        visor master(visor pequeño)*/
    
    function SlaveExtentChange(worldTLX, worldTLY, worldBRX, worldBRY){
        if(bLoaded==1){
            if(OverlayRect!=null)
                OverlayRect.SetRect(worldTLX, worldTLY, worldBRX, worldBRY, false);
        }
        self.focus();
        return;
    }
    function slaveMouseMove(buttonMask, screenx, screeny, worldx, worldy) {
        if(bBoxMode){
            OverlayRectSlave = new NCSOverlayRect(); 
            if(worldx-iCajaWorldX>1.0){
                bArrastra=true;
                OverlayRectSlave.SetRect(iCajaWorldX, iCajaWorldY, worldx,worldy);
            }
        }
        return;
    }    
    function slaveMouseUp(buttonMask, screenx, screeny, worldx, worldy) { 
        if(bBoxMode){
            ECWUpdateOverview(screenx, screeny, worldx, worldy, true,bArrastra,iCajaWorldX, iCajaWorldY);                             
            bArrastra=false;
        }
        return;
    }    
    
    /*  Controla que se cargue toda la imagen del visor slave(grande).
        Cuando ha terminado pone la variable bLoaded=1
     */
    function SlavePercentComplete(value){
        if(parseFloat(value)==100){
           bLoaded=1;
        }
        return;
    }
    
     function zoomOut(){
        var visor1 = document.ECWView1;
        var TLX,BRY,BRX,TLY;        
        var tamanioCaja=document.getElementById("tamanioCaja").value;
        var iBox=parseInt(tamanioCaja)+80000; 
        if(iBox<0)  
            return;
        document.getElementById("tamanioCaja").value=iBox;
        TLX =visor1.GetTopLeftWorldCoordinateX(); 
        TLY = visor1.GetTopLeftWorldCoordinateY(); 
	BRX = visor1.GetBottomRightWorldCoordinateX(); 
	BRY = visor1.GetBottomRightWorldCoordinateY();
        var X0,Y0,X1,Y1,X2,Y2;
        X0=(TLX+BRX)/2;//X medio
        Y0=(TLY+BRY)/2;//Y medio           
        
        X1=X0-iBox/2;//X sup-left referido a la caja
        Y1=Y0+iBox/2;//Y sup_left            
        X2=X1+iBox;//X inf-right
        Y2=Y1-iBox;//Y inf_right
        X1=parseInt(X1);
        X2=parseInt(X2);
        Y1=parseInt(Y1);
        Y2=parseInt(Y2);
        if(Y2>Y1)
            OverlayRect.SetRect(X1,Y2,X2,Y1, true);
        else
            OverlayRect.SetRect(X1,Y1,X2,Y2, true);
        
        if(Y2>Y1)
            visor1.SetExtents(X1,Y2,X2,Y1);
        else
            visor1.SetExtents(X1,Y1,X2,Y2);
        return;
     }
     function zoomIn(){
        var visor1 = document.ECWView1;
        var visor2 = document.ECWView2;
        var TLX,BRY,BRX,TLY;        
        var tamanioCaja=document.getElementById("tamanioCaja").value;
        var iBox=parseInt(tamanioCaja)-80000; 
        if(iBox<0)  
            return;
        document.getElementById("tamanioCaja").value=iBox;
        TLX =visor1.GetTopLeftWorldCoordinateX(); 
        TLY = visor1.GetTopLeftWorldCoordinateY(); 
	BRX = visor1.GetBottomRightWorldCoordinateX(); 
	BRY = visor1.GetBottomRightWorldCoordinateY();

        var X0,Y0,X1,Y1,X2,Y2;
        X0=(TLX+BRX)/2;//X medio
        Y0=(TLY+BRY)/2;//Y medio           
        
        X1=X0-iBox/2;//X sup-left referido a la caja
        Y1=Y0+iBox/2;//Y sup_left            
        X2=X1+iBox;//X inf-right
        Y2=Y1-iBox;//Y inf_right
        X1=parseInt(X1);
        X2=parseInt(X2);
        Y1=parseInt(Y1);
        Y2=parseInt(Y2);
    
        if(Y2>Y1)
            OverlayRect.SetRect(X1,Y2,X2,Y1, true);
        else
            OverlayRect.SetRect(X1,Y1,X2,Y2, true);

        if(Y2>Y1)
            visor1.SetExtents(X1,Y2,X2,Y1);
        else
            visor1.SetExtents(X1,Y1,X2,Y2);
        return;
     }
    
    
     function MasterOnOff(){
         if(document.getElementById("capaMaster").style.visibility=="hidden"){
            document.getElementById("capaMaster").style.visibility="visible";
            bMasterOnOff=false;
        }    
        else{
            document.getElementById("capaMaster").style.visibility="hidden";
            bMasterOnOff=true;
         }   
         return;
     }
     
     function resetVisor(){
        var visor1=document.ECWView1;
        visor1.SetExtentsAll();
        var X1,X2,Y1,Y2;        
        X1 =visor1.GetLayerImageTopLeftWorldCoordinateX("layer1"); 
        Y2 = visor1.GetLayerImageTopLeftWorldCoordinateY("layer1"); 
	X2 = visor1.GetLayerImageBottomRightWorldCoordinateX("layer1"); 
	Y1 = visor1.GetLayerImageBottomRightWorldCoordinateY("layer1");
        
        OverlayRect.SetRect(X1,Y2,X2,Y1, true);
        return;
     }
     
    function findPos(obj) { /*calcula la posicion de un objeto dentro de la web*/
        var curleft = curtop = 0;
        if (obj.offsetParent) {
            curleft = obj.offsetLeft;
            curtop = obj.offsetTop;
            while (obj = obj.offsetParent) {
                curleft += obj.offsetLeft;
                curtop += obj.offsetTop;
            }
        }
        return [curleft,curtop];
    }
    
    function checkGrid25(obj){
        var visor1 = document.ECWView1;
        var TLX,BRY,BRX,TLY;        
        var geoTL = new Array(2);
        var geoBR = new Array(2);
        TLX =visor1.GetLayerImageTopLeftWorldCoordinateX("layer1"); 
        TLY =visor1.GetLayerImageTopLeftWorldCoordinateY("layer1"); 
	BRX =visor1.GetLayerImageBottomRightWorldCoordinateX("layer1"); 
	BRY =visor1.GetLayerImageBottomRightWorldCoordinateY("layer1");
        
        var isChecked=obj.checked;
        if(isChecked){
            geoTL=utmgeo(TLX, TLY, 30);            
            geoBR=utmgeo(BRX, BRY, 30);
            construyeCuadricula25(geoTL,geoBR);
            deleteGrid("H50");
            document.getElementById("grid50").checked=false;
        }
        else{
            deleteGrid("H25");
        }
       return;
    }

    function checkGrid50(obj){
        var visor1 = document.ECWView1;
        var TLX,BRY,BRX,TLY;        
        var geoTL = new Array(2);
        var geoBR = new Array(2);
        TLX =visor1.GetLayerImageTopLeftWorldCoordinateX("layer1"); 
        TLY = visor1.GetLayerImageTopLeftWorldCoordinateY("layer1"); 
	BRX = visor1.GetLayerImageBottomRightWorldCoordinateX("layer1"); 
	BRY = visor1.GetLayerImageBottomRightWorldCoordinateY("layer1");
        var isChecked=obj.checked;
        if(isChecked){
            geoTL=utmgeo(TLX, TLY, 30);            
            geoBR=utmgeo(BRX, BRY, 30);
            construyeCuadricula50(geoTL,geoBR);
            deleteGrid("H25");
            document.getElementById("grid25").checked=false;
       }
        else{
            deleteGrid("H50");
        }
       return;
    }
    function construyeCuadricula50(geoTL,geoBR){
        //alert("construyeCuadricula geoTL[0]="+geoTL[0]+"    geoTL[1]="+geoTL[1]);
        var TLX,BRY,BRX,TLY; 
        var visor1 = document.ECWView1;
        var ccffTL=new Array(2);        
        var ccffBR=new Array(2);        
        ccffTL=getCCFFFromLonLat_MTN50(geoTL[1],geoTL[0]);//saca la ccff Superior Izquierda
        ccffBR=getCCFFFromLonLat_MTN50(geoBR[1],geoBR[0]);//saca la ccff Inferior Derecha
        var sParams = ""; 
	var sFillColor="#CCCCCC"; 
	var sPenColor="#D1DBF4"; 
	var iLineWidth=1.0; 
	var visor=document.ECWView1;
        var i,j;
        var lonLatTL,lonLatBR,arrTL,BRY;
        var numCols=ccffBR[0]-ccffTL[0];
        var numRows=ccffBR[1]-ccffTL[1];
        //alert("num filas="+numCols+"   numRows="+numRows);
        lonLatTL=getCoordsFromCCFF_MTN50(ccffTL[0],ccffTL[1]);
        lonLatBR=getCoordsFromCCFF_MTN50(ccffBR[0],ccffBR[1]);
        arrTL =geoutm(lonLatTL[1], lonLatTL[0], 30);
        arrBR =geoutm(lonLatBR[1], lonLatBR[0], 30);
        sParams="fillcolor="+sFillColor+";"+"color="+sPenColor+";"+"linewidth="+iLineWidth+";"; 
        sParams+="filledpolygon1="; 
        sParams+=arrTL[0]+","+arrTL[1]+"|"+arrBR[0]+","+arrTL[1]+"|"+arrBR[0]+","+arrBR[1]+"|"+arrTL[0]+","+arrBR[1]+";";
        var width=Math.abs(arrBR[0]-arrTL[0]);
        var height=Math.abs(arrBR[1]-arrTL[1]);
        var gapWidth=width/numCols;
        var gapHeight=height/numRows;            
        for(i=0;i<numRows+1;i++){
            sParams="color=#D1DBF4;linewidth=1.0;"; 
            sParams+="polyline1="; 
            sParams+=arrTL[0]+","+parseInt(arrTL[1]-gapHeight*i)+"|"+arrBR[0]+","+parseInt(arrTL[1]-gapHeight*i)+";";
            visor.AddLayer("simplevector", "", "Line50W"+i, ";");
            visor.SetLayerParameter("Line50W"+i, sParams); 
        }
        for(j=0;j<numCols+1;j++){
            sParams="color=#D1DBF4;linewidth=1.0;"; 
            sParams+="polyline1="; 
            sParams+=parseInt(arrTL[0]+gapWidth*j)+","+arrTL[1]+"|"+parseInt(arrTL[0]+gapWidth*j)+","+arrBR[1]+";";
            visor.AddLayer("simplevector", "", "Line50H"+j, ";");
            visor.SetLayerParameter("Line50H"+j, sParams); 
        }            
        return;
    }

    function construyeCuadricula25(geoTL,geoBR){
        var TLX,BRY,BRX,TLY; 
        var visor1 = document.ECWView1;
        var ccffTL=new Array(2);        
        var ccffBR=new Array(2);        
        ccffTL=getCCFFFromLonLat_MTN25(geoTL[1],geoTL[0]);//saca la ccff Superior Izquierda
        ccffBR=getCCFFFromLonLat_MTN25(geoBR[1],geoBR[0]);//saca la ccff Inferior Derecha
        var sParams = ""; 
	var sFillColor="#CCCCCC"; 
	var sPenColor="#FF8000"; 
	var iLineWidth=1.0; 
	var visor=document.ECWView1;
        var i,j;
        
        var lonLatTL,lonLatBR,arrTL,BRY;
        var numCols=ccffBR[0]-ccffTL[0];
        var numRows=ccffBR[1]-ccffTL[1];
        lonLatTL=getCoordsFromCCFF_MTN25(ccffTL[0],ccffTL[1]);
        lonLatBR=getCoordsFromCCFF_MTN25(ccffBR[0],ccffBR[1]);
        arrTL =geoutm(lonLatTL[1], lonLatTL[0], 30);
        arrBR =geoutm(lonLatBR[1], lonLatBR[0], 30);
        sParams="fillcolor="+sFillColor+";"+"color="+sPenColor+";"+"linewidth="+iLineWidth+";"; 
        sParams+="filledpolygon1="; 
        sParams+=arrTL[0]+","+arrTL[1]+"|"+arrBR[0]+","+arrTL[1]+"|"+arrBR[0]+","+arrBR[1]+"|"+arrTL[0]+","+arrBR[1]+";";
        var width=Math.abs(arrBR[0]-arrTL[0]);
        var height=Math.abs(arrBR[1]-arrTL[1]);
        var gapWidth=width/numCols;
        var gapHeight=height/numRows;
        for(i=0;i<numRows+1;i++){
            sParams="color=#FFFFFF;linewidth=1.0;"; 
            sParams+="polyline1="; 
            sParams+=arrTL[0]+","+parseInt(arrTL[1]-gapHeight*i)+"|"+arrBR[0]+","+parseInt(arrTL[1]-gapHeight*i)+";";
            visor.AddLayer("simplevector", "", "Line25W"+i, ";");
            visor.SetLayerParameter("Line25W"+i, sParams); 
        }
        for(j=0;j<numCols+1;j++){
            sParams="color=#FFFFFF;linewidth=1.0;"; 
            sParams+="polyline1="; 
            sParams+=parseInt(arrTL[0]+gapWidth*j)+","+arrTL[1]+"|"+parseInt(arrTL[0]+gapWidth*j)+","+arrBR[1]+";";
            visor.AddLayer("simplevector", "", "Line25H"+j, ";");
            visor.SetLayerParameter("Line25H"+j, sParams); 
        }
        return;
    }
    
    function deleteGrid(hoja){
        var visor1 = document.ECWView1;
        var TLX,BRY,BRX,TLY;        
        var geoTL = new Array(2);
        var geoBR = new Array(2);
        var ccffTL= new Array(2);        
        var ccffBR= new Array(2);        
        TLX = visor1.GetLayerImageTopLeftWorldCoordinateX("layer1"); 
        TLY = visor1.GetLayerImageTopLeftWorldCoordinateY("layer1"); 
	BRX = visor1.GetLayerImageBottomRightWorldCoordinateX("layer1"); 
	BRY = visor1.GetLayerImageBottomRightWorldCoordinateY("layer1");
        geoTL=utmgeo(TLX, TLY, 30);            
        geoBR=utmgeo(BRX, BRY, 30);
        var numCols,numRows;
        switch(hoja){
            case "H50":
                ccffTL=getCCFFFromLonLat_MTN50(geoTL[1],geoTL[0]);//saca la ccff Superior Izquierda
                ccffBR=getCCFFFromLonLat_MTN50(geoBR[1],geoBR[0]);//saca la ccff Inferior Derecha
                numCols=ccffBR[0]-ccffTL[0];
                numRows=ccffBR[1]-ccffTL[1];
                for(i=0;i<numRows+1;i++){
                    visor1.DeleteLayer("Line50W"+i);
                }
                for(j=0;j<numCols+1;j++){
                    visor1.DeleteLayer("Line50H"+j);
                }
            break;    
            case "H25":
                ccffTL=getCCFFFromLonLat_MTN25(geoTL[1],geoTL[0]);//saca la ccff Superior Izquierda
                ccffBR=getCCFFFromLonLat_MTN25(geoBR[1],geoBR[0]);//saca la ccff Inferior Derecha
                numCols=ccffBR[0]-ccffTL[0];
                numRows=ccffBR[1]-ccffTL[1];
                for(i=0;i<numRows+1;i++){
                    visor1.DeleteLayer("Line25W"+i);
                }
                for(j=0;j<numCols+1;j++){
                    visor1.DeleteLayer("Line25H"+j);
                }
            break;    
        }
        return;
    }
    
    
    function cargaDivAdmin(obj){
        var codDivAdmin=obj.options[obj.options.selectedIndex].value;
        switch(codDivAdmin){
            case "1":
                document.getElementById("selectCCAA").style.display="block";
                document.getElementById("selectProv").style.display="none";                
            break;    
            case "2":
                document.getElementById("selectProv").style.display="block";
                document.getElementById("selectCCAA").style.display="none";                
            break;    
            default:
                document.getElementById("selectCCAA").style.display="none";
                document.getElementById("selectProv").style.display="none";
            break;    
        }        
        return;
    }
   
    function checkBusquedaHoja(obj){
        var opcion=obj.value;

    	var tipoHoja = document.buscadorForm.MTN;
    	var mtn50 = tipoHoja[0].checked;
        switch(opcion){
        	case "MTN25":
        		if(document.getElementById("divNumHoja").style.display == "block"){
        			document.getElementById("divNumHoja").style.display="none";
        			document.getElementById("divNumHoja25").style.display="block";
        		}
        	break;
        	case "MTN50":
        		if(document.getElementById("divNumHoja25").style.display == "block"){
        			document.getElementById("divNumHoja").style.display="block";
        			document.getElementById("divNumHoja25").style.display="none";
        		}
        	break;
        	case "numHoja":
                if(mtn50){
                	document.getElementById("divNumHoja").style.display="block";
                	document.getElementById("divNumHoja25").style.display="none";
                }else{
                	document.getElementById("divNumHoja").style.display="none";
                	document.getElementById("divNumHoja25").style.display="block";
                }
                document.getElementById("divCCFF").style.display="none";
            	document.getElementById("divNomHoja").style.display="none";
            break;
            case "ccff":
                document.getElementById("divNumHoja").style.display="none";
                document.getElementById("divNumHoja25").style.display="none";
                document.getElementById("divCCFF").style.display="block";
                document.getElementById("divNomHoja").style.display="none";
            break;
            case "nomHoja":
                document.getElementById("divNomHoja").style.display="block";
                document.getElementById("divNumHoja").style.display="none";
                document.getElementById("divNumHoja25").style.display="none";
                document.getElementById("divCCFF").style.display="none";
            break;
        }
        return;
    }
    
    function localizarHojas(){
        return;
    }
    
    function localizarCoords(){        
        var utm30= new Array(2);//en huso30 para mostrar en el visor
        var boxSize=document.getElementById("tamanioCaja").value;     
        //BusquedaNomen
        if(document.getElementById("blockBusqNomen").style.display=="block"){
            var xMiddle=document.getElementById("xMiddle").value;
            var yMiddle=document.getElementById("yMiddle").value;
            var husoNomen=document.getElementById("husoNomen").value;            
            if(xMiddle=="" || yMiddle=="" || husoNomen==""){
                alert("debe seleccionar un registro de la lista Nomenclator");
                return;
            }
            utm30=cambioHuso(parseFloat(xMiddle), parseFloat(yMiddle),parseInt(husoNomen), 30);
        }
        else if(document.getElementById("divOpcionesHoja").style.display=="block"){//Busqueda por Hoja
            /*Hoja 25-50, y numHoja-ccff*/
            var tipoHoja=document.buscadorForm.MTN;
            var hoja=document.buscadorForm.tipoBusqHoja;
            /*arrHoja[0]-->hoja;arrHoja[1]-->col;arrHoja[2]-->fila*/
            if(tipoHoja[0].checked){//MTN50
                if(hoja[0].checked){
                    var numHoja=document.getElementById("txtNumHoja").value;
                    var isHoja=existeHoja(numHoja);//alert("Busqueda NUMHOJA:"+isHoja);
                    if(isHoja){
                        var ccff=getCCFFFromHoja(numHoja);//alert("ccff[0]="+ccff[0]+"      ccff[1]="+ccff[1]);
                        var coords=getCoordsFromCCFF_MTN50(ccff[0],ccff[1]);//landa,phi
                        utm30=geoutm(coords[1],coords[0],30);                        
                    }
                    else{
                        alert("no hay datos para esa Hoja");
                        return;
                    }
                }
                else if(hoja[1].checked){
                    var cc=document.getElementById("txtCC").value;
                    var ff=document.getElementById("txtFF").value;
                    var isCCFF=existeCCFF(cc,ff);//alert("Busqueda CCFF:"+isCCFF);
                    if(isCCFF){
                        var coords=getCoordsFromCCFF_MTN50(cc,ff);//landa,phi
                        utm30=geoutm(coords[1],coords[0],30);
                    }
                    else{
                        alert("no hay datos para esa CCFF");
                        return;
                    }
                }                
                else if(hoja[2].checked){//alert("50....");
                    var xMiddle=document.getElementById("xMiddle").value;
                    var yMiddle=document.getElementById("yMiddle").value;
                    var husoHojas=document.getElementById("husoNomHojas").value;            
                    if(xMiddle=="" || yMiddle=="" || husoHojas==""){
                        alert("debe seleccionar un registro de la lista de Hojas");
                        return;
                    }
                    //alert("xMiddle="+xMiddle+"  yMiddle="+yMiddle+"     husoHojas="+husoHojas);
                    
                    utm30=cambioHuso(parseFloat(xMiddle), parseFloat(yMiddle),parseInt(husoHojas), 30);
                }
            }//FIN MTN50
            else if(tipoHoja[1].checked){
                if(hoja[0].checked){
                    //alert("Hoja 25 (hay q validar formato hoja)");
                    var numHoja25=document.getElementById("txtNumHoja").value;
                    var temp=numHoja25.split("-");
                    var hoja=temp[0];
                    var cuadrante=temp[1];
                    //alert("Hoja="+hoja+" cuadrante="+cuadrante);                    
                    return;
                }
                else if(hoja[1].checked){
                    var cc=document.getElementById("txtCC").value;
                    var ff=document.getElementById("txtFF").value;
                     var coords=getCoordsFromCCFF_MTN25(cc,ff);//landa,phi
                     utm30=geoutm(coords[1],coords[0],30);
                }
                else if(hoja[2].checked){//alert("25....");
                    var xMiddle=document.getElementById("xMiddle").value;
                    var yMiddle=document.getElementById("yMiddle").value;
                    var husoHojas=document.getElementById("husoNomHojas").value; 
                    //alert("xMiddle="+xMiddle+"  yMiddle="+yMiddle+"     husoHojas="+husoHojas);
                    if(xMiddle=="" || yMiddle=="" || husoHojas==""){
                        alert("debe seleccionar un registro de la lista de Hojas");
                        return;
                    }
                    utm30=cambioHuso(parseFloat(xMiddle), parseFloat(yMiddle),parseInt(husoHojas), 30);
                }
                else return;
            }//FIN MTN25            
        }// fin  busquedaHoja
        else if(document.getElementById("divOpcionesCoords").style.display=="block"){//Busqueda por Coordenadas
            var tipoBusqueda=document.buscadorForm.tipoCoord;
            if(tipoBusqueda[0].checked){                
                var NS=document.getElementById("selectNS").options[document.getElementById("selectNS").options.selectedIndex].value;
                var EO=document.getElementById("selectEO").options[document.getElementById("selectEO").options.selectedIndex].value;
                
                var gradLon=document.getElementById("textGradLon").value;
                var minLon=document.getElementById("textMinLon").value;
                var segLon=document.getElementById("textSegLon").value;
                
                var landa=convertSexagToDec(gradLon,minLon,segLon);

                var gradLat=document.getElementById("textGradLat").value;
                var minLat=document.getElementById("textMinLat").value;
                var segLat=document.getElementById("textSegLat").value;
                
                var phi=convertSexagToDec(gradLat,minLat,segLat);
                
                if(EO=='O')
                    landa=-parseFloat(landa);        
                if(NS=='S')
                    phi=-parseFloat(phi);
                utm30=geoutm(parseFloat(phi), parseFloat(landa),30);
                //alert("utm30[0]="+utm30[0]+"        utm30[1]="+utm30[1]+"\n"+"utm30WGS84[0]="+utm30[0]+"   utm30WGS84[1]="+utm30WGS84[1]);
            }
            else if(tipoBusqueda[1].checked){
                var huso=document.getElementById("selectHuso").options[document.getElementById("selectHuso").options.selectedIndex].value;
                var xUTM=document.getElementById("txtX").value;
                var yUTM=document.getElementById("txtY").value;
                //alert(" xUTM="+xUTM+"   yUTM="+yUTM+"   huso="+huso);
                //if(huso!=30)
                utm30=cambioHuso(xUTM, yUTM,parseInt(huso), 30);
                //else{utm30[0]=xUTM;utm30[1]=yUTM;}
            }
            else{
                alert("seleccione busqueda por coords Geográficas o UTM");
                return;
            }
        }
        else{
            alert("seleccionar tipo busqueda");
        }
        //común para cualquier tipo de búsqueda
        //alert("utm30[0]="+utm30[0]+"    utm30[1]="+utm30[1]);
        var X1=parseFloat(utm30[0])-parseFloat(boxSize)/2;//X sup-left referido a la caja o visor
        var Y2=parseFloat(utm30[1])+parseFloat(boxSize)/2;//Y Sup-left
        var Y1=Y2-parseFloat(boxSize);//Y inf-right
        var X2=X1+parseFloat(boxSize);//X inf-right
        OverlayRect.SetRect(X1,Y2,X2,Y1, true);
        
        return;
    }
    
    function existeHoja(numHoja){
        var i;
        for(i=0;i<arrHoja.length;i=i+3){
            if(arrHoja[i]==numHoja)
                return true;            
        }
        return false;
    }
    function existeCCFF(cc,ff){
        var i,j;
        for(i=1;i<arrHoja.length;i=i+3){
            if(arrHoja[i]==cc){
                for(j=2;j<arrHoja.length;j=j+3){
                    if(arrHoja[j]==ff){
                        return true;
                    }            
                }                
            }
        }
        return false;
    }
    function getCCFFFromHoja(numHoja){
        var arrCCFF= new Array(2);
        var i;
        for(i=0;i<arrHoja.length;i=i+3){
            if(arrHoja[i]==numHoja){
                arrCCFF[0]=arrHoja[i+1];
                arrCCFF[1]=arrHoja[i+2];
                return arrCCFF;
            }    
        }
        return arrCCFF;
    }
    
    function pointerMode(event){
        if(bPointerMode)
            bPointerMode=false;
        else
            bPointerMode=true;
        bBoxMode=false;
        document.ECWView1.SetPointerMode(PM_POINTER);
        document.getElementById("imArea").src='img/toolBox.png';
        return;
    }
    function boxMode(event){
        document.ECWView1.DeleteLayer("pointerLayer");
        var visor=document.ECWView1;
        var isChecked50=document.getElementById("grid50").checked;
        var isChecked25=document.getElementById("grid25").checked;
        if(isChecked50){
            document.getElementById("grid50").checked=false;
             deleteGrid("H50");
        }
        else if(isChecked25){
            document.getElementById("grid25").checked=false;
            deleteGrid("H25");
        } 
        visor.SetPointerMode(PM_ZOOMBOX);
        document.getElementById("imPointer").src='img/toolPoint.png';
        bPointerMode=false;
        if(bBoxMode)
            bBoxMode=false;
        else 
            bBoxMode=true;
        return;
    }
    function slaveClick(buttonMask, screenx, screeny, worldx, worldy) { 
        if(bPointerMode){
            ECWUpdateOverview(screenx, screeny, worldx, worldy, true,false,iCajaWorldX, iCajaWorldY);//true->actualiza slave,false->no arrastra
            var visor=document.ECWView1;
            visor.DeleteLayer("pointerLayer");        
            iCajaWorldX=worldx; 
            iCajaWorldY=worldy;   
            var TLX,BRY,BRX,TLY;        
            var tamanioCaja=document.getElementById("tamanioCaja").value;
            var iBox=parseInt(tamanioCaja)-parseInt(tamanioCaja)*0.8; 
            if(iBox<0)  
                return;
            TLX = visor.GetTopLeftWorldCoordinateX(); 
            TLY = visor.GetTopLeftWorldCoordinateY(); 
            BRX = visor.GetBottomRightWorldCoordinateX(); 
            BRY = visor.GetBottomRightWorldCoordinateY();

            var X0,Y0,X1,Y1,X2,Y2;
            X0=(TLX+BRX)/2;//X medio
            Y0=(TLY+BRY)/2;//Y medio           
        
            X1=X0-iBox/2;//X sup-left referido a la caja
            Y1=Y0+iBox/2;//Y sup_left            
            X2=X1+iBox;//X inf-right
            Y2=Y1-iBox;//Y inf_right
            X1=parseInt(X1);
            X2=parseInt(X2);
            Y1=parseInt(Y1);
            Y2=parseInt(Y2);
            var sParams = "mode=0;"
            sParams = sParams + "body=;"; 
            sParams = sParams + "action=GET;"; 
            sParams = sParams + "worldTLX=" + parseInt(X1+1000) + ";"; 
            sParams = sParams + "worldTLY=" + parseInt(Y1-1000) + ";"; 
            sParams = sParams + "worldBRX=" + parseInt(X2-1000) + ";"; 
            sParams = sParams + "worldBRY=" + parseInt(Y2+1000) + ";"; 
            sParams = sParams + "url=http://wxpcnig060:8080/pnoa/imagenes/visor/location.png"; 
            
            visor.AddLayer( "GISOverlay", "","pointerLayer", sParams); 
            visor.SetLayerParameter("pointerLayer", sParams);
        } 
        else if(bBoxMode){
            bArrastra=true; 
            iCajaWorldX=worldx; 
            iCajaWorldY=worldy;
        }
	return;
    }
    
    function capaHoja(ViewWidth, ViewHeight){
        var bTodosOcultos=true;
	var dWorldTLX, dWorldTLY, dWorldBRX, dWorldBRY; 
	var xmin,ymin,xmax,ymax; 
	var dWorldX,dWorldY; 
	var XScale,YScale; 
	var DivX,DivY; 
	var DivHeight,DivWidth; 
	var elemento; 
	var ViewOffsetX = 0; 
	var ViewOffsetY = 0; 
	var CapaHTML; 
        var visor=document.ECWView1; 
        var posicion= new Array(); 
        var anclaje;
	dWorldTLX = parseInt(visor.GetTopLeftWorldCoordinateX()); 
	dWorldTLY = parseInt(visor.GetTopLeftWorldCoordinateY()); 
	dWorldBRX = parseInt(visor.GetBottomRightWorldCoordinateX()); 
	dWorldBRY = parseInt(visor.GetBottomRightWorldCoordinateY());

	XScale = ViewWidth / (dWorldBRX - dWorldTLX); 
	YScale = ViewHeight / (dWorldBRY- dWorldTLY); 
	    xmin=parseFloat(dWorldTLX); 
            ymin=parseFloat(dWorldBRY); 
            xmax=parseFloat(dWorldBRX ); 
            ymax=parseFloat(dWorldTLY);             
        
        
            CapaHTML = "<DIV class='capaHoja' id='CAPA_0'"; 
            CapaHTML += " OnMouseOver=overHoja('cod_0',0);"; 
            CapaHTML += " OnMouseOut=overHoja('cod_0',1);> "; 
            CapaHTML += "JJJ"+'<\/DIV>';
            Crea_Objeto(CapaHTML,"CAPA_0");
            DivWidth=Ancho_Div("CAPA_0"); 
            DivHeight=Alto_Div("CAPA_0"); 
            dWorldX = xmin + (xmax - xmin)/ 2.0; 
            dWorldY = ymin + (ymax - ymin)/ 2.0; 
            DivX =parseFloat((dWorldX- dWorldTLX) * XScale- (DivWidth / 2)); 
            DivY =parseFloat((dWorldY- dWorldTLY) * YScale- (DivHeight / 2)); 
            
            if(DivX < 0|| (DivX + DivWidth - 1) > ViewWidth || DivY < 0 || (DivY + DivHeight - 1) > ViewHeight) { 
               Ver_Objeto("CAPA_0","hidden");
            }
            else{
                elemento=new capturaObj("CAPA_0"); 
		if(Es.IE){
                    elemento.style.top =parseInt(findPosY(visor) + DivY); 
                    elemento.style.left =parseInt(findPosX(visor)+ DivX);
                }    
                else{
                anclaje=document.getElementById('divMapa'); 
                posicion=findPos(anclaje);        
                elemento.style.top =DivY; 
		elemento.style.left =DivX;
                }    
                Ver_Objeto("CAPA_0","visible");                
		bTodosOcultos=false;
            }            
        }
    
    function overHoja(codigo,opcion){
        var dWorldTLX, dWorldTLY, dWorldBRX, dWorldBRY; 
	var sParams = ""; 
	var sFillColor="#CCCCCC"; 
	var sPenColor="#FF0000"; 
	var iLineWidth=3.0; 
	var bComenzar=true; 
	var visor=document.ECWView1; 
        if(visor.GetLayerIndex("VectorOverlay")>0)
            bComenzar=false; 
	if(opcion!=1){ 
            dWorldTLX=visor.GetLayerImageTopLeftWorldCoordinateX("layer1"); 
            dWorldTLY=visor.GetLayerImageTopLeftWorldCoordinateY("layer1"); 
            dWorldBRX=visor.GetLayerImageBottomRightWorldCoordinateX("layer1"); 
            dWorldBRY=visor.GetLayerImageBottomRightWorldCoordinateY("layer1");         

            sParams+="fillcolor="+sFillColor+";"; 
            sParams+="color="+sPenColor+";"; 
            sParams+="linewidth="+iLineWidth+";"; 
            sParams+="filledpolygon1="; 
            sParams+=dWorldTLX+","+dWorldTLY+"|"; 
            sParams+=dWorldBRX+","+dWorldTLY+"|"; 
            sParams+=dWorldBRX+","+dWorldBRY+"|";
            sParams+=dWorldTLX+","+dWorldBRY+";";
            if(bComenzar)
                visor.AddLayer("simplevector", "", "VectorOverlay", ";");
            visor.SetLayerParameter("VectorOverlay", sParams); 
            visor.SetLayerTransparency("VectorOverlay", sFillColor, 0.38); 
            //if(Es.IE){ 
                var DivWidthAntes=Ancho_Div("CAPA_0")/4; 
		Escribe_Objeto('CAPA_0',"jotaCapa"); 
		var DivWidth=Ancho_Div("CAPA_0")/4; 
		elemento=new capturaObj("CAPA_0"); 
		var iPosicion=parseInt(elemento.style.left); 
		elemento.style.left=(iPosicion+DivWidthAntes)-DivWidth; 
		Cambia_Zindex('CAPA_0',"90");
            //}
	}
	else if(opcion==1){ 
            //if(Es.IE){ 
                var DivWidthAntes=Ancho_Div("CAPA_0")/2; 
		Escribe_Objeto("CAPA_0","etiquetaOFF"); 
		var DivWidth=Ancho_Div("CAPA_0")/2; 
		elemento=new capturaObj("CAPA_0"); 
		var iPosicion=parseInt(elemento.style.left); 
		elemento.style.left=(iPosicion+DivWidthAntes)-DivWidth; 
		Cambia_Zindex('CAPA_0',"80");
            //}
	}
    }
    
    function activaBusqAvanzada(){
        document.getElementById("blockBusqHoja").style.display="block";
        document.getElementById("blockBusqCoord").style.display="block";
        document.getElementById("blockBusqNomen").style.display="none";
        return;
    }
    function activaBusqNomen(){
        document.getElementById("nomenclator").value="";
        document.getElementById("blockBusqHoja").style.display="none";
        document.getElementById("blockBusqCoord").style.display="none";
        document.getElementById("blockBusqNomen").style.display="block";
        document.getElementById("divOpcionesCoords").style.display="none";
        document.getElementById("divOpcionesHoja").style.display="none";        
        return;
    }
    function activaBusqHoja(){
        document.getElementById("divOpcionesCoords").style.display="none";
        document.getElementById("divOpcionesHoja").style.display="block";        
        return;
    }
    function activaBusqCoord(){
        document.getElementById("divOpcionesCoords").style.display="block";
        document.getElementById("divOpcionesHoja").style.display="none";        
        return;
    }
    
    function limpiaCoordsGeo(){
        document.getElementById("textGradLon").value="";
        document.getElementById("textMinLon").value="";
        document.getElementById("textSegLon").value="";        
        document.getElementById("textGradLat").value="";
        document.getElementById("textMinLat").value="";
        document.getElementById("textSegLat").value="";
        return;
    }
    function limpiaCoordsUTM(){
        document.getElementById("selectHuso").selectedIndex="0";//.options[document.getElementById("selectHuso").options.selectedIndex].value="0";
        document.getElementById("txtX").value="";
        document.getElementById("txtY").value="";
        return;
    }    
    
    function checkOutToolBox(obj){
        if(!bBoxMode)
            obj.src='img/toolBox.png';
        return;
    }
    function checkOutToolPoint(obj){
        if(!bPointerMode)
            obj.src='img/toolPoint.png';
        return;
    }
    function checkOutMasterOnOff(obj){
        if(!bMasterOnOff)
            obj.src='img/MasterOnOff.png';
        return;
    }
        
