// Script starts here!
    var DEBUG = false;
    var httpRequest = false;


    /*
     * Make XMLHttpRequest.
     * Handles cleint request/server response. Set callbackFuncName var to the function that will handle responses.
     */
    function makeRequest(url) {
        httpRequest = false;
        var callbackFuncName = processRequestResults; // Name of callback function.

        if(DEBUG) alert("url in makeRequest function: " + url);
        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
                // This line overrides mime type of response, but may cause error in Firefox 1.5b if not valid xml.
            }
        } else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!httpRequest) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        httpRequest.onreadystatechange = callbackFuncName;
        httpRequest.open('GET', url, true);
        httpRequest.send(null);

    }


    /*
     * This is the callback function to process server responses to XMLHttpRequests. It ultimately calls 
     * a function dedicated to a particular form input.
     * The server response must be in a comma separated list of args that can be used to create a js object.
     * Example: getPersonalData,dave,male,age,zip,city,etc    (white space is significant)
     *     where the first token is the name of the function to be called upon server response which allows 
     *     the call to the function to be generated dynamcially.
     */
    function processRequestResults() {
       // only if req shows "complete"
       if (httpRequest.readyState == 4) {
        
            // only if "OK"
            if (httpRequest.status == 200) {

                // Get the text returned by the server and create a js object.
                resultText  = httpRequest.responseText;
                if(DEBUG) alert("response text is: " + resultText);

                // Get name of function to call for this request result; it must be the first token.
                delimiterPosition = resultText.indexOf(",");
                functionName = resultText.substring(0, delimiterPosition);
                if(DEBUG) alert("Function name is: " + functionName);
                
                // Create a function call from the name of the first token. It includes two params.
                var evalParam = functionName + '(\'\', resultText)';
                if(DEBUG) alert("Function script to eval: " + evalParam);
                eval(evalParam);
            }
            else {
                alert("There was a problem retrieving the XML data:\n" + httpRequest.statusText);
            }
        }
    }



    /*
     * This method is called by the user submittal for employee info and is the utility function that handles
     * the response from the server related to this particular submittal.
     */
    function getPersonData(formInput, results) {
        // Input mode: If the formInput arg is not empty, assume this is user input and make the request to the server.
        if (formInput != ''){ 
            url = '/servlet/org/fieldmaster/RemotingSamples/AjaxPersonServlet2?function_name=getPersonData&employee_Id=' + formInput;
            if(DEBUG) alert("getPersonalData was called in INPUT MODE; url: " + url);
            makeRequest(url);
        }

        // Response mode: If the formInput arg is empty, this function was called to handle the response.
        else {
            // Split the comma separted server response into a js array.
            var paramArray = results.split(",");
            if(DEBUG) alert("getPersonData(); First array element: " + paramArray[0]);

            // Define object for this server response.
            function Person(array) {
                this.functionName = array[0]; this.name = array[1]; this.sex = array[2]; this.city = array[3];
                this.zip = array[4]; this.startDate = array[5]; this.position = array[6]; this.birthDate = array[7];
                this.startSalary = array[8]; this.currentSalary = array[9];
            }

            // Define functions to add to prototype.
            function salaryIncrease() {
                salaryChange = person.currentSalary - person.startSalary;
                return salaryChange;
            }

            // Add functions to the prototype.
            Person.prototype.salaryIncrease = salaryIncrease;

            // Create the object.
            person = new Person(paramArray);

            // Update span element with server response information.
            document.getElementById('emp_id_display').innerHTML = ""
            employeeDataString = "Name: " + person.name +
                                 "< br>sex: "  + person.sex +
                                 "< br>city: "  + person.city +
                                 "< br>start Date: "  + person.startDate +
                                 "< br>position: "  + person.position +
                                 "< br>birth date: "  + person.birthDate +
                                 "< br>starting salary: " + person.startSalary +
                                 "< br>current salary: " + person.currentSalary +
                                 "< br>change in salary: " + person.salaryIncrease();
            document.getElementById('emp_id_display').innerHTML = employeeDataString;
        }
    }