// JavaScript Document
function numbersonly(myfield, e, dec)
{
	var key;
	var keychar;
	
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	
	// control keys
	if ((key==null) || (key==0) || (key==8) || 
		(key==9) || (key==13) || (key==27) )
	   return true;
	
	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
	   return true;
	
	// decimal point jump
	else if (dec && (keychar == "."))
	   {
	   myfield.form.elements[dec].focus();
	   return false;
	   }
	else
	   return false;
}

//


// MOUSE POSITION
function CheckmapClick(lang) 
{
	str = location.search;
	if (str != "") 
	{
		commaloc = str.indexOf(",");
		
		x = parseInt(str.substring(1, commaloc));
		y = parseInt(str.substring(commaloc+1, str.length));
	 
	  destUrl = "terraflyer.html?initCoordX=" + getX(x) + "&initCoordY=" + getY(y) + "&initHeading=0" + "&langId=" + lang ;
	  window.location = destUrl;
	}
}

function getX(mouseLocX) 
{
	var imgwidth = 392;
	var aoileft = 1544436;
	var aoiright = 1783183;
	
	var xPos = aoileft + mouseLocX * (aoiright - aoileft)/imgwidth;
	return xPos;
}

function getY(mouseLocY) 
{
	var imgheight = 422;
	var aoitop = 4931603;
	var aoibottom = 4675148;
	
	var yPos = aoitop - mouseLocY * (aoitop - aoibottom)/imgheight;
	return yPos;
}
   
function ShowOnMap()
{
	layerElem = document.getElementById("Layer1");
	if (layerElem)
	{
		alert("pix");
	}
}

function selectComune(lang)
{
	theData = document.getElementById("combo").value;
	coords = theData.split(" ");
	if(coords[0]!="--")
	{
		destUrl = "terraflyer.html?initCoordX=" + coords[0] + "&initCoordY=" + coords[1] + "&initHeading=0" + "&langId=" + lang;
		//alert(destUrl);
 	 	window.location = destUrl;
	}
}

function UpdateCoordField( destId ) 
{
	
	if(destId == "x")
	{
	    val =  parseFloat((document.getElementById("lon").value)) + (parseFloat(document.getElementById("lon_dec").value)/60.0); 
		newVal = parseInt(LatLongToXY(val, 0));	
		
		if(newVal>1400000 && newVal<1900000)
		{
			document.getElementById("x").value = newVal;
		}
	}
	else if(destId == "y")
	{
		val = parseFloat(document.getElementById("lat").value) + parseFloat(document.getElementById("lat_dec").value)/60.0;
		newVal = parseInt(LatLongToXY(val, 1));	
		
		if(newVal>4000000 && newVal<5200000)
		{
			document.getElementById("y").value = newVal;
		}		
	}
	else if(destId == "lon")
	{
		
		val = parseInt(document.getElementById("x").value);
		val = XYToLatLong(val, 0);
		
		gVal = parseInt(val);		
		pVal = parseInt((val - gVal)* 60);
		if(gVal>=8 && gVal<=14)
		{
			document.getElementById("lon").value = gVal;
			document.getElementById("lon_dec").value = pVal;
		}
	}
	else if(destId == "lat")
	{
		
		val = parseInt(document.getElementById("y").value);
		val = XYToLatLong(val, 1);
		gVal = parseInt(val);
		pVal = parseInt((val - gVal)* 60);
		if(gVal>=40 && gVal<=46)
		{ 
			document.getElementById("lat").value = gVal;
			document.getElementById("lat_dec").value = pVal;
		}
	}
	
}


function LatLongToXY(val, type)
{	
	if(type == 0)
	{
		var xG  = 1628793 + ( val - 10.586234) * 81024;
		return xG;		
	}
	else
	{
		var yG =  4788823 + ( val - 43.240429) * 112844;
		return yG;
	}
}

function XYToLatLong(val, type)
{
	if(type == 0)
	{
		var xG  = 10.586234 + parseFloat(( val - 1628793)) / 81024;
		return xG;		
	}
	else
	{
		var yG =  43.240429 + parseFloat(( val - 4788823)) / 112844;
		return yG;
	}

}

function CheckLatLon(lang)
{
	latElem = document.getElementById("lat");
	latElemDec = document.getElementById("lat_dec");
	longElem = document.getElementById("lon");
	longElemDec = document.getElementById("lon_dec");
	
	if (latElem && longElem )
	{
		var latPrimo = parseFloat( latElemDec.value);
		
		if (  isNaN(latPrimo) || latPrimo > 60.0 || latPrimo < 0.0)
		{
			alert("Inserire un valore valido per i primi della latitudine");
			return;
			
		}
			
		var longPrimo = parseFloat( longElemDec.value);
		if ( longPrimo > 60.0 || isNaN(longPrimo) || longPrimo < 0.0  )
		{
			alert("Inserire un valore valido per i primi della longitudine");
			return;
		}
		
		var  latInt = parseInt(latElem.value);
		if ( isNaN(latInt)  )
		{
			alert("Inserire un valore valido per i gradi della latitudine");
			return;
		}
		
		var  longInt = parseInt(longElem.value);
		if ( isNaN(longInt)  )
		{
			alert("Inserire un valore valido per i gradi della longitudine");
			return;
			
		}
		
		
		var latDec = latInt  + ( latPrimo / 60.0 );
		var longDec = longInt + ( longPrimo / 60.0);
		
		if (latDec < 42 || latDec > 45  || longDec < 9 || longDec > 13  )
		{
			alert("La latitudine deve essere compresa tra 42 e 45 gradi,\nla longitudine tra 9 e 13 gradi");
			
		}	
		else
		{
			
			xG = parseFloat(document.getElementById("x").value);
			if (  isNaN(xG) )
			{
				alert("Inserire un valore valido la coordinata piana x");
				return;
			
			}
			
			yG = parseFloat(document.getElementById("y").value);
			if (  isNaN(yG) )
			{
				alert("Inserire un valore valido la coordinata piana y");
				return;
			
			}
			
			if(xG>=1800000 || xG <1500000 || yG< 4650000 || yG> 5000000)
			{
				alert("La coordinata piana x deve essere compresa tra 1500000 e 1800000 m\nla coordinata piana y tra 465000 e 500000 m");
				return;
			}
			
			destUrl = "terraflyer.html?initCoordX=" + xG + "&initCoordY=" + yG + "&initHeading=0" + "&langId=" + lang;
 	 		window.location = destUrl;
		}
		
	}
	else
	{
		alert("Inserire latitudine e longitudine");
	}
	
}
