
// used to find the Automation server name
function getDomDocumentPrefix() {
	if (getDomDocumentPrefix.prefix)
		return getDomDocumentPrefix.prefix;
	
	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
	var o;
	for (var i = 0; i < prefixes.length; i++) {
		try {
			// try to create the objects
			o = new ActiveXObject(prefixes[i] + ".DomDocument");
			return getDomDocumentPrefix.prefix = prefixes[i];
		}
		catch (ex) {};
	}
	
	throw new Error("Could not find an installed XML parser");
}

function getXmlHttpPrefix() {
	if (getXmlHttpPrefix.prefix)
		return getXmlHttpPrefix.prefix;
	
	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
	var o;
	for (var i = 0; i < prefixes.length; i++) {
		try {
			// try to create the objects
			o = new ActiveXObject(prefixes[i] + ".XmlHttp");
			return getXmlHttpPrefix.prefix = prefixes[i];
		}
		catch (ex) {};
	}
	
	throw new Error("Could not find an installed XML parser");
}

//////////////////////////
// Start the Real stuff //
//////////////////////////


// XmlHttp factory
function XmlHttp() {}

XmlHttp.create = function () {
	try {
		if (window.XMLHttpRequest) {
			var req = new XMLHttpRequest();
			
			// some versions of Moz do not support the readyState property
			// and the onreadystate event so we patch it!
			if (req.readyState == null) {
				req.readyState = 1;
				req.addEventListener("load", function () {
					req.readyState = 4;
					if (typeof req.onreadystatechange == "function")
						req.onreadystatechange();
				}, false);
			}
			
			return req;
		}
		if (window.ActiveXObject) {
			return new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp");
		}
	}
	catch (ex) {}
	// fell through
	throw new Error("Your browser does not support XmlHttp objects");
};

// XmlDocument factory
function XmlDocument() {}

XmlDocument.create = function () {
	try {
		// DOM2
		if (document.implementation && document.implementation.createDocument) {
			var doc = document.implementation.createDocument("", "", null);
			
			// some versions of Moz do not support the readyState property
			// and the onreadystate event so we patch it!
			if (doc.readyState == null) {
				doc.readyState = 1;
				doc.addEventListener("load", function () {
					doc.readyState = 4;
					if (typeof doc.onreadystatechange == "function")
						doc.onreadystatechange();
				}, false);
			}
			
			return doc;
		}
		if (window.ActiveXObject)
			return new ActiveXObject(getDomDocumentPrefix() + ".DomDocument");
	}
	catch (ex) {}
	throw new Error("Your browser does not support XmlDocument objects");
};

// Create the loadXML method and xml getter for Mozilla
if (window.DOMParser &&
	window.XMLSerializer &&
	window.Node && Node.prototype && Node.prototype.__defineGetter__) {

	// XMLDocument did not extend the Document interface in some versions
	// of Mozilla. Extend both!
	XMLDocument.prototype.loadXML = 
	Document.prototype.loadXML = function (s) {
		
		// parse the string to a new doc	
		var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
		
		// remove all initial children
		while (this.hasChildNodes())
			this.removeChild(this.lastChild);
			
		// insert and import nodes
		for (var i = 0; i < doc2.childNodes.length; i++) {
			this.appendChild(this.importNode(doc2.childNodes[i], true));
		}
	};
	
	
	/*
	 * xml getter
	 *
	 * This serializes the DOM tree to an XML String
	 *
	 * Usage: var sXml = oNode.xml
	 *
	 */
	// XMLDocument did not extend the Document interface in some versions
	// of Mozilla. Extend both!
	XMLDocument.prototype.__defineGetter__("xml", function () {
		return (new XMLSerializer()).serializeToString(this);
	});
	Document.prototype.__defineGetter__("xml", function () {
		return (new XMLSerializer()).serializeToString(this);
	});
}

//--------------------------------------------------------------------------
var	ie = document.all?true:false;
var	ie6strict = (document.documentElement && document.documentElement.clientHeight)?true:false;

