/*
version:		1.0
*/

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// XML Writing function
function writeXml( targetObject, targetXml )
{
	// Manually create an "object" type of node in the xml, to store loose variables
	var xmlParentPath:XMLNode = targetXml;
	trace("object:   data:  create node");
	var newXmlTarget:XMLNode = addNode("data", xmlParentPath);
	addAttr( newXmlTarget, "xmlType", "object" );
	
	scanObject( targetObject, newXmlTarget );
}

function scanObject( parentData, targetPath:XMLNode )
{
	trace("");
	trace("");
	trace("");
	trace("scanObject()");
	trace("sourceData:  "+parentData);
	trace("targetPath:  "+targetPath);
	trace("");
	
	
	
	// write attributes
	trace("- Writing attributes -");
	for (childName in parentData)
	{
		trace("childName:  "+childName);
		childPath = parentData[childName];
		trace("childPath:  "+childPath);
		
		var targetPath2:XMLNode = targetPath;		// XML target, can be altered if this is an array
		var childName2 = childName;						// variable name, can be altered if this is an array
		
		if ( typeof( childPath ) == "object")
		{
			//trace("childPath.length:  "+childPath.length);
			if (childPath.length == undefined)
			{
				// this is an object
				childType = "object"
			}else{
				// this is an array
				childType = "array";
			}
		}else{
			// this is a regular variable
			childType = "variable";
		}
		trace("childType:  "+childType);
		
		// write variables
		// // ignore all other types of data  (objects, arrays)
		if ( childType == "variable")
		{
			// if the name is a number, then create a node for this array element
			if ( isNaN( Number(childName)) == false )
			{
				trace(childName+" is a number.");
				// childName is a number, therefore it as an array element
				// add element node  &  set targetPath2 to this new node (for attribute-writing)
				delete targetPath2;
				var targetPath2:XMLNode = addNode( childName, targetPath );
				childName2 = "value";
				addAttr( targetPath2, "xmlType", "variable" );
			}
			// write the current variable as an attribute
			addAttr( targetPath2, childName2, childPath );
		}
	}
	trace("");
	
	
	
	// write nodes
	trace("- Writing nodes -");
	for (childName in parentData)
	{
		//
		trace("childName:  "+childName);
		childPath = parentData[childName];
		trace("childPath:  "+childPath);
		
		if ( typeof( childPath ) == "object")
		{
			//trace("childPath.length:  "+childPath.length);
			if (childPath.length == undefined)
			{
				// this is an object
				childType = "object"
			}else{
				// this is an array
				childType = "array";
			}
		}else{
			// this is a regular variable
			childType = "variable";
		}
		trace("childType:  "+childType);
		
		// write objects, arrays, variable elements
		// // ignore regular variables  (not array elements)
		if ( childType != "variable")
		{
			if ( childType == "object" )
			{
				var newNode:XMLNode = addNode( childName, targetPath );
				addAttr( newNode, "xmlType", "object" );
				scanObject( childPath, newNode );
			}
			
			if ( childType == "array" )
			{
				// create array node
				var newNode:XMLNode = addNode( childName, targetPath );
				addAttr( newNode, "xmlType", "array" );
				scanObject( childPath, newNode );
			}
		}
	}
	delete childName;
	delete childPath;
	delete childType;
}



function addNode( newNodeName:String, xmlPath:XMLNode )
{
	trace("\tADDNODE()");
	trace("\t\tnewNodeName:  "+newNodeName);
	trace("\t\txmlPath:  "+xmlPath);
	var tempXml = new XML();
	var newNode:XMLNode = tempXml.createElement(newNodeName);
	delete tempXml;
	xmlPath.appendChild(newNode);
	var newXmlPath:XMLNode = xmlPath.lastChild;
	trace("\t\tReturn new node:   "+newXmlPath);

	return newXmlPath;
}

function addAttr( targetNode, newVariable, newValue )
{
	targetNode.attributes[newVariable] = newValue;
}
// END:	XML Writing function
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
