function kencode(stval) {
	var res = "";
	var i=0;
	for (i=0; i<stval.length; i++) {
		if (stval.charAt(i) == " ") {
			res += "+";
		} else {
			res += stval.charAt(i);
		}
	}
	//alert(res+","+stval);
	return escape(res);
}
var req;
function loadLocation(street,city,state) {
    req=null;
    // branch for native XMLHttpRequest object
	var kstreet=kencode(street);
	var kcity=kencode(city);
	var kstate=kencode(state);
    //alert("Receiving "+kstreet+","+kcity+","+kstate);
	url="generator.php?street="+kstreet+"&city="+kcity+"&state="+kstate;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
		
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"		
        if (req.status == 200||req.status==0) { // use 0 for checking with local disk
            parseData(req.responseXML);
         } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
         }
    }
}
function getFieldText(xml,field) {
  var ni;
  try {
  	ni=xml.getElementsByTagName(field);
  } catch (error) {
     return "element not found";
  }
  try {
  	return ni[0].firstChild.data;
  } catch (error) {
     return "No text found";
  }
}
function getParameter(xml,tag,param){
  try {
  	return xml.getElementsByTagName(tag)[0].attributes.getNamedItem(param).nodeValue;
  } catch (error) {
     return "No text found";
  }
}

function parseData(xml) {
	// this walks through the xml data to fill up the javascript data
	var error=null;
	var warning=null;
	//<Latitude>37.416402</Latitude><Longitude>-122.025078</Longitude>
	error=getFieldText(xml,"Error");
	if (error!="No text found"){
		error=getFieldText(xml,"Message");
		alert("There was an error in the address: "+error);
		return false;
	}
	// now we want to check the arning stuff
	//Result precision="address" warning="
	warning=getParameter(xml,"Result","warning");
    var latitude=getFieldText(xml,"Latitude");
    var longitude=getFieldText(xml,"Longitude");
	var name=document.dd.name.value.replace('"',"''");
	var speed=document.dd.speed.value;
	if (speed!="") name=name+"@"+speed;
	var description=document.dd.description.value;
	description=description.replace(/\"/g, "''");
	
	if (document.dd.bkk[1].checked) {
		description=description.replace(/\n/g, '<br>');
		description=description.replace(/\r/g, '');
	}
	if (document.dd.bkk[2].checked) {
		description=description.replace(/\n/g, "<br/>");
		description=description.replace(/\r/g, '');
	}
	// add the code to the list box
	if (warning!="No text found") {
		var resp=window.confirm("There was an warning (spelling mistake?): "+warning+" -- Continue?");
		if (!resp) {
		   return false;
		}
	}
	document.dd.csv.value=document.dd.csv.value+longitude+","+latitude+',"'+name+'","'+description+'"'+"\n";
    //alert("Here: "+latitude+","+longitude+","+error+","+warning);
}
function test() {
   loadLocation('238 Germonds Road','West Nyack','NY');
   return false;
}
function testError() {
   // now an error
   loadLocation('','nowhere','');
   return false;
   
}