if (typeof XMLHttpRequest  == "undefined") {
  XMLHttpRequest = function() {
    try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
      catch(e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
      catch(e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP"); }
      catch(e) {}
    try { return new ActiveXObject("Microsoft.XMLHTTP"); }
      catch(e) {}
    throw new Error("This browser does not support XMLHttpRequest.");
  }
  alert("It got past the XML object bit!");
}

var geo = google.gears.factory.create('beta.geolocation');

function updatePosition(position) {
  alert('Current lat/lon is: ' + position.latitude + ',' + position.longitude);
}

function handleError(positionError) {
  alert('Attempt to get location failed: ' + positionError.message);
}

function setLocationForm(position) {
document.getElementById("geo-lat").value=position.latitude;
document.getElementById("geo-long").value=position.longitude;
}
/*
function showJSON() {
var method=document.getElementById("method-find-me").value;
var geoLong=document.getElementById("geo-long").value;
var geoLat=document.getElementById("geo-lat").value;
var postcodePresets=document.getElementById("fields-presets").value;
var streetAddress=document.getElementById("address").value;
var city=document.getElementById("city").value;
var postcode=document.getElementById("postcode").value;
var additional=document.getElementById("query").value;
var myJSON=qryFormObject(method, geoLong, geoLat, postcodePresets, streetAddress, city, postcode, additional);
//alert("Your instance of myJSON is a "+ myJSON);
return myJSON;
}*/

function updateLocation() {
var method="";
var geoLong="";
var geoLat="";
var postcodePresets="";
var streetAddress="";
var city="";
var postcode="";
var additional="";

var m=document.getElementsByName("method");
//alert(m[2].value);
for (i=0; i<m.length; i++) {
if (m[i].checked==true) {
	method=m[i].value;
	}
//	alert(typeof m[i]);
}

//method=document.getElementsByName("method").value;
geoLong=document.getElementById("geo-long").value;
geoLat=document.getElementById("geo-lat").value;
postcodePresets=document.getElementById("fieldsPresets").value;
streetAddress=document.getElementById("address").value;
city=document.getElementById("city").value;
postcode=document.getElementById("postcode").value;
additional=document.getElementById("query").value;

var request=new XMLHttpRequest;
var url="http://www.wiardfm.co.uk/wp-content/themes/wiardfm_mobile/updateLocation.php";
request.onreadystatechange=returnLocation;

var formContent=qryFormObject(method, geoLong, geoLat, postcodePresets, streetAddress, city, postcode, additional);
var parameters="method="+formContent.method+"&geoLong="+formContent.geoLong+"&geoLat="+formContent.geoLat+"&postcodePresets="+formContent.postcodePresets+"&streetAddress="+formContent.streetAddress+"&city="+formContent.city+"&postcode="+formContent.postcode+"&additional="+formContent.additional;
request.open("POST",url,true);
// Must set request to appear as a FORM request, providing the parameter length and closing the connection.
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.setRequestHeader("Content-length", parameters.length);
request.setRequestHeader("Connection", "close")

alert("Request opened");
alert("The preset address used is: "+document.getElementById('fieldsPresets').value);
alert(parameters);

request.send(parameters);
function returnLocation() {
if (request.readyState==4)
  {
  if (request.status==200)
    {
	alert("Information was received! "+request.responseText);
    document.getElementById("user-location").value=request.responseText;
	document.getElementById("user-location").focus();
    }

} //eoIf

} //eoFunction returnLocation
} // eoFunction updateLocation

function qryFormObject(method, geoLong, geoLat, postcodePresets, streetAddress, city, postcode, additional) {
var locationFormSubmission=

{
    "method": method,
    "geoLong": geoLong,
    "geoLat": geoLat,
    "postcodePresets": postcodePresets,
    "streetAddress": streetAddress,
    "city": city,
    "postcode": postcode,
    "additional": additional
};

return locationFormSubmission;
} // eoFunction qryFormObject

//var eventList;


function getRealId(partialid,tagname){
var list =new Array;
var re= new RegExp(partialid,'g');
if (tagname==''||tagname==null) tagname = '*';
var el = document.getElementsByTagName(tagname);
for(var i=0;i<el.length;i++){
if(el[i].id.match(re)){
//alert(el[i].id);
list[list.length]=document.getElementById(el[i].id);
//alert(list[list.length-1].innerHTML);
}// eoIf
}// eoFor
return list;
} // eoFunction getRealId taken from following forum page: http://www.webdeveloper.com/forum/showthread.php?t=114892

function compareDistance(a,b) {
return b.total - a.total;
}

var eventList =new Array;
var contentDiv;
contentDiv=document.getElementById('content');

function sortEventListings() {

buildEventList();

eventList.sort(compareTotals);
for(i=0;i<eventList.length;i++) {
//alert("Sorted - Events "+i+" distance is: "+eventList[i].distance);
document.getElementById('content').removeChild(eventList[i]);
}

for(i=0;i<eventList.length;i++) {
//alert("Sorted - Events "+i+" distance is: "+eventList[i].distance);
document.getElementById('content').appendChild(eventList[i]);
}
var button=document.getElementById("sortButton");
button.value="Sorted by distance!";
button.onclick=null;
//button.style.font-size="1.1em";
button.style.color="#458B00";
button.style.background="#323232";
}

function buildEventList() {
var tag =new Array;
eventList=getRealId('post-id-','div');
//alert("The function says that the length of the array is: "+eventList.length);

for(i=0;i<eventList.length;i++) {
tag=eventList[i].getElementsByTagName("span");
//alert(tag[i].innerHTML);
eventList[i].distance=tag[0].innerHTML;
//alert("Events "+i+" distance is: "+eventList[i].distance);
}
//alert(tag.length);
}

function compareTotals(a, b) {
return a.distance - b.distance;
}

function sortEventListingsNew() {
var x=document.getElementById("content");
//alert(x.childNodes.length);
//alert(x.childNodes[0].getElementsbyTagname("span")[0].nodeValue);

}

/*var so = device.getServiceObject("Service.Location", "ILocation");
nokiaPosition = so.ILocation.GetLocation(criteria.LocationInformationClass.BasicLocationInformation, callback);
if (typeof "The type of the Nokia location object is: "+device.getServiceObject("Service.Location", "ILocation")=='undefined') {

alert("The type of the Nokia location object is: "+typeof device.getServiceObject("Service.Location", "ILocation")!=='undefined');
}

else if (typeof device.getServiceObject("Service.Location", "ILocation")=='undefined') {
alert("Sorry you don't have the S60 platform installed... =(");
}*/
/*if (typeof google.gears.factory.create('beta.geolocation')!=='undefined') {
alert("The type of the location object is: "+typeof google.gears.factory.create('beta.geolocation'));
}*/

/*if device.getServiceObject("Service.Location", "ILocation")=='undefined') {
alert("The type of the Nokia location object is unknown");
}*/

//var so = device.getServiceObject("Service.Location", "ILocation");

/*
function setLocationFormNokia(position) {
document.getElementById("geo-lat").value=nokiaPosition.latitude;
document.getElementById("geo-long").value=nokiaPosition.longitude;
}
*/
/*function clearInputField(fieldName) {
document.getElementById(fieldName).value="";
}*/
