var timer = setInterval('blinkIt()', 1000);
function blinkIt() {
    if (!document.all) return;
    else {
        for (i = 0; i < document.all.tags('blink').length; i++) {
            s = document.all.tags('blink')[i];
            s.style.visibility = (s.style.visibility == 'visible') ? 'hidden' : 'visible';
        }
    }
}

function findObj(theObj, theDoc) {
    var p, i, foundObj;

    if (!theDoc)
        theDoc = document;

    if ((p = theObj.indexOf("?")) > 0 && parent.frames.length) {
        theDoc = parent.frames[theObj.substring(p + 1)].document;
        theObj = theObj.substring(0, p);
    }
    if (!(foundObj = theDoc[theObj]) && theDoc.all)
        foundObj = theDoc.all[theObj];

    for (i = 0; !foundObj && i < theDoc.forms.length; i++)
        foundObj = theDoc.forms[i][theObj];

    for (i = 0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)
        foundObj = findObj(theObj, theDoc.layers[i].document);

    if (!foundObj && document.getElementById)
        foundObj = document.getElementById(theObj);

    return foundObj;
}

// Funzione per gestire i messaggi di avviso
function MessageBox(Text, MessageMode) {
    if (MessageMode == "alert") {
        alert(Text);
    }
    else {
        if (confirm(Text)) {
            document.getElementById("__IgnoreWarnings").value = true;
            document.getElementById("__Save").click();
        }
        else
            document.getElementById("__IgnoreWarnings").value = false;
    }
}

function FieldValidator(id, context, controlType, validatorType, validatorArgs, errorMessage) {
    // id del controllo da validare
    this.Id = id;
    // risultato della validazione sul controllo
    this.IsValid = true;
    // il nome della tabella/classe a cui fa riferimento
    this.Context = context;
    // il tipo di validatore (ex SearchFound, Required, Range, RegExpr ecc)
    this.ValidatorType = validatorType;
    // messaggio di alert per l'utente
    this.ErrorMessage = errorMessage;
    // indica il tipo di controllo da validare
    this.ControlType = controlType;
    // indica dei parametri per il validatore (ex range di valori, esprssioni regolari ecc.)
    this.ValidatorArgs = validatorArgs;
}

function PageValidator() {
    this.Validators = new Array();
    var oThis = this;
    // risultato della validazione sul form
    this.IsValid = true;
    // Contesto su cui opera la validazione (nome tabella/vista)
    this.ValidationContext = "";

    this.ContextValidators = null;
    this.setValidationContext = function(context) {
        oThis.ValidationContext = context;
        oThis.ContextValidators = new Array();
        for (i = 0; i < oThis.getLength(); i++) {
            if (oThis.Validators[i].Context == context)
                oThis.ContextValidators.push(oThis.Validators[i]);
        }
    }

    this.getLength = function() { return oThis.Validators.length; }



}

PageValidator.prototype.Add = function(fValidator) {
    this.Validators.push(fValidator);
}

// effettua la validazione nel caso di azione salva
PageValidator.prototype.ValidateOnSave = function(Context) {
    this.setValidationContext(Context);
    this.IsValid = true;

    for (i = 0; i < this.ContextValidators.length; i++) {
        switch (this.ContextValidators[i].ValidatorType) {
            case "Required": this.ValidateRequiredValidator(this.ContextValidators[i]);
                break;
            case "RequiredIF": this.ValidateRequiredIFValidator(this.ContextValidators[i]);
                break;
            case "SearchFound": this.ValidateSearchFoundValidator(this.ContextValidators[i]);
                break;
            case "eMailFormat": this.ValidateEmailFormatValidator(this.ContextValidators[i]);
                break;
        }
        this.IsValid = this.IsValid && this.ContextValidators[i].IsValid;
    }
    return this.IsValid;
}

PageValidator.prototype.ValidateRequiredValidator = function(validator) {
    if (validator.ControlType == "SearchTextBox" || validator.ControlType == "MaskTextBox" || validator.ControlType == "BaseTextBox" || validator.ControlType == "ImageBrowserTextBox" || validator.ControlType == "eMailTextBox") {
        var Text = document.getElementById(validator.Id).value;
        if (Text == "") {
            validator.IsValid = false;
            document.getElementById(validator.Id + "RequiredErrorIcon").style.display = "inline";
        }
        else {
            validator.IsValid = true;
            document.getElementById(validator.Id + "RequiredErrorIcon").style.display = "none";
        }
    }
    else {
        if (validator.ControlType == "RadioYesNo") {
            var Yes = document.getElementById(validator.Id + "_0").checked;
            var No = document.getElementById(validator.Id + "_1").checked;
            if (!(Yes || No)) {
                validator.IsValid = false;
                document.getElementById(validator.Id + "RequiredErrorIcon").style.display = "inline";
            }
            else {
                validator.IsValid = true;
                document.getElementById(validator.Id + "RequiredErrorIcon").style.display = "none";
            }
        }
        else {
            if (validator.ControlType == "ChoseList") {
                var index = document.getElementById(validator.Id).selectedIndex;
                if (index == 0) {
                    validator.IsValid = false;
                    document.getElementById(validator.Id + "RequiredErrorIcon").style.display = "inline";
                }
                else {
                    validator.IsValid = true;
                    document.getElementById(validator.Id + "RequiredErrorIcon").style.display = "none";
                }
            }
            else {
                if (validator.ControlType == "CheckList") {
                    validator.IsValid = false;
                    var listLength = validator.ValidatorArgs;
                    for (chkI = 0; chkI < listLength; chkI++) {
                        if (document.getElementById(validator.Id + "_" + chkI).checked) {
                            validator.IsValid = true;
                            break;
                        }
                    }
                    if (validator.IsValid)
                        document.getElementById(validator.Id + "RequiredErrorIcon").style.display = "none";
                    else
                        document.getElementById(validator.Id + "RequiredErrorIcon").style.display = "inline";
                }
                else {
                    // qui altri controlli
                }

            }
        }
    }
}

PageValidator.prototype.ValidateRequiredIFValidator = function(validator) {
    var IFCondition = eval(validator.ValidatorArgs);

    if (IFCondition == null || IFCondition == "undefined" || IFCondition == false) {
        validator.IsValid = true;
        document.getElementById(validator.Id + "RequiredErrorIcon").style.display = "none";
    }
    else {
        this.ValidateRequiredValidator(validator);
    }
}

PageValidator.prototype.ValidateSearchFoundValidator = function(validator) {
    if (validator.ControlType == "SearchTextBox") {
        var Text = document.getElementById(validator.Id).value;
        var PrevText = document.getElementById(validator.Id + "PrevText").value;
        var thisCode = document.getElementById(validator.Id + "thisCode").value;
        if (Text != "" && (thisCode == "" || Text != PrevText))
            validator.IsValid = false;
        else
            validator.IsValid = true;
    }
}

PageValidator.prototype.ValidateEmailFormatValidator = function(validator) {
    if (validator.ControlType == "eMailTextBox") {
        var Text = document.getElementById(validator.Id).value;
        var re = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
        var rx = new RegExp(re);
        var matches = rx.exec(Text);
        if ((matches != null && Text == matches[0]) || Text == "") {
            validator.IsValid = true;
            document.getElementById(validator.Id + "ErrorIcon").style.display = "none";
        }
        else {
            validator.IsValid = false;
            document.getElementById(validator.Id + "ErrorIcon").style.display = "inline";
        }
    }
}





