/*
TInputKod = function()
{
	var param =
	{
		className:'input_kadr',
		mask : new RegExp("[0-9]{1}"),
		AJAXsrc:''
	};
	for (var i in arguments[0]) if (typeof(arguments[0][i]!= 'function')) param[i]=arguments[0][i];
	
	this.obj		= _c('div',param);
	this.obj.rysuj	= function()
	{
		this.i	= _c('input',{parent:this,className:'in',type:'text',value:'',maxLength:1, pattern:'[0-9]*'});
		
		this.i.onkeyup = function(e)
		{
			this.parent.keydownHandler(e,this.parent.nr);
		}
		this.i.onfocus = function()
		{
			if (this.setSelectionRange) 
				this.setSelectionRange(0, 2);
			else 
			if (this.createTextRange) 
			{
				var range = input.createTextRange();
				range.collapse(true);
				range.moveEnd('character', 2);
				range.moveStart('character', 0);
				range.select();
			}
		}
		this.i.onblur = function()
		{
			//this.style.fontWeight = 'normal';
			//this.style.color = '#000';
		}
		this.apC(this.i);		
	}
	this.obj.keydownHandler = function(e,v)
	{
		// lewo - 37, prawo - 39, góra - 38, dół - 40
		
		var evt = e ? e:event;
	  	var keyCode = evt.keyCode; 
		
		console.log(evt.keyCode);
		
//		if (keyCode < 46 || keyCode > 57) 
//		{
			if (this.mask.test(this.i.value)) {
				this.parent.setNext(v);
				this.parent.sendKod()
			}
			else 
				this.i.value = '';
			
			switch (parseInt(keyCode)) {
				case 39:
					console.log(keyCode);
					this.parent.setNext(v);
					break;
				case 37:
					this.parent.setPrev(v);
					break;
				case 38:
					this.parent.setNext(v);
					break;
				case 40:
					this.parent.setPrev(v);
					break;
				default:
					//this.parent.setNext(v);
					break;
			}
//		}
		
	}
	
	this.obj.rysuj();
	return this.obj;
}


TFormKodPocztowy = function(param)
{
	var param =
	{
		className	:'kod_pocztowy',
		status		: 0,
		AJAXsrc		:'ajax/kod_pocztowy_gazeta/'
	};
	for (var i in arguments[0]) if (typeof(arguments[0][i]!= 'function')) param[i]=arguments[0][i];
	
	this.obj		= _c('div',param);
	this.obj.lista_input = new Array();
	
	this.obj.rysuj = function(dane)
	{
		this.kod_p 	= _c('div',{className:'kod_pv2'});
		this.pkl	= _c('div',{className:'postkod postkod_l'});
		this.pkp	= _c('div',{className:'postkod postkod_p'});
		this.kr_p1	= _c('div',{parent : this,className : 'kr_p'});
		this.kr_p2	= _c('div',{parent : this,className : 'kr_p'});
		this.kr_p3	= _c('div',{parent : this,className : 'kr_p'});

		for(var i=0;i<5;i++)
			this.lista_input[this.lista_input.length] = new TInputKod({parent:this,nr:i});  

		this.apC(this.kod_p);
		this.pkl.apC(this.lista_input[0]);
		this.pkl.apC(this.kr_p1);
		this.pkl.apC(this.lista_input[1]);
		this.kod_p.apC(  _cTxt('Kod pocztowy') );

		this.apC(this.pkl);
		this.pkp.apC(this.lista_input[2]);
		this.pkp.apC(this.kr_p2);
		this.pkp.apC(this.lista_input[3]);
		this.pkp.apC(this.kr_p3);
		this.pkp.apC(this.lista_input[4]);
		
		this.apC(this.pkp);

		
		this.sendKod = function()
		{
			//var kod = new RegExp("[0-9]{2}\-[0-9]{3}");
			var kod = new RegExp("[0-9]{5}");
			var text = '';
			for(var i in this.lista_input)
			{
				text = text + this.lista_input[i].i.value;
			}
			
			//console.log(text);
			//text = this.in1.i.value + this.in2.i.value + this.in3.i.value+this.in4.i.value + this.in5.i.value;
			if(kod.test(text))
			{
				this.status = 1;
				setByAjax( '/strona_v2/ajax/set/', this, {'kod_pocztowy_gazeta' : encodeURI(text)} );
			}
		}
		
		if (dane != undefined){	this.update( dane ); }
	}
	this.obj.load = function( dane ){this.update( dane ); }
	this.obj.update = function( dane )
	{
		//ustawienie focus na pierwszym input
		this.lista_input[0].i.focus();
		
		if (dane != undefined)
		for(var i in this.lista_input)
		{
			this.lista_input[i].i.value = dane['kod_pocztowy_gazeta'][i];
		}
		if (dane['kod_pocztowy_gazeta'][i] == undefined)
		for(var i in this.lista_input)
		{
			this.lista_input[i].i.value = '';
		}
		
	}
	this.obj.setNext = function(v)
	{
		if(v == 4)
			this.lista_input[0].i.focus();
		else
			this.lista_input[v+1].i.focus();
	}
	this.obj.setPrev = function(v)
	{
		if(v == 0)
			this.lista_input[4].i.focus();
		else
			this.lista_input[v-1].i.focus();
	}
	
	this.obj.rysuj();
	return this.obj;
}

*/



