// ajax_utilities.js

// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix,local,parentElem,index) {
  var result=null;
  if (prefix && isIE) {
    // IE/Windows way of handling namespaces
    result=parentElem.getElementsByTagName(prefix+":"+local)[index];
  } else {
    // the namespace versions of this method 
    // (getElementsByTagNameNS()) operate
    // differently in Safari and Mozilla, but both
    // return value with just local name, provided 
    // there aren't conflicts with non-namespace element
    // names
    result=parentElem.getElementsByTagName(local)[index];
  }
  if (result) {
    // get text, accounting for possible
    // whitespace (carriage return) text nodes 
    if (result.childNodes.length > 1) {
      return result.childNodes[1].nodeValue;
    } else {
      return result.firstChild.nodeValue;    		
    }
  } else {
    return null;
  }
}

function parseResponse(responseXML,errors_only) {
  var list=new Array(new Array,new Array);
  var id,value;
  if (errors_only)
    var content=responseXML.getElementsByTagName("error");
  else
    var content=responseXML.getElementsByTagName("content");
  if (content.length>0) {
    for (var i=0;i<content.length;++i) {
      id=getElementTextNS("", "id", content[i], 0);
      value=getElementTextNS("", "value", content[i], 0);
      if (id && value) {
        list[0].push(id);
        list[1].push(value);
      }
		}
		if (list[0].length>0) {
		  return list;
		} else {
		  return false;
		}
  } else {
    return false;
  }
}

function getParsedItemById(parsedItems,id) {
  for (var i=0;i<parsedItems[0].length;++i) {
    if (parsedItems[0][i]==id) {  // is this the id sought?
      return parsedItems[1][i];   // return the value for this id
    }
  }
  return null;
}
