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 = null;
	this.status = null;
	this.docTitle = null;
	this.error = null;
	this.nextAction = nextAction;
	
	//A list of rows
	this.getTable = function (table_name)
	{
		//try
		//{
			if(this.xmlDoc==null || this.xmlDoc.tagName==null || this.xmlDoc.tagName.toLowerCase()!="xml") return null;
			return this.xmlDoc.selectNodes("MetaData/"+ table_name);
		//}catch (err) {}
	}
	//A singler row
	this.getRow = function (table_name)
	{
		//try
		//{
			if(this.xmlDoc==null || this.xmlDoc.tagName==null || this.xmlDoc.tagName.toLowerCase()!="xml") return null;
			return this.xmlDoc.selectSingleNode("MetaData/"+ table_name);		
		//}catch (err) {}
	}
	
	this.onreadystatechange=function(){	
		setTimeout(this.nextAction + "(comm_instances[" + this.id + "])", 100);
		return false;}
	
	
	this.open=function(method,url){
		this.id=++comm_counter; // id number of this request
		this.url=url;
		this.method = method;
		
		
		/*
		var iframe=document.createElement("iframe");
		
		with (iframe)
		{
			id= "communicator_"+ this.id;
			name="communicator_"+ this.id;
			type="text/plain";
			style="display:none";
			width=600;
			height=300;
		}*/
		var iframe = document.createElement(
		"<iframe id='communicator_"+ this.id+ "' name='communicator_"+ this.id+ "' type='text/plain' style='display:none' width=600 height=300></iframe>");
		if(showCommunicator)
			iframe.style.display = "";
		document.body.appendChild(iframe);
	}
	
	this.send=function(postdata){		
		var iframe=document.getElementById("communicator_"+this.id);
		//Create a form element
		var form = document.createElement("<form style=\"display:none\" id='communicator_form_" + this.id + "' " + 
			" target=\"communicator_" + this.id + "\" action=\"" + this.url + "\" method=\"" + this.method +"\"></form>");
	
		document.body.appendChild(form);
		//form.target = iframe;
		var resArr = [];
	
		if(typeof(postdata)=="object" && postdata.length>0) //An array
		{
			resArr = postdata;
		}
		else
		{
			var arr = postdata.split("&");
			
			for(var i in arr)
			{
				var e = arr[i];					
				var arr2 = e.split("=");
				resArr[arr2[0]] = arr2[1];					
			}
		}
		for (p in resArr)
		{
			var input = document.createElement("INPUT");				
			input.type = "hidden";
			input.name = p;
			input.value = resArr[p];
			form.appendChild (input);	
		}
		
		form.submit();
		
		form.parentNode.removeChild(form);
		comm_instances[this.id]=this;
		window.setTimeout("CommunicatorCheckState(" + this.id + ")",500);
		
	}
	return true;
	
}

function CommunicatorCheckState(inst)
{
	
	var iframe=document.getElementById("communicator_"+inst);
	if(!iframe) return;
	if(iframe.contentWindow.document.readyState=="complete")
	{
		if(iframe.contentWindow.window==null  ||
		iframe.contentWindow.window.document == null ||
		iframe.contentWindow.window.document.body == null ||
		iframe.contentWindow.window.document.body.childNodes==null ||
		iframe.contentWindow.window.document.body.childNodes.length==0)
		{
			comm_instances[inst].error = "No xml data retrieved!";
			comm_instances[inst].onreadystatechange();
			
		}
		else
		{
			var responseText=iframe.contentWindow.window.document.body.childNodes[0].data;
			//var win = window.open(); alert(iframe.contentWindow.window.document.body.innerHTML);
			
			var xmlNode = iframe.contentWindow.window.document.body.childNodes[0];
			//Check for errors
			comm_instances[inst].xmlDoc = xmlNode;
			var err = comm_instances[inst].getRow("Error");
			
			//window.status = (err!=null?err.inerHTML:"");
			if(err!=null)
			{
				comm_instances[inst].error = err.text;
				comm_instances[inst].xmlDoc = null;
				comm_instances[inst].readyState=4;
				comm_instances[inst].status=200;
				comm_instances[inst].onreadystatechange();	
			}
			else {		
				comm_instances[inst].responseText=responseText;
				comm_instances[inst].readyState=4;
				comm_instances[inst].status=200;
				comm_instances[inst].onreadystatechange();			
			}
			comm_instances[inst].docTitle=iframe.contentWindow.window.document.title;
				
		}
		if(!showCommunicator)
			iframe.parentNode.removeChild(iframe);
		
	}else
	{
		setTimeout("CommunicatorCheckState(" + inst + ")",500);
	}
}



//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;
}