﻿function Login() {
    chk = true;
    if (document.getElementById("formHeader_Utente").value == "") chk = false;
    if (document.getElementById("formHeader_Pass").value == "") chk = false;

    if (chk == false) {
        return;
    }
    document.getElementById("formHeader_LogCmd").value = "LOG";
    document.formAsis.submit();
}

function Logout() {
    document.getElementById("formHeader_LogCmd").value = "OUT";
    document.formAsis.submit();
}

function Aggiorna() {
    document.formAsis.submit();
}

function isEmail(string) {
    if (string.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1)
        return true;
    else
        return false;
}

/* Registrazione */
function CheckReg() {
    switch (document.getElementById("Fase").value) {
        case "1":
            chk = true;
            if (document.getElementById("NumUtenza").value == "") chk = false;
            if (document.getElementById("Nome").value == "") chk = false;
            if (document.getElementById("PinSi").checked == true) {
                if (document.getElementById("txtPIN").value == "") chk = false;
            }

            if (chk == false) {
                alert("Per completare la registrazione è necessario inserire i dati richiesti.");
                return;
            }
            if (document.getElementById("PinNo").checked == true) {
                document.getElementById("Comando").value = "F1A";
            }
            else {
                document.getElementById("Comando").value = "F1B";
            }
            document.formAsis.submit();
            break;
        case "2":
            chk = true;
            if (document.getElementById("Mail").value == "") chk = false;
            if (document.getElementById("Utente").value == "") chk = false;
            if (document.getElementById("Pass").value == "") chk = false;

            if (chk == false) {
                alert("Per completare la registrazione è necessario inserire i dati richiesti.");
                return;
            }

            if (!isEmail(document.getElementById("Mail").value)) {
                alert("Attenzione, l'indirizzo email inserito non è valido.");
                return;
            }

            if (document.getElementById("Pass").value != document.getElementById("Conferma").value) {
                alert("Attenzione, la password inserita e la password di conferma non coincidono.");
                return;
            }

            document.getElementById("Comando").value = "F2";
            document.formAsis.submit();
            break;
    }
}

/* Utenze */
function SelUtenza(ID) {
    document.getElementById("IDUtenza").value = ID;
    document.formAsis.submit();
}
function Verifica() {
    chk = true;
    if (document.getElementById("Utenza").value == "") chk = false;
    if (document.getElementById("Fattura").value == "") chk = false;
    if (document.getElementById("Importo").value == "") chk = false;

    if (chk == false) {
        alert("Per attivare l'utenza inserire i campi richiesti.");
        return;
    }
    document.getElementById("Comando").value = "ADD";
    document.formAsis.submit();
}

/* Autolettura */
function InviaLettura() {
    chk = true;
    obj = document.getElementById("Lettura").value;

    if (obj == "") {
        alert("Inserire la lettura corrente del contatore");
        return;
    }
    if (obj.length < 5) {
        alert("Il numero inserito non è valido");
        return;
    }
    for (n = 0; n < obj.length; n++) {
        if (isNaN(obj.substr(n, 1))) chk = false;
    }

    if (chk == false) {
        alert("Il numero inserito non è valido");
        return;
    }
    document.getElementById("Comando").value = "SEND";
    document.formAsis.submit();
}

