﻿//conversione in maiuscolo
document.onkeypress = function () {
    key = window.event.keyCode;
    if ((key > 0x60) && (key < 0x7B))
        window.event.keyCode = key - 0x20;
    return true;
}

window.getAccept = function (group) {
    var modo = "";
    var Options = document.getElementsByName(group);
    if (Options.length > 0) {
        for (i = 0; i < Options.length; i++)
            if (Options[i].checked == true)
                modo = Options[i].value;
    }
    return modo;
}

window.checkControl = function (name, error) {
    var oB = getObject(name);
    if (oB != null) {
        if (Trim(oB.value) == '') {
            alert(error);
            oB.focus();
            return false;
        }
    }
    return true;
}

window.checkPartitaIva = function (oB) {
    if (oB != null) {
        var backcolor = "#ffff33";
        if (isPartitaIva(oB.value))
            backcolor = "Lime";
        oB.style.backgroundColor = backcolor;
    }
}

window.submitForm = function () {

    if (!checkControl("CCIAA", oTexts.CCIAA))
        return false;

    if (!checkControl("PartitaIva", oTexts.PartitaIva))
        return false;

    if (!checkControl("RagioneSociale", oTexts.RagioneSociale))
        return false;

    if (!checkControl("Denominazione", oTexts.Denominazione))
        return false;

    if (!checkControl("Indirizzo", oTexts.Indirizzo))
        return false;

    if (!checkControl("CAP", oTexts.CAP))
        return false;

    if (!checkControl("Citta", oTexts.Citta))
        return false;

    var tProvincia = Trim(Provincia.GetText()); 
    if (tProvincia == '' ) 
    {
    alert( oTexts.Provincia );
    Provincia.Focus();
    return false;
    }
   
    var tRegione = Trim(Regione.GetText());
    if (tRegione == '') {
        alert(oTexts.Regione);
        Regione.Focus();
        return false;
    }

    var tNazione = Trim(Nazione.GetText());
    if (tNazione.value == '') {
        alert(oTexts.Nazione);
        Nazione.Focus();
        return false;
    }

    if (!checkControl("Telefono", oTexts.Telefono))
        return false;

    if (!checkControl("Fax", oTexts.Fax))
        return false;

    if (!checkControl("Email", oTexts.Email))
        return false;

    oB = getObject("Email");
    if (oB != null) {
        if (!isEmail(oB.value)) {
            alert(oTexts.MailInvalid);
            oB.focus();
            return false;
        }
    }
    var mailControl = window.MailIsValid["Email"];
    if (mailControl == null) mailControl = false;
    if (!mailControl) {
        alert(oTexts.MailInvalid);
        oB.focus();
        return false;
    }

    if (!checkControl("Titolare", oTexts.Titolare))
        return false;

    if (!checkControl("Cognome", oTexts.Cognome))
        return false;

    if (!checkControl("Nome", oTexts.Nome))
        return false;

    if (!checkControl("ReferenteEmail", oTexts.ReferenteEmail))
        return false;

    var oB = getObject("ReferenteEmail");
    if (oB != null) {
        if (!isEmail(oB.value)) {
            alert(oTexts.MailInvalid);
            oB.focus();
            return false;
        }
    }
    mailControl = window.MailIsValid["ReferenteEmail"];
    if (mailControl == null) mailControl = false;
    if (!mailControl) {
        alert(oTexts.MailInvalid);
        oB.focus();
        return false;
    }

    var UserID = getObject("UserID");
    if (UserID != null) {
        UserID.value = Trim(UserID.value);
        if (UserID.value == '') {
            alert(oTexts.UserIDRequired);
            UserID.focus();
            return false;
        }
        if (!isText(UserID.value)) {
            alert(oTexts.UserIDInvalid);
            UserID.focus();
            return false;
        }
        if (UserID.value.length < 5 || UserID.value.length > 20) {
            alert(oTexts.UserIDRequired);
            UserID.focus();
            return false;
        }
    }

    var Password = getObject("Password1");
    if (Password != null) {
        Password.value = Trim(Password.value);
        if (Password.value == '') {
            alert(oTexts.PasswordRequired);
            Password.focus();
            return false;
        }
        if (!isText(Password.value)) {
            alert(oTexts.PasswordInvalid);
            Password.focus();
            return false;
        }
        if (Password.value.length < 5 || Password.value.length > 20) {
            alert(oTexts.PasswordRequired);
            Password.focus();
            return false;
        }
    }

    var Password2 = getObject('Password2');
    if (Password2 != null) {
        Password2.value = Trim(Password2.value);
        if (Password2.value == '') {
            alert(oTexts.Password2Required);
            Password2.focus();
            return false;
        }
        if (!isText(Password2.value)) {
            alert(oTexts.Password2Invalid);
            Password2.focus();
            return false;
        }
        if (Password.value != Password2.value) {
            alert(oTexts.PasswordNotMacth);
            Password2.focus();
            return false;
        }
    }

    if (getAccept("Condizioni") != "SI") {
        alert(oTexts.Accettazione);
        return false;
    }

    if (getAccept("Promo") == "") {
        alert(oTexts.Promo);
        return false;
    }

    SubmitCallBack();
    return false;
}

