(function($){'use strict';var pluginName='keypad';$.JQPlugin.createPlugin({name:pluginName,deepMerge:false,defaultOptions:{showOn:'focus',buttonImage:'',buttonImageOnly:false,showAnim:'show',showOptions:null,duration:'normal',appendText:'',useThemeRoller:false,keypadClass:'',prompt:'',layout:[],separator:'',target:null,keypadOnly:true,randomiseAlphabetic:false,randomiseNumeric:false,randomiseOther:false,randomiseAll:false,beforeShow:null,onKeypress:null,onClose:null},regionalOptions:{'':{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:'&#160;',spacebarStatus:'Space',enterText:'Enter',enterStatus:'Carriage return',tabText:'→',tabStatus:'Horizontal tab',shiftText:'Shift',shiftStatus:'Toggle upper/lower case characters',alphabeticLayout:[],fullLayout:[],isAlphabetic:null,isNumeric:null,toUpper:null,isRTL:false}},BS:'\x08',DEL:'\x7F',_curInst:null,_disabledFields:[],_keypadShowing:false,_keyCode:0,_specialKeys:[],_mainDivClass:pluginName+'-popup',_inlineClass:pluginName+'-inline',_appendClass:pluginName+'-append',_triggerClass:pluginName+'-trigger',_disableClass:pluginName+'-disabled',_inlineEntryClass:pluginName+'-keyentry',_rtlClass:pluginName+'-rtl',_rowClass:pluginName+'-row',_promptClass:pluginName+'-prompt',_specialClass:pluginName+'-special',_namePrefixClass:pluginName+'-',_keyClass:pluginName+'-key',_keyDownClass:pluginName+'-key-down',addKeyDef:function(id,name,action,noHighlight){if(this._keyCode===32){throw'Only 32 special keys allowed';}
this[id]=String.fromCharCode(this._keyCode++);this._specialKeys.push({code:this[id],id:id,name:name,action:action,noHighlight:noHighlight});return this;},_init:function(){this.mainDiv=$('<div class="'+this._mainDivClass+'" style="display: none;"></div>');this._super();},_instSettings:function(elem,options){var inline=!elem[0].nodeName.toLowerCase().match(/input|textarea/);return{_inline:inline,ucase:false,_mainDiv:(inline?$('<div class="'+this._inlineClass+'"></div>'):plugin.mainDiv)};},_postAttach:function(elem,inst){if(inst._inline){elem.append(inst._mainDiv).on('click.'+inst.name,function(){inst._input.focus();});this._updateKeypad(inst);}
else if(elem.is(':disabled')){this.disable(elem);}},_setInput:function(elem,inst){inst._input=$(!inst._inline?elem:inst.options.target||'<input type="text" class="'+this._inlineEntryClass+'" disabled/>');if(inst._inline){elem.find('input').remove();if(!inst.options.target){elem.append(inst._input);}}},_optionsChanged:function(elem,inst,options){$.extend(inst.options,options);elem.off('.'+inst.name).siblings('.'+this._appendClass).remove().end().siblings('.'+this._triggerClass).remove();var appendText=inst.options.appendText;if(appendText){elem[inst.options.isRTL?'before':'after']('<span class="'+this._appendClass+'">'+appendText+'</span>');}
if(!inst._inline){if(inst.options.showOn==='focus'||inst.options.showOn==='both'){elem.on('focus.'+inst.name,this.show).on('keydown.'+inst.name,this._doKeyDown);}
if(inst.options.showOn==='button'||inst.options.showOn==='both'){var buttonStatus=inst.options.buttonStatus;var buttonImage=inst.options.buttonImage;var trigger=$(inst.options.buttonImageOnly?$('<img src="'+buttonImage+'" alt="'+
buttonStatus+'" title="'+buttonStatus+'"/>'):$('<button type="button" title="'+buttonStatus+'"></button>').html(buttonImage===''?inst.options.buttonText:$('<img src="'+buttonImage+'" alt="'+
buttonStatus+'" title="'+buttonStatus+'"/>')));elem[inst.options.isRTL?'before':'after'](trigger);trigger.addClass(this._triggerClass).click(function(){if(plugin._keypadShowing&&plugin._lastField===elem[0]){plugin.hide();}
else{plugin.show(elem[0]);}
return false;});}}
inst.saveReadonly=elem.attr('readonly');elem[inst.options.keypadOnly?'attr':'removeAttr']('readonly',true).on('setData.'+inst.name,function(event,key,value){inst.options[key]=value;}).on('getData.'+inst.name,function(event,key){return inst.options[key];});this._setInput(elem,inst);this._updateKeypad(inst);},_preDestroy:function(elem,inst){if(this._curInst===inst){this.hide();}
elem.siblings('.'+this._appendClass).remove().end().siblings('.'+this._triggerClass).remove().end().prev('.'+this._inlineEntryClass).remove();elem.empty().off('.'+inst.name)
[inst.saveReadonly?'attr':'removeAttr']('readonly',true);inst._input.removeData(inst.name);},enable:function(elem){elem=$(elem);if(!elem.hasClass(this._getMarker())){return;}
var nodeName=elem[0].nodeName.toLowerCase();if(nodeName.match(/input|textarea/)){elem.prop('disabled',false).siblings('button.'+this._triggerClass).prop('disabled',false).end().siblings('img.'+this._triggerClass).css({opacity:'1.0',cursor:''});}
else if(nodeName.match(/div|span/)){elem.children('.'+this._disableClass).remove();this._getInst(elem)._mainDiv.find('button').prop('disabled',false);}
this._disabledFields=$.map(this._disabledFields,function(value){return(value===elem[0]?null:value);});},disable:function(elem){elem=$(elem);if(!elem.hasClass(this._getMarker())){return;}
var nodeName=elem[0].nodeName.toLowerCase();if(nodeName.match(/input|textarea/)){elem.prop('disabled',true).siblings('button.'+this._triggerClass).prop('disabled',true).end().siblings('img.'+this._triggerClass).css({opacity:'0.5',cursor:'default'});}
else if(nodeName.match(/div|span/)){var inline=elem.children('.'+this._inlineClass);var offset=inline.offset();var relOffset={left:0,top:0};inline.parents().each(function(){if($(this).css('position')==='relative'){relOffset=$(this).offset();return false;}});elem.prepend('<div class="'+this._disableClass+'" style="width: '+
inline.outerWidth()+'px; height: '+inline.outerHeight()+'px; left: '+(offset.left-relOffset.left)+'px; top: '+(offset.top-relOffset.top)+'px;"></div>');this._getInst(elem)._mainDiv.find('button').prop('disabled',true);}
this._disabledFields=$.map(this._disabledFields,function(value){return(value===elem[0]?null:value);});this._disabledFields[this._disabledFields.length]=elem[0];},isDisabled:function(elem){return(elem&&$.inArray(elem,this._disabledFields)>-1);},show:function(elem){elem=elem.target||elem;if(plugin.isDisabled(elem)||plugin._lastField===elem){return;}
var inst=plugin._getInst(elem);plugin.hide(null,'');plugin._lastField=elem;plugin._pos=plugin._findPos(elem);plugin._pos[1]+=elem.offsetHeight;var isFixed=false;$(elem).parents().each(function(){isFixed=isFixed||$(this).css('position')==='fixed';return!isFixed;});var offset={left:plugin._pos[0],top:plugin._pos[1]};plugin._pos=null;inst._mainDiv.stop(true,true).css({position:'absolute',display:'block',top:'-1000px',width:'auto'});plugin._updateKeypad(inst);offset=plugin._checkOffset(inst,offset,isFixed);inst._mainDiv.css({position:(isFixed?'fixed':'absolute'),display:'none',left:offset.left+'px',top:offset.top+'px'});var duration=inst.options.duration;var showAnim=inst.options.showAnim;var postProcess=function(){plugin._keypadShowing=true;};if($.effects&&($.effects[showAnim]||($.effects.effect&&$.effects.effect[showAnim]))){var data=inst._mainDiv.data();for(var key in data){if(key.match(/^ec\.storage\./)){data[key]=inst._mainDiv.css(key.replace(/ec\.storage\./,''));}}
inst._mainDiv.data(data).show(showAnim,inst.options.showOptions||{},duration,postProcess);}
else{inst._mainDiv[showAnim||'show']((showAnim?duration:0),postProcess);}
if(inst._input[0].type!=='hidden'){inst._input[0].focus();}
plugin._curInst=inst;},_updateKeypad:function(inst){inst._mainDiv.empty().append(this._generateHTML(inst)).removeClass().addClass(inst.options.keypadClass+
(inst.options.useThemeRoller?' ui-widget ui-widget-content':'')+
(inst.options.isRTL?' '+this._rtlClass:'')+' '+
(inst._inline?this._inlineClass:this._mainDivClass));if($.isFunction(inst.options.beforeShow)){inst.options.beforeShow.apply((inst._input?inst._input[0]:null),[inst._mainDiv,inst]);}},_checkOffset:function(inst,offset,isFixed){var pos=inst._input?this._findPos(inst._input[0]):null;var browserWidth=window.innerWidth||document.documentElement.clientWidth;var browserHeight=window.innerHeight||document.documentElement.clientHeight;var scrollX=document.documentElement.scrollLeft||document.body.scrollLeft;var scrollY=document.documentElement.scrollTop||document.body.scrollTop;var width=0;inst._mainDiv.find(':not(div)').each(function(){width=Math.max(width,this.offsetLeft+$(this).outerWidth(true));});inst._mainDiv.css('width',width+1);if(inst.options.isRTL||(offset.left+inst._mainDiv.outerWidth()-scrollX)>browserWidth){offset.left=Math.max((isFixed?0:scrollX),pos[0]+(inst._input?inst._input.outerWidth():0)-
(isFixed?scrollX:0)-inst._mainDiv.outerWidth());}
else{offset.left=Math.max((isFixed?0:scrollX),offset.left-(isFixed?scrollX:0));}
if((offset.top+inst._mainDiv.outerHeight()-scrollY)>browserHeight){offset.top=Math.max((isFixed?0:scrollY),pos[1]-(isFixed?scrollY:0)-inst._mainDiv.outerHeight());}
else{offset.top=Math.max((isFixed?0:scrollY),offset.top-(isFixed?scrollY:0));}
return offset;},_findPos:function(obj){while(obj&&(obj.type==='hidden'||obj.nodeType!==1)){obj=obj.nextSibling;}
var position=$(obj).offset();return[position.left,position.top];},hide:function(elem,duration){var inst=this._curInst;if(!inst||(elem&&inst!==$.data(elem,this.name))){return;}
if(this._keypadShowing){inst._mainDiv.stop(true,true);duration=(typeof duration!=='undefined'&&duration!==null?duration:inst.options.duration);var showAnim=inst.options.showAnim;if($.effects&&($.effects[showAnim]||($.effects.effect&&$.effects.effect[showAnim]))){inst._mainDiv.hide(showAnim,inst.options.showOptions||{},duration);}
else{inst._mainDiv[(showAnim==='slideDown'?'slideUp':(showAnim==='fadeIn'?'fadeOut':'hide'))](showAnim?duration:0);}}
if($.isFunction(inst.options.onClose)){inst.options.onClose.apply((inst._input?inst._input[0]:null),[inst._input.val(),inst]);}
if(this._keypadShowing){this._keypadShowing=false;this._lastField=null;}
if(inst._inline){inst._input.val('');}
this._curInst=null;},_doKeyDown:function(event){if(event.keyCode===9){plugin.mainDiv.stop(true,true);plugin.hide();}},_checkExternalClick:function(event){if(!plugin._curInst){return;}
var target=$(event.target);if(target.closest('.'+plugin._mainDivClass).length===0&&!target.hasClass(plugin._getMarker())&&target.closest('.'+plugin._triggerClass).length===0&&plugin._keypadShowing){plugin.hide();}},_shiftKeypad:function(inst){inst.ucase=!inst.ucase;this._updateKeypad(inst);inst._input.focus();},_clearValue:function(inst){this._setValue(inst,'',0);this._notifyKeypress(inst,plugin.DEL);},_backValue:function(inst){var elem=inst._input[0];var value=inst._input.val();var range=[value.length,value.length];range=(inst._input.prop('readonly')||inst._input.prop('disabled')?range:(elem.setSelectionRange?[elem.selectionStart,elem.selectionEnd]:(elem.createTextRange?this._getIERange(elem):range)));this._setValue(inst,(value.length===0?'':value.substr(0,range[0]-1)+value.substr(range[1])),range[0]-1);this._notifyKeypress(inst,plugin.BS);},_selectValue:function(inst,value){this.insertValue(inst._input[0],value);this._setValue(inst,inst._input.val());this._notifyKeypress(inst,value);},insertValue:function(input,value){input=(input.jquery?input:$(input));var elem=input[0];var newValue=input.val();var range=[newValue.length,newValue.length];range=(input.attr('readonly')||input.attr('disabled')?range:(elem.setSelectionRange?[elem.selectionStart,elem.selectionEnd]:(elem.createTextRange?this._getIERange(elem):range)));input.val(newValue.substr(0,range[0])+value+newValue.substr(range[1]));var pos=range[0]+value.length;if(input.is(':visible')){input.focus();}
if(elem.setSelectionRange){if(input.is(':visible')){elem.setSelectionRange(pos,pos);}}
else if(elem.createTextRange){range=elem.createTextRange();range.move('character',pos);range.select();}},_getIERange:function(elem){elem.focus();var selectionRange=document.selection.createRange().duplicate();var beforeRange=this._getIETextRange(elem);beforeRange.setEndPoint('EndToStart',selectionRange);var checkCRLF=function(range){var origText=range.text;var text=origText;while(true){if(range.compareEndPoints('StartToEnd',range)===0){break;}
else{range.moveEnd('character',-1);if(range.text===origText){text+='\r\n';}
else{break;}}}
return text;};var beforeText=checkCRLF(beforeRange);var selectionText=checkCRLF(selectionRange);return[beforeText.length,beforeText.length+selectionText.length];},_getIETextRange:function(elem){var isInput=(elem.nodeName.toLowerCase()==='input');var range=(isInput?elem.createTextRange():document.body.createTextRange());if(!isInput){range.moveToElementText(elem);}
return range;},_setValue:function(inst,value){var maxlen=inst._input.attr('maxlength');if(maxlen>-1){value=value.substr(0,maxlen);}
inst._input.val(value);if(!$.isFunction(inst.options.onKeypress)){inst._input.trigger('change');}},_notifyKeypress:function(inst,key){if($.isFunction(inst.options.onKeypress)){inst.options.onKeypress.apply((inst._input?inst._input[0]:null),[key,inst._input.val(),inst]);}},_generateHTML:function(inst){var html=(!inst.options.prompt?'':'<div class="'+this._promptClass+
(inst.options.useThemeRoller?' ui-widget-header ui-corner-all':'')+'">'+
inst.options.prompt+'</div>');var layout=this._randomiseLayout(inst);for(var i=0;i<layout.length;i++){html+='<div class="'+this._rowClass+'">';var keys=layout[i].split(inst.options.separator);for(var j=0;j<keys.length;j++){if(inst.ucase){keys[j]=inst.options.toUpper(keys[j]);}
var keyDef=this._specialKeys[keys[j].charCodeAt(0)];if(keyDef){html+=(keyDef.action?'<button type="button" class="'+
this._specialClass+' '+this._namePrefixClass+keyDef.name+
(inst.options.useThemeRoller?' ui-corner-all ui-state-default'+
(keyDef.noHighlight?'':' ui-state-highlight'):'')+'" title="'+inst.options[keyDef.name+'Status']+'">'+
(inst.options[keyDef.name+'Text']||'&#160;')+'</button>':'<div class="'+this._namePrefixClass+keyDef.name+'"></div>');}
else{html+='<button type="button" class="'+this._keyClass+
(inst.options.useThemeRoller?' ui-corner-all ui-state-default':'')+'">'+(keys[j]===' '?'&#160;':keys[j])+'</button>';}}
html+='</div>';}
html=$(html);var thisInst=inst;var activeClasses=this._keyDownClass+
(inst.options.useThemeRoller?' ui-state-active':'');html.find('button').mousedown(function(){$(this).addClass(activeClasses);}).mouseup(function(){$(this).removeClass(activeClasses);}).mouseout(function(){$(this).removeClass(activeClasses);}).filter('.'+this._keyClass).click(function(){plugin._selectValue(thisInst,$(this).text());});$.each(this._specialKeys,function(i,keyDef){html.find('.'+plugin._namePrefixClass+keyDef.name).click(function(){keyDef.action.apply(thisInst._input,[thisInst]);});});return html;},_randomiseLayout:function(inst){if(!inst.options.randomiseNumeric&&!inst.options.randomiseAlphabetic&&!inst.options.randomiseOther&&!inst.options.randomiseAll){return inst.options.layout;}
var numerics=[];var alphas=[];var others=[];var newLayout=[];var i,j,keys;for(i=0;i<inst.options.layout.length;i++){newLayout[i]='';keys=inst.options.layout[i].split(inst.options.separator);for(j=0;j<keys.length;j++){if(this._isControl(keys[j])){continue;}
if(inst.options.randomiseAll){others.push(keys[j]);}
else if(inst.options.isNumeric(keys[j])){numerics.push(keys[j]);}
else if(inst.options.isAlphabetic(keys[j])){alphas.push(keys[j]);}
else{others.push(keys[j]);}}}
if(inst.options.randomiseNumeric){this._shuffle(numerics);}
if(inst.options.randomiseAlphabetic){this._shuffle(alphas);}
if(inst.options.randomiseOther||inst.options.randomiseAll){this._shuffle(others);}
var n=0;var a=0;var o=0;for(i=0;i<inst.options.layout.length;i++){keys=inst.options.layout[i].split(inst.options.separator);for(j=0;j<keys.length;j++){newLayout[i]+=(j>0?inst.options.separator:'')+(this._isControl(keys[j])?keys[j]:(inst.options.randomiseAll?others[o++]:(inst.options.isNumeric(keys[j])?numerics[n++]:(inst.options.isAlphabetic(keys[j])?alphas[a++]:others[o++]))));}}
return newLayout;},_isControl:function(ch){return ch<' ';},isAlphabetic:function(ch){return(ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z');},isNumeric:function(ch){return(ch>='0'&&ch<='9');},toUpper:function(ch){return ch.toUpperCase();},_shuffle:function(values){for(var i=values.length-1;i>0;i--){var j=Math.floor(Math.random()*(i+1));var ch=values[i];values[i]=values[j];values[j]=ch;}}});var plugin=$.keypad;plugin.addKeyDef('CLOSE','close',function(inst){plugin._curInst=(inst._inline?inst:plugin._curInst);plugin.hide();});plugin.addKeyDef('CLEAR','clear',function(inst){plugin._clearValue(inst);});plugin.addKeyDef('BACK','back',function(inst){plugin._backValue(inst);});plugin.addKeyDef('SHIFT','shift',function(inst){plugin._shiftKeypad(inst);});plugin.addKeyDef('SPACE_BAR','spacebar',function(inst){plugin._selectValue(inst,' ');},true);plugin.addKeyDef('SPACE','space');plugin.addKeyDef('HALF_SPACE','half-space');plugin.addKeyDef('ENTER','enter',function(inst){plugin._selectValue(inst,'\x0D');},true);plugin.addKeyDef('TAB','tab',function(inst){plugin._selectValue(inst,'\x09');},true);plugin.numericLayout=['123'+plugin.CLOSE,'456'+plugin.CLEAR,'789'+plugin.BACK,plugin.SPACE+'0'];plugin.qwertyAlphabetic=['qwertyuiop','asdfghjkl','zxcvbnm'];plugin.qwertyLayout=['!@#$%^&*()_='+plugin.HALF_SPACE+plugin.SPACE+plugin.CLOSE,plugin.HALF_SPACE+'`~[]{}<>\\|/'+plugin.SPACE+'789','qwertyuiop\'"'+plugin.HALF_SPACE+'456',plugin.HALF_SPACE+'asdfghjkl;:'+plugin.SPACE+'123',plugin.SPACE+'zxcvbnm,.?'+plugin.SPACE+plugin.HALF_SPACE+'-0+',''+plugin.TAB+plugin.ENTER+plugin.SPACE_BAR+plugin.SHIFT+
plugin.HALF_SPACE+plugin.BACK+plugin.CLEAR];$.extend(plugin.regionalOptions[''],{alphabeticLayout:plugin.qwertyAlphabetic,fullLayout:plugin.qwertyLayout,isAlphabetic:plugin.isAlphabetic,isNumeric:plugin.isNumeric,toUpper:plugin.toUpper});plugin.setDefaults($.extend({layout:plugin.numericLayout},plugin.regionalOptions['']));$(function(){$(document.body).append(plugin.mainDiv).on('mousedown.'+pluginName,plugin._checkExternalClick);});})(jQuery);;