function loginValidate(myField,whichURL,myRoot)
{
	rootUrl = myRoot;
	//alert(rootUrl);
	myTD = myField.parentNode;
	uName = document.getElementById("username").value;
	sName = uName;
	uPass = document.getElementById("pass").value;
	ajaxLoginValidate(uName,uPass,whichURL);
}

function ajaxLoginValidate(myName,myPass,myUrl)
{
	req = null;
	myURL = myUrl+"?name="+myName+"&password="+myPass;

	if (window.XMLHttpRequest) // Mozilla, Safari, ...
	{
		req = new XMLHttpRequest();
		if (req.overrideMimeType) { req.overrideMimeType('text/xml'); }
	}
	else if (window.ActiveXObject) // IE
	{
		try { req = new ActiveXObject("Msxml2.XMLHTTP"); } // Vers. 5.5 o inferiore
		catch (e)
		{
			try { req = new ActiveXObject("Microsoft.XMLHTTP"); } // Vers. 5.5 o superiore
			catch (e) {}
		}
	}

	if (!req) {
	   alert('Giving up :( Cannot create an XMLHTTP instance');
	   return false;
	}
	req.open("POST", myURL, true); // 1) POST or GET;  2) URL of the script to execute;  3) true for asynchronous (false for synchronous).
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	req.send(""); // POSTs data to server.
	
	result = false;

	req.onreadystatechange = function()
	{
		/** Ora THIS e' questa funzione! **/
		if (req.readyState == 4) // The 4 state means for the response is ready and sent by the server.
		{
			if (req.status == 200) // This status means "OK", otherwise some error code is returned, 404 for example.
			{
				//alert("Pagina trovata... \nSubmit effettuata!");

				/*** Following are the actions to be performed with the server response ***/
				//alert(req.responseText);
				rispostaServer = req.responseXML;
				manageResponse(rispostaServer);
			}
			else
			{
				//alert("The page generating XML has not been found!");
				alert("An error occurred while submitting data to the server!\nCode: " + req.status + " " + req.statusText); // statusText ?una propriet?dell'oggetto XMLHttpRequest che contiene il messaggio di errore.
			}
		}
	}
}

function manageResponse(XMLresponse)
{
	myRoot = (XMLresponse.firstChild.nodeName == "xml") ? XMLresponse.childNodes[1] : XMLresponse.childNodes[0];  //alert("myRoot= "+myRoot.nodeName);
	myForm = document.getElementById("loginForm");
	if (myRoot.getAttribute("success")==1) // If DB query was successful...
	{
		myForm.submit();
	}
	else if(myRoot.getAttribute("success")==2)
	{
		document.getElementById("pass").parentNode.getElementsByTagName("span")[0].innerHTML = myRoot.getAttribute("pwdError");
		showTips( document.getElementById("pass") );
	}
	else
	{
		document.getElementById("username").parentNode.getElementsByTagName("span")[0].innerHTML = myRoot.getAttribute("userError");
		showTips( document.getElementById("username") );
	}
}

/* Catch keys on login form */
function loginCatchKey(theField,e)
{
	if (window.event) {
		key = window.event.keyCode; //IE
	}
	else {
		key = e.which; //firefox
	}
	if (key == 13) {
		if (theField.id=='username') { setTimeout("document.getElementById('pass').focus()",100); }
		else { loginValidate(this,document.getElementById('loginForm').name,'/'); }
	}
}

function logOut()
{
	//Please note: IE requires the form submit to be inside HREF (and NOT onclick)
	document.getElementById('loginForm').submit();
}
