//+++++++++++++++++++++++++++Funciones de coordendas

var pi=3.14159265358979;var sm_a=6378137.0;var sm_b=6356752.314;var sm_EccSquared=6.69437999013e-03;var UTMScaleFactor=0.9996;var i=0;function FootpointLatitude(y)
{var y_,alpha_,beta_,gamma_,delta_,epsilon_,n;var result;n=(sm_a-sm_b)/(sm_a+sm_b);alpha_=((sm_a+sm_b)/2.0)*(1+(Math.pow(n,2.0)/4)+(Math.pow(n,4.0)/64));y_=y/alpha_;beta_=(3.0*n/2.0)+(-27.0*Math.pow(n,3.0)/32.0)
+(269.0*Math.pow(n,5.0)/512.0);gamma_=(21.0*Math.pow(n,2.0)/16.0)
+(-55.0*Math.pow(n,4.0)/32.0);delta_=(151.0*Math.pow(n,3.0)/96.0)
+(-417.0*Math.pow(n,5.0)/128.0);epsilon_=(1097.0*Math.pow(n,4.0)/512.0);result=y_+(beta_*Math.sin(2.0*y_))
+(gamma_*Math.sin(4.0*y_))
+(delta_*Math.sin(6.0*y_))
+(epsilon_*Math.sin(8.0*y_));return result;}
function MapXYToLatLon(x,y,lambda0,philambda)
{var phif,Nf,Nfpow,nuf2,ep2,tf,tf2,tf4,cf;var x1frac,x2frac,x3frac,x4frac,x5frac,x6frac,x7frac,x8frac;var x2poly,x3poly,x4poly,x5poly,x6poly,x7poly,x8poly;phif=FootpointLatitude(y);ep2=(Math.pow(sm_a,2.0)-Math.pow(sm_b,2.0))/Math.pow(sm_b,2.0);cf=Math.cos(phif);nuf2=ep2*Math.pow(cf,2.0);Nf=Math.pow(sm_a,2.0)/(sm_b*Math.sqrt(1+nuf2));Nfpow=Nf;tf=Math.tan(phif);tf2=tf*tf;tf4=tf2*tf2;x1frac=1.0/(Nfpow*cf);Nfpow*=Nf;x2frac=tf/(2.0*Nfpow);Nfpow*=Nf;x3frac=1.0/(6.0*Nfpow*cf);Nfpow*=Nf;x4frac=tf/(24.0*Nfpow);Nfpow*=Nf;x5frac=1.0/(120.0*Nfpow*cf);Nfpow*=Nf;x6frac=tf/(720.0*Nfpow);Nfpow*=Nf;x7frac=1.0/(5040.0*Nfpow*cf);Nfpow*=Nf;x8frac=tf/(40320.0*Nfpow);x2poly=-1.0-nuf2;x3poly=-1.0-2*tf2-nuf2;x4poly=5.0+3.0*tf2+6.0*nuf2-6.0*tf2*nuf2
-3.0*(nuf2*nuf2)-9.0*tf2*(nuf2*nuf2);x5poly=5.0+28.0*tf2+24.0*tf4+6.0*nuf2+8.0*tf2*nuf2;x6poly=-61.0-90.0*tf2-45.0*tf4-107.0*nuf2
+162.0*tf2*nuf2;x7poly=-61.0-662.0*tf2-1320.0*tf4-720.0*(tf4*tf2);x8poly=1385.0+3633.0*tf2+4095.0*tf4+1575*(tf4*tf2);philambda[0]=phif+x2frac*x2poly*(x*x)
+x4frac*x4poly*Math.pow(x,4.0)
+x6frac*x6poly*Math.pow(x,6.0)
+x8frac*x8poly*Math.pow(x,8.0);philambda[1]=lambda0+x1frac*x
+x3frac*x3poly*Math.pow(x,3.0)
+x5frac*x5poly*Math.pow(x,5.0)
+x7frac*x7poly*Math.pow(x,7.0);return;}
function UTMCentralMeridian(zone)
{var cmeridian;cmeridian=DegToRad(-183.0+(zone*6.0));return cmeridian;}
function UTMXYToLatLon(x,y,zone,southhemi,latlon)
{var cmeridian;x-=500000.0;x/=UTMScaleFactor;if(southhemi)
y-=10000000.0;y/=UTMScaleFactor;cmeridian=UTMCentralMeridian(zone);MapXYToLatLon(x,y,cmeridian,latlon);return;}
function DegToRad(deg)
{return(deg/180.0*pi)}
function RadToDeg(rad)
{return(rad/pi*180.0)}
function ArcLengthOfMeridian(phi)
{var alpha,beta,gamma,delta,epsilon,n;var result;n=(sm_a-sm_b)/(sm_a+sm_b);alpha=((sm_a+sm_b)/2.0)*(1.0+(Math.pow(n,2.0)/4.0)+(Math.pow(n,4.0)/64.0));beta=(-3.0*n/2.0)+(9.0*Math.pow(n,3.0)/16.0)
+(-3.0*Math.pow(n,5.0)/32.0);gamma=(15.0*Math.pow(n,2.0)/16.0)
+(-15.0*Math.pow(n,4.0)/32.0);delta=(-35.0*Math.pow(n,3.0)/48.0)
+(105.0*Math.pow(n,5.0)/256.0);epsilon=(315.0*Math.pow(n,4.0)/512.0);result=alpha*(phi+(beta*Math.sin(2.0*phi))
+(gamma*Math.sin(4.0*phi))
+(delta*Math.sin(6.0*phi))
+(epsilon*Math.sin(8.0*phi)));return result;}
function MapLatLonToXY(phi,lambda,lambda0,xy)
{var N,nu2,ep2,t,t2,l;var l3coef,l4coef,l5coef,l6coef,l7coef,l8coef;var tmp;ep2=(Math.pow(sm_a,2.0)-Math.pow(sm_b,2.0))/Math.pow(sm_b,2.0);nu2=ep2*Math.pow(Math.cos(phi),2.0);N=Math.pow(sm_a,2.0)/(sm_b*Math.sqrt(1+nu2));t=Math.tan(phi);t2=t*t;tmp=(t2*t2*t2)-Math.pow(t,6.0);l=lambda-lambda0;l3coef=1.0-t2+nu2;l4coef=5.0-t2+9*nu2+4.0*(nu2*nu2);l5coef=5.0-18.0*t2+(t2*t2)+14.0*nu2
-58.0*t2*nu2;l6coef=61.0-58.0*t2+(t2*t2)+270.0*nu2
-330.0*t2*nu2;l7coef=61.0-479.0*t2+179.0*(t2*t2)-(t2*t2*t2);l8coef=1385.0-3111.0*t2+543.0*(t2*t2)-(t2*t2*t2);xy[0]=N*Math.cos(phi)*l
+(N/6.0*Math.pow(Math.cos(phi),3.0)*l3coef*Math.pow(l,3.0))
+(N/120.0*Math.pow(Math.cos(phi),5.0)*l5coef*Math.pow(l,5.0))
+(N/5040.0*Math.pow(Math.cos(phi),7.0)*l7coef*Math.pow(l,7.0));xy[1]=ArcLengthOfMeridian(phi)
+(t/2.0*N*Math.pow(Math.cos(phi),2.0)*Math.pow(l,2.0))
+(t/24.0*N*Math.pow(Math.cos(phi),4.0)*l4coef*Math.pow(l,4.0))
+(t/720.0*N*Math.pow(Math.cos(phi),6.0)*l6coef*Math.pow(l,6.0))
+(t/40320.0*N*Math.pow(Math.cos(phi),8.0)*l8coef*Math.pow(l,8.0));return;}
function LatLonToUTMXY(lat,lon,zone,xy)
{MapLatLonToXY(lat,lon,UTMCentralMeridian(zone),xy);xy[0]=xy[0]*UTMScaleFactor+500000.0;xy[1]=xy[1]*UTMScaleFactor;if(xy[1]<0.0)
xy[1]=xy[1]+10000000.0;return zone;}
var _screenWidthCm=32.0;var _screenSize=1024.0;function getMetersByPixel(){if(getWidth()>0){extentWidth=top.getMap().getCurrentExtent().getXMax()-top.getMap().getCurrentExtent().getXMin();return(getDistanceInMeters(extentWidth)/getWidth());}
else return 0.0;}
function getMetersByPixel2(realWidth,pixelWidth){if(getWidth()>0){return(getDistanceInMeters(realWidth)/pixelWidth);}
else return 0.0;}
function getScale2(realWidth,pixelWidth){metersPerPixel=getMetersByPixel2(realWidth,pixelWidth);centimetersPerPixelMap=metersPerPixel*100;centimetersPerPixelDevice=_screenWidthCm/_screenSize;scale=centimetersPerPixelMap/centimetersPerPixelDevice;scale=scale*(1-0.11417275);scale=Math.round(scale);return scale;}
function getWidth(){return parseInt(map.mapWidthPX);}
function getHeight(){return parseInt(map.mapHeightPX);}
function getScale(){extentWidth=map.cBounds[2]-map.cBounds[0];var num=parseInt(getScale2(extentWidth,getWidth()));var nNmb=num.toString();var sRes="";for(var j,i=nNmb.length-1,j=0;i>=0;i--,j++){sRes=nNmb.charAt(i)+((j>0)&&(j%3==0)?".":"")+sRes;}
return sRes;}
function getAmplada(){extentWidth=map.cBounds[2]-map.cBounds[0];return extentWidth}

