// resituisce nodo
function getNode(node){
	if((document.getElementById && !document.all)||(document.getElementById && document.all))
		return document.getElementById(node);
	else if(!document.getElementById && document.all) 
		return document.all[node];
	else if(document.layers) 
		return document.layers[node];
}

//funzione di trim
function trim(str) {
	return str.replace(/\s+$|^\s+/g,"");
}

//controllo di mail valida
function checkMail(mail){
	var espressione = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;
	if (!espressione.test(mail))
		return false;
	else 
		return true;
}

//restituisce oggetto ajax
function createAjRequest() {
	var xmlHttp = null,
	browser = navigator.userAgent.toUpperCase();
 	if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
  		xmlHttp = new XMLHttpRequest();
 	else if(window.ActiveXObject && browser.indexOf("MSIE 4") < 0){
		if(browserUtente.indexOf("MSIE 5") < 0)
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		 else
		   	xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		 }
 return xmlHttp;
}

//controllo del recuper password
function checkLostPassword(){
	var nick = document.lostForm.nick.value;
	var email = document.lostForm.email.value;
	var pwd = document.lostForm.pwd.value;
	var url = "manageData.php?w=lostPassword";
	var divBlock = getNode("dl");
	var nodi = divBlock.childNodes;
	for(i=0;i<nodi.length;i++){
		if(nodi[i].tagName == "H4")
		nodi[i].parentNode.removeChild(nodi[i]);
	}
	//nick
	if((nick=="")||(nick==undefined)||(nick.length>12)){
		divBlock.innerHTML += "<h4 class=\"erroreLost\">Devi inserire un nickname valido.</h4>";
		return false;
	}
	if((pwd=="")||(pwd==undefined)||(pwd.length>12)){
		divBlock.innerHTML += "<h4 class=\"erroreLost\">Devi inserire una password valida, [max 12 caratteri].</h4>";
		return false;
	}
	if((email=="")||(email==undefined)||(!checkMail(email))){
		divBlock.innerHTML += "<h4 class=\"erroreLost\">E-mail non valida.</h4>";
		return false;
	}
	xmlObj = createXMLHttpRequest();
	if(xmlObj) {
		xmlObj.open("post", url, true);
		xmlObj.setRequestHeader("content-type", "application/x-www-form-urlencoded");
		xmlObj.setRequestHeader("connection", "close");
		xmlObj.onreadystatechange = function() {
	    if ((xmlObj.readyState == 4) && (xmlObj.status == 200)) {
			var str = trim(xmlObj.responseText);
			if(str=="ok"){
				getNode("iLost").style.display = "none";
				getNode("fieldLost").style.display = "none";
				divBlock.innerHTML += "<h4 class=\"okLost\">Ti è stato inviato inviato un messaggio alla casella di posta segnalata, la mail contiene il link per l'attivazione della nuova password.</h4>";
				divBlock.innerHTML += "<input class=\"mexButtLost\" type=\"button\" onclick=\"location.href='index.php';\" value=\"Home\"/>";
				return false;
				}
			else if(str=="ko"){
				getNode("iLost").style.display = "none";
				getNode("fieldLost").style.display = "none";
				divBlock.innerHTML += "<h4 class=\"erroreLost\">Si è verificato un errore durante l'invio della richiesta, ti prego di riprovare più tardi.</h4>";
				divBlock.innerHTML += "<input class=\"mexButtLost\" type=\"button\" onclick=\"location.reload();\" value=\"Reload\"/>";
				return false;
				}
			}
	    else{
	    	getNode("fieldLost").style.display = "none";
			getNode("iLost").style.display = "block";
			return false;
			}
	    return false;
	    }
	    postString = "nick="+encodeURIComponent(escape(nick))+"&email="+encodeURIComponent(escape(email))+"&pwd="+encodeURIComponent(escape(pwd));
		xmlObj.send(postString);
		return true;
		}
	return false;
}