function checkUser() {	
		
		if(xmlhttp != null) {
			xmlhttp.abort();
			xmlhttp=getHTTPObject();
		}
		
		var email=document.getElementById("emailfield").value;

 
		
		if (email != "") {		
			showSaveBox(true);			
			  	
			var params="email="+email;
			
			var callURL="check_user.pl";
			xmlhttp.open("POST",callURL,true);
			xmlhttp.onreadystatechange=handleCheckUser;
			xmlhttp.setRequestHeader("Method", "POST "+callURL+" HTTP/1.1");		
			xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xmlhttp.send(params);
		}	 

}

function handleCheckUser()  {
	if (xmlhttp.readyState==4) {
		if (xmlhttp.status == 200) {		
			var xmlDocument = xmlhttp.responseXML;
			var result = xmlDocument.getElementsByTagName('result')[0].firstChild.nodeValue;
			
			if (result == 1) {
				alert("L'indirizzo email indicato è già registrato in trovaeventi");
				var email=document.getElementById("email");
				email.focus();
			}
			showSaveBox(false);			
		}	
	}	
}

function regUser() {	
		
		if(xmlhttp != null) {
			xmlhttp.abort();
			xmlhttp=getHTTPObject();
		}
		
		showSaveBox(true);			
		  	
		var fullname=document.getElementById("namefield").value;
		var email=document.getElementById("emailfield").value;
		var password=document.getElementById("passwordfield").value;
		var confermapassword=document.getElementById("confermapassword").value;
		var state=document.getElementById("state").value;
		var tags=document.getElementById("tags").value;
		
// 		alert(password);
		
		if(email == "") {
			alert("L'indirizzo email è obbligatorio");
			showSaveBox(false);
			return;			
		}
		
		if(password=="" || password.length<6)  {
			alert("La password è obbligatoria e deve essere di almeno 6 caratteri");
			showSaveBox(false);
			return;			
		}
		
		if(password != confermapassword) {
			alert("Le password inserite non coincidono");
			showSaveBox(false);
			return;
		}
				
		
		if(state == "") {
			alert("La provincia è obbligatoria");
			showSaveBox(false);
			return;						
		}		
		
		var locationField=document.getElementById("location").value;
		
		if(locationField == "") {
			alert("La città è obbligatoria");
			showSaveBox(false);
			return;						
		}		
				
		var checkflag = "false";
		var field=document.getElementById("registrationform").tagcheck;
		for (i = 0; i < field.length; i++) 
		{
			if (field[i].checked) {tags=tags + " " +field[i].value;}
		}
		
		var params="fullname="+fullname+"&email="+email+"&password="+password+"&state="+state+"&location="+locationField+"&tags="+tags;
		
		var callURL="reg_user.pl";
		xmlhttp.open("POST",callURL,true);
		xmlhttp.onreadystatechange=handleRegUser;
		xmlhttp.setRequestHeader("Method", "POST "+callURL+" HTTP/1.1");		
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlhttp.send(params);	 

}

function handleRegUser()  {
	if (xmlhttp.readyState==4) {
		if (xmlhttp.status == 200) {		
			var xmlDocument = xmlhttp.responseXML;
			var completed = xmlDocument.getElementsByTagName('completed')[0].firstChild.nodeValue;
			var result = xmlDocument.getElementsByTagName('result')[0].firstChild.nodeValue;
		  	showSaveBox(false);			
			alert(completed);		
			
			if (result == 1) {
				var fullname=document.getElementById("namefield");
				var email=document.getElementById("emailfield");
				var password=document.getElementById("passwordfield");
				var confermapassword=document.getElementById("confermapassword");
				var state=document.getElementById("state");
				var tags=document.getElementById("tags");
				
				email.value="";
				password.value="";
				confermapassword.value="";
				tags.value="";				
		
				var locationLabelSpan=document.getElementById("locationLabelSpan");
				clearTextComplete(locationLabelSpan);
				var locationSpan=document.getElementById("locationSpan");
				clearTextComplete(locationSpan);	
				state.selectedIndex=-1;				
			}
		}	
	}	
}