function onLoading (b) {
	var load_div = document.getElementById("loadinfo");
	var load_img = document.getElementById("ajaxloading");
	if(load_div)
	{
		if(b)
		{
		//if(load_div)
		//{
			y=0;
			if (self.innerWidth) // all except Explorer
				y = self.innerHeight;
			else if (document.documentElement && document.documentElement.clientHeight)// Explorer 6 Strict Mode
				y = document.documentElement.clientHeight;
			else if (document.body) // other Explorers
				y = document.body.clientHeight;
			
			if (self.pageYOffset) // all except Explorer
				tp = window.pageYOffset
			else if (ie6strict)// Explorer 6 Strict Mode
				tp = document.documentElement.scrollTop
			else if (document.body)//MOZILLA anche
				tp = document.body.scrollTop
			
			pad=parseInt((y-100)/2)//100=height immagine di loading
			//h=parseInt(y)-pad;
			
			if(ie)//perchè non vuole px mentre mozilla si	
			{
				//load_div.style.height=h;
				load_div.style.paddingTop=pad;
				load_div.style.top=tp;
				load_img.style.paddingTop=pad;
				load_img.style.top=tp;
			}
			else
			{
				//MOZILLA vuole anche px ie no
				load_div.style.paddingTop=pad+"px";
				load_div.style.top=tp+"px"; 
				load_img.style.paddingTop=pad+"px";
				load_img.style.top=tp+"px";
			}
		//}
		
			if(ie)
				load_div.style.filter='alpha(opacity=30)'
			else
				load_div.style.MozOpacity=0.3//non va su firefox
		}
		else
		{
			if(ie)
				load_div.style.filter='alpha(opacity=100)'
			else
				load_div.style.MozOpacity=1//non va su firefox
		}
		
    load_div.style.display = b ? "block" : "none";
	load_img.style.display = b ? "block" : "none";
	}//end if loaddiv
}

//LOGIN IN AJAX ---------------------------------------------------------
function _loginAjax(rnd)
{
	f=document.forms['formloginajax']
	div_le = document.getElementById('div_loginerror');
	
	sSrc="/Login.jsp?loginmode=ajax&username="+f.username.value+"&password="+f.password.value+"&rnd="+rnd;
	//alert(sSrc)
	timer_onloading=window.setTimeout("onLoading(true)","500");
	
	var xmlHttp = XmlHttp.create();
	xmlHttp.open("GET", sSrc, true);	// async
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4) {			
			window.clearTimeout(timer_onloading)
			onLoading(false);
			if(xmlHttp.responseText.indexOf("logged=true")!=-1)
			{
				//alert(document.location.href)
				idgruppo = xmlHttp.responseText.substring(xmlHttp.responseText.indexOf("idgruppo")+9)
				//if(idgruppo==-6)//venditore
				//	document.location.replace("/home.jsp?idrub=2&rnd="+rnd)
				//else
				//	document.location.replace("/home.jsp?idrub=-22&rispristina=false&rnd="+rnd)
				document.location.replace(document.location.href+"&rnd="+rnd)
			}
			else
			{
				div_le.innerHTML="Username o password errata"
				f.username.value="";
				f.password.value="";
			}
		}
	};
	// call in new thread to allow ui to update
	window.setTimeout(function () {
		xmlHttp.send(null);
	}, 10);
}
//UPDATE cal.jsp ---------------------------------------------------------
function _updateCal(sSrc,idrubcal,rnd)
{
	sSrc=sSrc+"&rnd="+rnd; 
	//alert(sSrc)
	obj=document.getElementById(idrubcal);	
	if(obj)
	{
		timer_onloading=window.setTimeout("onLoading(true)","500");
		
		var xmlHttp = XmlHttp.create();
		xmlHttp.open("GET", sSrc, true);	// async
		xmlHttp.onreadystatechange = function () {
			if (xmlHttp.readyState == 4) {			
				window.clearTimeout(timer_onloading)
				onLoading(false); 
				//alert(xmlHttp.responseText)
				res = xmlHttp.responseText;
				res = res.replace('<div id="'+idrubcal+'">','');
				res = res.replace('</div><!--endcalcontainer//-->',"")
				//alert(res)
				//obj.innerHTML=xmlHttp.responseText
				obj.innerHTML=res
			}
		};
		// call in new thread to allow ui to update
		window.setTimeout(function () {
			xmlHttp.send(null);
		}, 10);
	
	}
}