// Using this function:
// This will grab all form elements on your page, and build an XML string out of their current values.
// Each item needs a valid ID.  The XML node will be named after the item's ID.
// If you have HTML editor windows, you need to list them as the second paramater, after they've all been initialized.
// The second paramater expects data in an object built like an associative array, with the key for the entry being what
// you would like the XML node to be named (i.e. "pageText" or "newsletterBody") and the entry's value being the editor object
// (most likely oEdit0, if you've used the 'initializeWYSIWYG' method)
//
// EX: I have my NewModule textContent attribute in a wysiwg, and I'm doing AJAX-style saving.
// var editorFields = {'textContent': oEdit0};
// var xmlString = getXmlFromPageElements('moduleItem', editorFields);
// this.ro.connect(this.script, "POST", xmlString, this.handler);


function getXmlFromPageElements(nodeName, editors)
{
	//Create the XML writer object...
	this.xml = new XMLWriter();

	//Begin the page XML container.
	this.xml.BeginNode(nodeName);

	//Get all inputs of the document.
	inputArray = document.getElementsByTagName('input');
	textareaArray = document.getElementsByTagName('textarea');
	selectArray = document.getElementsByTagName('select');

	for(i=0; i < textareaArray.length; i++ )
	{
		//add them to the XML as setting nodes when found.
		if (textareaArray[i].id != "" && !isArrayKey(textareaArray[i].id, editors))
		{
			this.xml.BeginNode(textareaArray[i].id);
			this.xml.WriteString(textareaArray[i].value);
			this.xml.EndNode();
		}
	}

	// Let's add the Editor() areas, shall we?
	if (null != editors && null !== editors && typeof(editors) == 'object')
	{
		for (var currentEditor in editors)
		{
			this.xml.BeginNode(currentEditor);
			this.xml.WriteString(editors[currentEditor].getXHTMLBody());
			this.xml.EndNode();
		}
	}

	for(i=0; i < selectArray.length; i++ )
	{
		//add them to the XML as setting nodes when found.
		if (selectArray[i].id != "")
		{
			this.xml.BeginNode(selectArray[i].id);
			this.xml.WriteString(selectArray[i].value);
			this.xml.EndNode();
		}
	}

	for(i=0; i < inputArray.length; i++)
	{
		// We only want checkboxes and radios if they've been selected
		if (inputArray[i].id != "" && ((inputArray[i].type != 'radio' && inputArray[i].type != 'checkbox') || inputArray[i].checked))
		{
			this.xml.BeginNode(inputArray[i].id);
			this.xml.WriteString(inputArray[i].value);
			this.xml.EndNode();
		}
	}

	//close the container.
	this.xml.EndNode();

	return this.xml.ToString();
}

function isArrayKey(value, object)
{
	if (null == object || null === object || typeof(object) == 'undefined')
	{
		return false;
	}

	for (var key in object)
	{
		if (key == value)
		{
			return true;
		}
	}

	return false;
}
