

//<script>

/* Constants */
var XML_NOT_SUPPORTED = "Not supported";
var XML_MICROSOFT = "Microsoft";
var XML_MOZILLA = "Mozilla";


function GetNodeAttribute(node, attrName, defValue)
{
	if (defValue==null) defValue = "";
	try
	{
		var result = node.getAttribute(attrName);
		return IsEmpty(result) ? defValue : result;			
	}
	catch(e)
	{
		return defValue;
	}
}

/* CXmlHttpRequest */
function CXmlHttpRequest()
{
	this.request = this.create();
}
CXmlHttpRequest.prototype.factory = null;
CXmlHttpRequest.prototype.version = null;
CXmlHttpRequest.prototype.create = function()
{
	var result = null;
	var f = [
		[XML_NOT_SUPPORTED, null, NullFunction],
		[XML_MICROSOFT, 4.0, function(){try { return new ActiveXObject("Msxml2.XMLHTTP.4.0")}catch(e){return null}}],
		[XML_MICROSOFT, 3.0, function(){try { return new ActiveXObject("Msxml2.XMLHTTP")}catch(e){return null}}],
		[XML_MICROSOFT, 2.6, function(){try { return new ActiveXObject("Microsoft.XMLHTTP")}catch(e){return null}}],
		[XML_MOZILLA, null, function(){try { return new XMLHttpRequest()}catch(e){return null}}]
	];
	try
	{
		if (CXmlHttpRequest.factory == undefined)
			for (var i=0; i<f.length; i++)
			{
				result = f[i][2]();
				if (result != null)
				{
					CXmlHttpRequest.prototype.factory = f[i][0];
					CXmlHttpRequest.prototype.version = f[i][1];
					return result;
				}
			}
		else
			for (var i=0; i<f.length; i++)
				if (f[i][0] == this.factory && (f[i][1] == this.version || f[i][1]==null))
					return f[i][2]();
	}
	catch(e)
	{
		result = null;
	}
	MsgFormat(Msg_XmlHttpRequestNotSupported);
	return result;
}

/* CXmlDoc */
function CXmlDoc()
{
	this.doc = this.create();
}
CXmlDoc.prototype.factory = null;
CXmlDoc.prototype.version = null;
CXmlDoc.prototype.create = function()
{
	var result = null;
	var f = [
		[XML_NOT_SUPPORTED, null, NullFunction],
		[XML_MICROSOFT, 4.0, function(){try { return new ActiveXObject("Msxml2.DOMDocument.4.0")}catch(e){return null}}],
		[XML_MICROSOFT, 3.0, function(){try { return new ActiveXObject("Msxml2.DOMDocument")}catch(e){return null}}],
		[XML_MICROSOFT, 2.6, function(){try { return new ActiveXObject("Microsoft.XMLDOM")}catch(e){return null}}],
		[XML_MOZILLA, null, function(){try { return document.implementation.createDocument("", "", null)}catch(e){return null}}]
	];
	try
	{
		if (IsEmpty(this.factory))
			for (var i=0; i<f.length; i++)
			{
				result = f[i][2]();
				if (result != null)
				{
					CXmlDoc.prototype.factory = f[i][0];
					CXmlDoc.prototype.version = f[i][1];
					return result;
				}
			}
		else
			for (var i=0; i<f.length; i++)
				if (f[i][0] == this.factory && (f[i][1] == this.version || f[i][1]==null))
					return f[i][2]();
	}
	catch(e)
	{
		result = null;
	}
	CXmlDoc.prototype.factory = XML_NOT_SUPPORTED;
	CXmlDoc.prototype.version = 0;	
	MsgFormat(Msg_XmlNotSupported);
	return result;
}
CXmlDoc.prototype.loadXML = function(str)
{
	switch(this.factory)
	{
		case XML_MOZILLA:
			CXmlDoc.prototype.loadXML = function (str)
			{
				if (IsEmpty(str)) str = "<root/>"; 
				this.doc = (new DOMParser()).parseFromString(str, "text/xml");
				return true;
			}
			break;
		case XML_MICROSOFT:
			CXmlDoc.prototype.loadXML = function (str)
			{
				if (IsEmpty(str)) str = "<root/>"; 
				return this.doc.loadXML(str);
			}
			break;
		default:
			CXmlDoc.prototype.loadXML = function (){return false};
	}
	return this.loadXML(str);
}
CXmlDoc.prototype.xml = function()
{
	switch(this.factory)
	{
		case XML_MOZILLA:
			CXmlDoc.prototype.xml = function(){return (new XMLSerializer()).serializeToString(this.doc)}
			break;			
		case XML_MICROSOFT:
			CXmlDoc.prototype.xml = function(){return this.doc.xml};
			break;
		default:
			CXmlDoc.prototype.xml = function(){return null};
	}
	return this.xml();
}