function getDistanceInMeters(distance){return distance;}

//++++++++++++++++++++++++

//tiles
var NUMTILE = 1;
var numeroTalls=4;
image = null;
var BBOXT = new Array();
var srvTiles = new Array();
/*******************************************/
//Especificar Servidores sin tiles
srvTiles[0] = "onearth.jpl.nasa.gov";
srvTiles[1] = "www.geoportaligm.gov.ec";
srvTiles[2] = "onearth.jpl.nasa.gov";

/*******************************************/

function crearMapa(mapa){
//2 dic 2007

var nombreTitW = 2;
var nombreTitH = 2;

	nombreTitW = calculaNumTiles(mapa.mapWidthPX);
	
	nombreTitH = calculaNumTiles(mapa.mapHeightPX);
	var container = mapa.gid + "_container";
	var h = document.getElementById(container);
	if (h.hasChildNodes()){
		for (var i = 0; i < h.childNodes.length; i++){
			var divT = h.childNodes[i];
			var idDiv = divT.id;
			var nombreDiv = "div_"+mapa.prefix;
			if (idDiv != null){
				if (idDiv.indexOf(nombreDiv) != -1){
					var pos = idDiv.lastIndexOf("_");
					var ind = idDiv.substring(pos+1);
					
					var surl = formaUrl(mapa.layers[parseInt(ind)],ind,mapa);
					 
					if (surl.indexOf(".gif") == -1){
                    
						//crearGrid(idDiv, surl, mapa.cBounds, 4);
						
						var esta = 0;
						if (mapa.layers[parseInt(ind)].tiles == false || mapa.layers[parseInt(ind)].tiles == "false"){
							esta = 1;
						}
						for (var s = 0; s < srvTiles.length; s++){
							var srvTil = srvTiles[s];
                           
							if (surl.indexOf(srvTil) != -1){
                            
								esta = 1;
								break;
							}
						}
						if (esta == 1){
							crearGrid(idDiv, surl, mapa.cBounds, 1, 1);
						}
						else{
						crearGrid(idDiv, surl, mapa.cBounds, nombreTitW, nombreTitH);
							//crearGrid(idDiv, surl, mapa.cBounds, 1, 1);
						}
						
					}
				}
			}
            
		}
	}
    
    
}

