//Version: 1.03, 20/01/2005
//Version: 1.04, 31/01/2005: Corrected bug for validation summary being presented only once
//Version: 1.05, 06/04/2005: Corrected bug for m_IsValid not being re-set to true when controls to be validated are empty
//Version: 1.10, 13/04/2005: Introduce DropDownListNested validator
//Version: 1.11, 18/10/2005: Introduced date\time variants: DateOnly, TimeOnly, DateTime
//Version: 1.14, 03/07/2006: Added support for tab control. Page with control not validating is shown
//Version: 1.15, 27/11/2006: Modificato RequiredFieldValidator_Validate per gestire la radiobutton
//Version: 1.16, 27/11/2006: Rinominato alcune funzioni per conflitto con WebResource.axd
//Version: 1.17, 27/11/2006: Aggiunto controllo sul tipo Email
//Version: 1.18, 25/07/2007: RequiredFieldValidator: gestione dell'elemento multiplo Radio
//Version: 1.19, 30/01/2008: Aggiunto il Validatori: NotBothFieldValidator, CompareDateValidator
//Version: 1.20, 31/01/2008: TypeValidator: aggiunto il controllo per email multiple separate da ';'
//Version: 1.21, 23/03/2010: Aggiunta funzione StringToDate

//The page validators array
var Page_ValidatorsAltamira = new Array;
var Page_ValidationSummariesAltamira = new Array;
var i=0;
var bValidationSummaryAlertShown = false;
var Altamira_Web_Validation_ScriptVersion=118;

//The page validate call
function Validate(strGlobalValidation)
{
    var i = 0; var bValid = true; var bFirstInvalidValidatorShown = false; var bFirstInvalidValidatorinPanelShown = false;
	for (i=0; i< Page_ValidatorsAltamira.length; ++i)
	{
		if ( (Page_ValidatorsAltamira[i].m_ValidatorControl.id.indexOf(strGlobalValidation) != -1)  && (!Page_ValidatorsAltamira[i].Validate()) )
		{
			bValid = false;
			if(!bFirstInvalidValidatorShown)
			{
				//Show tab if hidden
				var arTabControls = BuildTabControlHeirarchy(Page_ValidatorsAltamira[i].m_ValidatorControl, null);
				//Activate the tabs
				if(arTabControls)
				{
				  for(j=0; j < arTabControls.length; ++j)
					  arTabControls[j].TabControl.showLayerNumber(arTabControls[j].TabPage);
				}
				try
				{				
					//Set the focus if possibile
					Page_ValidatorsAltamira[i].m_ControlToValidate.focus();
				}
				catch(e)
				{
				}				
				//We have shown the tab page of the first validation control that is not valid.
				//Avoid showing any more
				bFirstInvalidValidatorShown = true;
           }
            //Vediamo se sto' validatore è pure dentro un panel
           if (typeof (BuildPanelControlHeirarchy) != 'undefined') {
               if (!bFirstInvalidValidatorinPanelShown) {

                   var arPanelControls = BuildPanelControlHeirarchy(Page_ValidatorsAltamira[i].m_ValidatorControl, null);

                   for (j = 0; j < arPanelControls.length; ++j) {
                       if (!isOpen(arPanelControls[j].m_PanelID))
                           TogglePanel(arPanelControls[j].m_PanelID);
                   }
                   try {
                       //Set the focus if possibile
                       Page_ValidatorsAltamira[i].m_ControlToValidate.focus();
                   }
                   catch (e) {
                   }
                   bFirstInvalidValidatorinPanelShown = true;
               }
           }

		}
	}
	//Show validation summaries
	bValidationSummaryAlertShown = false;
	for (i=0; i < Page_ValidationSummariesAltamira.length; ++i)
	{
		if (Page_ValidationSummariesAltamira[i].m_ValidatorControl.id.indexOf(strGlobalValidation) != -1) 
			Page_ValidationSummariesAltamira[i].Show(strGlobalValidation);
	}
	//Return the result
	return bValid
}

//***************************** The required field validator *****************************
function RequiredFieldValidator(strIDValidatorCtrl, strIDControlToValidate, strErrorMessage, strText, strInitialValue, bDisplay, bEnabled)
{
	this.m_ValidatorControl=GetElement(strIDValidatorCtrl);
	var objControlToValidate = GetElement(strIDControlToValidate);
	//25/07/2007: elemento multiplo Radio, devo recuperare l'elemento con il getElementsByName perchè il getelementByName recupera solo il primo elemento
	if (objControlToValidate.type=='radio') objControlToValidate = GetElementsName(objControlToValidate.name)
	this.m_ControlToValidate=objControlToValidate; //GetElement(strIDControlToValidate);
	this.m_ErrorMessage=strErrorMessage;
	this.m_Text = strText;
	this.m_Display = bDisplay;
	this.m_Enabled = bEnabled;
	this.m_IsValid = true;
	
	this.m_InitialValue=strInitialValue;
	
	this.Validate = RequiredFieldValidator_Validate;
	this.FormatValidatorMessage = FormatValidatorMessage;
}