window.LastMailAddress = "";
window.LastDnsStatus = 0;
window.MailIsValid = new Array();
window.checkingField = "";

window.MailCallBack = function (fieldName) {
    var oB = getObject(fieldName);
    if (oB != null) {
        window.checkingField = fieldName;
        var mail = Trim(oB.value);
        if (mail.length > 0) {
            if ((mail.length < 7) || !isEmail(mail.toLowerCase())) {
                window.LastMailAddress = mail;
                window.LastDnsStatus = 1;
                MailCallBackProcess(window.LastDnsStatus);
                return false;
            }
            if (mail != window.LastMailAddress) {
                //callback eseguito se la mail è modificata
                eMailCallBack.PerformCallback(mail);
                LoadingPanel.ShowInElementByID(fieldName);
            }
            else {
                //optional - rivisualizza l'esito precedente
                MailCallBackProcess(window.LastDnsStatus);
            }
        }
    }
}

window.MailCallBackProcess = function (result) {
    var resultCode = parseInt(result);
    var backcolor = "#ffff33"; //yellow;
    window.MailIsValid[window.checkingField] = true;
    if (resultCode != 0) {
        switch (resultCode) {
            case 1:
                backcolor = "#ff3300"; //red;             
                alert(oTexts.MailInvalid);
                break;
            case 2:
                alert(oTexts.ErrMailServerDown);
                break;
            case 3:
            default:
                alert(oTexts.ErrDnsConnection);
        }
        window.MailIsValid[window.checkingField] = false;
    }
    else {
        backcolor = "Lime";
    }
    var oB = getObject(window.checkingField);
    if (oB != null) {
        oB.style.backgroundColor = backcolor;
    }
}

window.MailCallBackComplete = function (s, e) {
    window.LastDnsStatus = e.result; //esito
    window.LastMailAddress = e.parameter; //mail
    MailCallBackProcess(e.result);
}

window.SubmitCallBack = function () {
    ConfirmCallBack.PerformCallback();
    ConfirmPanel.Show();
}

window.SubmitCallBackProcess = function (result) {
    if (result != null) {
        if (result.length > 3) {
            var esito = result.split("#");
            var resultCode = parseInt(esito[0]);
            if (resultCode != 0) {
                alert(esito[1]);
                return;
            }
            alert(esito[1]);
            oB = getObject("Registra");
            if (oB != null) {
                oB.disabled = true;
                oB.src = "content/images/pulsante_invia2.png";
            }
            return;
        }
    }
    alert(oTexts.Conferma);
    return;
}

window.SubmitCallBackComplete = function (s, e) {
    SubmitCallBackProcess(e.result);
}



