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;
			case "DateFormat": this.ValidateDateFormatValidator(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";
		}
	}
}




PageValidator.prototype.ValidateDateFormatValidator = function(validator) {

    if (validator.ControlType == "MaskTextBox") {


        //try {

       
        var Text = document.getElementById(validator.Id).value;


        if (isDate(Text, false)) {



            /*
             
            var d = new Date(Text);
            var d2 = new Date("01-01-1900"); // Le date vanno inserite nel formato US


            alert(d);
            alert(d2);


            if (isNaN(d.valueOf())) {
            validator.IsValid = false;
            document.getElementById("IMG" + validator.Id).style.display = "inline";
            return;
            }

            if (d < d2) {

                validator.IsValid = false;
            document.getElementById("IMG" + validator.Id).style.display = "inline";
            }

            else {
            validator.IsValid = true;
            document.getElementById("IMG" + validator.Id).style.display = "none";
            }

        }

        catch (e) {

            validator.IsValid = false;
            document.getElementById("IMG" + validator.Id).style.display = "inline";

        }
        
        

        }
            */

            validator.IsValid = true;
            document.getElementById("IMG" + validator.Id).style.display = "none";
        }
        else {
            validator.IsValid = false;
            document.getElementById("IMG" + validator.Id).style.display = "inline";


        }

    }

}


function isDate(text, typing) {
   
    month = text.slice(3, 5);
    day = text.slice(0, 2);
    year = text.slice(6, 10);
    if (typing) {
        if (day > 31 || (isNaN(day) && month > 0)) {
            return false;
        }
        if (month > 12 || (isNaN(month) && year > 0)) {
            return false;
        }
    }
    else {
        if (isNaN(day) || isNaN(month) || isNaN(year))
            return false;
        if (day > 31) {
            return false;
        }
        if (month > 12) {
            return false;
        }
    }
    if (year > 2050 || year < 1900) {
        return false;
    }
    if ((month == 11 || month == 4 || month == 6 || month == 9) && day > 30) {
        return false;
    }
    else if (month == 2 && day > 28 && year % 4 > 0) {
        return false;
    }
    else if (month == 2 && day > 29 && year % 4 == 0) {
        return false;
    }
    return true;
}