function RequiredFieldValidator_Validate()
{
	this.m_IsValid = true;
	if (!this.m_Enabled) return true;
	//If it's disabled then we are valid
	if (this.m_ControlToValidate.tagName)
	{
		//Control
		if (this.m_ControlToValidate.value == null) return true;
		//Run the validation
		if (ValidatorTrimA(this.m_ControlToValidate.value) == ValidatorTrimA(this.m_InitialValue))
		{
			this.FormatValidatorMessage()
			return false;
		}
	}
	else
	{
		//Array
		var bChecked=false;
		for (var i=0; i<this.m_ControlToValidate.length; i++) 
		{
			if (this.m_ControlToValidate[i].checked) {
				bChecked=true;
			}
		}
		if (bChecked==false) 
		{
			this.FormatValidatorMessage();
			return false;
		}
	}	
	//We are valid
	this.m_ValidatorControl.style.display="none";
	this.m_IsValid = true;
	return true
}

//***************************** The string length validator *****************************
function StringLengthValidator(strIDValidatorCtrl, strIDControlToValidate, strErrorMessage, strText, lMinimumLength, lMaximumLength, bDisplay, bEnabled)
{
	this.m_ValidatorControl=GetElement(strIDValidatorCtrl);
	this.m_ControlToValidate=GetElement(strIDControlToValidate);
	this.m_ErrorMessage=strErrorMessage;
	this.m_Text = strText;
	this.m_Display = bDisplay;
	this.m_Enabled = bEnabled;
	this.m_IsValid = true;
	
	this.m_MinimumLength=lMinimumLength;
	this.m_MaximumLength=lMaximumLength;
	
	this.Validate = StringLengthValidator_Validate;
	this.FormatValidatorMessage = FormatValidatorMessage;
}

function StringLengthValidator_Validate()
{
	this.m_IsValid = true;
	if (!this.m_Enabled) return true;
	//If it's disabled then we are valid
	if (this.m_ControlToValidate.value == null) return true;
	//Run the validation
	var controlValue = ValidatorTrimA(this.m_ControlToValidate.value)
	//Empty controls are valid
	if (controlValue=="") return true;
	//TODO: Trim texts
	var bValid=false;
	if (this.m_MinimumLength == 0 && this.m_MaximumLength == 0)
		{bValid=true;}
	if (this.m_MinimumLength == 0 && this.m_MaximumLength != 0)
		{if (controlValue.length <= this.m_MaximumLength) bValid=true;}
	if (this.m_MinimumLength != 0 && this.m_MaximumLength == 0)
		{if (controlValue.length >= this.m_MinimumLength) bValid=true;}
	if (this.m_MinimumLength != 0 && this.m_MaximumLength != 0)
		{if (controlValue.length >= this.m_MinimumLength && controlValue.length <= this.m_MaximumLength) bValid=true;}
	//Are we valid?	
	if (!bValid)
	{
		this.FormatValidatorMessage()
		return false;
	}
	//We are valid
	this.m_ValidatorControl.style.display="none";
	this.m_IsValid = true;
	return true
}

//***************************** The type validator *****************************
function TypeValidator(strIDValidatorCtrl, strIDControlToValidate, strErrorMessage, strText, strType, lCentury, lCutoffyear, strDecimalchar, strGroupchar, lDigits, strDateorder, strDatechar, strTimechar, bDisplay, bEnabled)
{
	this.m_ValidatorControl=GetElement(strIDValidatorCtrl);
	this.m_ControlToValidate=GetElement(strIDControlToValidate);
	this.m_ErrorMessage=strErrorMessage;
	this.m_Text = strText;
	this.m_Display = bDisplay;
	this.m_Enabled = bEnabled;
	this.m_IsValid = true;
	
	this.m_Type=strType;
	this.century=lCentury;
	this.cutoffyear=lCutoffyear;
	this.decimalchar=strDecimalchar;
	this.groupchar=strGroupchar;
	this.digits=lDigits;
	this.dateorder=strDateorder;
	this.datechar=strDatechar;
	this.timechar=strTimechar;
	
	this.Validate = TypeValidator_Validate;
	this.FormatValidatorMessage = FormatValidatorMessage;
}

