/*** Code by Stefano Roncari :: Eggsist Ltd ***/

function loginValidate(myField,whichURL)
{
	myTD = myField.parentNode;
	uName = document.getElementById("email").value;
	uPass = document.getElementById("password").value;
	ajaxLoginValidate(uName,uPass,whichURL);
}

function ajaxLoginValidate(myEmail,myPass,myUrl)
{
	/** THIS e' l'Input Field **/
	/*** Questa funzione si limita a fare la POST dei dati inseriti dall'utente, e a ricevere la risposta del server. Poi chiama lo script che gestisce la risposta del server. ***/
	req = null;
	//alert(myEmail);
	myURL = myUrl+"?email="+myEmail+"&password="+myPass;

	//alert("Submit effettuato. Attendo feedback dal server...");

	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;
	}
   // alert(myURL);
	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 the server.
	
	result = false;

	req.onreadystatechange = function()
	{
		/** Ora THIS e' questa funzione! **/
		  //  alert(req.readyState);
		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 ***/
				respText = req.responseText;
				respXML = req.responseXML;
				//alert(respText);

				/** Sometimes (not always!) IE can't interpret the XML document produced by PHP. In that case, we have to re-build it from a String! **/
				if (respXML.childNodes.length!=0) { manageResponse(respXML); }
				else
				{
					xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
					xmlDocument.loadXML(respText); // Create an XML document starting from the given String ;-)
					manageResponse(xmlDocument);
				}

			}
			else
			{
				//alert("The page generating XML has not been found!");
				alert("<br/><font color='red'>An error occurred while submitting data to the server!</font><br/>Code: " + 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("password").parentNode.getElementsByTagName("span")[0].innerHTML = myRoot.getAttribute("pwdError");
		showTips( document.getElementById("password") );
	}
	else
	{
		document.getElementById("email").parentNode.getElementsByTagName("span")[0].innerHTML = myRoot.getAttribute("userError");
		showTips( document.getElementById("email") );
	}
}

