var iMask=new Class({options:{targetClass:".iMask",maskEmptyChr:"_",validNumbers:"1234567890",validAlphas:"abcdefghijklmnopqrstuvwxyz",validAlphaNums:"abcdefghijklmnopqrstuvwxyz1234567890",onFocus:Class.empty,onBlur:Class.empty,onValid:Class.empty,onInvalid:Class.empty,onKeyDown:Class.empty},initialize:function(B){this.setOptions(B);var A=$$(this.options.targetClass);A.each(function(D,C){D.options=JSON.decode(D.alt);if(D.options.type=="number"){D.setStyle("text-align","right")}D.addEvent("mousedown",function(E){E=new Event(E);E.stop()});D.addEvent("mouseup",function(E){E=new Event(E);E.stop();this._onMouseUp(E,D)}.bind(this));D.addEvent("click",function(E){E=new Event(E);E.stop()});D.addEvent("keydown",function(E){E=new Event(E);this._onKeyDown(E,D);this.fireEvent("onKeyDown",D,20)}.bind(this));D.addEvent("keypress",function(E){E=new Event(E);this._onKeyPress(E,D)}.bind(this));D.addEvent("focus",function(E){E=new Event(E);E.stop();this._onFocus(E,D);this.fireEvent("onFocus",D,20)}.bind(this));D.addEvent("blur",function(E){E=new Event(E);E.stop();this._onBlur(E,D);this.fireEvent("onBlur",D,20)}.bind(this))}.bind(this))},_onMouseUp:function(A,C){if(C.options.type=="fixed"){var B=this._getSelectionStart(C);this._setSelection(C,B,(B+1))}else{if(C.options.type=="number"){this._setEnd(C)}}},_onKeyDown:function(B,D){if(B.code==13){D.blur();this._submitForm(D)}else{if(!(B.code==9)){B.stop();if(D.options.type=="fixed"){var C=this._getSelectionStart(D);switch(B.code){case 8:this._selectPrevious(D);break;case 36:this._selectFirst(D);break;case 35:this._selectLast(D);break;case 37:case 38:this._selectPrevious(D);break;case 39:case 40:this._selectNext(D);break;case 46:this._updSelection(D,C,this.options.maskEmptyChr);break;default:var A=this._chrFromEvent(B);if(this._isViableInput(D,C,A)){if(B.shift){this._updSelection(D,C,A.toUpperCase())}else{this._updSelection(D,C,A)}this.fireEvent("onValid",[B,D],20);this._selectNext(D)}else{this.fireEvent("onInvalid",[B,D],20)}break}}else{if(D.options.type=="number"){switch(B.code){case 8:case 46:this._popNumber(D);break;default:var A=this._chrFromEvent(B);if(this.options.validNumbers.indexOf(A)>=0){this._pushNumber(D,A);this.fireEvent("onValid",[B,D],20)}else{this.fireEvent("onInvalid",[B,D],20)}break}}}}}},_onKeyPress:function(A,B){if(!(A.code==9)&&!(A.shift&&A.code==9)&&!(A.code==13)&&!(A.ctrl&&A.code==67)&&!(A.ctrl&&A.code==86)&&!(A.ctrl&&A.code==88)){A.stop()}},_onFocus:function(A,B){if(B.options.stripMask){B.value=this._wearMask(B,B.value)}else{if(!B.value){B.value=this._wearMask(B,B.value)}}if(B.options.type=="fixed"){this._selectFirst.delay(20,this,B)}else{this._setEnd.delay(20,this,B)}},_onBlur:function(B,C){var A=this._stripMask(C);if(C.options.stripMask){C.value=A}else{if(!A){C.value=""}}},_selectAll:function(A){this._setSelection(A,0,A.value.length)},_selectFirst:function(C){for(var B=0,A=C.options.mask.length;B<A;B++){if(this._isInputPosition(C,B)){this._setSelection(C,B,(B+1));return}}},_selectLast:function(B){for(var A=(B.options.mask.length-1);A>=0;A--){if(this._isInputPosition(B,A)){this._setSelection(B,A,(A+1));return}}},_selectPrevious:function(B,A){if(!$chk(A)){A=this._getSelectionStart(B)}if(A<=0){this._selectFirst(B)}else{if(this._isInputPosition(B,(A-1))){this._setSelection(B,(A-1),A)}else{this._selectPrevious(B,(A-1))}}},_selectNext:function(B,A){if(!$chk(A)){A=this._getSelectionEnd(B)}if(A>=B.options.mask.length){this._selectLast(B)}else{if(this._isInputPosition(B,A)){this._setSelection(B,A,(A+1))}else{this._selectNext(B,(A+1))}}},_setSelection:function(D,B,A){if(D.setSelectionRange){D.focus();D.setSelectionRange(B,A)}else{if(D.createTextRange){var C=D.createTextRange();C.collapse();C.moveStart("character",B);C.moveEnd("character",(A-B));C.select()}}},_updSelection:function(E,D,B){var C=E.value;var A="";A+=C.substring(0,D);A+=B;A+=C.substr(D+1);E.value=A;this._setSelection(E,D,(D+1))},_setEnd:function(B){var A=B.value.length;this._setSelection(B,A,A)},_getSelectionStart:function(C){var B=0;if(C.selectionStart){if($type(C.selectionStart)=="number"){B=C.selectionStart}}else{if(document.selection){var A=document.selection.createRange().duplicate();A.moveEnd("character",C.value.length);B=C.value.lastIndexOf(A.text);if(A.text==""){B=C.value.length}}}return B},_getSelectionEnd:function(C){var B=0;if(C.selectionEnd){if($type(C.selectionEnd)=="number"){B=C.selectionEnd}}else{if(document.selection){var A=document.selection.createRange().duplicate();A.moveStart("character",-C.value.length);B=A.text.length}}return B},_isInputPosition:function(D,C){var A=D.options.mask.toLowerCase();var B=A.charAt(C);if("9ax".indexOf(B)>=0){return true}return false},_isViableInput:function(E,D,B){var A=E.options.mask.toLowerCase();var C=A.charAt(D);switch(C){case"9":if(this.options.validNumbers.indexOf(B)>=0){return true}break;case"a":if(this.options.validAlphas.indexOf(B)>=0){return true}break;case"x":if(this.options.validAlphaNums.indexOf(B)>=0){return true}break;default:return false;break}},_wearMask:function(F,G){var B=F.options.mask.toLowerCase();var C="";for(var E=0,D=0,A=B.length;E<A;E++){switch(B.charAt(E)){case"9":if(this.options.validNumbers.indexOf(G.charAt(D).toLowerCase())>=0){if(G.charAt(D)==""){C+=this.options.maskEmptyChr}else{C+=G.charAt(D++)}}else{C+=this.options.maskEmptyChr}break;case"a":if(this.options.validAlphas.indexOf(G.charAt(D).toLowerCase())>=0){if(G.charAt(D)==""){C+=this.options.maskEmptyChr}else{C+=G.charAt(D++)}}else{C+=this.options.maskEmptyChr}break;case"x":if(this.options.validAlphaNums.indexOf(G.charAt(D).toLowerCase())>=0){if(G.charAt(D)==""){C+=this.options.maskEmptyChr}else{C+=G.charAt(D++)}}else{C+=this.options.maskEmptyChr}break;default:C+=B.charAt(E);break}}return C},_stripMask:function(E){var D=E.value;if(""==D){return""}var B="";if(E.options.type=="fixed"){for(var C=0,A=D.length;C<A;C++){if((D.charAt(C)!=this.options.maskEmptyChr)&&(this._isInputPosition(E,C))){B+=D.charAt(C)}}}else{if(E.options.type=="number"){for(var C=0,A=D.length;C<A;C++){if(this.options.validNumbers.indexOf(D.charAt(C))>=0){B+=D.charAt(C)}}}}return B},_chrFromEvent:function(B){var A="";switch(B.code){case 48:case 96:A="0";break;case 49:case 97:A="1";break;case 50:case 98:A="2";break;case 51:case 99:A="3";break;case 52:case 100:A="4";break;case 53:case 101:A="5";break;case 54:case 102:A="6";break;case 55:case 103:A="7";break;case 56:case 104:A="8";break;case 57:case 105:A="9";break;default:A=B.key;break}return A},_pushNumber:function(B,A){B.value=B.value+A;this._formatNumber(B)},_popNumber:function(A){A.value=A.value.substring(0,(A.value.length-1));this._formatNumber(A)},_formatNumber:function(F){var D=this._stripMask(F);var E="";for(var B=0,A=D.length;B<A;B++){if("0"!=D.charAt(B)){E=D.substr(B);break}}D=E;E="";for(var A=D.length,B=F.options.decDigits;A<=B;A++){E+="0"}E+=D;D=E.substr(E.length-F.options.decDigits);E=E.substring(0,(E.length-F.options.decDigits));var C=new RegExp("(\\d+)(\\d{"+F.options.groupDigits+"})");while(C.test(E)){E=E.replace(C,"$1"+F.options.groupSymbol+"$2")}F.value=E+F.options.decSymbol+D},_getObjForm:function(B){var A=B.getParent();if(A.getTag()=="form"){return A}else{return this._getObjForm(A)}},_submitForm:function(B){var A=this._getObjForm(B);A.submit()}});iMask.implement(new Events);iMask.implement(new Options);
