/*
*/
(function ($)
{
	$.fn.selectOther = function (settings)
	{
		settings = $.fn.extend(
		  {
		  	style: '',
		  	cssClass: '',
		  	otherValueText: 'other',
		  	selectedValue: null,
		  	mask: null,
		  	mode: "text"
		  }, settings);


		$.fn.selectOther.InputValueChange = function (e)
		{
			var $OtherInputControl = $(e[0]);
			var otherVal = $OtherInputControl.val();
			var associatedControlID = $OtherInputControl.attr('assoccontrolid');
			var $AssociatedControl = $('#' + associatedControlID);
			$.fn.selectOther.SetValue($AssociatedControl, otherVal);
			$OtherInputControl.remove();
		}

		$.fn.selectOther.SetValue = function ($AssociatedControl, value)
		{
			var exists = false;
			$('option', $AssociatedControl).each(function ()
			{
				if ($(this).val() == value)
				{
					exists = true;
				}
			});
			if (!exists)
			{
				$AssociatedControl.append(String.format("<option value='{0}'>{0}</option>", value));
			}

			$AssociatedControl.val(value);

			//this is required to let inputValidation control know that the value of select has been changed
			$AssociatedControl.trigger('change');
		}

		return this.each(function ()
		{

			var $select = $(this);
			$select.append(String.format("<option value='{0}'>{0}</option>", settings.otherValueText));

			$(this).change(function ()
			{

				if ($select.val() == settings.otherValueText)
				{
					var associatedControlID = $select.attr('id');
					var id = associatedControlID + '_other';
					if ($("#" + id).length == 0)
					{
						var otherHTML = String.format("<input type='text' id='{0}' style='{1}' class='{2}' assoccontrolid='{3}' ></input>"
																		, id, settings.style, settings.cssClass, associatedControlID);
						$select.after(otherHTML);

						if (settings.mask)
						{
							$('#' + id).setMask({ mask: settings.mask });
						}

						$("#" + id).blur(function (event)
						{
							$.fn.selectOther.InputValueChange($(event.currentTarget));
						});

						$("#" + id).keypress(function (event)
						{
							if (isEnterKey(event))
							{
								$("#" + id).blur();
								return false;
							}
							else if (settings.mode = "currency" && event.which > 39)
							{
								var characterPressed = String.fromCharCode(event.which);
								
								// If this is not a number or a decimal or already contains a decimal, stop the event
								if (!/[\d\.]/.test(characterPressed) || (characterPressed == "." && $("#" + id).val().indexOf(".") >= 0))
								{
									event.preventDefault();
								}

								var textbox = $(event.target);
								var oldValue = textbox.val();
								setTimeout(function ()
								{
									// If this is not valid currency, put the old value back
									if(!/^\d*(\.\d{0,2})?$/.test(textbox.val()))
									{
										textbox.val(oldValue);
									}
								}, 1);
							}
						});

						$("#" + id).focus();
					}
				}
			});

			if (settings.selectedValue)
			{
				$.fn.selectOther.SetValue($select, settings.selectedValue);
			}
		});
	};
})(jQuery);
