//<![CDATA[
// This geocode script is dependent on yproxy.php
// It is for use with the user_photos.php and modify_photos.php files in geocoding user entered locations	
	var xmlhttp2 = false;
	var ymap;
	var appid = "whatsinvasive";
	
	function callWS(target,photoid) {
		if (target !== "") {
			if(!xmlhttp2){
				geoOnLoad();
			}
			var url = 'http://whatsinvasive.com/_yproxy.php?' + encodeURI(target);
			//var url = encodeURI(target);
			xmlhttp2.open('GET', url, true);
			xmlhttp2.onreadystatechange = function() {
				if (xmlhttp2.readyState == 4 && xmlhttp2.status == 200) {
					//document.getElementById('result').innerHTML = '';
					parseResult(xmlhttp2.responseText,photoid);
				} else {					
					//document.getElementById('result').innerHTML = "Searching...";
				}
			};
			xmlhttp2.send(null);
		}
	}

	
	function parseResult(parseMeString,photoid) {
		var parser = new DOMImplementation();
		var photo_id = photoid;
		var domDoc = parser.loadXML(parseMeString);
		
		if (domDoc == null) {
			alert("There was a problem parsing search results.");
			return;
		}
		
		var docRoot = domDoc.getDocumentElement();	
		var items = docRoot.getElementsByTagName("Result");
		for (var i=0; i < items.length; i++) {
			var lat = items.item(i).getElementsByTagName("Latitude").item(0).getFirstChild().getNodeValue();
			var lon = items.item(i).getElementsByTagName("Longitude").item(0).getFirstChild().getNodeValue();
			document.getElementById('lat2' + photo_id).value = lat;
		    document.getElementById('lng2' + photo_id).value = lon;
		    document.getElementById('coord' + photo_id).value = "(" + lat + "," + lon + ")";
			if(window['gmap' + photo_id]){
				window['gmap' + photo_id].panTo(new GLatLng(lat,lon));
				window['gmark' +photo_id].setLatLng(new GLatLng(lat,lon));
			}		    

		}
	}
	
	function geoOnLoad() {
		if (window.XMLHttpRequest) {
			xmlhttp2 = new XMLHttpRequest();
			xmlhttp2.overrideMimeType('text/xml');
		} else if (window.ActiveXObject) {
			xmlhttp2 = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	function callGeocode(location,photoid) {
		//var location = document.getElementById("address").value;
		var uri = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=sensorbase&location=" + location;
		document.getElementById('lat2' + photoid).value = "";		
		callWS(uri,photoid);
	}
	
//]]>