CXmlDoc.prototype.getElementsByTagName = function(tag)
{
	try { return this.doc.getElementsByTagName(tag) }
	catch(e) { return [] }
}

CXmlDoc.prototype.getElementsByTagNameNS = function(ns, tag)
{
	var result = [];
	try
	{
		switch(this.factory)
		{
			case XML_MOZILLA:
				if (ns != null)
					result = this.doc.getElementsByTagNameNS(ns, tag);
				else
					result = this.doc.getElementsByTagName(tag);
				break;
			case XML_MICROSOFT:
				if (ns != null)
				{
					this.doc.setProperty("SelectionNamespaces", "xmlns:_ns='"+ ns + "'");
					result = this.doc.documentElement.selectNodes("//_ns:"+ tag);
				}
				else
					result = this.doc.getElementsByTagName(tag);
				break;
		}
	}
	catch(e) { result = [] }
	return result;
}

CXmlDoc.prototype.getNodeAttribute = function(nodeName, attrName, ns)
{
	try	{ return this.getElementsByTagNameNS(ns, nodeName).item(0).getAttribute(attrName) }
	catch(e) { return null }
}

CXmlDoc.prototype.getAttributeCollection = function(node, tagName, attrFilter, attrList)
{
/*
attrFilter = [[attr_name, attr_value],...] filtra i soli nodi tagName con valore(attr_name)==attr_value 
attrList = [attr_name,...]
*/
	try
	{
		var result = new CAttributeCollection(attrList);
		var i, j, k, ok, attr, all = node.getElementsByTagName(tagName);
		for (i=0; i<all.length; i++)
		{
			ok = false;
			if (!IsEmpty(attrFilter))
				for (j=0; j<attrFilter.length; j++)
					if (ok = (all.item(i).getAttribute(attrFilter[j][0]) != attrFilter[j][1]))
						break;
			if (!ok)
			{				
				result.node[k=result.node.length] = [];
				for (j=0; j<attrList.length; j++)
				{
					attr = all.item(i).getAttribute(attrList[j]);
					result.node[k][attrList[j]] = attr;
					if (!IsEmpty(attr))
					{
						if (IsEmpty(result.attr[attrList[j]]))
							result.attr[attrList[j]] = [];
						result.attr[attrList[j]][result.attr[attrList[j]].length] = attr;
					}
				}
			}
		}
	}
	catch(e) { result = new CAttributeCollection(attrList) }
	return result;
}

function CAttributeCollection(attrList)
{
	this.attrList = attrList;	// elenco attributi considerati
	this.attr = [];				// attr[attr_name] array con tutti i valori trovati per attr_name
	this.node = [];				// node[i][attr_name] valore attributo per il nodo i-esimo
}

/*
CXmlDoc.prototype.createNode = function(name, attributes, text, cdata)
{
	switch(this.factory)
	{
		case XML_MOZILLA:
		case XML_MICROSOFT:
			CXmlDoc.prototype.createNode = function(name, attributes, cdata, text)
			{
				try
				{
					var node = this.doc.createElement(name);
					if (attributes != null)
						for (var i=0; i<attributes.length; i++)
							node.setAttribute(attributes[i][0], attributes[i][1]);
					if (cdata != null)
						node.appendChild(this.doc.createCDATASection(cdata));
					if (text != null)
						node.appendChild(this.doc.createTextNode(text));
					return node;
				}
				catch(e)
				{
					return null;
				}
			}
			break;
		default:
			CXmlDoc.prototype.createNode = function(){return null};
	}
	return this.createNode(name, attributes, cdata, text);
}
CXmlDoc.prototype.addChild = function(parentPath, node)
{

function XMLAppendNode(dom, parentName, childName, data, attributes)
{
	try
	{
		var parent = dom.selectSingleNode(parentName);
		var node = XMLCreateNode(dom, childName, data, attributes);
		parent.appendChild(node);
	return node;
}
catch(e)
{
	return null;
}
}	
}
*/

