/** \class TeWMSGetCapabilitiesResponse
  * \brief Models a GetCapabilities response from a WMS.
  *
  *
  * This class can be used to parse a Capabilities document from
  * a WMS server. An instance of this class is created when
  * you call GetCapabilities method from an instance of TeWMS (see GetCapabilities method of
  * TeWMS class).
  *
  * \sa TeWMS
  *
  */
function TeWMSGetCapabilitiesResponse()
{
    var serviceVersion_ = null;
	var serviceName_ = null;
	var serviceTitle_ = null;
	var serviceGetCapabilitiesURL_ = null;
	var serviceGetMapURL_ = null;
	var serviceGetFeatureInfoURL_ = null;
	var serviceException_ = null;
	var isException_ = false;
    var layer_ = null;
	
	TeWMSGetCapabilitiesResponse.prototype.getServiceVersion = getServiceVersion;
	TeWMSGetCapabilitiesResponse.prototype.setServiceVersion = setServiceVersion;
	TeWMSGetCapabilitiesResponse.prototype.isSetServiceException = isSetServiceException;
	TeWMSGetCapabilitiesResponse.prototype.setServiceException = setServiceException;
	TeWMSGetCapabilitiesResponse.prototype.getServiceExceptionResponse = getServiceExceptionResponse;
	TeWMSGetCapabilitiesResponse.prototype.setServiceExceptionResponse = setServiceExceptionResponse;
	TeWMSGetCapabilitiesResponse.prototype.getServiceName = getServiceName;
	TeWMSGetCapabilitiesResponse.prototype.setServiceName = setServiceName;
	TeWMSGetCapabilitiesResponse.prototype.getServiceTitle = getServiceTitle;
	TeWMSGetCapabilitiesResponse.prototype.setServiceTitle = setServiceTitle;
	TeWMSGetCapabilitiesResponse.prototype.getServiceGetCapabilitiesURL = getServiceGetCapabilitiesURL;
	TeWMSGetCapabilitiesResponse.prototype.setServiceGetCapabilitiesURL = setServiceGetCapabilitiesURL;
	TeWMSGetCapabilitiesResponse.prototype.getServiceGetMapURL = getServiceGetMapURL;
	TeWMSGetCapabilitiesResponse.prototype.setServiceGetMapURL = setServiceGetMapURL;
	TeWMSGetCapabilitiesResponse.prototype.getServiceGetFeatureInfoURL = getServiceGetFeatureInfoURL;
	TeWMSGetCapabilitiesResponse.prototype.setServiceGetFeatureInfoURL = setServiceGetFeatureInfoURL;
	TeWMSGetCapabilitiesResponse.prototype.getLayer = getLayer;
	TeWMSGetCapabilitiesResponse.prototype.setLayer = setLayer;	
	TeWMSGetCapabilitiesResponse.prototype.toString = toString;
	TeWMSGetCapabilitiesResponse.prototype.toStringHTML = toStringHTML;
	TeWMSGetCapabilitiesResponse.prototype.show = show;
	TeWMSGetCapabilitiesResponse.prototype.showHTML = showHTML;
	TeWMSGetCapabilitiesResponse.prototype.readFromXML = readFromXML;
	TeWMSGetCapabilitiesResponse.prototype.readFromXML_IE = readFromXML_IE;
	TeWMSGetCapabilitiesResponse.prototype.readFromXML_ = readFromXML_;
	TeWMSGetCapabilitiesResponse.prototype.getElementXML = getElementXML;
		
	function getServiceVersion()
	{
		return this.serviceVersion_;
	}

	function setServiceVersion(version)
	{
		//alert("VERSAO: " + version);
		this.serviceVersion_ = version;
	}
	
	function isSetServiceException() {
		return isException_;
	}

	function setServiceException(boolException) {
		isException_ = boolException;
	}
	
	function getServiceExceptionResponse() {
		return serviceException_;
	}
	
	function setServiceExceptionResponse(exception) {
		serviceException_ = exception;
	}
	
	function getServiceName()
	{
		return this.serviceName_;
	}

	function setServiceName(name)
	{
		//alert("NOME: " + name);
		this.serviceName_ = name;
	}
	
	function getServiceTitle()
	{
		return this.serviceTitle_;
	}

	function setServiceTitle(t)
	{
		this.serviceTitle_ = t;
	}
	
	function getServiceGetCapabilitiesURL()
	{
		return this.serviceGetCapabilitiesURL_;
	}

	function setServiceGetCapabilitiesURL(url)
	{
		//alert("GetCapabilities URL: " + url);
		this.serviceGetCapabilitiesURL_ = url;
	}
	
	function getServiceGetMapURL()
	{
		return this.serviceGetMapURL_;
	}

	function setServiceGetMapURL(url)
	{
		//alert("GetMap URL: " + url);
		this.serviceGetMapURL_ = url;
	}
	
	function getServiceGetFeatureInfoURL()
	{
		return this.serviceGetFeatureInfoURL_;
	}

	function setServiceGetFeatureInfoURL(url)
	{
		//alert("GetFeatureInfo URL: " + url);
		this.serviceGetFeatureInfoURL_ = url;
	}
	
	function getLayer()
	{
		return this.layer_;
	}

	function setLayer(layer)
	{
		this.layer_ = layer;
	}	

	function toString()
	{
		var message  = "Service Version: ";
		    message += this.getServiceVersion();
			message += "\n";
			message += "Service Name: ";
			message += this.getServiceName();
			message += "\n";
			message += "Service Title: ";
			message += this.getServiceTitle();
			message += "\n";
			message += "Service GetCapabilities URL: ";
			message += this.getServiceGetCapabilitiesURL();
			message += "\n";
			message += "Service GetMap URL: ";
			message += this.getServiceGetMapURL();
			message += "\n";
			message += "Service GetFeatureInfo URL: ";
			message += this.getServiceGetFeatureInfoURL();
			message += "\n";
			message += "Layers:\n";
			if(this.getLayer())
				message += this.getLayer().toString();
			
		return message;
	}
	
	function toStringHTML()
	{
		var message  = "<table border=0 align=center><tr><td><font size=-4>";
			message += "Service Version: ";
		    message += this.getServiceVersion();
			message += "</font></td></tr><tr><td><font size=-4>";
			message += "Service Name: ";
			message += this.getServiceName();
			message += "</font></td></tr><tr><td><font size=-4>";
			message += "Service Title: ";
			message += this.getServiceTitle();
			message += "</font></td></tr><tr><td><font size=-4>";
			message += "Service GetCapabilities URL:";
			message += this.getServiceGetCapabilitiesURL();
			message += "</font></td></tr><tr><td><font size=-4>";
			message += "Service GetMap URL:";
			message += this.getServiceGetMapURL();
			message += "</font></td></tr><tr><td><font size=-4>";
			//message += "Service GetFeatureInfo URL: ";
			//message += this.getServiceGetFeatureInfoURL();
			//message += "</td></tr><tr><td>";
			message += "Layers:</font>";
			message += "</td></tr><tr><td>";
			if(this.getLayer())
				message += this.getLayer().toStringHTML(0);

			message += "</td></tr></table>";
			
		return message;
	}
	
	function show()
	{
		alert(this.toString());
	}
	
	function showHTML()
	{
		return this.toStringHTML();
	}

	function readFromXML(xmlDoc)
	{
		if (navigator.appName == "Microsoft Internet Explorer") {
			this.readFromXML_IE(xmlDoc);
		}
		else if (navigator.appName == "Netscape") {
			this.readFromXML_(xmlDoc);
		}
	}
	
	function readFromXML_IE(xmlDoc)
	{
		/*if(!xmlDoc.documentElement)
		{
			alert("XML Capabilities is empty!");
			return;
		}*/
		
		this.setServiceVersion(xmlDoc.documentElement.getAttribute('version'));
		
		var serviceNameNodeList = xmlDoc.selectNodes("/WMT_MS_Capabilities/Service/Name");

		if(serviceNameNodeList.length > 0)
			this.setServiceName(serviceNameNodeList[0].childNodes[0].nodeValue);

		var serviceTitleNodeList = xmlDoc.selectNodes("/WMT_MS_Capabilities/Service/Title");
		
		if(serviceTitleNodeList.length > 0)
			this.setServiceTitle(serviceTitleNodeList[0].childNodes[0].nodeValue);
		
		var serviceGetCapabilitiesURLNodeList = xmlDoc.selectNodes("/WMT_MS_Capabilities/Capability/Request/GetCapabilities/DCPType/HTTP/Get/OnlineResource");
		
		if(serviceGetCapabilitiesURLNodeList.length > 0)
			this.setServiceGetCapabilitiesURL(serviceGetCapabilitiesURLNodeList[0].attributes.getNamedItem("xlink:href").value);
			
		var serviceGetMapURLNodeList = xmlDoc.selectNodes("/WMT_MS_Capabilities/Capability/Request/GetMap/DCPType/HTTP/Get/OnlineResource");
		
		if(serviceGetMapURLNodeList.length > 0)
			this.setServiceGetMapURL(serviceGetMapURLNodeList[0].attributes.getNamedItem("xlink:href").value);
			
		var serviceGetFeatuteInfoURLNodeList = xmlDoc.selectNodes("/WMT_MS_Capabilities/Capability/Request/GetFeaturInfo/DCPType/HTTP/Get/OnlineResource");
		
		if(serviceGetFeatuteInfoURLNodeList.length > 0)
			this.setServiceGetFeatureInfoURL(serviceGetFeatuteInfoURLNodeList[0].attributes.getNamedItem("xlink:href").value);
			
		var serviceLayerNodeList = xmlDoc.selectNodes("/WMT_MS_Capabilities/Capability/Layer");
		
		if(serviceLayerNodeList.length > 0)
		{
			this.layer_ = new TeWMSLayer();
			this.layer_.readFromXML(serviceLayerNodeList[0]);
			//alert("TOTAL DE SUB: " + this.layer_.getLayers().length);
		}
	}
	
	function readFromXML_(xmlDoc)
	{
		
		var elem = xmlDoc.getElementsByTagName('ServiceExceptionReport');
		if (elem.length > 0) {
			var arrayBusca = "ServiceExceptionReport/ServiceException".split("/");
			var elem_ = elem[0];
			var serviceExceptionNodeList = getElementXML(elem_, arrayBusca, 0);
			//alert(serviceExceptionNodeList.firstChild.nodeValue);
			this.setServiceException(true);
			this.setServiceExceptionResponse("O serviço solicitado está indisponível no momento!");
			return false;
		}else {
			this.setServiceException(false);
		}

		this.setServiceVersion(xmlDoc.documentElement.getAttribute('version'));
		
		var arrayBusca = "WMT_MS_Capabilities/Service/Name".split("/");
				
		var elem = xmlDoc.getElementsByTagName('WMT_MS_Capabilities')[0];
		
		var serviceNameNodeList = getElementXML(elem, arrayBusca, 0);
		
		if(serviceNameNodeList.length > 0)
			this.setServiceName(serviceNameNodeList.childNodes[0].nodeValue);

		arrayBusca = "WMT_MS_Capabilities/Service/Title".split("/");

		var serviceTitleNodeList = getElementXML(elem, arrayBusca, 0);
		
		if(serviceTitleNodeList.childNodes.length > 0)
			this.setServiceTitle(serviceTitleNodeList.childNodes[0].nodeValue);
		
		arrayBusca = "WMT_MS_Capabilities/Capability/Request/GetCapabilities/DCPType/HTTP/Get/OnlineResource".split("/");
		
		var serviceGetCapabilitiesURLNodeList = getElementXML(elem, arrayBusca, 0);

		if(serviceGetCapabilitiesURLNodeList && serviceGetCapabilitiesURLNodeList.nodeName == "OnlineResource")
			this.setServiceGetCapabilitiesURL(serviceGetCapabilitiesURLNodeList.attributes.getNamedItem("xlink:href").value);
			
		arrayBusca = "WMT_MS_Capabilities/Capability/Request/GetMap/DCPType/HTTP/Get/OnlineResource".split("/");
		
		var serviceGetMapURLNodeList = getElementXML(elem, arrayBusca, 0);

		if(serviceGetMapURLNodeList && serviceGetMapURLNodeList.nodeName == "OnlineResource")
			this.setServiceGetMapURL(serviceGetMapURLNodeList.attributes.getNamedItem("xlink:href").value);
		
		arrayBusca = "WMT_MS_Capabilities/Capability/Request/GetFeatureInfo/DCPType/HTTP/Get/OnlineResource".split("/");
		
		var serviceGetFeatuteInfoURLNodeList = getElementXML(elem, arrayBusca, 0);
		
		if(serviceGetFeatuteInfoURLNodeList) {
			if(serviceGetFeatuteInfoURLNodeList.nodeName == "OnlineResource")
				this.setServiceGetFeatureInfoURL(serviceGetFeatuteInfoURLNodeList.attributes.getNamedItem("xlink:href").value);
		}else {
			this.setServiceGetFeatureInfoURL = null;
		}

		arrayBusca = "WMT_MS_Capabilities/Capability/Layer".split("/");
		
		var serviceLayerNodeList = getElementXML(elem, arrayBusca, 0);
		
		if(serviceLayerNodeList.childNodes.length > 0)
		{
			this.layer_ = new TeWMSLayer();
			this.layer_.readFromXML(serviceLayerNodeList);
			//alert("TOTAL DE SUB: " + this.layer_.getLayers().length);
		}
	}
	
	function getElementXML(elem, arr, indice) {
		//alert(elem.nodeName + "\n" + elem.childNodes.length + "\n" + indice);
		if (arr.length == indice) return null;
		if (elem.nodeType != Node.ELEMENT_NODE) return null;
		
		if (elem.nodeName == arr[indice] && indice == (arr.length-1))
			return elem;
		if (elem.nodeName == arr[indice])
			for (var i=0;i<elem.childNodes.length;i++) {
				var returnElem = getElementXML(elem.childNodes[i], arr, indice+1);
				if (returnElem) return returnElem;
			}
		return null;
	}
}
/*
    * Node.ELEMENT_NODE == 1
    * Node.ATTRIBUTE_NODE == 2
    * Node.TEXT_NODE == 3
    * Node.CDATA_SECTION_NODE == 4
    * Node.ENTITY_REFERENCE_NODE == 5
    * Node.ENTITY_NODE == 6
    * Node.PROCESSING_INSTRUCTION_NODE == 7
    * Node.COMMENT_NODE == 8
    * Node.DOCUMENT_NODE == 9
    * Node.DOCUMENT_TYPE_NODE == 10
    * Node.DOCUMENT_FRAGMENT_NODE == 11
    * Node.NOTATION_NODE == 12 
*/
