//++Class DOMIterator+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Constructor and prototype declaration for my own personal implementation of the DocumentTraversal.NodeIterator
// object, since none of the browser vendors can be bothered to implement this very useful spec.
// Parameters: topNode is the top node to be considered for traversal, nodeTypeToAccept is the Node.TYPE constant
// that will be checked against topNode.nodeType to select only nodes of the given type.
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function DOMIterator (topNode, nodeTypeToAccept)  {
  this.topNode = topNode;
  this.nodeTypeToAccept = nodeTypeToAccept;
  this.nodes = [];
  
  //Initialize the nodes array, which contains a flattened, filtered version of the DOM hierarchy starting at node topNode
  var walkTheDOM = function (curNode, nodeArray, nodeTypeToAccept)  {
    if (curNode.nodeType == nodeTypeToAccept)  { nodeArray.push (curNode); }
    var returnVal = null;
    
    for (var i=0; i < curNode.childNodes.length; i++) {
      returnVal = walkTheDOM (curNode.childNodes.item(i), nodeArray, nodeTypeToAccept);
    }
    
    return nodeArray;
  };
  this.nodes = walkTheDOM (this.topNode, this.nodes, this.nodeTypeToAccept);
  
  this.nodeIndex = 0;
}

DOMIterator.prototype.nextNode = function()  {
  if (this.nodeIndex <= this.nodes.length - 1)  {
    this.nodeIndex++;
    return this.nodes[this.nodeIndex];
  }
  else  { return false; }
};

DOMIterator.prototype.prevNode = function()  {
  if (this.nodeIndex >= 0)  {
    this.nodeIndex--;
    return this.nodes[this.nodeIndex];
  }
  else  { return false; }
};

DOMIterator.prototype.firstNode = function()  {
  this.nodeIndex = 0;
  return this.nodes[0];
};

DOMIterator.prototype.lastNode = function()  {
  this.nodeIndex = this.nodes.length - 1;
  return this.nodes[this.nodeIndex];
};
//++End Class DOMIterator+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


//--function updatePriceIfReasonable------------------------------------------------------------------------------
// Calls updatePrice if it determines, by inspecting the state of the form widgets, if this is a reasonable thing
// to do. Responsible for gathering the data for updatePrice from the form.
//
// pid: the product id to price
// idSuffix: a suffix to be appended to all HTML ID's, because the edit dialogs have their ID's suffixed
//----------------------------------------------------------------------------------------------------------------
var optionsDataArray = null;  //Object/Array of options_valus_ids matched to their price and/or priceGroupID; initialized by a script lower in the page.
var checkboxOptionsArray = null;  //Ditto above but for checkbox options, which must be treated differently because their value is an option_id and not an option_value_id like the rest of them. Can't have the potential for key collision, now, can we?
var colorDataArray = null;  //Ditto above but for color: priceGroupID
function updatePriceIfReasonable(pid, idSuffix)  {
  var width = document.getElementById ('AddToCartWidth' + idSuffix);
  var height = document.getElementById ('AddToCartHeight' + idSuffix);
  if (!(width.value > 0) || !(height.value > 0))  { return; }
  
  var fracWidth = document.getElementById ('AddToCartFracWidth' + idSuffix);
  var fracHeight = document.getElementById ('AddToCartFracHeight' + idSuffix);
  if (fracWidth.value == -1)  { fracWidth = 0; }
  else  { fracWidth = parseInt(fracWidth.value); }
  if (fracHeight.value == -1)  { fracHeight = 0; }
  else  { fracHeight = parseInt(fracHeight.value); }
  
	// Getting the colors is trickier than it ought to be because it's a series of radio buttons
  if (typeof zoomingEditForm == 'undefined')  zoomingEditForm = false;
	if (zoomingEditForm)  var colors = document.editDialogForm.color;
	else  var colors = document.cart_quantity.color;
  var priceGroupID = 0;
  for (var i=0; i < colors.length; i++) {
    if (colors[i].checked)  priceGroupID = colorDataArray[pid][colors[i].value];
  };
  
  var optionsPrice = 0;
  //Walk the nodes in the options and act on their values
  var it = new DOMIterator (document.getElementById('AddToCartForm' + idSuffix), 1);
  var curNode = null;
	var dimCons = { minWidth: -1.0,
									maxWidth: -1.0,
									minHeight: -1.0,
									maxHeight: -1.0 };
  while (curNode = it.nextNode())  {
    if (curNode.checked)  {
      //Identify and process non-checkbox options
      if (curNode.name.substr(0, 2) == 'id')  {
        if (optionsDataArray[pid][curNode.value]['use_priceGroupID'] > 0)  { priceGroupID = optionsDataArray[pid][curNode.value]['use_priceGroupID']; }
        optionsPrice += optionsDataArray[pid][curNode.value]['ovPrice'];
				
				if (typeof(optionsDataArray[pid][curNode.value]['opDimCons'] != 'undefined'))  {
					if (optionsDataArray[pid][curNode.value]['opDimCons']['minWidth'] > dimCons['minWidth'])  dimCons['minWidth'] = optionsDataArray[pid][curNode.value]['opDimCons']['minWidth'];
					if (optionsDataArray[pid][curNode.value]['opDimCons']['maxWidth'] > dimCons['maxWidth'])  dimCons['maxWidth'] = optionsDataArray[pid][curNode.value]['opDimCons']['maxWidth'];
					if (optionsDataArray[pid][curNode.value]['opDimCons']['minHeight'] > dimCons['minHeight'])  dimCons['minHeight'] = optionsDataArray[pid][curNode.value]['opDimCons']['minHeight'];
					if (optionsDataArray[pid][curNode.value]['opDimCons']['maxHeight'] > dimCons['maxHeight'])  dimCons['maxHeight'] = optionsDataArray[pid][curNode.value]['opDimCons']['maxHeight'];
				}
      }
      //Identify and process checkbox options
      else if (curNode.name.substr(0, 8) == 'checkIDs')  {
        if (checkboxOptionsArray[pid][curNode.value]['use_priceGroupID'] > 0)  { priceGroupID = checkboxOptionsArray[pid][curNode.value]['use_priceGroupID']; }
        optionsPrice += checkboxOptionsArray[pid][curNode.value]['ovPrice'];
      }
    }
  }

  updatePrice (parseInt(width.value), fracWidth, parseInt(height.value), fracHeight, dimCons['minWidth'], dimCons['maxWidth'], dimCons['minHeight'], dimCons['maxHeight'], optionsPrice, priceGroupID, 'PinfoPriceDisplay'+idSuffix, 'WidthErrorMessage'+idSuffix, 'HeightErrorMessage'+idSuffix);
  return;
}