
 /***************************************************************
  * @authot : Randrianarivelo Tiana
  * @Date ! 18/06/08
  * @version : 0.1
  * @Dependancies : dojox.xml 
  * SOS response utilities
  * Function lists :
  */
  /** Components utilities **/
var sensorinfos = {
	getComponentInfos  : function(response){
		var component = {};
		try{
			var root = this.convertResponse(response);
			component.description=this.getComponentName(root);
			component.phenomenon=this.getComponentPhenomenon(root);
			component.units=this.getComponentUnits(root);
		}
		catch(e){
			component = null;
		}
		return component;
	},
	
	getSystemInfos : function(response){
		var system = {};
		var root = this.convertResponse(response);
		system.description=this.getSystemName(root);
		system.components=[];
		system.components=this.getSystemComponents(root);
		return system;
	},
	
	convertResponse : function(response){
		var string = (new XMLSerializer()).serializeToString(response);
		string=string.replace(/&lt;/gi, "\<").replace(/&gt;/gi, "\>");
		var root = dojox.xml.DomParser.parse(string);
		return root;
	},
	
	isValidSystem : function(system){
		return (root.byName('System')!=null);
	},
	isValidComponent : function(component){
		return (root.byName('Component')!=null);
	},
	getComponentName : function(root){
		return root.byName('Component')[0].byName('gml:name')[0].childNodes[0].nodeValue;
	},
	
	getComponentPhenomenon : function(root){
		return root.byName('Component')[0].byName('outputs')[0].byName('OutputList')[0].byName('output')[0].attributes[0].nodeValue;
	},
	
	getComponentUnits : function(root){
		var i = this.getElementByAttributeValue(root.byName("parameter"),"name","precision");
		var uom ="???";
		if (i!=-1 ){
			var elt = root.byName("parameter")[i];
			elt = elt.byName("swe:Quantity")[0].childNodes[0];
			uom=this.getAttributes(elt,"code");
		}
		if(uom == null || i==-1){
			uom =this.getComponentPhenomenon(root);
			if(uom.indexOf("depth",0)!=-1)  uom="m";
			if(uom.indexOf("temperature",0)!=-1) uom="&deg;C";
			if(uom.indexOf("conductivity",0)!=-1) uom="---";			
		}
		return uom ;		

	},
	getElementByAttributeValue : function(nodes,attribute,value){
		var j =-1;
		var temp;
		for(var i=0;i<nodes.length;i++){
			temp= this.getAttributes(nodes[i],attribute);
			if(temp.indexOf(value,0)!=-1 ){
				j = i;
				i=nodes.length;
			}
		}
		return j;
	},
	
	
	getSystemName : function(root){
		return root.byName('System')[0].byName('gml:name')[0].childNodes[0].nodeValue;
	},
	
	getSystemComponents : function(root){
		var components = [];
		var lists = root.byName('components')[0].byName('ComponentList')[0].byName('component');
		for(var i=0;i<lists.length;i++){
			components[i]=this.getAttributes(lists[i],"xlink:href");
		}
		return components;	
	},
	
	getAttributes : function(node,name){
		for(var i=0; i<node.attributes.length; i++){
			if(node.attributes[i].nodeName==name){
				return node.attributes[i].nodeValue;
			}
		}
		return null;
	},
	getObservationTemplate : function(response){
		var return_value="";
		try{
			var root = this.convertResponse(response);
			return_value= root.byNameNS('Observation','http://www.opengis.net/om/1.0')[0].byNameNS('name','http://www.opengis.net/gml')[0].childNodes[0].nodeValue;
			return return_value;
		}
		catch(e){
			return "";
		}
	},
	getResult : function(response){
		var return_value="";
		try{
			var root = this.convertResponse(response);
			return_value=root.byNameNS('result','http://www.opengis.net/sos/1.0')[0].childNodes[0].nodeValue;
		}
		catch(e){
			return_value="";
		}
		return return_value;
	},
	
	getIdValid : function(componentid){
		if(componentid=="00ARGLELES_0001") return "00ARGLELES_2000-valid";
		if(componentid=="0HIPPOLYTE_0001") return "0HIPPOLYTE_2000-valid";
		if(componentid=="0000195554_0010") return "0000195554_2000-valid";
		if(componentid=="0000195554_0020") return "0000195554_2002-valid";
		if(componentid=="0000195554_0021") return "0000195554_2003-valid";
		if(componentid=="F_EXTRAMAR_0001") return "F_EXTRAMAR_2000-valid";
		if(componentid=="F_EXTRAMAR_0002") return "F_EXTRAMAR_2002-valid";
		if(componentid=="000MOUSSAN_0001") return "000MOUSSAN_2000-valid";
		if(componentid=="0000189940_0001") return "0000189940_2000-valid";
	}
	
}
