function CheckInput(e) {
	e = e || window.event;
	var k=e.charCode || e.keyCode || e.which;
	
	if (k == 8 || //backspace
		k == 9 ||
		k == 17 || //control
		37 <= k && k <= 40 || // arrows
		k == 46 || // delete
		112 <= k && k <= 123 || // f1 .. f12
		k == 190) // . (dot)
		{
			return true;
		}
	
	if ((k < 48 || k > 57) && (k < 96 || k > 105)) {
		preventDefault(e);
		return true;
	}
	
	return false;
}

function preventDefault(e) {
	if (e.preventDefault) {	e.preventDefault() }
	else { e.returnValue = false }
}

function moveCur(e, active) {
	var len = e.value.length;
	if(e.setSelectionRange && active){
		e.setSelectionRange(len-3,len-3);
	} else if (e.createTextRange && active){
		var range = e.createTextRange();
		range.collapse(true);
		range.moveEnd('character', len-3);
		range.moveStart('character', len-3);
		range.select();
	}	
	if (!(/.* Lt$/.test(e.value))) {
		e.value = parseInt(e.value)+' Lt';
	}
	if (e.value == "") {
		e.value = 0 + ' Lt';
	}
}

function getSelectionStart(o) {
	if (o.createTextRange) {
		var r = document.selection.createRange().duplicate()
		r.moveEnd('character', o.value.length)
		if (r.text == '') return o.value.length
		return o.value.lastIndexOf(r.text)
	} else return o.selectionStart
}

function getSelectionEnd(o) {
	if (o.createTextRange) {
		var r = document.selection.createRange().duplicate()
		r.moveStart('character', -o.value.length)
		return r.text.length
	} else return o.selectionEnd
}

$(document).ready(function(){
	var active = 1;
	$(".inp").focus(function(){
		var len = this.value.length;
		if (getSelectionStart(this)<len-3 && getSelectionEnd(this)<len-3) {
			active = 0;
		} else {
			moveCur(this, active);	 			
			active = 1;
		}
	});

	$(".inp").mouseup(function(){
		var len = this.value.length;
		if (getSelectionStart(this)<len-3 && getSelectionEnd(this)<len-3) {
			active = 0;
		} else {
			moveCur(this, active);				
			active = 1;
		}
	});
	
	$(".inp").keydown(function(e){
		CheckInput(e);
		thisObj = this;
/*		setTimeout(function(){
			if ((/[ąčęėįšųū]/.test(thisObj.value))) {
				var len = thisObj.value.length;
				thisObj.value = thisObj.value.replace(/[ąčęėįšųū]+/, '');
				active = 1;
				moveCur(thisObj, active);
			}				
		}, 10);
*/
		moveCur(this, active);
	});

	$(".inp").keyup(function(e){
		if ((/[ąčęėįšųū]/.test(this.value))) {
			var len = this.value.length;
			this.value = this.value.replace(/[ąčęėįšųū]+/, '');
			active = 1;
		}		
		moveCur(this, active);				
	});	

});