function zoomMapa(mapa, activa){
	if (activa != 1){
		limpiarMapa(mapa);
		crearMapa(mapa);
	}
	else{
		for(i = 0; i < mapa.servidores.length; i++){
			var ncapas = 0;
			var activo = false; 
			var posSrv = mapa.servidores[i][0];
			if (mapa.servidores[i][3] == true || mapa.servidores[i][3]=='true'){
				activo = true;
			}
			for(j = i+1; j < mapa.servidores.length; j++){
				var posSrv1 = mapa.servidores[j][0];
				if (posSrv == posSrv1){
					ncapas++;
					if (mapa.servidores[j][3] == true || mapa.servidores[j][3]=='true'){
						activo = true;
					}
				}else{
					break;
				}
			}
			if (activo){
				var posCapa = mapa.servidores[i][0];
                
				var nombreDiv = "div_"+mapa.prefix+posCapa;
				var capa = document.getElementById(nombreDiv);
				//limpiarCapa(capa);
				var ind = posCapa;
				var idDiv = nombreDiv;
				var nombreTitW = calculaNumTiles(mapa.mapWidthPX);
				var nombreTitH = calculaNumTiles(mapa.mapHeightPX);
				var surl = formaUrl(mapa.layers[parseInt(ind)],ind,mapa);
				if (capa.hasChildNodes()){
					var divT = capa.childNodes[0];
					if (divT.style.backgroundImage.indexOf(surl) != -1){
					
					}else{
						limpiarCapa(capa);
						if (surl.indexOf(".gif") == -1){
							//crearGrid(idDiv, surl, mapa.cBounds, 4);
							var esta = 0;
							if (mapa.layers[parseInt(ind)].tiles == false || mapa.layers[parseInt(ind)].tiles == "false"){
								esta = 1;
							}
							for (var s = 0; s < srvTiles.length; s++){
                           
								var srvTil = srvTiles[s];
								if (surl.indexOf(srvTil) != -1){
									esta = 1;
									break;
								}
							}
							if (editor){
								crearGrid(idDiv, surl, mapa.cBounds, 1, 1);
							}else{
                            
								if (esta == 1){
                               
									crearGrid(idDiv, surl, mapa.cBounds, 1, 1);
									//crearGrid(idDiv, surl, mapa.cBounds, nombreTitW, nombreTitH);
								}
								else{
                               
									crearGrid(idDiv, surl, mapa.cBounds, nombreTitW, nombreTitH);
									//crearGrid(idDiv, surl, mapa.cBounds, 1, 1);
								}
							}
						}
					}
				}else{ 
					if (surl.indexOf(".gif") == -1){
						//crearGrid(idDiv, surl, mapa.cBounds, 4);
						var esta = 0;
						if (mapa.layers[parseInt(ind)].tiles == false || mapa.layers[parseInt(ind)].tiles == "false"){
							esta = 1;
						}
						for (var s = 0; s < srvTiles.length; s++){
							var srvTil = srvTiles[s];
							if (surl.indexOf(srvTil) != -1){
								esta = 1;
								break;
							}
						}
						if (editor){
							crearGrid(idDiv, surl, mapa.cBounds, 1, 1);
						}else{
							if (esta == 1){
								crearGrid(idDiv, surl, mapa.cBounds, 1, 1);
								//crearGrid(idDiv, surl, mapa.cBounds, nombreTitW, nombreTitH);
							}
							else{
								crearGrid(idDiv, surl, mapa.cBounds, nombreTitW, nombreTitH);
								//crearGrid(idDiv, surl, mapa.cBounds, 1, 1);
							}
						}
					}
				}
			}else{
				var posCapa = mapa.servidores[i][0];
				var nombreDiv = "div_"+mapa.prefix+posCapa;
				var capa = document.getElementById(nombreDiv);
				limpiarCapa(capa);
			}
			i = i + ncapas;		
			//alert(posSrv + " CAPAS: " + ncapas);
		}
	}
}

