/* http://keith-wood.name/keypad.html Keypad field entry extension for jQuery v1.5.0. Written by Keith Wood (kbwood{at}iinet.com.au) August 2008. Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses. Please attribute the author if you use it. */ (function($){function Keypad(){this._curInst=null;this._disabledFields=[];this._keypadShowing=false;this._keyCode=0;this._specialKeys=[];this.addKeyDef('CLOSE','close',function(a){l._curInst=(a._inline?a:l._curInst);l._hidePlugin()});this.addKeyDef('CLEAR','clear',function(a){l._clearValue(a)});this.addKeyDef('BACK','back',function(a){l._backValue(a)});this.addKeyDef('SHIFT','shift',function(a){l._shiftKeypad(a)});this.addKeyDef('SPACE_BAR','spacebar',function(a){l._selectValue(a,' ')},true);this.addKeyDef('SPACE','space');this.addKeyDef('HALF_SPACE','half-space');this.addKeyDef('ENTER','enter',function(a){l._selectValue(a,'\x0D')},true);this.addKeyDef('TAB','tab',function(a){l._selectValue(a,'\x09')},true);this.qwertyAlphabetic=['qwertyuiop','asdfghjkl','zxcvbnm'];this.qwertyLayout=['!@#$%^&*()_='+this.HALF_SPACE+this.SPACE+this.CLOSE,this.HALF_SPACE+'`~[]{}<>\\|/'+this.SPACE+'789','qwertyuiop\'"'+this.HALF_SPACE+'456',this.HALF_SPACE+'asdfghjkl;:'+this.SPACE+'123',this.SPACE+'zxcvbnm,.?'+this.SPACE+this.HALF_SPACE+'-0+',''+this.TAB+this.ENTER+this.SPACE_BAR+this.SHIFT+this.HALF_SPACE+this.BACK+this.CLEAR];this.regional=[];this.regional['']={buttonText:'...',buttonStatus:'Open the keypad',closeText:'Close',closeStatus:'Close the keypad',clearText:'Clear',clearStatus:'Erase all the text',backText:'Back',backStatus:'Erase the previous character',spacebarText:' ',spacebarStatus:'Space',enterText:'Enter',enterStatus:'Carriage return',tabText:'→',tabStatus:'Horizontal tab',shiftText:'Shift',shiftStatus:'Toggle upper/lower case characters',alphabeticLayout:this.qwertyAlphabetic,fullLayout:this.qwertyLayout,isAlphabetic:this.isAlphabetic,isNumeric:this.isNumeric,toUpper:this.toUpper,isRTL:false};this._defaults={showOn:'focus',buttonImage:'',buttonImageOnly:false,showAnim:'show',showOptions:{},duration:'normal',appendText:'',useThemeRoller:false,keypadClass:'',prompt:'',layout:['123'+this.CLOSE,'456'+this.CLEAR,'789'+this.BACK,this.SPACE+'0'],separator:'',target:null,keypadOnly:true,randomiseAlphabetic:false,randomiseNumeric:false,randomiseOther:false,randomiseAll:false,beforeShow:null,onKeypress:null,onClose:null};$.extend(this._defaults,this.regional['']);this.mainDiv=$('
')}$.extend(Keypad.prototype,{markerClassName:'hasKeypad',propertyName:'keypad',_mainDivClass:'keypad-popup',_inlineClass:'keypad-inline',_appendClass:'keypad-append',_triggerClass:'keypad-trigger',_disableClass:'keypad-disabled',_inlineEntryClass:'keypad-keyentry',_coverClass:'keypad-cover',_rtlClass:'keypad-rtl',_rowClass:'keypad-row',_promptClass:'keypad-prompt',_specialClass:'keypad-special',_namePrefixClass:'keypad-',_keyClass:'keypad-key',_keyDownClass:'keypad-key-down',setDefaults:function(a){$.extend(this._defaults,a||{});return this},addKeyDef:function(a,b,c,d){if(this._keyCode==32){throw'Only 32 special keys allowed';}this[a]=String.fromCharCode(this._keyCode++);this._specialKeys.push({code:this[a],id:a,name:b,action:c,noHighlight:d});return this},_attachPlugin:function(a,b){a=$(a);if(a.hasClass(this.markerClassName)){return}var c=!a[0].nodeName.toLowerCase().match(/input|textarea/);var d={options:$.extend({},this._defaults,b),_inline:c,_mainDiv:(c?$(''):l.mainDiv),ucase:false};this._setInput(a,d);this._connectKeypad(a,d);if(c){a.append(d._mainDiv).bind('click.'+this.propertyName,function(){d._input.focus()});this._updateKeypad(d)}else if(a.is(':disabled')){this._disablePlugin(a)}},_setInput:function(a,b){b._input=$(!b._inline?a:b.options.target||'');if(b._inline){a.find('input').remove();if(!b.options.target){a.append(b._input)}}},_connectKeypad:function(d,e){d=$(d);var f=e.options.appendText;if(f){d[e.options.isRTL?'before':'after'](''+f+'')}if(!e._inline){if(e.options.showOn=='focus'||e.options.showOn=='both'){d.bind('focus.'+this.propertyName,this._showPlugin).bind('keydown.'+this.propertyName,this._doKeyDown)}if(e.options.showOn=='button'||e.options.showOn=='both'){var g=e.options.buttonStatus;var h=e.options.buttonImage;var i=$(e.options.buttonImageOnly?$(''):$('').html(h==''?e.options.buttonText:$('')));d[e.options.isRTL?'before':'after'](i);i.addClass(this._triggerClass).click(function(){if(l._keypadShowing&&l._lastField==d[0]){l._hidePlugin()}else{l._showPlugin(d[0])}return false})}}e.saveReadonly=d.attr('readonly');d.addClass(this.markerClassName).data(this.propertyName,e)[e.options.keypadOnly?'attr':'removeAttr']('readonly',true).bind('setData.'+this.propertyName,function(a,b,c){e.options[b]=c}).bind('getData.'+this.propertyName,function(a,b){return e.options[b]})},_optionPlugin:function(a,b,c){a=$(a);var d=a.data(this.propertyName);if(!b||(typeof b=='string'&&c==null)){var e=b;b=(d||{}).options;return(b&&e?b[e]:b)}if(!a.hasClass(this.markerClassName)){return}b=b||{};if(typeof b=='string'){var e=b;b={};b[e]=c}if(this._curInst==d){this._hidePlugin()}$.extend(d.options,b);this._setInput(a,d);this._updateKeypad(d)},_destroyPlugin:function(a){a=$(a);if(!a.hasClass(this.markerClassName)){return}var b=a.data(this.propertyName);if(this._curInst==b){this._hidePlugin()}a.siblings('.'+this._appendClass).remove().end().siblings('.'+this._triggerClass).remove().end().prev('.'+this._inlineEntryClass).remove();a.removeClass(this.markerClassName).empty().unbind('.'+this.propertyName).removeData(this.propertyName)[b.saveReadonly?'attr':'removeAttr']('readonly',true);b._input.removeData(this.propertyName)},_enablePlugin:function(b){b=$(b);if(!b.hasClass(this.markerClassName)){return}var c=b[0].nodeName.toLowerCase();if(c.match(/input|textarea/)){b[0].disabled=false;b.siblings('button.'+this._triggerClass).each(function(){this.disabled=false}).end().siblings('img.'+this._triggerClass).css({opacity:'1.0',cursor:''})}else if(c.match(/div|span/)){b.children('.'+this._disableClass).remove();var d=b.data(this.propertyName);d._mainDiv.find('button').removeAttr('disabled')}this._disabledFields=$.map(this._disabledFields,function(a){return(a==b[0]?null:a)})},_disablePlugin:function(b){b=$(b);if(!b.hasClass(this.markerClassName)){return}var c=b[0].nodeName.toLowerCase();if(c.match(/input|textarea/)){b[0].disabled=true;b.siblings('button.'+this._triggerClass).each(function(){this.disabled=true}).end().siblings('img.'+this._triggerClass).css({opacity:'0.5',cursor:'default'})}else if(c.match(/div|span/)){var d=b.children('.'+this._inlineClass);var e=d.offset();var f={left:0,top:0};d.parents().each(function(){if($(this).css('position')=='relative'){f=$(this).offset();return false}});b.prepend('');var g=b.data(this.propertyName);g._mainDiv.find('button').attr('disabled','disabled')}this._disabledFields=$.map(this._disabledFields,function(a){return(a==b[0]?null:a)});this._disabledFields[this._disabledFields.length]=b[0]},_isDisabledPlugin:function(a){return(a&&$.inArray(a,this._disabledFields)>-1)},_showPlugin:function(b){b=b.target||b;if(l._isDisabledPlugin(b)||l._lastField==b){return}var c=$.data(b,l.propertyName);l._hidePlugin(null,'');l._lastField=b;l._pos=l._findPos(b);l._pos[1]+=b.offsetHeight;var d=false;$(b).parents().each(function(){d|=$(this).css('position')=='fixed';return!d});if(d&&$.browser.opera){l._pos[0]-=document.documentElement.scrollLeft;l._pos[1]-=document.documentElement.scrollTop}var e={left:l._pos[0],top:l._pos[1]};l._pos=null;c._mainDiv.css({position:'absolute',display:'block',top:'-1000px',width:($.browser.opera?'1000px':'auto')});l._updateKeypad(c);e=l._checkOffset(c,e,d);c._mainDiv.css({position:(d?'fixed':'absolute'),display:'none',left:e.left+'px',top:e.top+'px'});var f=c.options.duration;f=(f=='normal'&&$.ui&&$.ui.version>='1.8'?'_default':f);var g=c.options.showAnim;var h=function(){l._keypadShowing=true;var a=l._getBorders(c._mainDiv);c._mainDiv.find('iframe.'+l._coverClass).css({left:-a[0],top:-a[1],width:c._mainDiv.outerWidth(),height:c._mainDiv.outerHeight()})};if($.effects&&($.effects[g]||($.effects.effect&&$.effects.effect[g]))){var i=c._mainDiv.data();for(var j in i){if(j.match(/^ec\.storage\./)){i[j]=c._mainDiv.css(j.replace(/ec\.storage\./,''))}}c._mainDiv.data(i).show(g,c.options.showOptions,f,h)}else{c._mainDiv[g||'show']((g?f:''),h)}if(!g){h()}if(c._input[0].type!='hidden'){c._input[0].focus()}l._curInst=c},_updateKeypad:function(a){var b=this._getBorders(a._mainDiv);a._mainDiv.empty().append(this._generateHTML(a)).find('iframe.'+this._coverClass).css({left:-b[0],top:-b[1],width:a._mainDiv.outerWidth(),height:a._mainDiv.outerHeight()});a._mainDiv.removeClass().addClass(a.options.keypadClass+(a.options.useThemeRoller?' ui-widget ui-widget-content':'')+(a.options.isRTL?' '+this._rtlClass:'')+' '+(a._inline?this._inlineClass:this._mainDivClass));if($.isFunction(a.options.beforeShow)){a.options.beforeShow.apply((a._input?a._input[0]:null),[a._mainDiv,a])}},_getBorders:function(c){var d=function(a){var b=($.browser.msie?1:0);return{thin:1+b,medium:3+b,thick:5+b}[a]||a};return[parseFloat(d(c.css('border-left-width'))),parseFloat(d(c.css('border-top-width')))]},_checkOffset:function(a,b,c){var d=a._input?this._findPos(a._input[0]):null;var e=window.innerWidth||document.documentElement.clientWidth;var f=window.innerHeight||document.documentElement.clientHeight;var g=document.documentElement.scrollLeft||document.body.scrollLeft;var h=document.documentElement.scrollTop||document.body.scrollTop;if(($.browser.msie&&parseInt($.browser.version,10)<7)||$.browser.opera){var i=0;a._mainDiv.find(':not(div,iframe)').each(function(){i=Math.max(i,this.offsetLeft+$(this).outerWidth()+parseInt($(this).css('margin-right'),10))});a._mainDiv.css('width',i)}if(a.options.isRTL||(b.left+a._mainDiv.outerWidth()-g)>e){b.left=Math.max((c?0:g),d[0]+(a._input?a._input.outerWidth():0)-(c?g:0)-a._mainDiv.outerWidth()-(c&&$.browser.opera?document.documentElement.scrollLeft:0))}else{b.left-=(c?g:0)}if((b.top+a._mainDiv.outerHeight()-h)>f){b.top=Math.max((c?0:h),d[1]-(c?h:0)-a._mainDiv.outerHeight()-(c&&$.browser.opera?document.documentElement.scrollTop:0))}else{b.top-=(c?h:0)}return b},_findPos:function(a){while(a&&(a.type=='hidden'||a.nodeType!=1)){a=a.nextSibling}var b=$(a).offset();return[b.left,b.top]},_hidePlugin:function(a,b){var c=this._curInst;if(!c||(a&&c!=$.data(a,this.propertyName))){return}if(this._keypadShowing){b=(b!=null?b:c.options.duration);b=(b=='normal'&&$.ui&&$.ui.version>='1.8'?'_default':b);var d=c.options.showAnim;if($.effects&&($.effects[d]||($.effects.effect&&$.effects.effect[d]))){c._mainDiv.hide(d,c.options.showOptions,b)}else{c._mainDiv[(d=='slideDown'?'slideUp':(d=='fadeIn'?'fadeOut':'hide'))](d?b:'')}}if($.isFunction(c.options.onClose)){c.options.onClose.apply((c._input?c._input[0]:null),[c._input.val(),c])}if(this._keypadShowing){this._keypadShowing=false;this._lastField=null}if(c._inline){c._input.val('')}this._curInst=null},_doKeyDown:function(e){if(e.keyCode==9){l.mainDiv.stop(true,true);l._hidePlugin()}},_checkExternalClick:function(a){if(!l._curInst){return}var b=$(a.target);if(!b.parents().andSelf().hasClass(l._mainDivClass)&&!b.hasClass(l.markerClassName)&&!b.parents().andSelf().hasClass(l._triggerClass)&&l._keypadShowing){l._hidePlugin()}},_shiftKeypad:function(a){a.ucase=!a.ucase;this._updateKeypad(a);a._input.focus()},_clearValue:function(a){this._setValue(a,'',0);this._notifyKeypress(a,l.DEL)},_backValue:function(a){var b=a._input[0];var c=a._input.val();var d=[c.length,c.length];if(b.setSelectionRange){d=(a._input.attr('readonly')||a._input.attr('disabled')?d:[b.selectionStart,b.selectionEnd])}else if(b.createTextRange){d=(a._input.attr('readonly')||a._input.attr('disabled')?d:this._getIERange(b))}this._setValue(a,(c.length==0?'':c.substr(0,d[0]-1)+c.substr(d[1])),d[0]-1);this._notifyKeypress(a,l.BS)},_selectValue:function(a,b){this.insertValue(a._input[0],b);this._setValue(a,a._input.val());this._notifyKeypress(a,b)},insertValue:function(a,b){a=(a.jquery?a:$(a));var c=a[0];var d=a.val();var e=[d.length,d.length];if(c.setSelectionRange){e=(a.attr('readonly')||a.attr('disabled')?e:[c.selectionStart,c.selectionEnd])}else if(c.createTextRange){e=(a.attr('readonly')||a.attr('disabled')?e:this._getIERange(c))}a.val(d.substr(0,e[0])+b+d.substr(e[1]));pos=e[0]+b.length;if(a.is(':visible')){a.focus()}if(c.setSelectionRange){if(a.is(':visible')){c.setSelectionRange(pos,pos)}}else if(c.createTextRange){e=c.createTextRange();e.move('character',pos);e.select()}},_getIERange:function(e){e.focus();var f=document.selection.createRange().duplicate();var g=this._getIETextRange(e);g.setEndPoint('EndToStart',f);var h=function(a){var b=a.text;var c=b;var d=false;while(true){if(a.compareEndPoints('StartToEnd',a)==0){break}else{a.moveEnd('character',-1);if(a.text==b){c+='\r\n'}else{break}}}return c};var i=h(g);var j=h(f);return[i.length,i.length+j.length]},_getIETextRange:function(a){var b=(a.nodeName.toLowerCase()=='input');var c=(b?a.createTextRange():document.body.createTextRange());if(!b){c.moveToElementText(a)}return c},_setValue:function(a,b){var c=a._input.attr('maxlength');if(c>-1){b=b.substr(0,c)}a._input.val(b);if(!$.isFunction(a.options.onKeypress)){a._input.trigger('change')}},_notifyKeypress:function(a,b){if($.isFunction(a.options.onKeypress)){a.options.onKeypress.apply((a._input?a._input[0]:null),[b,a._input.val(),a])}},_generateHTML:function(b){var c=(!b.options.prompt?'':' ');var d=this._randomiseLayout(b);for(var i=0;i