var not_whitespace = new RegExp(/[^\s]/);
var parent_count;
var prefix = "a_";
var flagConcatenate = 0;
//Process the xml data
function xml2array(xmlDoc,parent_count)
{

	var arr = new Object;
	var parent = "";
	parent_count = parent_count || new Object;

	if(xmlDoc.nodeName && xmlDoc.nodeName.charAt(0) != "#")
	{
			//arr = new Object;
			parent = xmlDoc.nodeName;
			
	}

	var value = xmlDoc.nodeValue;
	//If its a child
	if(xmlDoc.parentNode && xmlDoc.parentNode.nodeName && value)
	{
		if(not_whitespace.test(value))
		{
			//arr = new Object;
// 			if ((browser.isGecko) && (value.length > 4095))

			if (value.length > 4095)
			{
				nextSiblingElement = xmlDoc.nextSibling;
				while (nextSiblingElement)
				{
					value = value + nextSiblingElement.nodeValue;
					flagConcatenate = flagConcatenate + 1;
					if (nextSiblingElement.nodeValue.length > 4095)
					{
						nextSiblingElement = nextSiblingElement.nextSibling;
					}
					else
					{
						nextSiblingElement = false;
					}
				}
				arr[xmlDoc.parentNode.nodeName] = value;
			}
			else
				arr[xmlDoc.parentNode.nodeName] = value;
		}
	}

	//If have child
	if(xmlDoc.childNodes)
	if(xmlDoc.childNodes.length)
	{
		//Just one item in this tag.
		if(xmlDoc.childNodes.length == 1)
		{
			arr = xml2array(xmlDoc.childNodes[0],parent_count); //:RECURSION:
		}
		//If there is more than one childNodes, go thru them one by one and get their results.
		else
		{
			var indexElement = 0;
			//Go thru all the child nodes.
			for(var i=0; i<xmlDoc.childNodes.length; i++)
			{
				//:RECURSION:
				var temp = xml2array(xmlDoc.childNodes[i],parent_count);
				if(temp)
				{

					var assoc = false;
					var arr_count = 0;
					for(key in temp)
					{
						if(isNaN(key)) assoc = true;
						arr_count++;
						//We just need to know wether it is a single value array or not
						if(arr_count>2) break;
					}
					if(assoc && arr_count == 1)
					{
						//If another element exists with the same tag name before,
						if(arr[key])
						{
							//put it in a numeric array.
							//Find out how many time this parent made its appearance
							if(!parent_count || !parent_count[key])
							{
								parent_count[key] = 0;

								var temp_arr = arr[key];
								arr[key] = new Object;
								arr[key][0] = temp_arr[key];
							}
							parent_count[key]++;
							//Members of of a numeric array
							arr[key][parent_count[key]] = temp[key][key];
						}
						else
						{
							parent_count[key] = 0;
							arr[key] = temp[key];
						}
					}
					else
					{

// 						if (!arr)
// 						{
// 							arr = new Object;
// 							arr[indexElement] = new Object;
// 						}
						if (temp)
							arr[indexElement] = temp;
						else
							arr[indexElement] = "";
						indexElement++;
					}
					if (flagConcatenate > 0)
					{
						i = i + flagConcatenate;
						flagConcatenate = 0;
					}
				}
			}
		}
	}

	if(parent && arr) {
		var temp = arr;
		arr = new Object;
		
		arr[parent] = temp;
	}

	if(xmlDoc.attributes)
		for(var j=0; j<xmlDoc.attributes.length; j++)
		{
			var nname = xmlDoc.attributes[j].nodeName;
			if(nname)
				/* Value in the tag and Attribute otside the tag(in parent) */
				arr[prefix + nname] = xmlDoc.attributes[j].nodeValue;
		}
	return arr;
}

