//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Summary description for RequiredCheckBoxControl.
	/// </summary>
	public class RequiredCheckBoxValidator : BaseValidator
	{
		private CheckBox checkBox;

		protected override bool ControlPropertiesValid()
		{
			// Validate ControlToValidate property is not empty
			if (ControlToValidate.Trim().Length == 0)
				throw new HttpException(string.Format("The ControlToValidate property of '{0}' cannot be blank.", this.ID));

			// Validate ControlToValidate control exists
			Control control = FindControl(ControlToValidate);
			if (control == null)
				throw new HttpException(string.Format("Unable to find control id '{0}' referenced by the '{1}' property of '{2}'.", ControlToValidate, "ControlToValidate", this.ID));

			// Validate ControlToValidate control is a CheckBox
			if (!(control is CheckBox))
				throw new HttpException(string.Format("The control '{0}' referenced by the ControlToValidate property of '{1}' does not refer to a CheckBox control.", ControlToValidate, this.ID));

			checkBox = (CheckBox)control;
			return (checkBox != null);
		}

		protected override bool EvaluateIsValid()
		{
			return checkBox.Checked;
		}
	}
}
