// sab_xml_MapServ.js
// Assists in parsing a Mapserver WFS response.  
// This file uses the following third-party open source files from XML for script: http://xmljs.sourceforge.net/index.html
// formFunctions.js and xmldom.js (Cross-browser xml parsing scripts)



// sab_readXMLString
// Read the XML String and store in the obj Object.
// obj object will contain the xml, AttNames array, and AttValues array       
function sab_readXMLString(objDom, strXML) {

    var src = strXML;
    errs = "";
    var xd;
    
    objDom.loadXML(strXML);    
    
    if(objDom.hasErrors){
        writeErrorReport();
    }            
    
    obj.xmlDoc = objDom;
    obj.AttNames = [];
    obj.AttValues = [];
    
    return obj;
}

// sab_getFeatureNode
// Selects the featureMember node with the ms:Geometry node removed
function sab_getFeatureNode(objDom, strQueryLayer) {

 // Do not need to specify the root node (wfs:FeatureCollection) in tag path below...
    var node = objDom.selectNode("/gml:featureMember/ms:" + strQueryLayer);    
    return node;        
}
 
 // sab_removeGeoNode
 // Remove the ms:Geometry node.  Other attributes elements also begin with the ms:, 
 //         so may want to delete the ms:Geometry element before looping throught the attributes.
function sab_removeGeoNode(objDom, strQueryLayer) {   
    
    var varGeoNode = objDom.selectNode("/gml:featureMember/ms:" + strQueryLayer + "/ms:msGeometry");  
    if (varGeoNode != null){     
        objDom = objDom.removeNodeFromTree(varGeoNode);
    }    
    return objDom;
}
 
// sab_getAttributeNamesValues
// Retrieves the attribute names and values from xml.  Stores them in parallel
// arrays of the object.  Obj is returned.  It contains original xml and arrays: AttNames and AttValues.
function sab_getAttributeNamesValues(obj, node) {
    var fnames = [];
    var fvalues = [];  
    var counter = 0;

    var nodeArray = [];
    nodeArray = node.getElements();
       
    for(var i = 0; i < node.children.length; ++i) {     
       if (node.children[i].tagName.indexOf("ms:") != -1) {
            fnames[counter] = (node.children[i].tagName).substr(3);  //remove the ms:         
            fvalues[counter] = node.children[i].getText();                
            counter++;            
        }                    
     }
      obj.AttNames = fnames;
      obj.AttValues = fvalues;
     
     return obj;  
}
 
 
 
 
 
 