var comm_counter=0;
var comm_instances=[];
var showCommunicator = 0;

function Communicator(nextAction)
{
	this.id = 0;
	this.url = "";
	this.method = "get";
	this.responseText = "";
	this.xmlDoc ;
	this.xhttpObject;
	this.status = null;
	this.docTitle = null;
	this.error = null;
	this.nextAction = nextAction;
	this.attributes = {}; //these attributes will be passed to the instance and returned with the instance to the callback method
	
	this.getTable = function (table_name, prefix)
	{
		if(this.xmlDoc==null) // || this.xmlDoc.tagName==null || this.xmlDoc.tagName.toLowerCase()!="xml") 
			return null;
		if(!prefix) prefix = "MetaData";
		return this.xmlDoc.selectNodes(prefix + "/"+ table_name);
	}
	
	this.getRow = function (table_name, prefix)
	{
		if(this.xmlDoc==null) // || this.xmlDoc.tagName==null || this.xmlDoc.tagName.toLowerCase()!="xml") 
			return null;
		if(!prefix) prefix = "MetaData";
		return this.xmlDoc.selectSingleNode(prefix + "/"+ table_name);		
	}
	
	this.open=function(method,url)
	{
		this.id=++comm_counter; // id number of this request
		this.url=url;
		this.method = method;
		
		var objXMLHttp=null;
		if (window.XMLHttpRequest)objXMLHttp=new XMLHttpRequest();
		else if (window.ActiveXObject) objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
		this.xhttpObject=objXMLHttp;
	}
	
	
	
	this.send=function(postdata)
	{
		var url=this.url;
		comm_instances[this.id]=this;
		var commstate=new CommunicatorCheckState(this.id);
		this.xhttpObject.onreadystatechange=commstate.statechanged;
		
		this.xhttpObject.open(this.method,url,true);
		this.xhttpObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		
		var poststr="";
		if(typeof(postdata)=="object" && postdata.length>0)
		{
			for(p in postdata)
			{
				 poststr+=p+"="+postdata[p]+"&";
			}
			postdata=poststr;
		}
		this.xhttpObject.send(postdata);
		
	}
	
	this.onreadystatechange=function()
	{	
		comm_instances[this.id].attributes = this.attributes;
		setTimeout("try{" + this.nextAction + "(comm_instances[" + this.id + "]);}catch(err){}", 0);
		return false;
	}
}

function CommunicatorCheckState(parID)
{
	
	var parentId=parID;
	
	this.statechanged=function()
	{
		//for (prop in this) alert(prop);
		//alert(this.protoy);
		//alert(comm_instances[parentId].xhttpObject.readyState);
		if(comm_instances[parentId].xhttpObject.readyState==4) 
		{
			var reqstatus=0;
			try{reqstatus=comm_instances[parentId].xhttpObject.status;}
			catch(e){reqstatus=0;}
			if(reqstatus==200 || reqstatus==0)
			{	
				
				xml=comm_instances[parentId].xhttpObject.responseText;
				xml = xml.replace("<html><body>","");
				xml = xml.replace("</body></html>","");
				
				
				if (window.ActiveXObject)
				{
					comm_instances[parentId].xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
					comm_instances[parentId].xmlDoc.async=false;
					xml = String(xml).replace(/&/ig, "&amp;");
					comm_instances[parentId].xmlDoc.loadXML(xml);
				}
				else if(document.implementation.createDocument)
				{
					var parser=new DOMParser();
					comm_instances[parentId].xmlDoc=parser.parseFromString(xml,"text/xml");
					
				}
				if(comm_instances[parentId].xmlDoc.documentElement!=null)
					comm_instances[parentId].xmlDoc= comm_instances[parentId].xmlDoc.documentElement;
				
			
				for(i=0;i<comm_instances[parentId].xmlDoc.childNodes.length;++i)
				{
					if(comm_instances[parentId].xmlDoc.childNodes[i].tagName=="message")
					{
						alert(getNodeValue(comm_instances[parentId].xmlDoc.childNodes[i]))
						node=comm_instances[parentId].xmlDoc.childNodes[i]
						comm_instances[parentId].xmlDoc.removeChild(node);
					}
				}
				
				var err = comm_instances[parentId].getRow("Error");
				
				if(err!=null)
				{
					comm_instances[parentId].error = err.text;
					comm_instances[parentId].xmlDoc = null;
					comm_instances[parentId].onreadystatechange();	
				}
				else
				{
					comm_instances[parentId].responseText=xml;
					comm_instances[parentId].onreadystatechange();	
				}
			}
			else if(reqstatus==500 || reqstatus==0)
			{
				comm_instances[parentId].error = comm_instances[parentId].xhttpObject.responseText ;
				comm_instances[parentId].xmlDoc = null;
				comm_instances[parentId].onreadystatechange();	
			}
		}
		
	}
}	


	




if(!document.all)
{

Element.prototype.selectNodes = function( xpath, contextNode )
{
	if ( document.all )// IE
	{
		if ( contextNode )return contextNode.selectNodes( xpath ) ;
		else return this.DOMDocument.selectNodes( xpath ) ;
	}
	else// Gecko
	{
		var aNodeArray = new Array();

		var xPathResult = this.ownerDocument.evaluate( xpath, contextNode ? contextNode : this.ownerDocument, 
		this.ownerDocument.createNSResolver(this.ownerDocument.documentElement), XPathResult.ORDERED_NODE_ITERATOR_TYPE, null) ;
		if ( xPathResult ) 
		{
			var oNode = xPathResult.iterateNext() ;
			while( oNode )
			{
				aNodeArray[aNodeArray.length] = oNode ;
				oNode = xPathResult.iterateNext();
			}
		} 
		return aNodeArray ;
	}
}

Element.prototype.selectSingleNode = function( xpath, contextNode ) 
{	
	
	if ( document.all )// IE
	{
		if ( contextNode ) return contextNode.selectSingleNode( xpath ) ;
		else return this.DOMDocument.selectSingleNode( xpath ) ;
	}
	else// Gecko
	{
		var xPathResult = this.ownerDocument.evaluate( xpath, contextNode ? contextNode : this.ownerDocument,
		this.ownerDocument.createNSResolver(this.ownerDocument.documentElement), 9, null);

		if ( xPathResult && xPathResult.singleNodeValue ) return xPathResult.singleNodeValue ;
		else return null ;
	}
}

}

//Soap error handling 
function HandleTreeViewSoapErrors (inst, currentRequestTitle)
{
	if (inst && inst.compilerErrorObject)
	{
		ShowErrorPopup (inst.compilerErrorObject.htmlText);
		return false;
	}
	
	if (inst.errorObject)
	{
		alert (unescape (inst.errorObject.message) + 
				(inst.errorObject.description? 
				"\n" + unescape(inst.errorObject.description)
				: "")
		);	
		return false;
	}
	
	if (inst && inst.error)
	{
		alert (unescape(inst.error));
		return false;
	}
	
	
	if (inst && inst.xmlDoc == null)
	{
		alert (GetString ("HandleTreeViewSoapErrors", [currentRequestTitle]));
		return false;
	}
	return true;
}

//Retrieves resutls without xml 
Communicator.GetPlainText = function (method, url, postdata)
{
	var xmlHttpRequest =new ActiveXObject("Microsoft.XMLHTTP");
	xmlHttpRequest.open(method,url,false);
	xmlHttpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttpRequest.send(postdata);
	return xmlHttpRequest.responseText;
}