function TypeValidator_Validate()
{
	this.m_IsValid = true;
	if (!this.m_Enabled) return true;
	//If it's disabled then we are valid
	if (this.m_ControlToValidate.value == null) return true;
	//Run the validation
	var controlValue = ValidatorTrimA(this.m_ControlToValidate.value)
	//Empty controls are valid
	if (controlValue=="") return true;
	var bValid=false;
	var controlValueTyped = ValidatorConvertA(controlValue, this.m_Type, this);
	//Are we valid?	
	if (controlValueTyped == null)
	{
		this.FormatValidatorMessage()
		return false;
	}
	//Do we have a range to check?
	//We are valid
	this.m_ValidatorControl.style.display="none";
	this.m_IsValid = true;
	return true
}
//***************************** The range validator *****************************
function RangeValidator(strIDValidatorCtrl, strIDControlToValidate, strErrorMessage, strText, strType, lCentury, lCutoffyear, strDecimalchar, strGroupchar, lDigits, strDateorder, strDatechar, strTimechar, objMinimum, objMaximum, bDisplay, bEnabled)
{
	this.m_ValidatorControl=GetElement(strIDValidatorCtrl);
	this.m_ControlToValidate=GetElement(strIDControlToValidate);
	this.m_ErrorMessage=strErrorMessage;
	this.m_Text = strText;
	this.m_Display = bDisplay;
	this.m_Enabled = bEnabled;
	this.m_IsValid = true;
	
	this.m_Type=strType;
	this.century=lCentury;
	this.cutoffyear=lCutoffyear;
	this.decimalchar=strDecimalchar;
	this.groupchar=strGroupchar;
	this.digits=lDigits;
	this.dateorder=strDateorder;
	this.datechar=strDatechar;
	this.timechar=strTimechar;
	
	this.Minimum = objMinimum;
	this.Maximum = objMaximum;
	
	this.Validate = RangeValidator_Validate;
	this.FormatValidatorMessage = FormatValidatorMessage;
}

function RangeValidator_Validate()
{
	this.m_IsValid = true;
	if (!this.m_Enabled) return true;
	//If it's disabled then we are valid
	if (this.m_ControlToValidate.value == null) return true;
	//Run the validation
	var controlValue = ValidatorTrimA(this.m_ControlToValidate.value)
	//Empty controls are valid
	if (controlValue=="") return true;
	var bValid=true;
	var controlValueTyped = ValidatorConvertA(controlValue, this.m_Type, this);
	
	//If we have dates we want values of
	var obMin = null; var obMax = null;
	if (this.m_Type == "Date")
	{
		if (this.Minimum!=null) obMin = this.Minimum.valueOf();
		if (this.Maximum!=null) obMax = this.Maximum.valueOf();
	}
	else
	{
		if (this.Minimum!=null) obMin = this.Minimum;
		if (this.Maximum!=null) obMax = this.Maximum;
	}
	//Are we valid?	
	if (controlValueTyped==null)
	{
		this.FormatValidatorMessage()
		return false;
	}
	//Do we have a range to check?
	if (obMin!=null)
	{
		if (controlValueTyped < obMin)
			bValid = false;
	}
	if (obMax!=null)
	{
		if (controlValueTyped > obMax)
			bValid = false;
	}
	//Are we valid?	
	if (!bValid)
	{
		this.FormatValidatorMessage()
		return false;
	}
	//We are valid
	this.m_ValidatorControl.style.display="none";
	this.m_IsValid = true;
	return true
}

//***************************** The NestedDropDownList validator *****************************
function DropDownCollectorValidator(strIDValidatorCtrl, strIDControlToValidate, strErrorMessage, strText, strDesiredListCtrl, strRequiredListCtrl, bDisplay, bEnabled)
{
	this.m_ValidatorControl=GetElement(strIDValidatorCtrl);
	this.m_ControlToValidate=GetElement(strIDControlToValidate);
	this.m_ErrorMessage=strErrorMessage;
	this.m_Text = strText;
	this.m_Display = bDisplay;
	this.m_Enabled = bEnabled;
	this.m_IsValid = true;

	this.m_DesiredListCtrl = GetElement(strDesiredListCtrl);
	this.m_ControlToValidateID=strIDControlToValidate;
	//Only extract if set
	if (strRequiredListCtrl == "")
	{
		this.m_RequiredListCtrl = null;
		this.m_RequiredListCtrlID="";
	}
	else
	{
		this.m_RequiredListCtrl = GetElement(strRequiredListCtrl);
		this.m_RequiredListCtrlID=strRequiredListCtrl;
	}
	
	this.Validate = DropDownCollector_Validate;
	this.FormatValidatorMessage = FormatValidatorMessage;
}

