// define the namespace if necessary
if ( Lutz == undefined )
	var Lutz = { };

Lutz.FormUtil = { 
	// classes in this namespace
	SocialSecurityNumberFormatter: function(ctl) { this.initialize(ctl); },
	NumericStringFormatter: function(ctl, format) { this.initialize(ctl, format); },
	StringHelper: function() { this.initialize(); }
};


Lutz.FormUtil.SocialSecurityNumberFormatter.prototype = {

	// private properties
	_ctl : null,

	// public properties
	
	// constructor
	initialize: function(ctl) {
		this._ctl = Lutz.DomUtil.getControl(ctl);
		if ( this._ctl == null ) 
			return;
			
		// this control should only hook itself up to textboxes and textareas
		if ( !(this._ctl.tagName == 'INPUT' && this._ctl.type == 'text') && this._ctl.tagName != 'TEXTAREA' )
			return;

		// hook up the event
		YAHOO.util.Event.addListener(this._ctl, "change", this.onFieldChange, this, true);
	},
	
	// event handlers
	onFieldChange: function(e, me) {
		var str = Lutz.FormUtil.StringHelper.getNumericChars(me._ctl.value);
		if ( str.length != 9 )
			return;
			
		var ssnArea = str.substr(0, 3);
		var ssnGroup = str.substr(3, 2);
		var ssnSerial = str.substr(5, 4);
		me._ctl.value = ssnArea + "-" + ssnGroup + "-" + ssnSerial;
	}
};


Lutz.FormUtil.NumericStringFormatter.prototype = {

	// private properties
	_ctl          : null,
	_formatString : null,

	// public properties
	
	// constructor
	initialize: function(ctl, format) {
		this._ctl = Lutz.DomUtil.getControl(ctl);
		if ( this._ctl == null ) 
			return;
			
		// this control should only hook itself up to textboxes and textareas
		if ( !(this._ctl.tagName == 'INPUT' && this._ctl.type == 'text') && this._ctl.tagName != 'TEXTAREA' )
			return;
			
		if ( format == null || format.length == 0 || format.indexOf("#") == -1 )
			return;
			
		this._formatString = format;

		// hook up the event
		YAHOO.util.Event.addListener(this._ctl, "change", this.onFieldChange, this, true);
	},
	
	// event handlers
	onFieldChange: function(e, me) {
		var digitsExpected = 0;
		for ( var i = 0; i < me._formatString.length; i++ )
		{
			if ( me._formatString.charAt(i) == "#" )
				digitsExpected++;
		}
	
		var sourceDigits = Lutz.FormUtil.StringHelper.getNumericChars(me._ctl.value);
		if ( sourceDigits.length != digitsExpected )
		{
			// TODO: Raise an event signifying that this is not in the correct format
			return;
		}
		
		var result = "";
		var formatChars = me._formatString;
		for ( var i = 0; i < me._formatString.length; i++ )
		{
			var ch = me._formatString.charAt(i);
			switch ( ch )
			{
				case "#":
					result += sourceDigits.charAt(0);
					sourceDigits = sourceDigits.substr(1);
					break;
					
				default:
					result += formatChars.charAt(0);
					break;
					
			}
			formatChars = formatChars.substr(1);
		}
		me._ctl.value = result;
	}
};


Lutz.FormUtil.StringHelper = {

	// public properties

	// private properties
	
	// constructor
	initialize: function() {
	},
	
	isNumeric: function(str, includeDecimal) {
		if ( typeof includeDecimal == "undefined" )
			includeDecimal = false;

		var validChars = "1234567890";
		if ( includeDecimal )
			validChars += ".";
			
		for ( var i = 0; i < str.length; i++ )
		{
			if ( validChars.indexOf( str.charAt(i) ) == -1 )
				return false;
		}
		return true;
	},
	
	// methods
	getNumericChars: function(str, includeDecimal) {
		if ( typeof includeDecimal == "undefined" )
			includeDecimal = false;
		
	
		var result = "";
		for ( var i = 0; i < str.length; i++ )
		{
			var ch = str.charAt(i);
			if ( this.isNumeric(ch, includeDecimal) )
				result += ch;
		}
		
		return result;
	}

};
