/*!
 * jQuery TextChange Plugin
 * http://www.zurb.com/playground/jquery-text-change-custom-event
 *
 * Copyright 2010, ZURB
 * Released under the MIT License
 */
 (function(a){a.event.special.textchange={setup:function(){a(this).data("lastValue",this.contentEditable==="true"?a(this).html():a(this).val());a(this).bind("keyup.textchange",a.event.special.textchange.handler);a(this).bind("cut.textchange paste.textchange input.textchange",a.event.special.textchange.delayedHandler)},teardown:function(){a(this).unbind(".textchange")},handler:function(){a.event.special.textchange.triggerIfChanged(a(this))},delayedHandler:function(){var c=a(this);setTimeout(function(){a.event.special.textchange.triggerIfChanged(c)},
 25)},triggerIfChanged:function(a){var b=a[0].contentEditable==="true"?a.html():a.val();b!==a.data("lastValue")&&(a.trigger("textchange",[a.data("lastValue")]),a.data("lastValue",b))}};a.event.special.hastext={setup:function(){a(this).bind("textchange",a.event.special.hastext.handler)},teardown:function(){a(this).unbind("textchange",a.event.special.hastext.handler)},handler:function(c,b){b===""&&b!==a(this).val()&&a(this).trigger("hastext")}};a.event.special.notext={setup:function(){a(this).bind("textchange",
 a.event.special.notext.handler)},teardown:function(){a(this).unbind("textchange",a.event.special.notext.handler)},handler:function(c,b){a(this).val()===""&&a(this).val()!==b&&a(this).trigger("notext")}}})(jQuery);
/* 
extended code:
*/
if (!Array.prototype.indexOf){// IE
	Array.prototype.indexOf = function(elt/*, from*/){
    	var len = this.length,
    		from = Number(arguments[1]) || 0;
    	from = (from < 0) ? Math.ceil(from) : Math.floor(from);
        if (from < 0) from += len;
		for (; from < len; from++){
      		if (from in this && this[from] === elt)
        		return from;
    	}
    	return -1;
  	};
}
function numberFormat(str){ // Format string 12000 -> 12 000
	var 	str_array=[],
			length=str.length;
	var undefined2string=function(val){
		return val?val:"";
	}
	for(var i=length-1; i>=0;i-=3){
		str_array.unshift(
			undefined2string(str[i-2])+undefined2string(str[i-1])+undefined2string(str[i])
		);
	}
	var resStr=str_array.join(" ");
	return resStr;
}
/*function createFormatControl(element,onchange_callback){
	if (!(element instanceof HTMLInputElement))
		throw("Wrong type of node, need HTMLInputElement");
		
	onchange_callback=onchange_callback||function(){};
	//	@e.keyCode: 9 - Tab, 8 - Backspace, 46 - Del, 48-57 - 0-9, 13 - Enter, 37-40 - arrow: left,top,right,bottom, 35 -Home, 36 -End, 
	$(element).bind("keydown",function(e){//Контролируем ввод с клавиатуры
		var 	keyCodeArray=[8,9,13,46];
		if(
			(e.keyCode>47 &&  e.keyCode<58) ||
			keyCodeArray.indexOf(e.keyCode) >= 0 ||
			(e.keyCode>34 &&  e.keyCode<41)
		){
			return true;
		}else{
			return false;
		}
	}).bind("textchange",function(e){// Контролируем изменения (попытки вставить из буфера и т. п.)
		var		$this=$(this),
				origStr=$this.val().replace(/\D+/g,""),
				str=numberFormat(origStr);
		if(!(e.keyCode>34 &&  e.keyCode<41)){
			$this.val(str);
		}
		onchange_callback(str);
	});
}*/

function createFormatControl(element,onchange_callback){
//	if (!(element instanceof HTMLInputElement))
//		throw("Wrong type of node, need HTMLInputElement");
		
	onchange_callback=onchange_callback||function(){};
	//	@e.keyCode: 9 - Tab, 8 - Backspace, 46 - Del, 48-57 - 0-9, 13 - Enter, 37-40 - arrow: left,top,right,bottom, 35 -Home, 36 -End, 
	$(element).bind("textchange",function(e){// Контролируем изменения (попытки вставить из буфера и т. п.)
		var		$this=$(this),
				origStr=$this.val().replace(/\D+/g,""),
				str=numberFormat(origStr);
		if(!(e.keyCode>34 &&  e.keyCode<41)){
			$this.val(str);
		}
		onchange_callback(str);
	});
}