/* 
 * a-tools 1.0
 * 
 * Copyright (c) 2009 Andrey Kramarev, Ampparit Inc. (www.ampparit.com)
 * Licensed under the MIT license.
 * http://www.ampparit.fi/a-tools/license.txt
 */
jQuery.fn.extend({getSelection:function(){var k=this.jquery?this[0]:this;var b;var f;var c;if(document.selection){var p=document.selection.createRange();if(p.text){var d=0;c=p.text;if(k.value.match(/\n/g)!=null){d=k.value.match(/\n/g).length}var e=0;var j=0;var g=0;if(typeof(k.selectionStart)=="number"){b=k.selectionStart;f=k.selectionEnd;if(b==f){return this}}else{var n=k.createTextRange();var m;var o;var a=n.duplicate();m=n.text;n.moveToBookmark(p.getBookmark());o=n.text;a.setEndPoint("EndToStart",n);if(m==o&&m!=p.text){return this}b=a.text.length;f=a.text.length+p.text.length}if(d>0){for(var h=0;h<=d;h++){var l=k.value.indexOf("\n",j);if(l!=-1&&l<b){j=l+1;e++;g=e}else{if(l!=-1&&l>=b&&l<=f){if(l==b+1){e--;g--;j=l+1;continue}j=l+1;g++}else{h=d}}}}if(p.text.indexOf("\n",0)==1){g=g+2}b=b-e;f=f-g;return{start:b,end:f,text:p.text,length:f-b}}return false}else{if(typeof(k.selectionStart)=="number"&&k.selectionStart!=k.selectionEnd){b=k.selectionStart;f=k.selectionEnd;c=k.value.substring(k.selectionStart,k.selectionEnd);return{start:b,end:f,text:c,length:f-b}}else{return{start:undefined,end:undefined,text:undefined,length:undefined}}}},replaceSelection:function(d){var e=this.jquery?this[0]:this;var b;var c;if(document.selection){var h=document.selection.createRange();if(typeof(e.selectionStart)=="number"){b=e.selectionStart;c=e.selectionEnd;if(b==c){return this}}if(typeof(e.selectionStart)!="number"){var g=e.createTextRange();var f;var i;var a=g.duplicate();f=g.text;g.moveToBookmark(h.getBookmark());i=g.text;a.setEndPoint("EndToStart",g);if(f==i&&f!=h.text){return this}}if(h.text){h.text=d}return this}else{if(typeof(e.selectionStart)=="number"&&e.selectionStart!=e.selectionEnd){b=e.selectionStart;c=e.selectionEnd;e.value=e.value.substr(0,b)+d+e.value.substr(c);return this}}return this},countCharacters:function(b){var a=this.jquery?this[0]:this;if(a.value.match(/\r/g)!=null){return a.value.length-a.value.match(/\r/g).length}return a.value.length},setMaxLength:function(a,b){this.each(function(){var d=this.jquery?this[0]:this;var f=d.type;var e;var c;if(parseInt(a)<0){a=100000000}if(f=="text"){d.maxLength=a}if(f=="textarea"||f=="text"){d.onkeypress=function(j){var g=d.value.match(/\r/g);c=a;if(g!=null){c=parseInt(c)+g.length}var h=j||event;var i=h.keyCode;if(document.selection){e=document.selection.createRange().text.length>0}else{e=d.selectionStart!=d.selectionEnd}if(d.value.length>=c&&(i>47||i==32||i==0||i==13)&&!h.ctrlKey&&!h.altKey&&!e){d.value=d.value.substring(0,c);if(typeof(b)=="function"){b()}return false}};d.onkeyup=function(){var h=d.value.match(/\r/g);var k=0;var g=0;c=a;if(h!=null){for(var j=0;j<=h.length;j++){if(d.value.indexOf("\n",g)<=parseInt(a)){k++;g=d.value.indexOf("\n",g)+1}}c=parseInt(a)+k}if(d.value.length>c){d.value=d.value.substring(0,c);if(typeof(b)=="function"){b()}return this}}}else{return this}});return this}});
