var req;

function getXHR()
{
	if(window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		return true;
	}
	else try {
		req = new ActiveXObject('Msxml2.XMLHTTP');
		return true;
	} catch(e) {
		try {
			req = new ActiveXObject("Microsoft.XMLHTTP");
			return true;
		} catch(e) {
			req = false;
			return false;
		}
	}
}

function updateElm(url, id)
{
	if(getXHR())
	{
		req.open('GET', url, true);
		req.onreadystatechange = function()
		{
			if(req.readyState == 4)
			 {
				if(req.status==200)
				{
  				if(document.getElementById(id))
  				  {
    					document.getElementById(id).innerHTML = req.responseText;
    					window.location.href='#'+id;
            }
				}
				else
				{
				  if(document.getElementById(id))
					 document.getElementById(id).innerHTML = 'Could not retrieve data<br/><em>'+req.statusText+'</em>';
				}
			} 
			else 
			{
				if(document.getElementById(id))
          document.getElementById(id).innerHTML = '<div style="text-align:center"><img src="images/ajax-loader.gif" /><br/><span style="color:#528335">Loading...</span></div>';

			}
		}
		req.send('');
	}
	else return true;
	return false;
}

      function handler(location) {
        //var message = document.getElementById("message");
        //message.innerHTML="<p>Longitude: " + location.coords.longitude + "</p>";
        //message.innerHTML+="<p>Latitude: " + location.coords.latitude + "</p>";
        alert("Longitude: " + location.coords.longitude + "Latitude: " + location.coords.latitude);
      }
    
    
        function successCallback(location) {
          //alert("Longitude: " + location.coords.longitude + "Latitude: " + location.coords.latitude);
          var message = document.getElementById("message");
          message.innerHTML="<p>Latitude: " + location.coords.latitude + "</p>";
          message.innerHTML+="<p>Longitude: " + location.coords.longitude + "</p>";
          //message.innerHTML+="<a href=\"http://maps.google.com/maps?f=q&source=s_q&hl=hu&geocode=&q=" + location.coords.latitude + "," + location.coords.longitude + "&sll=37.0625,-95.677068&sspn=53.564699,95.976562&ie=UTF8&z=14\">map</a>";
          updateElm('http://appro.hu/pages/liinks/aLocSess.php?lng='+location.coords.longitude+'&lat='+location.coords.latitude, 0);
          updateElm('http://appro.hu/pages/liinks/aLocMap.php?lng='+location.coords.longitude+'&lat='+location.coords.latitude, 'aLocMap');
          //window.location.reload();
          //alert('http://iphone.albis-solutions.com/pages/liinks/aLocSess.php?lng='+location.coords.longitude+'&lat='+location.coords.latitude);
        }
    
        function errorCallback(error) {
          var message = document.getElementById("message");
          message.innerHTML="<p>Nem t‡mogatott kŽrŽs</p>";
          updateElm('http://appro.hu/pages/liinks/aLocSess.php?lng=noGPS&lat=noGPS', 0);
          //alert('http://iphone.albis-solutions.com/pages/liinks/aLocSess.php?lng=noGPS&lat=noGPS');

        }
        
        function getLoc(){
         // navigator.geolocation.getCurrentPosition(handler);
          var message = document.getElementById("message");
          if(confirm("Biztos?"))
          	{
          		if (navigator.geolocation) {  
          			message.innerHTML="<p>Kis tŸremlet...</p>";
            		//navigator.geolocation.watchPosition(successCallback);
            		for(i=0;i<10;i++)
            		  {
                    navigator.geolocation.getCurrentPosition(successCallback);
                    //var updateLocation = navigator.geolocation.watchPosition(successCallback, errorCallback);
                  }
       				} 
       				else {  
    			        message.innerHTML="<p>Nem t‡mogatott kŽrŽs</p>";
       				}  
          	}
//            navigator.geolocation.getCurrentPosition(successCallback,errorCallback,{maximumAge:600000});
        }
        
        function geoOK(){
          	//alert('sdfsdf');
            	if (navigator.geolocation) {  
    			      var r= 1;
                updateElm('http://appro.hu/pages/liinks/aLocSess.php?geoOK=1', 0);
       				} 
       				else {  
                updateElm('http://appro.hu/pages/liinks/aLocSess.php?geoOK=-1', 0);
    			      var r= -1;
       				}
            return r;  
        }        