function limpiarMapa(mapa){
	var container = mapa.gid + "_container";
    
	var h = document.getElementById(container);
    
	if (h.hasChildNodes()){
		
		for (var i = 0; i < h.childNodes.length; i++){
			var divT = h.childNodes[i];
			//alert(divT.innerHTML);
			var idDiv = divT.id;
			if (idDiv != null){
				var nombreDiv = "div_"+mapa.prefix;
				//alert(idDiv);
				if (idDiv.indexOf(nombreDiv) != -1){
            
					limpiarCapa(divT);
				}
			}
		}
	}
}

function limpiarCapa(capa){
	if (capa.hasChildNodes()){
		var id = capa.id;
  		id = id.substring(id.lastIndexOf("_")+1);
  		var pos = parseInt(id);
  		var img_div = "load_"+pos;
  		var err_div = "error_"+pos;
  		//alert(document.getElementsByName(img_div).length);
  		if (document.getElementsByName(err_div).length > 0){
  			var cap_lay = document.getElementsByName(err_div);
  			for (w = 0; w < cap_lay.length; w++){
  				var capa_lay = cap_lay[w];
  				capa_lay.style.visibility = "hidden";
                
  			}
  		}
  		if (document.getElementsByName(img_div).length > 0){
  			var cap_lay = document.getElementsByName(img_div);
  			//alert(cap_lay.length);
  			for (w = 0; w < cap_lay.length; w++){
  				var capa_lay = cap_lay[w];
  				var id_Cap = capa_lay.id;
  				var id_ck = id_Cap.substring(5);
  				if (document.getElementById(id_ck).checked){
  					capa_lay.style.visibility = "visible";
                     
  				}
  			}
  			//document.getElementsByName(img_div).style.visibility = "visible";
		}
  		//if (document.getElementById(img_div)){
		//	document.getElementById(img_div).style.visibility = "visible";
		//}
		while(capa.hasChildNodes()){
			capa.removeChild(capa.firstChild);
		}
	}
}