/* CSoap */
function CSoap()
{
	this.field = [];
	this.autoclear = false;
	this.debug = 0;
	this.faultcode = null;	
	this.faultstring = null;	
	this.response = new CXmlDoc;
}
CSoap.prototype.http = new CXmlHttpRequest();
CSoap.prototype.clearField = function (name)
{
	if (arguments.length < 1)  
		return this.field = [];
	for (var i=0; i<this.field.length; i++)
		if ( this.field[i][0] == name )
			return this.field.splice(i, 1);
}
CSoap.prototype.setField = function (name, value)
// Es.:
//	setField("id", "val") => <id>val</id>
//	setField("id", [v1, v2]) => <id><i>v1</i><i>v2</i></id>
//	setField("id", object) => <id><object>...</object></id>
{
	for (var i=0; i<this.field.length; i++)
		if ( this.field[i][0] == name )
			return this.field[i] = [name, value];
	return this.field[this.field.length] = [name, value];
}
CSoap.prototype.fieldValue = function (name)
{
	for (var i=0; i<this.field.length; i++)
		if ( this.field[i][0] == name )
			return this.field[i][1];
	return null;
}
CSoap.prototype.envelopeHeader = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>';
CSoap.prototype.envelopeFooter = '</soap:Body></soap:Envelope>';
CSoap.prototype.buildRequest = function (method)
{
	var result = '<'+ method +' xmlns="http://italcom.biz/webservices/">';
	result += this.serializeRequest();
	return this.envelopeHeader + result + '</'+ method +'>'+ this.envelopeFooter;
}
CSoap.prototype.serializeRequest = function ()
{
	var i, j, result = tmp = "";
	if (arguments.length < 1)
	{
		for (i=0; i<this.field.length; i++)
			result += "<"+ this.field[i][0] +">"+ this.serializeRequest(this.field[i][1]) +"</"+ this.field[i][0] +">";
	}
	else
	{
		arg = arguments[0];
		if (IsArray(arg))
		{
			for (j=0; j<arg.length; j++)
			{
				if (IsArray(arg[j]))
				{
					tmp += '<I';
					for (var k=0; k<arg[j].length; k++)
						tmp += ' '+ arg[j][k][0] +'="'+ SerializeString(arg[j][k][1].toString()) +'"';
					tmp += '/>';
				}
				else
					tmp += '<I>'+ SerializeString(arg[j].toString()) +'</I>';
			}					
			result += tmp;
		}
		else if (IsObject(arg))
		{
			for (i in arg)
			{
				result += "<"+ i +">";
				if (IsArray(i))
				{
					for (j=0; j<arg.length; j++)
						tmp += '<I>'+ SerializeString(arg[j].toString()) +'</I>';
					result += tmp;
				}
				else if (IsObject(i))
					result += this.serializeRequest(i);
				else
					result += SerializeString(arg[i].toString());
				result += "</"+ i +">";
			}
		}
		else
			result = SerializeString(arg.toString())
	}
	return result;
}
CSoap.prototype.parseResponse = function (url, method, bDisplayError)
{
	if (arguments.length < 2) bDisplayError = true;
	this.faultcode = null;
	this.faultstring = null;
	try
	{
		var request = this.buildRequest(method);
if (this.debug >= 1) window.alert(url + "\n" + request.replace(/</g, "\n<"));
		this.http.request.open("POST", url, false);
		this.http.request.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
		this.http.request.setRequestHeader("SOAPAction", "http://italcom.biz/webservices/"+ method);
		this.http.request.send(request);
		if (this.autoclear)
			this.clearField();	
if (this.debug >= 3) window.alert(this.http.request.responseText);
		if (IsEmpty(this.http.request.responseXML))
			throw "";
		this.response.doc = this.http.request.responseXML;
if (this.debug >= 2) window.alert(this.response.xml().replace(/</g, "\n<"));
		this.faultcode = this.response.doc.getElementsByTagName("faultcode");
		this.faultstring = this.response.doc.getElementsByTagName("faultcode");
		if (this.faultcode.length || this.faultcode.length)
			return false;
		return true;
	}
	catch (e) 
	{
		if (!bDisplayError)
			return false;
		e.name = "";
		e.description = e.message = Msg_SoapServerError;
		MsgException(e, "CSoap.parseResponse()");
		return false;
	}
}
CSoap.prototype.getPageInfo = function ()
{
	var result = new Object();
	isNaN(result.pageNumber = parseInt(this.response.getNodeAttribute("DataPage", "PageNumber"), 10)) ? 0 : result.pageNumber;
	isNaN(result.pageSize = parseInt(this.response.getNodeAttribute("DataPage", "PageSize"), 10)) ? 0 : result.pageSize;
	isNaN(result.pageCount = parseInt(this.response.getNodeAttribute("DataPage", "PageCount"), 10)) ? 0 : result.pageCount;
	isNaN(result.recordCount = parseInt(this.response.getNodeAttribute("DataPage", "RecordCount"), 10)) ? 0 : result.recordCount;
	return result;
}

// Messaggi in italiano


var Msg_XmlNotSupported = "XML non supportato sulla versione corrente del browser";
var Msg_XmlHttpRequestNotSupported = "XMLHttpRequest non supportato sulla versione corrente del browser";
var Msg_SoapServerError = "Errore di comunicazione con il Server";

// o in inglese per le altre lingue


//</script>

