/**
 * Mise en forme automatique des champs de type Prix
 * @author christian@konfiture.com
 */
(function($)
	{
		$.fn.arcadimPrixInput = function(params)
			{
				function getCaretPosition(elt)
					{
						var res = 0;
						if (document.selection)
							{
								var sel = document.selection.createRange();
								sel.moveStart('character', -elt.value.length);
								res = sel.text.length;
							}
						else if (elt.selectionStart || elt.selectionStart == '0')
							res = elt.selectionStart;
						return res;
					}
					
				function setCaretPosition(elt, position)
					{
						if ( elt != null )
							{
								if ( elt.createTextRange )
									{
										var range = elem.createTextRange();
										range.move('character', position);
										range.select();
									}
								else
									{
										if ( elt.selectionStart )
											{
												elt.focus();
												elt.setSelectionRange(position, position);
											}
										else
											{
												elt.focus();
											}
									}
							}
					}
				
				this.each( function()
					{
						if ( this.tagName.toUpperCase().trim() != 'INPUT'
							|| $(this).attr('type') != 'text' )
							return;
						
						var me = this;
						
						$(me).change ( function(event)
							{
								var curVal = '' + $(this).val();
								curVal = curVal.stripNonNumeric();
								
								var tReg = new RegExp('([0-9]+)([0-9]{3})')
								
								while( tReg.test(curVal) )
									curVal = curVal.replace(tReg, '$1 $2');
								
								$(this).val(curVal);
							});
					});
			};
	})(jQuery);