function DropDownCollector_Validate()
{
	this.m_IsValid = true;
	if (!this.m_Enabled) return true;
	//If it's disabled then we are valid
	if (this.m_ControlToValidate.value == null) return true;
	//Run the validation
	var controlValue = ValidatorTrimA(this.m_ControlToValidate.value)
	//Empty controls are valid
	if (controlValue=="") return true;
	var bValid=false;
	//Do we have a selection in the desired list?
	var strDesiredValue  = this.m_DesiredListCtrl.options.length ? this.m_DesiredListCtrl.options[this.m_DesiredListCtrl.selectedIndex].value : '';
	//If so we are valid
	if (strDesiredValue!='')
	{
		return true;
	}
	//Is there a required list
	if (this.m_RequiredListCtrl != null)
	{
		//Does it have a selection?
		var strRequiredValue = this.m_RequiredListCtrl.options.length ? this.m_RequiredListCtrl.options[this.m_RequiredListCtrl.selectedIndex].value : '';
		//If not we are not valid
		if (strRequiredValue=='')
		{
			this.FormatValidatorMessage()
			return false;
		}
		//It does. Ensure there are no items in the combos below
		var nLevel = eval(this.m_ControlToValidateID + "_object.GetComboLevel('" + this.m_RequiredListCtrlID + "')");
		var objSelectArray = eval(this.m_ControlToValidateID + "_object.SelectArray");
		for(var i=nLevel + 1; i <= objSelectArray.length - 1; ++i)
		{
			//Get the select
			var objCmbFig = GetElement(objSelectArray[i]);
			//Does it have elements (take into account empty elements)
			if (!(objCmbFig.options.length == 0 || (objCmbFig.options.length == 1 &&  objCmbFig.options[0].value == '')))
				{
					this.FormatValidatorMessage()
					return false;
				}
		}
	}
	else
	{
		//We are not valid
		this.FormatValidatorMessage()
		return false;
	}
		
		
	//We are valid
	this.m_ValidatorControl.style.display="none";
	this.m_IsValid = true;
	return true
}

//***************************** The DoubleRequiredFieldValidator validator *****************************
function NotBothFieldValidator(strIDValidatorCtrl, strIDControlToValidate1, strIDControlToValidate2,strErrorMessage, strText, strInitialValue, bDisplay, bEnabled)
{
	this.m_ValidatorControl=GetElement(strIDValidatorCtrl);
	var objControlToValidate1 = GetElement(strIDControlToValidate1);
	var objControlToValidate2 = GetElement(strIDControlToValidate2);
	//25/07/2007: elemento multiplo Radio, devo recuperare l'elemento con il getElementsByName perchè il getelementByName recupera solo il primo elemento
	this.m_ControlToValidate=objControlToValidate1; //GetElement(strIDControlToValidate);
	this.m_ControlToValidate2=objControlToValidate2;
	this.m_ErrorMessage=strErrorMessage;
	this.m_Text = strText;
	this.m_Display = bDisplay;
	this.m_Enabled = bEnabled;
	this.m_IsValid = true;
	
	this.m_InitialValue=strInitialValue;
	
	this.Validate = NotBothFieldValidator_Validate;
	this.FormatValidatorMessage = FormatValidatorMessage;
}

function NotBothFieldValidator_Validate()
{
	this.m_IsValid = true;
	if (!this.m_Enabled) return true;
	if (this.m_ControlToValidate == null && this.m_ControlToValidate2 == null) return true;
	//If it's disabled then we are valid
	if (this.m_ControlToValidate.value == null && this.m_ControlToValidate2.value == null) return true;
	//Run the validation
	if (ValidatorTrimA(this.m_ControlToValidate.value) != ValidatorTrimA(this.m_InitialValue) && ValidatorTrimA(this.m_ControlToValidate2.value) != ValidatorTrimA(this.m_InitialValue))
	{
		this.FormatValidatorMessage()
		return false;
	}
	
	//We are valid
	this.m_ValidatorControl.style.display="none";
	this.m_IsValid = true;
	return true
}

//***************************** The CompareDateValidator validator *****************************
function CompareDateValidator(strIDValidatorCtrl, strIDDateControlToValidate1, strIDDateControlToValidate2,Ctrl1CompareCtrl2, dateorder, dataType, datechar, timechar,strErrorMessage, strText, strInitialValue, bDisplay, bEnabled)
{
	this.m_ValidatorControl=GetElement(strIDValidatorCtrl);
	var objControlToValidate1 = GetElement(strIDDateControlToValidate1);
	var objControlToValidate2 = GetElement(strIDDateControlToValidate2);
	//25/07/2007: elemento multiplo Radio, devo recuperare l'elemento con il getElementsByName perchè il getelementByName recupera solo il primo elemento
	this.m_ControlToValidate=objControlToValidate1; //GetElement(strIDControlToValidate);
	this.m_ControlToValidate2=objControlToValidate2;
	this.m_ErrorMessage=strErrorMessage;
	this.m_Text = strText;
	this.m_Display = bDisplay;
	this.m_Enabled = bEnabled;
	this.m_IsValid = true;
	this.m_Ctrl1CompareCtrl2 = Ctrl1CompareCtrl2;
	this.m_InitialValue=strInitialValue;
	this.m_dateorder = dateorder;
	this.m_dataType = dataType;
	this.m_datechar = datechar;
	this.m_timechar = timechar;
	this.Validate = CompareDateValidator_Validate;
	this.FormatValidatorMessage = FormatValidatorMessage;
}

