
//AJAX Stuff
//Credits: http://developer.apple.com/internet/webcontent/xmlhttpreq.html
var req;
// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file
function loadXMLDoc(url, functName) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = functName;
        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 = functName;
            req.open("GET", url, true);
            req.send();
        }
    }
}

//ORGANIZATION CATEGORY DESCRIPTION LOOKUP
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            // ...processing statements go here...
            MM_setTextOfLayer("definition", '', req.responseText);
        } else {
            alert("There was a problem retrieving the description.");
        }
    }
}

function loadCatDefinition(selObj){
	var term = selObj.options[selObj.selectedIndex].text;
	/*if (term.indexOf("\/") > -1 ){
		var termArr = term.split("\/");
		term = termArr[0];
		term.replace(/ /, "");
	}*/
	loadXMLDoc("organizationCatDef.jsp?term=" + escape(term), processReqChange);
}

/// ZIP LOOKUP (IS NOT FINISHED ....)
function processReqChangeForZip() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            // ...processing statements go here...
            setGeoInfoVars();
        } else {
            alert("There was a problem retrieving the county for this zip code, please select from drop down.");
        }
    }
}

function loadGeoInfoForZip(txtObj){
    MM_setTextOfLayer('geoInfo', '', ''); 
    if (txtObj.value == "" || txtObj.value.length < 5)  return;
    MM_setTextOfLayer('geoInfo', '', 'setting city(county)'); 
    var term = txtObj.value;
    loadXMLDoc("faqZipLookup.jsp?zip=" + escape(term), processReqChangeForZip);
}

function setGeoInfoVars(){
	//geoInfo comes like: id|zip|elevation|city|county|state
	if (req.responseText == ""){
		alert("We couldn't find the county for this zip code, please select from drop down.");
	} else {
		var theForm = window.document.forms["wizard"];
		var geoInfo = req.responseText.split("|");
		if (geoInfo[0] == 0){
		    MM_setTextOfLayer('geoInfo', '', '<span class=\"redstarSmall\">Zip code not found</span>'); 
		    return;
		 }
		//Set the county field
		/*selObj = theForm.geographic_area;
		for (i=0; i < selObj.options.length; i++){
			if (selObj.options[i].value == geoInfo[4]) {
				selObj.options[i].selected = true;
				showWaterCompanies(selObj, '(select a water source)');
				break;
			}
		}*/
		//set the county, city and elevation fields
		theForm.county.value = geoInfo[4];
		theForm.city.value = geoInfo[3];
		theForm.elevation.value = geoInfo[2];
		MM_setTextOfLayer("geoInfo", '', geoInfo[3] + " (" + geoInfo[4] + ")");
		showWaterCompaniesForCounty(geoInfo[4], theForm, '(select a water source)');
	}

}



//End of AJAX