function crearGrid(div, url, bbox, tilesNumW, tilesNumH){
	//var im = document.createElement("img");
	//alert(div);
	var h = document.getElementById(div);
  	//while (h.firstChild)
    //h.removeChild(h.firstChild);
  	//h.appendChild(im);
	agregarTiles(h, url, bbox, tilesNumW, tilesNumH);
}

function agregarTiles(div, url, bbox, tilesNumW, tilesNumH){
	var NUMTILEW = parseInt(tilesNumW);
	var NUMTILEH = parseInt(tilesNumH);
	var NUMTILE = NUMTILEW;
	var width;
	var height;
	var tileWidth;
	var tileHeight;
	width = parseInt(div.style.width); // or naturalWidth, or computed width...
	height = parseInt(div.style.height);
	
	if (NUMTILEW == 1){
		tileWidth = width;
		tileHeight = height;
		
		
		if (width > height){
			tileWidth = width;
			tileHeight = width;
		}else{
			tileWidth = height;
			tileHeight = height;
		}
		
		
	}else{
		tileWidth = 256;
		tileHeight = 256;
	}
		
	var urlT = url+"WIDTH="+tileWidth+"&HEIGHT="+tileHeight+"&BBOX=";
	//alert(urlT);
	var tiles = new Array;
	var grid = new Array;
	for (var g=0; g<NUMTILEW; g++){
		tiles[g] = [];
		grid[g] = [];
	}
	
	var theEmptyT, emptyp, emptyq;
	for (var i=0; i<NUMTILEW; ++i)
	 for (var j=0; j<NUMTILEH; ++j)
	 {
	  var t = document.createElement("div");
	  var x = i * tileWidth;
	  var y = j * tileHeight;
	  t.style.position = "absolute";
	  t.style.width = tileWidth + "px";
	  t.style.height = tileHeight + "px";
	if (NUMTILEW == 1){
		//14 feb 08
		
		var dx = bbox[2] - bbox[0];
		var dy = bbox[3] - bbox[1];
		var pixelsW = dx / width;
		var intx = pixelsW * tileWidth;
		var inty = intx;
		//alert(intx + "  " + inty);
		BBOXT[0] = bbox[0] + (intx*i);
		BBOXT[1] = bbox[3] - (inty*(j+1));
		BBOXT[2] = bbox[0] + (intx*(i+1));
		BBOXT[3] = bbox[3] - (inty*j);
		
	}else{
		var dx = bbox[2] - bbox[0];
		var dy = bbox[3] - bbox[1];
		//3 dic 2007
		//var intx = dx/NUMTILE;
		//var inty = dy/NUMTILE;
		var pixelsW = dx / width;
		var intx = pixelsW * tileWidth;
		var inty = intx;
		//alert(intx + "  " + inty);
		BBOXT[0] = bbox[0] + (intx*i);
		BBOXT[1] = bbox[3] - (inty*(j+1));
		BBOXT[2] = bbox[0] + (intx*(i+1));
		BBOXT[3] = bbox[3] - (inty*j);
	}	
			
	
	
	/**********************************************/
	/**********************************************/
	/**********************************************/
	/**********************************************/
	/**********************************************/
		
	var urlTT = urlT + BBOXT;
	
	if ((i==(NUMTILEW-1)) && (j==(NUMTILEH-1))){
		var img = document.createElement("img");
		img.src = urlTT;
        
         var id = div.id;
			id = id.substring(id.lastIndexOf("_")+1);
			var pos = parseInt(id);
         map.layers[pos].timeI=new Date().getTime();
		img.width = 1;
		img.onerror = function (evt) {
			var id = div.id;
			id = id.substring(id.lastIndexOf("_")+1);
			var pos = parseInt(id);
			var img_div = "error_"+pos;
			var cap_lay = document.getElementsByName(img_div);
             map.layers[pos].timeF=0;
			for (w = 0; w < cap_lay.length; w++){
				var capa_lay = cap_lay[w];
				var id_Cap = capa_lay.id;
				var id_ck = id_Cap.substring(6);
				if (document.getElementById(id_ck).checked){
					capa_lay.style.visibility = "visible";
				}
			}
            
			img.src = "images/blank.gif";
		}
		img.onload = function (evt) {
			img.width = tileWidth;
			var id = div.id;
			id = id.substring(id.lastIndexOf("_")+1);
			var pos = parseInt(id);
			var img_div = "load_"+pos;
			var cap_lay = document.getElementsByName(img_div);
             map.layers[pos].timeF=new Date().getTime();
          
			for (w = 0; w < cap_lay.length; w++){
				var capa_lay = cap_lay[w];
				capa_lay.style.visibility = "hidden";
               
			}
		}
		t.appendChild(img);
	}else{
		t.style.backgroundImage = "url(" + urlTT + ")";
	  	t.style.backgroundAttachment = "scroll";
	  	t.style.backgroundRepeat = "no-repeat";
	}
	
	/**********************************************/
	/**********************************************/
	/**********************************************/
	/**********************************************/
	/**********************************************/
	  
	  /*Antes*/
	  //t.style.backgroundImage = "url(" + urlTT + ")";
	  //t.style.backgroundAttachment = "scroll";
	  //t.style.backgroundRepeat = "no-repeat";
	  /*fin antes*/
	  
	  //t.style.backgroundPosition = "-" + x + "px -" + y + "px";
	  // setting using shorthand works in ie but not in moz:
	  // t.style.background = "url(" + im.src + ") scroll -" + x + " -" + y + " no-repeat";
	  //t.className = "tile";
	  
	  // i,j is original position. used to determine when you've won.
	  t.i = i;
	  t.j = j;
	  // p,q is current position. initially set by repositionAll();
	  grid[i][j] = t;
	  div.appendChild(t);
	 } // huge for loop to create tiles

     
     
	function gradual(ob, prop, start, fin)
	{
		var n = NUMTILE;
		var step = (fin - start) / n;
		ob[prop] = start;
		for (var i=1; i<=n; ++i)
			(function(){
				var I = i;
				setTimeout(function() { ob[prop] = (start + step*I) + "px"; }, 30*i)
			})()
	}

	function repositionAll()
	{
	  var correct = 0, i, j;
	  for (i=0; i<NUMTILEW; ++i)
	   for (j=0; j<NUMTILEH; ++j)
	   {
	     t = grid[i][j];
	     if (t.p != i)
	     {
	       if (t.p != null && !t.isEmpty)
	         gradual(t.style, "left", tileWidth * t.p, tileWidth * i)
	       else
	         t.style.left = tileWidth * i + "px";
	       t.p = i;
	     }
	     if (t.q != j)
	     {
	       if (t.q != null && !t.isEmpty)
	         gradual(t.style, "top", tileHeight * t.q, tileHeight * j)
	       else  
	         t.style.top = tileHeight * j + "px";
	       t.q = j;
	     }
	     if (t.isEmpty)
	     {
	       emptyp = i;
	       emptyq = j;
	     }
	     if (t.i == i && t.j == j)
	       ++correct;
	   }
	   //game.style.borderColor = (correct == (NUMTILE*NUMTILE)) ? "lightgreen" : "black";
	}
	repositionAll();
	//var par = div.parentNode;
	//par.insertBefore(game, div);
	//par.removeChild(div);
}