function CompareDateValidator_Validate()
{
	this.m_IsValid = true;
	if (!this.m_Enabled) return true;
    
    var DateCtr1 = StringToDate(this.m_ControlToValidate.value, this.m_dateorder, this.m_dataType,this.m_datechar, this.m_timechar)
    var DateCtr2 = StringToDate(this.m_ControlToValidate2.value, this.m_dateorder, this.m_dataType,this.m_datechar, this.m_timechar)

    if(this.m_Ctrl1CompareCtrl2 == "GreatThan")
    {
        if(DateCtr1<=DateCtr2) 
        {
            this.FormatValidatorMessage()
            return false;
	    }
	}
    else if(this.m_Ctrl1CompareCtrl2 == "LessThan")
    {
        if(DateCtr1>=DateCtr2) 
        {
            this.FormatValidatorMessage()
		    return false;
		}
    }
    else if(this.m_Ctrl1CompareCtrl2 == "Egual")
    {
        if(DateCtr1>DateCtr2 || DateCtr1<DateCtr2) 
        {
            this.FormatValidatorMessage()
		    return false;
		}
	}
	else if(this.m_Ctrl1CompareCtrl2 == "GreatEqualThan")
    {
        if(DateCtr1<DateCtr2) 
        {
            this.FormatValidatorMessage()
            return false;
	    }
	}
	else if(this.m_Ctrl1CompareCtrl2 == "LessEqualThan")
    {
        if(DateCtr1>DateCtr2) 
        {
            this.FormatValidatorMessage()
            return false;
	    }
	}

    
	//We are valid
	this.m_ValidatorControl.style.display="none";
	this.m_IsValid = true;
	return true
}


//***************************** The validation summary *****************************

function ValidationSummary(strIDValidatorCtrl, strHeaderText, bEnabled, bDisplay, bShowMessagebox, lDisplayMode)
{
	this.m_ValidatorControl=GetElement(strIDValidatorCtrl);
	this.m_HeaderText=strHeaderText;
	this.m_Enabled = bEnabled;
	this.m_ShowMessagebox = bShowMessagebox;
	this.m_DisplayMode = lDisplayMode;
	this.m_Display = bDisplay;

	//Separators and mode	
	this.m_SeparatorHeader = "";	this.m_SeparatorFirst = "";	this.m_SeparatorPre = "";	this.m_SeparatorPost = "";	this.m_SeparatorFinal = "";
  this.DisplayModeList = 0;	this.DisplayModeBulletList = 1;	this.DisplayModeSingleParagraph = 2;	this.DisplayModeOrderedList = 3;
  
  this.Show = ValidationSummary_Show;
	this.FormatTags = ValidationSummarySetFormatTags;
	
	this.FormatTags();
}

function ValidationSummarySetFormatTags()
{
	switch (this.m_DisplayMode)
	{
		case this.DisplayModeBulletList:
			this.m_SeparatorHeader = "";	this.m_SeparatorFirst = "<ul>";	this.m_SeparatorPre = "<li>";	this.m_SeparatorPost = "</li>";	this.m_SeparatorFinal = "</ul>";	break;
		case this.DisplayModeOrderedList:
			this.m_SeparatorHeader = "";	this.m_SeparatorFirst = "<ol>";	this.m_SeparatorPre = "<li>";	this.m_SeparatorPost = "</li>";	this.m_SeparatorFinal = "</ol>";	break;
		case this.DisplayModeList:
		  this.m_SeparatorHeader = "<br />"; this.m_SeparatorFirst = ""; this.m_SeparatorPre = ""; this.m_SeparatorPost = "<br />"; this.m_SeparatorFinal = ""; break;
		case this.DisplayModeSingleParagraph:
			this.m_SeparatorHeader = " ";	this.m_SeparatorFirst = "";	this.m_SeparatorPre = "";	this.m_SeparatorPost = " ";	this.m_SeparatorFinal = "";	break;
	}
}

