var theRequest = false;

/* gets the Location data from the getLocationData.asp page, and
populates the text areas on the calling page.
*/
function getLocationData(){
	
    var msg = "";
    var selIndex = document.getElementById("selectedLocation").selectedIndex;
    theLocation = document.getElementById("selectedLocation").options[selIndex].value;

    if (!isBlank(theLocation)){
        msg = "Retrieving location data ...";
        document.getElementById("locationMsgDiv").innerHTML = msg;

        clearExistingData();

        if (theRequest && theRequest != null && theRequest.readyState != 0 && theRequest.readyState != 4){
            theRequest.abort();
        }
        theRequest = false;

        try {
            theRequest = new XMLHttpRequest();
        } catch(err) {
            try {
                theRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                theRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (theRequest.overrideMimeType) {
            theRequest.overrideMimeType('text/xml');
        }

        if (!theRequest) {
            showLocationData(-1);
            return false;
        }
		//alert(theLocation);
        var getLocationDataPage = "../includes/getLocationData.asp?locationID=" + theLocation;
        theRequest.onreadystatechange = showLocationData;
        theRequest.open('GET', getLocationDataPage, true);
        theRequest.send(null);
    }
}

function showLocationData(errCode){
    var msg = "";
    if (errCode == -1){
        msg = "Unable to access the database."
    } else {
        if (theRequest.readyState == 4 && theRequest.status == 200) {
            var returnVal = theRequest.responseXML;
			
            if (returnVal.getElementsByTagName("nodata")[0].childNodes[0].nodeValue == "XXemptyXX"){
                // no data found
                msg = "Location data can not be looked up at this time.";
            } else {
                var termName = document.getElementById("locationName");
                var tempVal = returnVal.getElementsByTagName("locationName")[0].childNodes[0].nodeValue;
                if (tempVal == "!!")
                    tempVal = "";
                termName.value = tempVal

                var add1 = document.getElementById("locationAddress1");
                tempVal = returnVal.getElementsByTagName("locationAddress1")[0].childNodes[0].nodeValue;
                if (tempVal == "!!")
                    tempVal = "";
                add1.value = tempVal;

                var add2 = document.getElementById("locationAddress2");
                tempVal = returnVal.getElementsByTagName("locationAddress2")[0].childNodes[0].nodeValue;
                if (tempVal == "!!")
                    tempVal = "";
                add2.value = tempVal;

                var city = document.getElementById("locationCity");
                tempVal = returnVal.getElementsByTagName("locationCity")[0].childNodes[0].nodeValue;
                if (tempVal == "!!")
                    tempVal = "";
                city.value = tempVal;

                var state = document.getElementById("locationState");
                tempVal = returnVal.getElementsByTagName("locationState")[0].childNodes[0].nodeValue;
                if (tempVal == "!!")
                    tempVal = "";
                state.value = tempVal;

                var zipCode = document.getElementById("locationZip");
                tempVal = returnVal.getElementsByTagName("locationZip")[0].childNodes[0].nodeValue;
                if (tempVal == "!!")
                    tempVal = "";
                zipCode.value = tempVal;

            } // end if 'empty' return value
        } // end if valid status and state
    } // end if error creating the object

    if (msg != "") {
        document.getElementById("locationMsgDiv").innerHTML = msg;
    } else {
        document.getElementById("locationMsgDiv").innerHTML = "";
    }
}

// clear the existing fields in the form
function clearExistingData(){
    document.getElementById("locationName").value = "";
    document.getElementById("locationAddress1").value = "";
    document.getElementById("locationAddress2").value = "";
    document.getElementById("locationCity").value = "";
    document.getElementById("locationState").value = "";
    document.getElementById("locationZip").value = "";
}

function isBlank(tVal){
    for (var i = 0; i < tVal.length; i++){
        var c = tVal.charAt(i);
        if((c != ' ') && (c != '\n') && (c != '\t'))
            return false;
    }
    return true;
}
