// This class encapsulate XML DOM Document as an array.

// This function is construction function.
function XMLArray(){
	DeliverXMLArray(this);
}

// This function is used for inheritance.
function DeliverXMLArray(toObject){
	DeliverXMLBaseObject(toObject);
	toObject.arrayName = "Item";
	toObject.idColumnName = "DataId";
	toObject.currentItem = null;
	toObject.addItem = XMLArrayAddItem;
	toObject.setAttribute = XMLArraySetAttribute;
	toObject.setAttributeBool = XMLArraySetAttributeBool;
	toObject.getAttribute = XMLArrayGetAttribute;
	toObject.setCurrentIndex = XMLArraySetCurrentIndex;
	toObject.getCount = XMLArrayGetCount;
	toObject.getId = XMLArrayGetId;
	toObject.locateItem = XMLArrayLocateItem;
	toObject.removeItem = XMLArrayRemoveItem;
	toObject.removeItems = XMLArrayRemoveItems;
	toObject.findAttribute = XMLFindAttribute;
	toObject.addItemWith = XMLArrayAddItemWith;
	toObject.moveItemFrom = XMLArrayMoveItemFrom;
	toObject.selectItems = XMLArraySelectItems;
	toObject.getCurrentItemObject = XMLArrayGetCurrentItemObject;
	toObject.getItems = XMLArrayGetItems;
	toObject.baseId = 0;
	toObject.idPrifix = "Id";
	toObject.idAutoGenerated = false;
}

// Add a new item in the array. The current item is the new one.
function XMLArrayAddItem(){
	this.currentItem = this.getXMLDocument().createElement(this.arrayName);
	this.rootElement.appendChild(this.currentItem);
	this.baseId++;
	this.setAttribute(this.idColumnName, this.baseId);
}

function XMLArrayAddItemWith(toItem){
	if(toItem!=null){
		this.rootElement.appendChild(toItem);
	}
}

function XMLArrayMoveItemFrom(toXMLArray, tnId){
	if(toXMLArray.locateItem(tnId)){
		var loCurrentItem = toXMLArray.currentItem;
		toXMLArray.removeItem();
		this.addItemWith(loCurrentItem);
	}
}

// Set an attribute to the current item with the given value.
function XMLArraySetAttribute(tsName, tsValue){
	var loAttribute;
	if(this.currentItem!=null){
		loAttribute = this.currentItem.attributes.getNamedItem(tsName);
		if(loAttribute==null){
			this.addAttribute(this.currentItem, tsName,tsValue);
		}else{
			if(tsValue==null) loAttribute.nodeValue = "";
			else loAttribute.nodeValue = tsValue;
		}
	}	
}

// Set an attribute with a boolean value. 
function XMLArraySetAttributeBool(tsName,tsValue){
	if(tsValue){
		this.setAttribute(tsName,"True");
	}else{
		this.setAttribute(tsName,"False")
	}
}

// Get an attribute value of the current item.
function XMLArrayGetAttribute(tsName){
	var loAttribute;
	if(this.currentItem!=null){
		loAttribute = this.currentItem.attributes.getNamedItem(tsName);
		if( loAttribute != null) return loAttribute.nodeValue;
		else return null;
	}else{
		return null;
	}
} 

// Find an attribute of the current item.
function XMLFindAttribute(tsName){
	var loAttribute=null;
	if(this.currentItem!=null){
		loAttribute = this.currentItem.attributes.getNamedItem(tsName);
	}
	return loAttribute;
}

// Set the index of the current item.
function XMLArraySetCurrentIndex(tnIndex){
	this.currentItem = this.rootElement.childNodes[tnIndex];
}

// Return the count of the array.
function XMLArrayGetCount(){
	return this.rootElement.childNodes.length;
}

function XMLArraySelectItems(tsFieldName, tsValue){
	var loItems = new XMLItemCollection();
	loItems.setNodeList(this.rootElement.selectNodes(this.arrayName+"[@"+tsFieldName+"=\""+tsValue+"\"]"));
	return loItems;
}

function XMLArrayGetItems(tsIds){
	var loItems = new XMLItemCollection();
	for(var lnIndex=0; lnIndex<tsIds.length; lnIndex++){
		if(this.locateItem(tsIds[lnIndex])) loItems.addItem(this.currentItem);
	}
	return loItems;
}

// Locate an item with an Id. The ID column is defined with the property - idColumnName. The default name is DataId.
function XMLArrayLocateItem(tnId){
	var loFoundNodes = this.rootElement.selectNodes(this.arrayName+"[@"+this.idColumnName+"=\""+tnId+"\"]");
	if(loFoundNodes.length>0){
		this.currentItem = loFoundNodes[0];
		return true;
	}else{
		this.currentItem = null;
		return false;
	}	
}

// Remove the current item from the array and set the current item null.
function XMLArrayRemoveItem(tnId){
	if(tnId!=null) this.locateItem(tnId);
	if(this.currentItem!=null){
		this.rootElement.removeChild(this.currentItem);
		this.currentItem = null;
	}
}
// Remove the select items
function XMLArrayRemoveItems(toItems){
	for(var lnIndex=0; lnIndex<toItems.getCount();lnIndex++)
	{
		this.rootElement.removeChild(toItems.items[lnIndex]);
	}
}
function XMLArrayGetCurrentItemObject(){
	if(this.currentItem!=null){
		var loItemObject = new Object();
		var loAttribute = null;
		for(var lnIndex=0; lnIndex<this.currentItem.attributes.length; lnIndex++){
			loAttribute = this.currentItem.attributes.Item(lnIndex);
			loItemObject[loAttribute.nodeName] = loAttribute.nodeValue;
		}
		return loItemObject;
	}
	return null;
}

function XMLArrayGetId(){
	return this.getAttribute(this.idColumnName);
}