function ValidationSummary_Show(strGlobalValidation)
{
	if (!this.m_Enabled) return true;
	var i = 0; var strMsgHTML=""; var strMsg=""; var bValid = true;
	//The header
	strMsg = strMsgHTML += this.m_HeaderText; if (this.m_HeaderText!="") strMsg+="\r\n\r\n"; if(this.m_HeaderText!="") strMsgHTML+=this.m_SeparatorHeader;
	strMsgHTML+=this.m_SeparatorFirst;
	for (i=0; i< Page_ValidatorsAltamira.length; ++i)
	{
		//Get the validator texts
		if ( (Page_ValidatorsAltamira[i].m_ValidatorControl.id.indexOf(strGlobalValidation) != -1) && (!Page_ValidatorsAltamira[i].m_IsValid) )
		{
			strMsgHTML+=this.m_SeparatorPre;
			strMsgHTML += Page_ValidatorsAltamira[i].m_ErrorMessage; strMsgHTML+=this.m_SeparatorPost;
			strMsg += Page_ValidatorsAltamira[i].m_ErrorMessage; strMsg+="\r\n"; 
			bValid = false;
		}
	}
	strMsgHTML+=this.m_SeparatorFinal;
	//Show the message box if required
	if (!bValid) 
	{
		//Set the text in the SPAN
		if (this.m_Display) this.m_ValidatorControl.innerHTML= strMsgHTML;
		if (this.m_ShowMessagebox && !bValidationSummaryAlertShown) 
		{
			alert(strMsg);
			bValidationSummaryAlertShown = true;
		}
	}
}

//***************************** The Captcha field validator *****************************
function CaptchaValidator(strIDValidatorCtrl, strIDControlToValidate, strErrorMessage, strText, strInitialValue, bDisplay, bEnabled) {
    this.m_ValidatorControl = GetElement(strIDValidatorCtrl);
    var objControlToValidate = GetElement(strIDControlToValidate);
    this.m_ControlToValidate = objControlToValidate; //GetElement(strIDControlToValidate);
    this.m_ErrorMessage = strErrorMessage;
    this.m_Text = strText;
    this.m_Display = bDisplay;
    this.m_Enabled = bEnabled;
    this.m_IsValid = true;

    this.m_InitialValue = strInitialValue;

    this.Validate = CaptchaValidator_Validate;
    this.FormatValidatorMessage = FormatValidatorMessage;
}

function CaptchaValidator_Validate() {
    this.m_IsValid = true;
    if (!this.m_Enabled) return true;

    //We are valid
    this.m_ValidatorControl.style.display = "none";
    this.m_IsValid = true;
    return true
}


//***************************** Utilities *****************************
function FormatValidatorMessage()
{
	//Do we display the message?
	if (this.m_Display)
	{
		//The errorMessage or Text
		var strMsg;
		if (this.m_Text=="")
			strMsg = this.m_ErrorMessage;
		else
			strMsg = this.m_Text;
		//Set the text in the SPAN
		this.m_ValidatorControl.innerHTML= strMsg;
		//Show the validation control
		this.m_ValidatorControl.style.display="inline";
	}
	else
		this.m_ValidatorControl.style.display="none";

	this.m_IsValid = false;
}

//Trim
function ValidatorTrimA(s) {
    var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}

