// Funzione per gestire i messaggi di avviso
function MessageBox(Text, MessageMode)
{
	if (MessageMode=="alert")
	{
		alert(Text);
	}
	else
	{
		if (confirm(Text)) 
		{ 
			document.getElementById("__IgnoreWarnings").setAttribute("value", "true"); 
			document.getElementById("__Save").click(); 
		} 
		else 
			document.getElementById("__IgnoreWarnings").setAttribute("value", "false"); 
	}
}

function FormSearch()
{
	var result = self.showModalDialog(Application_root+"SearchResult.aspx", null, "scroll: No; dialogHeight: 500px; dialogWidth: 950px; edge: Raised; center: Yes; help: No; resizable: Yes; status: No;");

	var codice = new String(result[0]);
	if (codice=="undefined") codice="";
	document.getElementById("__ObjectCode").setAttribute("value", codice);
	if (codice!="")
		document.getElementById("__GetOne").click();
	
}

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 "SearchFound" : this.ValidateSearchFoundValidator(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")
	{
		var Text = document.getElementById(validator.Id).getAttribute("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.ValidateSearchFoundValidator = function (validator) 
{
	if (validator.ControlType=="SearchTextBox")
	{
		var Text = document.getElementById(validator.Id).getAttribute("value");
		var PrevText = document.getElementById(validator.Id + "PrevText").getAttribute("value");
		var thisCode = document.getElementById(validator.Id + "thisCode").getAttribute("value");
		if (Text!="" && (thisCode=="" || Text!=PrevText))
			validator.IsValid=false;
		else
			validator.IsValid=true; 		
	}
}





