// JavaScript Document
function validateField(oEvent) {
	oEvent = oEvent || window.event;
	var txtField = oEvent.target || oEvent.srcElement;
  	var oXHR = zXmlHttp.createRequest();
	var oggi = new Date();
	//alert("ValidateProfileForm.asp?" + txtField.name + "=" + encodeURIComponent(txtField.value) + "&" + oggi.getTime());
	oXHR.open("get", "ValidateProfileForm.asp?" + txtField.name + "=" + encodeURIComponent(txtField.value) + "&" + oggi.getTime(), true);
    oXHR.onreadystatechange = function () {
		if (oXHR.readyState == 4) {
			if (oXHR.status == 200) {
				var arrInfo = oXHR.responseText.split("||");
				var imgError = document.getElementById("img" + txtField.id + "Error");
				var btnSingUp = document.getElementById("btnSubscribe");
				
				if (!eval(arrInfo[0])) {
					//alert("si errore");
					imgError.title = arrInfo[1];
					imgError.style.display = "";
					txtField.valid = false;
				} else {
					//alert("no errore");
					imgError.style.display = "none";
					txtField.valid = true;
				}
				btnSingUp.disabled = !isFormValid();
			} else {
				alert("Errore nella comunicazione con il server");
			}
		}
	};
	oXHR.send(null);
}

function isFormValid() {
	var frmMain = document.getElementById("frmInserimentoUtente");
	var blnValid = true;
	for (var i=0; i < frmMain.elements.length; i++) {
		if (typeof frmMain.elements[i].valid == "boolean") {
				blnValid = blnValid && frmMain.elements[i].valid;
		}
	}
	return blnValid;
}


//if Ajax is enabled, disable the submit button and assign event handlers
window.onload = function () {
    if (zXmlHttp.isSupported()) {
        var btnSignUp = document.getElementById("btnSubscribe");        
        var txtEmail = document.getElementById("email");
		var txtUser = document.getElementById("username");
		var txtPassword = document.getElementById("parolachiave");
		var txtCognome = document.getElementById("cognome");
		var txtNome = document.getElementById("nome");
		var txtIndirizzo = document.getElementById("indirizzo");
		var txtCap = document.getElementById("cap");
		var selProvincia = document.getElementById("idprovincia");
		var selNazione = document.getElementById("idnazione");

        btnSignUp.disabled = true;
        txtEmail.onchange = validateField;
        txtEmail.onblur = validateField;
		txtEmail.valid = false;
		if(txtUser){
			txtUser.onchange = validateField;
			txtUser.onblur = validateField;
			txtUser.valid = false;
		}
		if(txtPassword){
			txtPassword.onchange = validateField;
			txtPassword.onblur = validateField;
			txtPassword.valid = false;
		}
		txtCognome.onchange = validateField;
        txtCognome.onblur = validateField;
		txtCognome.valid = false;
		txtNome.onchange = validateField;
        txtNome.onblur = validateField;
		txtNome.valid = false;
		txtIndirizzo.onchange = validateField;
        txtIndirizzo.onblur = validateField;
		txtIndirizzo.valid = false;
		txtCap.onchange = validateField;
        txtCap.onblur = validateField;
		txtCap.valid = false;
		selProvincia.onchange = validateField;
        selProvincia.onblur = validateField;
		selProvincia.valid = false;
		selNazione.onchange = validateField;
        selNazione.onblur = validateField;
		selNazione.valid = false;

    } else {
		alert("alcune funzionalitą asincrone non sono supportate da questo browser");
	}
};