function formaUrl(aLayer,k, mapa) {
	//alert(k);
  var sURL = aLayer.url;
  var llarg=sURL.length;
  var ultim=sURL.substring(llarg-1,llarg);
  if(ultim != "?"){
  	if(ultim != "&"){
  		sURL=sURL+"&";
  	}
  }
  var nom = "";    
  for (var i=0; i<mapa.servidores.length; i++) {
	if (mapa.servidores[i][0] == k) {
		if (mapa.servidores[i][3] == true){
	  		if (nom == ""){
		  		nom = mapa.servidores[i][1];
		  	}
		  	else{
		  		nom = nom + "," + mapa.servidores[i][1];
		  	}
	  	}	
	}
  }
  
  if (nom != "") {	
  
  	//if(sURL.indexOf('?')!=-1){sURL += "REQUEST=GetMap";}else{sURL += "?REQUEST=GetMap";}
  		
  		if(sURL.indexOf('com.esri')!=-1){sURL=sURL.replace('esrimap','Esrimap');sURL += "&REASPECT=false&";}
  
	sURL += "REQUEST=GetMap";
	sURL += "&SERVICE=WMS";
	sURL += "&SRS=" + aLayer.epsg;
	sURL += "&VERSION=" + aLayer.version;
	sURL += "&STYLES=";
	if (aLayer.transparent == true){
		sURL += "&TRANSPARENT=TRUE";
	}
	else{
		sURL += "&TRANSPARENT=FALSE";
	}
	nom=nom.replace(/ /g,"%20");
	sURL += "&LAYERS=" + nom;
	
	//14 nov estaba comentado pero no se ve el del ING
	if (sURL.indexOf("shagrat.icc.es") != -1){
		sURL += ",&BGCOLOR=0xFFFFFF";
	}
	else{
		sURL += "&BGCOLOR=0xFFFFFF";
	}
   sURL += "&FORMAT=" + aLayer.format;
   sURL += "&";
   
   return sURL;
  }
  else { 
	sURL = mapa.baseDir + "blanco.gif";
	
    
    return sURL;
    
  }
}

