
// Errore di validazione. Chiede conferma
// Setta colore di sfondo della casella/caselle email
// Mostra popup con messaggio di errore

function emailValidateError(email1, email2, msg, level, conferma) {
    var colorError = "#ffff9d";
    var resp = false;

    if (email1 != null) {
        document.getElementById(email1).style.backgroundColor = colorError;
    }
	
	if (email2 != null) {
        document.getElementById(email2).style.backgroundColor = colorError;
    }
	
    if (conferma == 'true') {
		var answer = confirm(msg.replace('<br/>','\n').replace('<br/>','\n') + "\n\nVuole procedere comunque?")
		if (answer){
			document.formEmail.submit();
		}
	} else {
		document.getElementById("light").style.display = "block";
		document.getElementById("fade").style.display = "block";
		document.getElementById("imgValEmail").src = (level>0 ? "/common/img/mf/pop_up_windows/attenzione.gif" : "/common/img/mf/pop_up_windows/stop.jpg");
		document.getElementById("msgErrore").innerHTML = msg;
	}
}


// Validazione OK
// Setta colore di sfondo della casella/caselle email
function emailValidateOk(email1, email2,conferma) {
    var colorOk = "#99ff33";

    document.getElementById(email1).style.backgroundColor = colorOk;
    if (email2 != null) {
        document.getElementById(email2).style.backgroundColor = colorOk;
    }
	
	if (conferma == 'true') {
		document.formEmail.submit();
	} 
    
    return true;
}

// Chiama WS per validazione email
// Email1 - mail da validare
// Email2 (opzionale) - se specificato verifica che sia uguale a email1

function emailValidate(email1, email2, conferma) {
    var emailC = null;
    var emailConf = null;
    var validate = true;
	if (conferma == null) conferma ='false';
	
    if (email1 != null) {
        emailC = document.getElementById(email1).value;

        if (email2 != null) {
            emailConf = document.getElementById(email2).value;
            if (emailC != emailConf) {
                emailValidateError(email1, email2, "ATTENZIONE: ha inserito due indirizzi email differenti.", 1);
                validate = false;
            }
        }

        if (validate) {
            new Ajax.Request('/common/inc/email/emailvalidator_gate.asp', {
                method:'get',
                parameters: {email: emailC},
                onSuccess: function(transport) {
                    var level = "1"; // Level di default. Se il service non risponde status = 1
                    var errCode = "";
                    var errMsg = "";

                    if (transport.readyState == 4) { // Loaded
                        if (transport.status == 200) {
                            if (transport.responseXML) {
                                var response = transport.responseXML;
                                var ratingNodes = response.getElementsByTagName("Rating");
                                if (ratingNodes.length > 0) {
                                    var valueNodes = ratingNodes[0].getElementsByTagName("Value");
                                    if (valueNodes.length > 0) {
                                        if (valueNodes[0].childNodes[0]) {
                                            level = valueNodes[0].childNodes[0].nodeValue;
                                            if (level == "0") { // Se level 0 recupera codice e messaggio di errore
                                                var errorNodes = response.getElementsByTagName("Error");
                                                if (errorNodes.length > 0) {
                                                    var codeNodes = errorNodes[0].getElementsByTagName("Value");
                                                    var msgNodes = errorNodes[0].getElementsByTagName("Message");
                                                    if (codeNodes.length>0 && msgNodes.length>0) {
                                                        if (codeNodes[0].childNodes[0] && msgNodes[0].childNodes[0]) {
                                                            errCode = codeNodes[0].childNodes[0].nodeValue;
                                                            errMsg = msgNodes[0].childNodes[0].nodeValue;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

					switch (level) {
						case "0": // Error
							emailValidateError(email1, email2, "ERRORE: l'indirizzo email non è valido<br/>(" + errCode + " - " + errMsg + ").<br/>Si prega di reinserirlo.", 0, conferma);
							break;
						case "1": // Syntax
							emailValidateError(email1, email2, "ATTENZIONE: non e' possibile verificare l'indirizzo email.<br/>Si accerti che sia corretto.", 1, conferma);
							break;
						case "2": // DNS
							emailValidateOk(email1, email2, conferma);
							break;
						default:
							emailValidateOk(email1, email2, conferma);
					}
                }
            });
        }
    }
    else {
        emailValidateError(email1, email2, "ERRORE: Errore chiamata validate. Parametri mancanti", 0, conferma);
    }
}



