/// <reference path="../../JQ/jquery.js" />

var arrErr = new Array();
var indErrore = 0;

function FormVerify() {

    arrErr = new Array();
    indErrore = 0;

    var intCap = 0;

    var frm = $('form').first();

    recursiveCheck(frm);

    // Visualizzatore errori
    if (arrErr.length > 0) {
        showJDialog('Errore di validazione', arrErr);
        return false;
    } else { return true }

    // fine funzione form Verify

}

function recursiveCheck(startObj) {

    var children = startObj.children();

    children.each(function () {

        checkError(this);

        if ($(this).children().length > 0)
            recursiveCheck($(this));

    });

}


function checkError(e) {
    
 

        // Filtro i campi da validare
        if (e.getAttribute('NeedValidation') == 'True') {

  

            // Determino i valori minimi e massimi
            arrVal = e.getAttribute('Range').split(',');

            var minValue = arrVal[0];
            var maxValue = arrVal[1];


            switch (e.getAttribute('ValidationType')) {
                // elenco dei moduli di controllo          
                case 'Numerico':

                    var pattern = /^\d{1,}$/;
                    if ((e.value.length > 0 && e.getAttribute('Optional') == "True") || (e.getAttribute('Optional') == "False")) {
                        if (!((pattern.test(e.value)) && (e.value - 0 >= minValue) && (e.value - 0 <= maxValue))) {
                            arrErr[indErrore] = e.getAttribute('ErrorMessage') + '\r'; indErrore++; e.style.backgroundColor = '#ffffcc';
                        };
                    }

                    break;

                case 'alfanumerico':

                    var pattern = /^[^\\]*$/;
                    if ((e.value.length > 0 && e.getAttribute('Optional') == "True") || (e.getAttribute('Optional') == "False")) {
                        if (!((pattern.test(e.value)) && (e.value.length >= minValue) && (e.value.length <= maxValue))) {
                            arrErr[indErrore] = e.getAttribute('ErrorMessage') + '\r'; indErrore++; e.style.backgroundColor = '#ffffcc';
                        };
                    }

                    break;

                case 'Stringa':
                    var pattern = /^[\s\S]*$/;
                    if ((e.value.length > 0 && e.getAttribute('Optional') == "True") || (e.getAttribute('Optional') == "False")) {
                        if (!((pattern.test(e.value)) && (e.value.length >= minValue) && (e.value.length <= maxValue))) {
                            arrErr[indErrore] = e.getAttribute('ErrorMessage') + '\r'; indErrore++; e.style.backgroundColor = '#ffffcc';
                        };
                    }
                    break;

                case 'DueStringhe':

                    var pattern = /^\w*[\s]{1}\w*[^\?,"]*$/;

                    if ((e.value.length > 0 && e.getAttribute('Optional') == "True") || (e.getAttribute('Optional') == "False")) {
                        if (!((pattern.test(e.value)) && (e.value.length >= minValue) && (e.value.length <= maxValue))) {
                            arrErr[indErrore] = e.getAttribute('ErrorMessage') + '\r'; indErrore++; e.style.backgroundColor = '#ffffcc';
                        };
                    }
                    break;

                case 'Email':
                    var pattern = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
                    if ((e.value.length > 0 && e.getAttribute('Optional') == "True") || (e.getAttribute('Optional') == "False")) {
                        if (!((pattern.test(e.value)) && (e.value.length >= minValue) && (e.value.length <= maxValue))) {
                            arrErr[indErrore] = e.getAttribute('ErrorMessage') + '\r'; indErrore++; e.style.backgroundColor = '#ffffcc';
                        };
                    }
                    break;

                case 'Data':
                    var pattern = /^\d{2}\/{1}\d{2}\/{1}\d{4}$/;
                    if ((e.value.length > 0 && e.getAttribute('Optional') == "True") || (e.getAttribute('Optional') == "False")) {
                        if (!((pattern.test(e.value)) && (e.value.length >= minValue) && (e.value.length <= maxValue))) {
                            arrErr[indErrore] = e.getAttribute('ErrorMessage') + '\r'; indErrore++; e.style.backgroundColor = '#ffffcc';
                        };
                    }
                    break;

                case 'Valuta':
                    var pattern = /^(\d*|\d*[\,]{1}\d{1,2})$/;
                    if ((e.value.length > 0 && e.getAttribute('Optional') == "True") || (e.getAttribute('Optional') == "False")) {
                        if (!((pattern.test(e.value)) && (e.value.length >= minValue) && (e.value.length <= maxValue))) {
                            arrErr[indErrore] = e.getAttribute('ErrorMessage') + '\r'; indErrore++; e.style.backgroundColor = '#ffffcc';
                        };
                    }
                    break;

                case 'valuta2':
                    var pattern = /^\d{1,},{0,1}\d{0,2}$/;
                    if ((e.value.length > 0 && e.getAttribute('Optional') == "True") || (e.getAttribute('Optional') == "False")) {
                        if (!((pattern.test(e.value)) && (e.value.length >= minValue) && (e.value.length <= maxValue))) {
                            arrErr[indErrore] = e.getAttribute('ErrorMessage') + '\r'; indErrore++; e.style.backgroundColor = '#ffffcc';
                        };
                    }
                    break;

                case 'valuta4':
                    var pattern = /^\d{1,},{0,1}\d{0,4}$/;
                    if ((e.value.length > 0 && e.getAttribute('Optional') == "True") || (e.getAttribute('Optional') == "False")) {
                        if (!((pattern.test(e.value)) && (e.value.length >= minValue) && (e.value.length <= maxValue))) {
                            arrErr[indErrore] = e.getAttribute('ErrorMessage') + '\r'; indErrore++; e.style.backgroundColor = '#ffffcc';
                        };
                    }
                    break;



                case 'SoloNumeri':
                    var pattern = /^\d{1,}$/;
                    if ((e.value.length > 0 && e.getAttribute('Optional') == "True") || (e.getAttribute('Optional') == "False")) {
                        if (!((pattern.test(e.value)) && (e.value.length >= minValue) && (e.value.length <= maxValue))) {
                            arrErr[indErrore] = e.getAttribute('ErrorMessage') + '\r'; indErrore++; e.style.backgroundColor = '#ffffcc';
                        };
                    }
                    break;

                case 'regexp':

                    var _text = e.getAttribute('Options');
                    var pattern = new RegExp(_text, "g");

                    if (_text == 'string') {
                        pattern = new RegExp('^[^\42]*$', "g");

                        if ((e.value.length > 0 && e.getAttribute('Optional') == "True") || (e.getAttribute('Optional') == "False")) {
                            if (!((pattern.test(e.value)) && (e.value.length >= minValue) && (e.value.length <= maxValue))) {
                                arrErr[indErrore] = e.getAttribute('ErrorMessage') + '\r'; indErrore++; e.style.backgroundColor = '#ffffcc';
                            };
                        }
                    }
                    else if (_text == 'number') {
                        pattern = new RegExp('^\d{1,}$', "g");

                        if ((e.value.length > 0 && e.getAttribute('Optional') == "True") || (e.getAttribute('Optional') == "False")) {
                            if (!((pattern.test(e.value)) && (e.value - 0 >= minValue) && (e.value - 0 <= maxValue))) {
                                arrErr[indErrore] = e.getAttribute('ErrorMessage') + '\r'; indErrore++; e.style.backgroundColor = '#ffffcc';
                            };
                        }

                    } else if (_text != '') {
                        if (!pattern.test(e.value) && _text != '') {
                            arrErr[indErrore] = e.getAttribute('ErrorMessage') + '\r'; indErrore++; e.style.backgroundColor = '#ffffcc';
                        }
                    }
                    else {
                        alert('Attenzione una validazione non è compilata correttamente.');
                    }

                    break;

                case 'StringaComparata':
                    if ((e.value.length > 0 && e.getAttribute('Optional') == "True") || (e.getAttribute('Optional') == "False")) {
                        if (!(e.value == document.all[e.Options].value)) {
                            arrErr[indErrore] = e.getAttribute('ErrorMessage') + '\r'; indErrore++; e.style.backgroundColor = '#ffffcc';
                        };
                    }
                    break;

            } // Fine Switch

        } // Fine IF Need Validation



   


}




function ripristina(obj) {
    obj.style.color = 'black';
}






