
// converts XML string to an array of JS objects
// ---------------------------------------------
// function: xmlToObject(String text)

function xmlToObject (text)
{
var itt=0;
	if(text=="<data/>")
	{
		return new Array();
	}
	// code for IE
	var doc = null;
	if (window.ActiveXObject)
	{
		doc=new ActiveXObject("Microsoft.XMLDOM");
		doc.async="false";
  		doc.loadXML(text);
	}else {
		parser=new DOMParser();
    	doc=parser.parseFromString(text,"text/xml");
	}
	
	var dataNode=doc.documentElement;

	//Create Class
	var mynode=dataNode.childNodes[0];
/////////////////////////////////////////////////////////NEW
	
function categories(){
	this.value;
}

function rejectionReason(){
	this.value;
}

function News(){
	this.id;
	this.dbId;
	this.title;
	this.date;
	this.author;
	this.importance;
	this.mainImportance;
	this.badgeImportance;
	this.slideShow;
	this.rejectionReason;
	this.rejectionComment;
	this.rejectionStatus;
	this.categories;
	this.locker;
}
function Journalism(){
	this.id;
	this.dbId;
	this.title;
	this.date;
	this.author;
	this.importance;
	this.mainImportance;
	this.badgeImportance;
	this.slideShow;
	this.rejectionReason;
	this.rejectionComment;
	this.rejectionStatus;
	this.type;
	this.locker;
}
function FileReport(){
	this.id;
	this.dbId;
	this.title;
	this.date;
	this.prepareDate;
	this.author;
	this.importance;
	this.mainImportance;
	this.badgeImportance;
	this.slideShow;
	this.rejectionReason;
	this.rejectionComment;
	this.rejectionStatus;
	this.type;
	this.categories;
	this.locker;
}
function Episode(){
	this.id;
	this.dbId;
	this.title;
	this.date;
	this.author;
	this.importance;
	this.mainImportance;
	this.badgeImportance;
	this.slideShow;
	this.rejectionReason;
	this.rejectionComment;
	this.rejectionStatus;
	this.locker;
}
function IntWrapper(){
	this.value;
}
function ExceptionWrapper(){
	this.errorMessage;
}

	//////////////////////////////////////////////////END NEW

	var dataNode=doc.documentElement;
	//Create Class
	var mynode=dataNode.childNodes[0];
	////////////////////NEW
	for(var r=0;r<mynode.childNodes.length;r++)
	{
		if(mynode.childNodes[r].childNodes[0]!=null){
			if(mynode.childNodes[r].childNodes[0].tagName!=undefined)//create the second class in class
			{ 	
				if(veryFirstTag=="")
				{
					veryFirstTag=mynode.childNodes[r].childNodes[0].tagName;
					break;
				}
			}
		}
	}
	///////////////////End New

	
	var sString="";
	var sString2="";
	var lastTag;
	var veryFirstTag="";
	//TO DO: check if already exists.
	//alert(mynode.childNodes.length);
/*	for(var r=0;r<mynode.childNodes.length;r++)
	{
		if(mynode.childNodes[r].tagName==lastTag)
		{
			//alert(lastTag+"&"+mynode.childNodes[r].tagName);
			continue;
		}
		//alert("Value: "+mynode.childNodes[r].childNodes[0].tagName);
		if(mynode.childNodes[r].childNodes[0]!=null){
			if(mynode.childNodes[r].childNodes[0].tagName!=undefined)//create the second class in class
			{ 	
				if(veryFirstTag=="")
				{
					veryFirstTag=mynode.childNodes[r].childNodes[0].tagName;
				}
				for(it=0;it<mynode.childNodes[r].childNodes.length;it++)
				{
					//alert("mynode.childNodes[r].childNodes[it].tagName: "+mynode.childNodes[r].childNodes[it].tagName);
					sString2+="this."+mynode.childNodes[r].childNodes[it].tagName+";";//="+mynode.childNodes[r].tagName+";";
				}
				//alert(sString2);
				lastTag=mynode.childNodes[r].tagName;
				//alert("lastTag: "+lastTag);
				//alert("EVAL mynode.childNodes[r].tagName:"+ mynode.childNodes[r].tagName);
				eval ("function "+mynode.childNodes[r].tagName+"(){"+ sString2+"}")
				
			}
		}
		//alert("mynode.childNodes[r].tagName: "+mynode.childNodes[r].tagName);
		sString+="this."+mynode.childNodes[r].tagName+";";//="+mynode.childNodes[r].tagName+";";
		//alert(r+"  "+sString);
	}
	eval ("function "+mynode.tagName+"(){"+ sString+"}");
	//End
*/
	
	var objects = new Array();
	this.subObjectName=new Array();
	this.arraySubObjects2=new Array();
	lastTag=veryFirstTag;
	doIt();
	function doIt()
	{
		arraySubObjects=new Array();
		
		
		var j=0;
		//alert("Length of the array of monkeys:"+dataNode.childNodes.length);
		for (var i = 0; i < dataNode.childNodes.length; i++) {
		//alert("Now in node #:" + i);
		
		    var node = dataNode.childNodes[i];
		    if (node.tagName) {
				var obj = readObject(node,arraySubObjects);//alert("TT"+arraySubObjects[0][1].favorite);
				//if(this.arraySubObjects2!=undefined){//  The IF is ADDED on May6th
					for(jjj=0;jjj<itt+1;jjj++){
						obj[this.subObjectName[jjj]]=arraySubObjects[jjj];
					}
				//}
		        if (obj) {objects[j++] = obj;}
				
		    }
		}
	//alert(objects[0].name);	
	}
	function readObject(node,arraySubObjects) {
	//alert("Now in node " + node.tagName);
	var k=0;
	    var obj = createObject(node.tagName);
		//alert("#"+node.childNodes.length);
	    for (var i = 0; i < node.childNodes.length; i++) {
	      
			var childNode = node.childNodes[i];
			
			if(childNode.childNodes[0]!=null){
				//alert("Node Value: "+childNode.childNodes[0].nodeValue);
				if(childNode.childNodes[0].tagName){//Means this is food
					
					//alert("1"+childNode.tagName);
					if(childNode.tagName!=lastTag){
						itt++;
						k=0;
						//alert(childNode.tagName+" k "+lastTag);
						lastTag=childNode.tagName;
					}
					this.subObjectName[itt]=childNode.tagName;
					//alert(this.subObjectName[itt]);
					//alert("lastTag"+lastTag);
					var obj2=readObject(childNode);
					
					if(k==0){
						//alert(itt);
						arraySubObjects[itt] = new Array();
					}
					//alert("arraySubObjects[itt,k++]=obj2;");
					
					arraySubObjects[itt][k++]=obj2;
					//alert("itt="+itt+" K="+k+" LLL"+arraySubObjects[itt][k-1].d);
					//this.arraySubObjects2=arraySubObjects;	
					continue;
				}
			}
	        if (childNode.tagName) {
		    var val = readAttr(childNode);
	            if (val) setAttrValue(obj, childNode.tagName, val);
	        }
	    }
	    return obj;
	}

	function createObject(tagName) {
	   return eval("new " + tagName + "()");
	}

	function readAttr(node) {
		if(node.childNodes[0]==null)
		{
			return "null";
		}
	   return node.childNodes[0].nodeValue;
	}

	function setAttrValue(obj, tagName, val) {
	   return obj[tagName] = val;
	}
	

	return objects;
}
function ConcreteAuthoringContent() {
	this.id;
	this.title;
	this.preTitle;
	this.thumbnail;
	this.summary;
}

function ChatQuestion(){
	this.questionId;
	this.question;
	this.answer;
	this.personName;
	this.personProfession;
	this.questionState;
}