function ValidatorConvertA(op, dataType, val) {
    function GetFullYear(year) {
        return (year + parseInt(val.century)) - ((year < val.cutoffyear) ? 0 : 100);
    }
    var num, cleanInput, m, exp;
    if (dataType == "Integer") {
        exp = new RegExp("^\\s*[-\\+]?\\d+\\s*$");
        if (op.match(exp) == null) 
            return null;
        num = parseInt(op, 10);
        return (isNaN(num) ? null : num);
    }
    else if(dataType == "Double") {
        exp = new RegExp("^\\s*([-\\+])?(\\d+)?(\\" + val.decimalchar + "(\\d+))?\\s*$");
        m = op.match(exp);
        if (m == null)
            return null;
        cleanInput = m[1] + (m[2].length > 0 ? m[2] : "0") + "." + m[4];
        cleanInput = cleanInput.replace('undefined', '');
        num = parseFloat(cleanInput);
        return (isNaN(num) ? null : num);            
    } 
    else if (dataType == "Currency") {
        exp = new RegExp("^\\s*([-\\+])?(((\\d+)\\" + val.groupchar + ")*)(\\d+)"
                        + ((val.digits > 0) ? "(\\" + val.decimalchar + "(\\d{1," + val.digits + "}))?" : "")
                        + "\\s*$");
        m = op.match(exp);
        if (m == null)
            return null;
        var intermed = m[2] + m[5] ;
        cleanInput = m[1] + intermed.replace(new RegExp("(\\" + val.groupchar + ")", "g"), "") + ((val.digits > 0) ? "." + m[7] : 0);
        num = parseFloat(cleanInput);
        return (isNaN(num) ? null : num);            
    }
    else if (dataType == "DateOnly" | dataType == "DateTime" | dataType == "TimeOnly") {
				var regEx, day=1, month=0, year=0, hour=0, minute=0, second=0, iDay=0, iMonth=0, iYear=0, iHour=0, iMinute=0, iSecond=0;
				if(val.dateorder == "ymd")
				{
					if(dataType == "DateOnly")
						{regEx = new RegExp("^\\s*((\\d{4})|(\\d{2}))(" + val.datechar + ")(\\d{1,2})\\4(\\d{1,2})\\s*$"); iDay=6; iMonth=5; iYear=1; iHour=-1; iMinute=-1; iSecond=-1;}
					else if (dataType == "DateTime")
					{ regEx = new RegExp("^\\s*((\\d{4})|(\\d{2}))(" + val.datechar + ")(\\d{1,2})\\4(\\d{1,2})(\\s*|\\s*(\\d{1,2})(\\" + val.timechar + ")(\\d{1,2})(\\s*|(\\9(\\d{1,2}))))\\s*([am]*|[AM]*|[pm]*|[PM]*)$"); iDay = 6; iMonth = 5; iYear = 1; iHour = 8; iMinute = 10; iSecond = 13; }
					else if (dataType == "TimeOnly")
					{ regEx = new RegExp("^\\s*(\\d{1,2})(\\" + val.timechar + ")(\\d{1,2})(\\s*|(\\2(\\d{1,2})))\\s*([am]*|[AM]*|[pm]*|[PM]*)$"); iDay = -1; iMonth = -1; iYear = -1; iHour = 1; iMinute = 3; iSecond = 6; }
				}
				else
				{
					if(dataType == "DateOnly")
						{regEx = new RegExp("^\\s*(\\d{1,2})(" + val.datechar + ")(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$"); iDay=1; iMonth=3; iYear=4; iHour=-1; iMinute=-1; iSecond=-1; if(val.dateorder == "mdy") {iMonth=1; iDay=3;}}
					else if (dataType == "DateTime")
					{ regEx = new RegExp("^\\s*(\\d{1,2})(" + val.datechar + ")(\\d{1,2})\\2((\\d{4})|(\\d{2}))(\\s*|\\s*(\\d{1,2})(\\" + val.timechar + ")(\\d{1,2})(\\s*|(\\9(\\d{1,2}))))\\s*([am]*|[AM]*|[pm]*|[PM]*)$"); iDay = 1; iMonth = 3; iYear = 4; iHour = 8; iMinute = 10; iSecond = 13; if (val.dateorder == "mdy") { iMonth = 1; iDay = 3; } }
					else if (dataType == "TimeOnly")
					{ regEx = new RegExp("^\\s*(\\d{1,2})(\\" + val.timechar + ")(\\d{1,2})(\\s*|(\\2(\\d{1,2})))\\s*([am]*|[AM]*|[pm]*|[PM]*)$"); iDay = -1; iMonth = -1; iYear = -1; iHour = 1; iMinute = 3; iSecond = 6; }
				}
        m = op.match(regEx);
        //If no match then return false
        if(m==null)
					return null;
        //Get the values from the regex
        if( iYear>=0 && m[iYear]!="") year = (m[iYear].length == 4) ? m[iYear] : GetFullYear(parseInt(m[iYear], 10));
        if(iMonth>=0 && m[iMonth]!="") month = parseInt(m[iMonth], 10) - 1;
        if(iDay>=0 && m[iDay]!="") day = parseInt(m[iDay], 10);
        if(iHour>=0 && m[iHour]!="") hour = parseInt(m[iHour], 10);
        if(iMinute>=0 && m[iMinute]!="") minute = parseInt(m[iMinute], 10);
        if(iSecond>=0 && m[iSecond]!="") second = parseInt(m[iSecond], 10);
        if (second == null || isNaN(second)) second = 0;

        //Correzione Firefox
        if (isNaN(second)) { second = 0; }
        if (isNaN(minute)) { minute = 0; }
        if (isNaN(hour)) { hour = 0; }

        //Non è possibile salvare una data antecedente al 01-01-1753 (grazie a Philip Stanhope, 4th Earl of Chesterfield)
        if (dataType != "TimeOnly" && year < 1753)
            return null;

        //Build a date
        var date = new Date(year, month, day);
				//Year will be set incorrecly
				date.setSeconds(second);
				date.setMinutes(minute);
				date.setHours(hour);
				date.setDate(day);
				date.setMonth(month);
				date.setFullYear(year);
				
        return (typeof(date) == "object" && 
					year == date.getFullYear() && 
					month == date.getMonth() && 
					day == date.getDate() && 
					hour == date.getHours() && 
					minute == date.getMinutes() && 
					second == date.getSeconds()) ? date.valueOf() : null;
    }
    else if(dataType == "Email") 
    {
            exp = new RegExp("^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$");
            if (op.toString().match(exp) == null) 
                return null;
           
        return op.toString();
    }
    else if(dataType == "MultipleEmail") 
    {
        var returnValue = true;
        op =  Trim(op.toString(),';')
        var arrayEmail = op.toString().split(';');
        for(var i=0;i<arrayEmail.length;i++)
        {
            exp = new RegExp("^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$");
            arrayEmail[i] = Trim(arrayEmail[i],' ')
            if (arrayEmail[i].match(exp) == null) 
                return null;
            
        }
        return op.toString();
    }
    else {
        return op.toString();
    }
  }

  function StringToDate(strDate, dateorder, dataType, datechar, timechar) {
    var regEx, day = 1, month = 0, year = 0, hour = 0, minute = 0, second = 0, iDay = 0, iMonth = 0, iYear = 0, iHour = 0, iMinute = 0, iSecond = 0;
    if (dateorder == "ymd") {
      if (dataType == "DateOnly")
      { regEx = new RegExp("^\\s*((\\d{4})|(\\d{2}))(" + datechar + ")(\\d{1,2})\\4(\\d{1,2})\\s*$"); iDay = 6; iMonth = 5; iYear = 1; iHour = -1; iMinute = -1; iSecond = -1; }
      else if (dataType == "DateTime")
      { regEx = new RegExp("^\\s*((\\d{4})|(\\d{2}))(" + datechar + ")(\\d{1,2})\\4(\\d{1,2})(\\s*|\\s*(\\d{1,2})(\\" + timechar + ")(\\d{1,2})(\\s*|(\\9(\\d{1,2}))))\\s*$"); iDay = 6; iMonth = 5; iYear = 1; iHour = 8; iMinute = 10; iSecond = 13; }
      else if (dataType == "TimeOnly")
      { regEx = new RegExp("^\\s*(\\d{1,2})(\\" + timechar + ")(\\d{1,2})(\\s*|(\\2(\\d{1,2})))\\s*$"); iDay = -1; iMonth = -1; iYear = -1; iHour = 1; iMinute = 3; iSecond = 6; }
    }
    else {
      if (dataType == "DateOnly")
      { regEx = new RegExp("^\\s*(\\d{1,2})(" + datechar + ")(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$"); iDay = 1; iMonth = 3; iYear = 4; iHour = -1; iMinute = -1; iSecond = -1; if (dateorder == "mdy") { iMonth = 1; iDay = 3; } }
      else if (dataType == "DateTime")
      { regEx = new RegExp("^\\s*(\\d{1,2})(" + datechar + ")(\\d{1,2})\\2((\\d{4})|(\\d{2}))(\\s*|\\s*(\\d{1,2})(\\" + timechar + ")(\\d{1,2})(\\s*|(\\9(\\d{1,2}))))\\s*$"); iDay = 1; iMonth = 3; iYear = 4; iHour = 8; iMinute = 10; iSecond = 13; if (dateorder == "mdy") { iMonth = 1; iDay = 3; } }
      else if (dataType == "TimeOnly")
      { regEx = new RegExp("^\\s*(\\d{1,2})(\\" + timechar + ")(\\d{1,2})(\\s*|(\\2(\\d{1,2})))\\s*$"); iDay = -1; iMonth = -1; iYear = -1; iHour = 1; iMinute = 3; iSecond = 6; }
    }
    m = strDate.match(regEx);
    //If no match then return false
    if (m == null)
      return null;
    //Get the values from the regex
    if (iYear >= 0 && m[iYear] != "") year = (m[iYear].length == 4) ? m[iYear] : GetFullYear(parseInt(m[iYear], 10));
    if (iMonth >= 0 && m[iMonth] != "") month = parseInt(m[iMonth], 10) - 1;
    if (iDay >= 0 && m[iDay] != "") day = parseInt(m[iDay], 10);
    if (iHour >= 0 && m[iHour] != "") hour = parseInt(m[iHour], 10);
    if (iMinute >= 0 && m[iMinute] != "") minute = parseInt(m[iMinute], 10);
    if (iSecond >= 0 && m[iSecond] != "") second = parseInt(m[iSecond], 10);
    //Build a date
    if (!second || isNaN(second))
      second = 0;
    var date = new Date(year, month, day);
    //Year will be set incorrecly
    date.setSeconds(second);
    date.setMinutes(minute);
    date.setHours(hour);
    date.setDate(day);
    date.setMonth(month);
    date.setFullYear(year);
    return date;
  }  