//Funcions de zoom imatge i ratolí
var imgGlob="";
function canviaSize(){
 for (var i=0; i < map.layers.length; i++) {
	    
	    if (document.all) {
	      imgGlob = document.images[map.prefix+i];
	      
	    }
	    else {
	      imgGlob = ogcmap_findImage(map.prefix+i,document);
	    }
	    if(imgGlob.src.indexOf("blank.gif")==-1){
  changer();
  }
 }
 }
function handle(delta) {


        if (delta < 0){	
        
        	
		 map.zoomOut(position[0],position[1]);
        }else{
        
        //canviaSize();
       
		map.zoom(position[0],position[1]);
		}
}
/** This is high-level function.
 * It must react to delta being more/less than zero.
 */

/** Event handler for mouse wheel event.
 */
function wheel(event){

        var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;
        }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta)
                handle(delta);
        /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
        if (event.preventDefault)
                event.preventDefault();
	event.returnValue = false;
}

/** Initialization code. 
 * If you use your own event management code, change it as required.
 */
if (window.addEventListener)
        /** DOMMouseScroll is for mozilla. */
        window.addEventListener('DOMMouseScroll', wheel, false);
/** IE/Opera. */
window.onmousewheel = document.onmousewheel = wheel;

//obtencio de GML

function calculaAmpladaMapa(factor){
var ampla=(map.cBounds[2]-map.cBounds[0]);

if(ampla <= factor){
	return true;
	}else{
	return false;
	} 
}


