function Response() {
    
    Response.prototype.process = function(response, componentManager, mapObj, snapObj) {
        
        var document = null;
        try {
            document = this.getDocument(response);
        }
        catch(e) {
            throw e;
        }
        
        var node = zXPath.selectSingleNode(document, this.XPATH_MAP);
        if (node != null) {
            this.mapResponse(node, mapObj);
        }
        
        var node = zXPath.selectSingleNode(document, this.XPATH_TOOL);
        if (node != null) {
            this.toolResponse(node);
        }
        
        node = zXPath.selectSingleNode(document, this.XPATH_DRAWMODE);
        if (node != null) {
            this.drawModeResponse(node, mapObj);
        }
        
        node = zXPath.selectSingleNode(document, this.XPATH_PRINT);
        if (node != null) {
            this.printResponse(node);
        }
        
        node = zXPath.selectSingleNode(document, this.XPATH_SNAP);
        if ((node != null) && (snapObj != null)) {
            this.snapResponse(node, snapObj);
        }
        
        node = zXPath.selectSingleNode(document, this.XPATH_ZOOM);
        if (node != null) {
            this.zoomResponse(node, componentManager);
        }
        
        node = zXPath.selectSingleNode(document, this.XPATH_LEGEND);
        if (node != null) {
            this.legendResponse(node, componentManager);
        }
        
        node = zXPath.selectSingleNode(document, this.XPATH_SHAPES);
        if (node != null) {
            this.loadShape(node, mapObj);
        }
        
        node = zXPath.selectSingleNode(document, this.XPATH_DEBUG);
        if (node != null) {
            this.debugResponse(node, componentManager);
        }
        
        node = zXPath.selectSingleNode(document, this.XPATH_CONTROL);
        if (node != null) {
            this.controlResponse(node);
        }
        
        node = zXPath.selectSingleNode(document, this.XPATH_JAVSCRIPT);
        if (node != null) {
            this.javascriptResponse(node);
        }
        
    }
    
    Response.prototype.mapResponse = function(mapNode, mapObj) {
        
        var envelopeNode = zXPath.selectSingleNode(mapNode, this.XPATH_ENVELOPE);
        var envMinX = parseFloat(envelopeNode.getAttribute("minx"));
        var envMinY = parseFloat(envelopeNode.getAttribute("miny"));
        var envMaxX = parseFloat(envelopeNode.getAttribute("maxx"));
        var envMaxY = parseFloat(envelopeNode.getAttribute("maxy"));
        mapObj.setExtent(envMinX, envMinY, envMaxX, envMaxY);
        
        var outPutNode = zXPath.selectSingleNode(mapNode, this.XPATH_OUTPUT);
        mapObj.mapImage.width = mapObj.pageWidth;
        mapObj.mapImage.height = mapObj.pageHeight;
        mapObj.mapImage.src = outPutNode.getAttribute("url");
        mapObj.scale = mapNode.getAttribute("scale");
        mapObj.metersPerPixel = mapNode.getAttribute("metersperpixel");
        mapObj.scaleFactor = mapNode.getAttribute("scaleFactor");
    }
    
    Response.prototype.toolResponse = function(node) {
        var nodeList = zXPath.selectNodes(node, this.XPATH_TOOL);
        var length = nodeList.length;

        for (var i=0; i<length; i++) {
            var tool = nodeList[i];
            var id = tool.getAttribute("id");
            var state = tool.getAttribute("state");
            var toolObj = document.getElementById(id);
            
            if (toolObj != null) {
                toolObj.style.display = '';
                toolObj.src = toolObj.UpImage;
                toolObj.enabled = "true";

                switch (state) {
                    case "active":
                        toolObj.src = toolObj.getAttribute(toolObj.toolBarObj.TOOL_ATTR_DOWN_IMAGE);
                    break;
                    case "enabled":
                        toolObj.src = toolObj.getAttribute(toolObj.toolBarObj.TOOL_ATTR_UP_IMAGE);
                    break;
                    case "visible":
                        toolObj.src = toolObj.getAttribute(toolObj.toolBarObj.TOOL_ATTR_DISABLE_IMAGE);
                        toolObj.enabled = false;
                    break;
                    case "invisible":
                        toolObj.style.display = 'none';
                    break;
                }
            }            
        }
    }
    
    Response.prototype.printResponse = function(printNode) {
        
        var htmlString = zXPath.selectSingleNode(printNode, this.XPATH_PRINT).firstChild.nodeValue;
        var printWindow = window.open("", "PrintWindow");
        printWindow.document.open();
        printWindow.document.writeln(htmlString);
        printWindow.document.close();
        printWindow.focus();
    }
    
    Response.prototype.snapResponse = function(snapNode, snapObj) {
        
        var pointNode = zXPath.selectSingleNode(snapNode, this.XPATH_SNAP).firstChild;
        var coodList = new Array();
        while (pointNode) {
            coodList[coodList.length] = new Array(pointNode.getAttribute("x"), pointNode.getAttribute("y"));
            pointNode = pointNode.nextSibling;
        }

        snapObj.snapCoordinates = coodList;
        snapObj.snapPointsUpdated = true;
    }
    
    Response.prototype.zoomResponse = function(node, componentManager) {
        
        var step = 0;
        var scale = 0;
        node = zXPath.selectSingleNode(node, this.XPATH_STEP);
        if (node != null) {
            step = parseInt(node.getAttribute("value"));
        }
        node = zXPath.selectSingleNode(node, this.XPATH_SCALE);
        if (node != null) {
            scale = parseFloat(node.getAttribute("value"));
        }
        componentManager.setZoomInfo(step, scale);
    }
    
    Response.prototype.legendResponse = function(node, componentManager) {
        
        var url = node.getAttribute("url");
        componentManager.setLegend(url);
    }
    
    Response.prototype.loadShape = function(node, mapObj) {
        
        var nodeList = zXPath.selectNodes(node, this.XPATH_SHAPE);
        var nodeLength = nodeList.length;
        var shapeNode;
        var pointNode;
        var shapes = new Array();
        var coordList;
        
        for (var i=0; i<nodeLength; i++) {
            shapeNode = nodeList[i];
            coordList = new Array();
            if (shapeNode != null) {
                pointNode = shapeNode.firstChild;
                while (pointNode != null) {
                    coordList.push(new Array(parseFloat(pointNode.getAttribute("x")), parseFloat(pointNode.getAttribute("y"))));
                    pointNode = pointNode.nextSibling;
                }
            }
            shapes.push(coordList);
        }
        mapObj.setShape(shapes);
    }
    
    Response.prototype.debugResponse = function(node, componentManager) {
        
        var debugString = zXPath.selectSingleNode(node, this.XPATH_DEBUG).text;
        componentManager.setDebugMessage(debugString);
    }
    
    Response.prototype.controlResponse = function(node) {
        
        var nodeList = zXPath.selectNodes(node, this.XPATH_CONTENT);
        var nodeLength = nodeList.length;
        var content;
        var id;
        var parentId;
        var currentNode;
        var placerElement;
        var element;
        
        for (var i=0; i<nodeLength; i++) {
            id = "";
            parentId = "";
            placerId = "";
            
            currentNode = nodeList[i];
            content = currentNode.firstChild.nodeValue.replace(/"\\'"/g, "'");
            
            if (content.length > 0) {
                parentId = currentNode.getAttribute("parentid");
                placerid = currentNode.getAttribute("placerid");
                id = currentNode.getAttribute("id");
                placerElement = document.getElementById(placerid);
                
                if (!placerElement) {
                    placerElement = document.createElement("span");
                    placerElement.setAttribute("id", placerid);
                    
                    if (id.length > 0 && document.getElementById(id)) {
                        element = document.getElementById(id);
                        document.getElementById(id).parentNode.insertBefore(placerElement, element);
                        element.parentNode.removeChild(element);
                    }
                    else if (parentId.length > 0 && document.getElementById(parentId)) {
                        document.getElementById(parentId).appendChild(placerElement);
                    }
                    else {
                        document.getElementsByTagName("body")[0].appendChild(placerElement);
                    }
                }
                placerElement.innerHTML = content;
            }
        }
    }
    
    Response.prototype.javascriptResponse = function(node) {
        
        var nodeList = zXPath.selectNodes(node, this.XPATH_JAVSCRIPT_CONTENT);
        var nodeLength = nodeList.length;
        var currentNode;
        var content = "";
        for (var i=0; i<nodeLength; i++) {
            currentNode = nodeList[i];
            content += currentNode.firstChild.nodeValue;
        }
        eval(content);
    }
    
    Response.prototype.drawModeResponse = function(node, mapObj) {
        
        node = zXPath.selectSingleNode(node, this.XPATH_DRAWMODE);
        if (node != null) {
            mapObj.setDrawMode(node.getAttribute("mode"));
        }
    }
    
    Response.prototype.customResponse = function() {
        
        var node = zXPath.selectSingleNode(this.document, this.XPATH_CUSTOM);
        var customResponse = "";
        
        if (node != null) {
            customResponse = node.firstChild.nodeValue;
        }
        return customResponse;
    }
    
    Response.prototype.getDocument = function(xmlString) {
        
        var document = null;
        try {
            document = this.loadXML(xmlString);
        }
        catch(e) {
            throw e;
        }
        
        if (document == null) {
            e = new Object("[object error]");	//New error object.
			e.message = "XML Response was not loaded";
			e.description = "For some reason the document object is null";
			e.description += "after attempting to load the response: ";
			e.description += xmlString;
            throw e;
        }
        else {
            return document;
        }
    }
    
    Response.prototype.loadXML = function(xmlString) {
        
        var document = zXmlDom.createDocument();
        document.async = "false"; 
		document.onreadystatechange = verify;
        document.loadXML(xmlString);
        
        //Check Error
        var parseError = document.parseError;
        
        if (parseError.errorCode != 0) {
			e = new Object("[object error]");	//New error object.
			e.message = "Parse error while trying to load XML file.";
			e.number = parseError.errorCode;
			e.description = "File Position: " + parseError.filepos + "\n";
			e.description += "Line: " + parseError.line + "\n";
			e.description += "Line Position: " + parseError.linepos + "\n";
			e.description += "Reason: " + parseError.reason + "\n";
			e.description += "Error In XML: ---> " + xmlString.substr(parseError.linepos - 1);
			e.description += "\nurl: " + parseError.url + "\n";
			throw e;
		}
		else {
		    this.document = document;
		    return document;
		}
		
		function verify() { 
			// 0 Object is not initialized 
			// 1 Loading object is loading data 
			// 2 Loaded object has loaded data 
			// 3 Data from object can be worked with 
			// 4 Object completely initialized 
			if (document.readyState != 4) { 
				return false; 
			} 
		}
    }
    
    this.document = null;
    this.xmlSeriliser = new zXMLSerializer();
    this.XPATH_RESPONSE = "/ASKRESPONSE";
    this.XPATH_MAP = this.XPATH_RESPONSE + "/MAP";
    this.XPATH_ENVELOPE = this.XPATH_MAP + "/ENVELOPE";
    this.XPATH_OUTPUT = this.XPATH_MAP + "/OUTPUT";
    this.XPATH_TOOLBAR = this.XPATH_RESPONSE + "/TOOLBAR";
    this.XPATH_TOOL = this.XPATH_TOOLBAR + "/TOOL";
    this.XPATH_PRINT = this.XPATH_RESPONSE + "/PRINT";
    this.XPATH_SNAP = this.XPATH_RESPONSE + "/SNAP";
    this.XPATH_ZOOM = this.XPATH_RESPONSE + "/ZOOM";
    this.XPATH_STEP = this.XPATH_ZOOM + "/STEP";
    this.XPATH_SCALE = this.XPATH_ZOOM + "/SCALE";
    this.XPATH_LEGEND = this.XPATH_RESPONSE + "/LEGEND";
    this.XPATH_DEBUG = this.XPATH_RESPONSE + "/DEBUG";
    this.XPATH_CONTROL = this.XPATH_RESPONSE + "/CONTROL";
    this.XPATH_CONTENT = this.XPATH_CONTROL + "/CONTENT";
    this.XPATH_JAVSCRIPT = this.XPATH_RESPONSE + "/JAVASCRIPT";
    this.XPATH_JAVSCRIPT_CONTENT = this.XPATH_JAVSCRIPT + "/CONTENT";
    this.XPATH_DRAWMODE = this.XPATH_RESPONSE + "/DRAWMODE";
    this.XPATH_SHAPES = this.XPATH_RESPONSE + "/SHAPES";
    this.XPATH_SHAPE = this.XPATH_SHAPES + "/SHAPE";
    this.XPATH_CUSTOM = this.XPATH_RESPONSE + "/CUSTOM";
}
