using System;
using System.Web.UI;

namespace SK.MemberWeb.Controls
{
	public class NumericTextBox : System.Web.UI.WebControls.TextBox
	{
		public NumericTextBox()
		{
			_DecimalPlaces = - 1;
			js = "<script language=\'javascript\'>function extractNumber(obj, decimalPlaces, allowNegative)" + "\n" + "{" + "\n" + "	var temp = obj.value;" + "\n" + "	" + "\n" + "	// avoid changing things if already formatted correctly" + "\n" + "	var reg0Str = \'[0-9]*\';" + "\n" + "	if (decimalPlaces > 0) {" + "\n" + "       reg0Str += \'\\\\.?[0-9]{0,\' + decimalPlaces + \'}\';" + "\n" + "   } else if (decimalPlaces < 0) {"
			+ "\n" + "       reg0Str += \'\\\\.?[0-9]*\';" + "\n" + "   }" + "\n" + "	reg0Str = allowNegative ? \'^-?\' + reg0Str : \'^\' + reg0Str;" + "\n" + "	reg0Str = reg0Str + \'$\';" + "\n" + "	var reg0 = new RegExp(reg0Str);" + "\n" + "	if (reg0.test(temp)) return true;" + "\n" + "" + "\n" + "	// first replace all non numbers" + "\n" + "	var reg1Str = \'[^0-9\' + (decimalPlaces != 0 ? \'.\' : \'\') " + "+ (allowNegative ? \'-\' : \'\') + \']\';" + "\n" + "	var reg1 = new RegExp(reg1Str, \'g\');" + "\n"
			+ "	temp = temp.replace(reg1, \'\');" + "\n" + "" + "\n" + "	if (allowNegative) {" + "\n" + "		// replace extra negative" + "\n" + "		var hasNegative = temp.length > 0 && temp.charAt(0) == \'-\';" + "\n" + "		var reg2 = /-/g;" + "\n" + "		temp = temp.replace(reg2, \'\');" + "\n" + "		if (hasNegative) temp = \'-\' + temp;" + "\n" + "	}" + "\n" + "	" + "\n" + "	if (decimalPlaces != 0) {" + "\n" + "		var reg3 = /\\./g;" + "\n" + "       var reg3Array = reg3.exec(temp);" + "\n" + "       if (reg3Array != null) {"
			+ "\n" + "		    // keep only first occurrence of . " + "\n" + "   	    //  and the number of places specified by decimalPlaces " + "or the entire string if decimalPlaces < 0" + "\n" + "	        var reg3Right = temp.substring(reg3Array.index " + "+ reg3Array[0].length);" + "\n" + "	        reg3Right = reg3Right.replace(reg3, \'\');" + "\n" + "	        reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;" + "\n" + "	        temp = temp.substring(0,reg3Array.index) + \'.\' + reg3Right;"
			+ "\n" + "       }" + "\n" + "   }" + "\n" + "	" + "\n" + "	obj.value = temp;" + "\n" + "}" + "\n" + "function blockNonNumbers(obj, e, allowDecimal, allowNegative)" + "\n" + "{" + "\n" + "	var key;" + "\n" + "	var isCtrl = false;" + "\n" + "	var keychar;" + "\n" + "	var reg;" + "\n" + "	" + "\n" + "	if(window.event) {" + "\n" + "		key = e.keyCode;" + "\n" + "		isCtrl = window.event.ctrlKey" + "\n" + "	}" + "\n" + "	else if(e.which) {" + "\n" + "		key = e.which;" + "\n" + "		isCtrl = e.ctrlKey;" +
			"\n" + "	}" + "\n" + "	" + "\n" + "	if (isNaN(key)) return true;" + "\n" + "	" + "\n" + "	keychar = String.fromCharCode(key);" + "\n" + "		" + "\n" + "	// check for backspace or delete, or if Ctrl was pressed" + "\n" + "	if (key == 8 || isCtrl)" + "\n" + "	{" + "\n" + "		return true;" + "\n" + "	}" + "\n" + "	reg = /\\d/;" + "\n" + "	var isFirstN = allowNegative ? keychar == \'-\' && obj.value.indexOf(\'-\') == -1 : false;" + "\n" + "	var isFirstD = allowDecimal ? keychar == \'.\' && obj.value.indexOf(\'.\') == -1 : false;"
			+ "\n" + "	" + "\n" + "	return isFirstN || isFirstD || reg.test(keychar);" + "\n" + "}</script>";
		}
		
		
		private int _DecimalPlaces;
		private bool _AllowDecimal;
		private bool _AllowNegative;
		private string js;
		
		
		protected override void OnPreRender (System.EventArgs e)
		{
			base.OnPreRender(e);
			int decPlaces=0;
			if (_AllowDecimal)
			{
				decPlaces = _DecimalPlaces;
			}
			this.Page.RegisterClientScriptBlock("NumericValidation", js);
			this.Attributes.Add("onblur", "extractNumber(this," + decPlaces + "," + _AllowNegative.ToString().ToLower() + ");");
			this.Attributes.Add("onkeyup", "extractNumber(this," + decPlaces + "," + _AllowNegative.ToString().ToLower() + ");");
			this.Attributes.Add("onkeypress", "return blockNonNumbers(this, event," + _AllowDecimal.ToString().ToLower() + "," + _AllowNegative.ToString().ToLower() + ");");
			
		}
		
		public int DecimalPlaces
		{
			get{
				return _DecimalPlaces;
			}
			set
			{
				_DecimalPlaces = value;
			}
		}
		
		public bool AllowDecimal
		{
			get{
				return _AllowDecimal;
			}
			set
			{
				_AllowDecimal = value;
			}
		}
		
		public bool AllowNegative
		{
			get{
				return _AllowNegative;
			}
			set
			{
				_AllowNegative = value;
			}
		}
	}
	
	
}
