with (classes) {

with (dom) {

with (xpath) {

if (classes.XPathResult) xpath.XPathResult = classes.XPathResult;
else {

xpath.XPathResult = function (Result_Object, resultType) // http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathResult
{
 with (XPathResult)
 {
  this.The_Object = Result_Object;
  this.resultType = resultType;

  this.numberValue =
  this.stringValue =
  this.booleanValue =

  this.singleNodeValue =

  this.invalidIteratorState =

  this.snapshotLength = null;

  this.node_index =
  this.node_count = null;

  with (this)
  {
   if (The_Object == null) throw new XPathException(XPathException.TYPE_ERR);

   var isNodeList = ! isNaN(The_Object.length);

   var Value_Number = Number(isNodeList? The_Object.length : The_Object);

   switch (resultType)
   {
    case ANY_TYPE:
    case UNORDERED_NODE_ITERATOR_TYPE:
    case ORDERED_NODE_ITERATOR_TYPE:
    {
     if (! isNodeList)
     {
      if (resultType != ANY_TYPE) throw new XPathException(XPathException.TYPE_ERR);
     }
     else
     {
      node_index = 0;

      if (! (node_count = Value_Number)) invalidIteratorState = true;

      if (resultType == ANY_TYPE) resultType = UNORDERED_NODE_ITERATOR_TYPE;

      break;
     }
    }
    case UNORDERED_NODE_SNAPSHOT_TYPE:
    case ORDERED_NODE_SNAPSHOT_TYPE:
    {
     if (! isNodeList)
     {
      if (resultType != ANY_TYPE) throw new XPathException(XPathException.TYPE_ERR);
     }
     else
     {
      snapshotLength = Value_Number;

      if (resultType == ANY_TYPE) resultType = UNORDERED_NODE_SNAPSHOT_TYPE;

      break;
     }
    }
    case ANY_UNORDERED_NODE_TYPE:
    case FIRST_ORDERED_NODE_TYPE:
    {
     if (! (isNodeList /* && (Value_Number == 0 || Value_Number == 1) */))
     {
      if (resultType != ANY_TYPE) throw new XPathException(XPathException.TYPE_ERR);
     }
     else
     {
      singleNodeValue = The_Object.item(0);

      if (resultType == ANY_TYPE) resultType = ANY_UNORDERED_NODE_TYPE; // ??

      break;
     }
    }
    case NUMBER_TYPE:
    {
     if (isNaN(Value_Number))
     {
      if (resultType != ANY_TYPE) throw new XPathException(XPathException.TYPE_ERR);
     }
     else
     {
      numberValue = Value_Number;

      if (resultType == ANY_TYPE) resultType = NUMBER_TYPE;

      break;
     }
    }
    case STRING_TYPE:
    {
     stringValue = typeof The_Object == "string"? The_Object
                 : isNodeList? The_Object.length == 1? The_Object.item(0).text : "" // !!
                 : The_Object.text;

     resultType = STRING_TYPE;
    }
     break;
    case BOOLEAN_TYPE:
    {
     booleanValue = isNaN(Value_Number)? false : Value_Number? true : The_Object? true : false; // !!

     resultType = BOOLEAN_TYPE;
    }
     break;
    default: throw new XPathException(XPathException.TYPE_ERR);
   }
  }
 }

 return this;
}

XPathResult.ANY_TYPE = 0;
XPathResult.NUMBER_TYPE = 1;
XPathResult.STRING_TYPE = 2;
XPathResult.BOOLEAN_TYPE = 3;
XPathResult.UNORDERED_NODE_ITERATOR_TYPE = 4;
XPathResult.ORDERED_NODE_ITERATOR_TYPE = 5;
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE = 6;
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE = 7;
XPathResult.ANY_UNORDERED_NODE_TYPE = 8;
XPathResult.FIRST_ORDERED_NODE_TYPE = 9;

XPathResult.prototype.iterateNext = function () { with (XPathResult) with (this)
{
 if (! (resultType == UNORDERED_NODE_ITERATOR_TYPE || resultType == ORDERED_NODE_ITERATOR_TYPE)) throw new XPathException(XPathException.TYPE_ERR);

 if (node_index + 1 == node_count) invalidIteratorState = true;

 return node_index == node_count? null : The_Object.item(node_index ++);
}}

XPathResult.prototype.snapshotItem = function (index) { with (this)
{
 if (isNaN(index) || index < 0) return null; // throw new XPathException;

 return index >= snapshotLength? null : The_Object.item(index);
}}

}

}

}

}
