var bGiaCliccato = false;
function registrati() {
	document.getElementById("div_err_register").innerHTML="";	
	document.getElementById("s_nome").style.background = "#FFFFFF";
	document.getElementById("s_cognome").style.background = "#FFFFFF";
	document.getElementById("s_password").style.background = "#FFFFFF";
	document.getElementById("s_comune").style.background = "#FFFFFF";
	document.getElementById("s_indirizzo").style.background = "#FFFFFF";
	document.getElementById("s_civico").style.background = "#FFFFFF";
	document.getElementById("s_cap").style.background = "#FFFFFF";
	document.getElementById("s_provincia").style.background = "#FFFFFF";
	document.getElementById("s_email").style.background = "#FFFFFF";

	var privacy;
	if (document.frmReg.s_privacy[0].checked)
		privacy = "S";
	else if (document.frmReg.s_privacy[1].checked)
		privacy = "N";
	else 
		privacy = "";

	var privacy2;
	if (document.frmReg.s_privacy2[0].checked)
		privacy2 = "S";
	else if (document.frmReg.s_privacy2[1].checked)
		privacy2 = "N";
	else 
		privacy2 = "";

	var sParameters="?s_nome="+document.frmReg.s_nome.value+
	"&s_cognome="+document.frmReg.s_cognome.value+
	"&s_password="+document.frmReg.s_password.value+
	"&s_comune="+document.frmReg.s_comune.value+
	"&s_indirizzo="+document.frmReg.s_indirizzo.value+
	"&s_civico="+document.frmReg.s_civico.value+
	"&s_cap="+document.frmReg.s_cap.value+
	"&s_provincia="+document.frmReg.s_provincia.value+
	"&s_email="+document.frmReg.s_email.value+
	"&s_privacy="+privacy+
	"&s_privacy2="+privacy2+
	"&clickandrun="+document.frmReg.clickandrun.value;
		
	if (!bGiaCliccato) {
		bGiaCliccato=true;
		callAjax4register(sParameters);
	}
	bGiaCliccato=false;
}
// AJAX - START --------------------------------------------------------------------------------------
var ajax;
function callAjax4register(sParameters) {
	ajax = assegnaXMLHttpRequest();
	if(ajax) {
		ajax.open("get", servletPath+"SVRegister"+sParameters, true);		
		ajax.onreadystatechange = handleResponse4register;
		ajax.send(null);
	}	
}
function handleResponse4register() {
	var bSubmit = false;
	var doc;	
	if(ajax.readyState == 4){	
		 if(ajax.responseXML) {		
			// code for IE
			if (window.ActiveXObject){
				doc=new ActiveXObject("Microsoft.XMLDOM");
				doc.async="false";
				doc.loadXML(new String(ajax.responseText));
			}
			// code for Mozilla, Firefox, Opera, etc.
			else{
				var parser=new DOMParser();
				doc=parser.parseFromString(new String(ajax.responseText),"text/xml");
			}
			var x=doc.documentElement;
			if (x.childNodes.length>=1) {	
				// LETTURA RISPOSTE - START
				var status = x.getAttribute("STATUS");
				if (status == "0") { // ESITO POSITIVO				
					var major = "";
					var idutente = "";					
					var id_membro_cry = "";					
					if (x.childNodes[0]) {
						if (x.childNodes[0].nodeName == "MAJOR")
							if (x.childNodes[0].childNodes[0])
								major = x.childNodes[0].childNodes[0].nodeValue;
						if (x.childNodes[0].nodeName == "ID_USER")
							if (x.childNodes[0].childNodes[0])
								idutente = x.childNodes[0].childNodes[0].nodeValue;
					}
					if (x.childNodes[1]) {
						if (x.childNodes[1].nodeName == "ID_USER")
							if (x.childNodes[1].childNodes[0])
								idutente = x.childNodes[1].childNodes[0].nodeValue;
					}
					if (x.childNodes[2]) {
						if (x.childNodes[2].nodeName == "ID_MEMBRO_CRY")
							if (x.childNodes[2].childNodes[0])
								id_membro_cry = x.childNodes[2].childNodes[0].nodeValue;
					}
					if (idutente != ""){
						if (major == 'Y') {
							if (id_membro_cry == "")
								location.href = "register_msg.jsp?s_msg=1";	
							else
								location.href = "register_membro.jsp?major=Y&id_membro="+id_membro_cry+"&idutente="+idutente;	
						} else if (major == 'N') {	
							if (id_membro_cry == "")
								location.href = "register_parent_data.jsp?idutente="+idutente;	
							else
								location.href = "register_membro.jsp?major=N&id_membro="+id_membro_cry+"&idutente="+idutente;	
						} else {
							document.getElementById("div_err_register").innerHTML = "I tuoi dati sono stati modificati correttamente.";
						}
					}
				} else { // ESITO NEGATIVO
					if (x.childNodes[0]) {
						if (x.childNodes[0].nodeName == "MSG")
							if (x.childNodes[0].childNodes[0])
								document.getElementById("div_err_register").innerHTML=x.childNodes[0].childNodes[0].nodeValue;
					}					
					if (x.childNodes[1]) {
						if (x.childNodes[1].nodeName == "POSITION") {
							if (x.childNodes[1].childNodes[0]) {
								if (x.childNodes[1].childNodes[0].nodeValue == "11") {
									document.getElementById("s_nome").style.background = "#BDE400";	
									document.getElementById("s_nome").focus();
								} else if (x.childNodes[1].childNodes[0].nodeValue == "10") {
									document.getElementById("s_cognome").style.background = "#BDE400";								
									document.getElementById("s_cognome").focus();
								} else if (x.childNodes[1].childNodes[0].nodeValue == "8") {
									document.getElementById("s_password").style.background = "#BDE400";														
									document.getElementById("s_password").focus();
								} else if (x.childNodes[1].childNodes[0].nodeValue == "4") {
									document.getElementById("s_comune").style.background = "#BDE400";							
									document.getElementById("s_comune").focus();
								} else if (x.childNodes[1].childNodes[0].nodeValue == "7") {
									document.getElementById("s_indirizzo").style.background = "#BDE400";							
									document.getElementById("s_indirizzo").focus();
								} else if (x.childNodes[1].childNodes[0].nodeValue == "6") {
									document.getElementById("s_civico").style.background = "#BDE400";							
									document.getElementById("s_civico").focus();	
								} else if (x.childNodes[1].childNodes[0].nodeValue == "5") {
									document.getElementById("s_cap").style.background = "#BDE400";							
									document.getElementById("s_cap").focus();
								} else if (x.childNodes[1].childNodes[0].nodeValue == "3") {									
									document.getElementById("s_provincia").style.background = "#BDE400";							
									document.getElementById("s_provincia").focus();
								} else if (x.childNodes[1].childNodes[0].nodeValue == "9") {
									document.getElementById("s_email").style.background = "#BDE400";														
									document.getElementById("s_email").focus();
								} else if (x.childNodes[1].childNodes[0].nodeValue == "1") {									
									document.getElementById("s_privacy").style.background = "#BDE400";
								} else if (x.childNodes[1].childNodes[0].nodeValue == "2") {									
									document.getElementById("s_privacy2").style.background = "#BDE400";																
								}									
							}
						}
					}
					if (x.childNodes[0]) {
						if (x.childNodes[0].nodeName == "ERROR")
							if (x.childNodes[0].childNodes[0])
								document.getElementById("div_err_register").innerHTML=x.childNodes[0].childNodes[0].nodeValue;
					}
				}
				// LETTURA RISPOSTE - END
			}			
		}
	}	
}
function callAjaxReg_datiutente() {
	ajax = assegnaXMLHttpRequest();
	if(ajax) {
		ajax.open("get", servletPath+"SVUser_data_xml", true);		
		ajax.onreadystatechange = handleResponse4datiutente;
		ajax.send(null);
	}	
}
function handleResponse4datiutente() {
	var bSubmit = false;
	var doc;	
	if(ajax.readyState == 4){	
		 if(ajax.responseXML) {		
			// code for IE
			if (window.ActiveXObject){
				doc=new ActiveXObject("Microsoft.XMLDOM");
				doc.async="false";
				doc.loadXML(new String(ajax.responseText));
			}
			// code for Mozilla, Firefox, Opera, etc.
			else{
				var parser=new DOMParser();
				doc=parser.parseFromString(new String(ajax.responseText),"text/xml");
			}
			var x=doc.documentElement;
			if (x.childNodes.length>=0) {	
				// LETTURA RISPOSTE - START
				var status = x.getAttribute("STATUS");
				if (status == "0") { // ESITO POSITIVO - utente loggato			
					if (x.childNodes[2].childNodes[0])
						document.frmReg.s_nome.value=x.childNodes[2].childNodes[0].nodeValue;
					if (x.childNodes[3].childNodes[0])
						document.frmReg.s_cognome.value=x.childNodes[3].childNodes[0].nodeValue;
					if (x.childNodes[1].childNodes[0]) {
						document.frmReg.s_password.value=x.childNodes[1].childNodes[0].nodeValue;
					}
					if (x.childNodes[9].childNodes[0])
						document.frmReg.s_indirizzo.value=x.childNodes[9].childNodes[0].nodeValue;
					if (x.childNodes[10].childNodes[0])	
						document.frmReg.s_civico.value=x.childNodes[10].childNodes[0].nodeValue;
					if (x.childNodes[11].childNodes[0])							
						document.frmReg.s_cap.value=x.childNodes[11].childNodes[0].nodeValue;
					if (x.childNodes[12].childNodes[0])	
						document.frmReg.s_comune.value=x.childNodes[12].childNodes[0].nodeValue;
					setOptionValueSelected(document.frmReg.s_provincia, x.childNodes[13].childNodes[0].nodeValue);	
					/*
					if ((x.childNodes[14].childNodes[0]) && (x.childNodes[15].childNodes[0])){
						document.frmReg.s_pre_tel.value=x.childNodes[14].childNodes[0].nodeValue;
						document.frmReg.s_post_tel.value=x.childNodes[15].childNodes[0].nodeValue;
					} else if ((x.childNodes[16].childNodes[0]) && (x.childNodes[17].childNodes[0])){
						document.frmReg.s_pre_tel.value=x.childNodes[16].childNodes[0].nodeValue;
						document.frmReg.s_post_tel.value=x.childNodes[17].childNodes[0].nodeValue;						
					}
					*/
					document.frmReg.s_email.value=x.childNodes[18].childNodes[0].nodeValue;
					//document.frmReg.s_email2.value=x.childNodes[18].childNodes[0].nodeValue;
					if (x.childNodes[20].childNodes[0]) {
						if (x.childNodes[20].childNodes[0].nodeValue =='S')
							document.frmReg.s_privacy[0].checked = true;
						else if (x.childNodes[20].childNodes[0].nodeValue == 'N')
							document.frmReg.s_privacy[1].checked = true;
					}

					document.frmReg.s_privacy2[0].checked = true;
					
					document.frmReg.s_nome.disabled = true;
					document.frmReg.s_cognome.disabled = true;
					document.frmReg.s_email.disabled = true;					
				}
				// LETTURA RISPOSTE - END
			}			
		}
	}	
}
// AJAX - END --------------------------------------------------------------------------------------