//2 dic 2007
function calculaNumTiles(ancho){
	var numT = 0;
	ancho = parseInt(ancho);
	var num = ancho / 256;
	numT = Math.ceil(num);
	return numT;
}

function activaNoOGC(obj){



	if (obj.checked){
		var latlon = new Array(2);
		var latlonMax= new Array(2);
		UTMXYToLatLon (map.cBounds[0], map.cBounds[1], 31, false, latlon);
		var xmin = RadToDeg (latlon[1]);
		var ymin = RadToDeg (latlon[0]);
		UTMXYToLatLon (map.cBounds[2], map.cBounds[3], 31, false, latlonMax);    
		var xmax = RadToDeg (latlonMax[1]);
		var ymax = RadToDeg (latlonMax[0]);
		
		
		
		document.getElementById('OLdiv').style.width='200px';
		
		if(document.all){
		document.getElementById('OLdiv').style.top='150px';
		}else{
		document.getElementById('OLdiv').style.top='150px';
		}
		document.getElementById('OLdiv').style.left='10px';
		document.getElementById('OLdiv').style.color='#000000';
		//document.getElementById('OLdiv').style.zIndex='10000';
		
		document.getElementById('OLdiv').style.fontFamily='Arial, Helvetica, sans-serif'; 
		document.getElementById('OLdiv').style.fontSize='7pt';
		var lonLat = lonLatToMercator(new OpenLayers.LonLat(xmin, ymax));
		var lonLat1 = lonLatToMercator(new OpenLayers.LonLat(xmax, ymin));         
        map1.zoomToExtent(new OpenLayers.Bounds(lonLat.lon,lonLat.lat,lonLat1.lon,lonLat1.lat));
		//map1.zoomToExtent(new OpenLayers.Bounds(xmin,ymin,xmax,ymax));
		document.getElementById('OLdiv').style.visibility = "visible";
		document.getElementById('OLdiv').style.display = "block";
		document.getElementById('mapOpen').style.display = "block";
	}else{
		document.getElementById('OLdiv').style.visibility = "hidden";
		document.getElementById('OLdiv').style.display = "none";
		document.getElementById('mapOpen').style.display = "none";
	}
}

function pausecomp(millis){
	var date = new Date();
	var curDate = null;
	do { curDate = new Date(); } 
	while(curDate-date < millis);
}

function onInputClick(e){
	alert(this.inputElem);
	Event.stop(e);
}