/*
	Masked Input plugin for jQuery
	Copyright (c) 2007-2011 Josh Bush (digitalbush.com)
	Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) 
	Version: 1.3
*/
(function($) {
	var pasteEventName = ($.browser.msie ? 'paste' : 'input') + ".mask";
	var iPhone = (window.orientation != undefined);

	$.mask = {
		//Predefined character definitions
		definitions: {
			'9': "[0-9]",
			'a': "[A-Za-z]",
			'*': "[A-Za-z0-9]"
		},
		dataName:"rawMaskFn"
	};

	$.fn.extend({
		//Helper Function for Caret positioning
		caret: function(begin, end) {
			if (this.length == 0) return;
			if (typeof begin == 'number') {
				end = (typeof end == 'number') ? end : begin;
				return this.each(function() {
					if (this.setSelectionRange) {
						this.setSelectionRange(begin, end);
					} else if (this.createTextRange) {
						var range = this.createTextRange();
						range.collapse(true);
						range.moveEnd('character', end);
						range.moveStart('character', begin);
						range.select();
					}
				});
			} else {
				if (this[0].setSelectionRange) {
					begin = this[0].selectionStart;
					end = this[0].selectionEnd;
				} else if (document.selection && document.selection.createRange) {
					var range = document.selection.createRange();
					begin = 0 - range.duplicate().moveStart('character', -100000);
					end = begin + range.text.length;
				}
				return { begin: begin, end: end };
			}
		},
		unmask: function() { return this.trigger("unmask"); },
		mask: function(mask, settings) {
			if (!mask && this.length > 0) {
				var input = $(this[0]);
				return input.data($.mask.dataName)();
			}
			settings = $.extend({
				placeholder: "_",
				completed: null
			}, settings);

			var defs = $.mask.definitions;
			var tests = [];
			var partialPosition = mask.length;
			var firstNonMaskPos = null;
			var len = mask.length;

			$.each(mask.split(""), function(i, c) {
				if (c == '?') {
					len--;
					partialPosition = i;
				} else if (defs[c]) {
					tests.push(new RegExp(defs[c]));
					if(firstNonMaskPos==null)
						firstNonMaskPos =  tests.length - 1;
				} else {
					tests.push(null);
				}
			});

			return this.trigger("unmask").each(function() {
				var input = $(this);
				var buffer = $.map(mask.split(""), function(c, i) { if (c != '?') return defs[c] ? settings.placeholder : c });
				var focusText = input.val();

				function seekNext(pos) {
					while (++pos <= len && !tests[pos]);
					return pos;
				};
				function seekPrev(pos) {
					while (--pos >= 0 && !tests[pos]);
					return pos;
				};

				function shiftL(begin,end) {
					if(begin<0)
					   return;
					for (var i = begin,j = seekNext(end); i < len; i++) {
						if (tests[i]) {
							if (j < len && tests[i].test(buffer[j])) {
								buffer[i] = buffer[j];
								buffer[j] = settings.placeholder;
							} else
								break;
							j = seekNext(j);
						}
					}
					writeBuffer();
					input.caret(Math.max(firstNonMaskPos, begin));
				};

				function shiftR(pos) {
					for (var i = pos, c = settings.placeholder; i < len; i++) {
						if (tests[i]) {
							var j = seekNext(i);
							var t = buffer[i];
							buffer[i] = c;
							if (j < len && tests[j].test(t))
								c = t;
							else
								break;
						}
					}
				};

				function keydownEvent(e) {
					var k=e.which;

					//backspace, delete, and escape get special treatment
					if(k == 8 || k == 46 || (iPhone && k == 127)){
						var pos = input.caret(),
							begin = pos.begin,
							end = pos.end;
						
						if(end-begin==0){
							begin=k!=46?seekPrev(begin):(end=seekNext(begin-1));
							end=k==46?seekNext(end):end;
						}
						clearBuffer(begin, end);
						shiftL(begin,end-1);

						return false;
					} else if (k == 27) {//escape
						input.val(focusText);
						input.caret(0, checkVal());
						return false;
					}
				};

				function keypressEvent(e) {
					var k = e.which,
						pos = input.caret();
					if (e.ctrlKey || e.altKey || e.metaKey || k<32) {//Ignore
						return true;
					} else if (k) {
						if(pos.end-pos.begin!=0){
							clearBuffer(pos.begin, pos.end);
							shiftL(pos.begin, pos.end-1);
						}

						var p = seekNext(pos.begin - 1);
						if (p < len) {
							var c = String.fromCharCode(k);
							if (tests[p].test(c)) {
								shiftR(p);
								buffer[p] = c;
								writeBuffer();
								var next = seekNext(p);
								input.caret(next);
								if (settings.completed && next >= len)
									settings.completed.call(input);
								
							}
						}
						return false;
					}
				};

				function clearBuffer(start, end) {
					for (var i = start; i < end && i < len; i++) {
						if (tests[i])
							buffer[i] = settings.placeholder;
					}
				};

				function writeBuffer() { return input.val(buffer.join('')).val(); };

				function checkVal(allow) {
					//try to place characters where they belong
					var test = input.val();
					var lastMatch = -1;
					for (var i = 0, pos = 0; i < len; i++) {
						if (tests[i]) {
							buffer[i] = settings.placeholder;
							while (pos++ < test.length) {
								var c = test.charAt(pos - 1);
								if (tests[i].test(c)) {
									buffer[i] = c;
									lastMatch = i;
									break;
								}
							}
							if (pos > test.length)
								break;
						} else if (buffer[i] == test.charAt(pos) && i!=partialPosition) {
							pos++;
							lastMatch = i;
						}
					}
					if (!allow && lastMatch + 1 < partialPosition) {
						input.val("");
						clearBuffer(0, len);
					} else if (allow || lastMatch + 1 >= partialPosition) {
						writeBuffer();
						if (!allow) input.val(input.val().substring(0, lastMatch + 1));
					}
					return (partialPosition ? i : firstNonMaskPos);
				};

				input.data($.mask.dataName,function(){
					return $.map(buffer, function(c, i) {
						return tests[i]&&c!=settings.placeholder ? c : null;
					}).join('');
				})

				if (!input.attr("readonly"))
					input
					.one("unmask", function() {
						input
							.unbind(".mask")
							.removeData($.mask.dataName);
					})
					.bind("focus.mask", function() {
						focusText = input.val();
						var pos = checkVal();
						writeBuffer();
						var moveCaret=function(){
							if (pos == mask.length)
								input.caret(0, pos);
							else
								input.caret(pos);
						};
						($.browser.msie ? moveCaret:function(){setTimeout(moveCaret,0)})();
					})
					.bind("blur.mask", function() {
						checkVal();
						if (input.val() != focusText)
							input.change();
					})
					.bind("keydown.mask", keydownEvent)
					.bind("keypress.mask", keypressEvent)
					.bind(pasteEventName, function() {
						setTimeout(function() { input.caret(checkVal(true)); }, 0);
					});

				checkVal(); //Perform initial check for existing values
			});
		}
	});
})(jQuery);

