Committer: dpetrov
LJSUP-7918: client side development (SMS notification - Add Kazahstan).U trunk/htdocs/js/editprofile.js
Modified: trunk/htdocs/js/editprofile.js =================================================================== --- trunk/htdocs/js/editprofile.js 2011-02-11 09:02:45 UTC (rev 10025) +++ trunk/htdocs/js/editprofile.js 2011-02-11 09:35:43 UTC (rev 10026) @@ -1,40 +1,68 @@ jQuery(function($) { + var options = { + defaultCode: '+7' + } - $.fn.maskInput = function(options) + function checkDEF(val) { - var mask = /^(\+?[7]|8)\d{10}$/, - allowedChars = "0-9", - stripChars = "()\\- "; - var lastValue; + var def = parseInt(val.substr(0,3),10); - function checkDEF(val) - { - var def = parseInt(val.substr(1,3),10); + return ($.inArray(def, supportedDEF) > -1); + } - return ($.inArray(def, supportedDEF) > -1); - } + /** + * PhoneNumber looks into the two inputs and checks their values according to the mask. + */ + var PhoneNumber = { + /** + * @param Node code + * @param Node number + */ + init: function( code, number, onUpdate ) { + var self = this; + this._onUpdate = onUpdate || jQuery.noop; - function processInput(ev) - { - var val = $.trim(this.value).replace(new RegExp("[" + stripChars + "]", "g"),""); - if(typeof lastValue == undefined || lastValue != val) + this._code = code + .attr('autocomplete', 'off') + .input( function() { self.updateState(); } ); + this._number = number + .attr('autocomplete', 'off') + .input( function() { self.updateState(); } ); + this.updateState(); + }, + + updateState: function() { + var mask = /^\d{10}$/, + stripChars = "()\\- "; + + var code = this._code.val(), + number = $.trim( this._number.val() ).replace(new RegExp("[" + stripChars + "]", "g"),""); + + var state = 'correctnum'; + + if( number.length == 0 && code === options.defaultCode ) { - options.update(mask.test(val), checkDEF(val.replace(/^\+/,''))); - lastValue = val; + state = 'initial'; } + else if( !( mask.test( number ) ) || !( /^(\+7|7|8)$/.test( code ) ) ) + { + state = 'badnum'; + } + else if( !checkDEF( number ) ) + { + state = 'unsupporteddef'; + } + + this._onUpdate( state ); } - - this.input(processInput) - .attr('autocomplete', 'off'); - processInput.call(this.get(0)); } var PhoneVerification = { init: function() { - this.supportedProviders = ['beeline', 'megafon', 'mts']; + this.supportedProviders = ['beeline', 'megafon', 'mts', 'kcell']; this.table = $('#phone_verification'); this.state = []; @@ -47,12 +75,15 @@ table: this.table, provider: this.table.find('[name=txtmsg_provider]'), resendBtn: this.table.find('[name=sendsms]'), + phCodeInput: this.table.find('[name=txtmsg_number_code]'), phInput: this.table.find('[name=txtmsg_number]'), phVerification: $('#phone_vercode'), notification: this.table.find('.b-manage-smsn-status'), verCode: $('#phone_vercode [name=txtmsg_verify]'), sendCodeBtn: $('#phone_vercode [name=verify]') } + + this.verifyTimer = null; this.endpoint = "/tools/endpoints/validate_phone.bml"; this.stopVerification = false; @@ -66,23 +97,18 @@ this.viewOptionsValue = ''; var initial = true; - this.ui.phInput.maskInput({ - update: function(isok, isGoodDef) - { - if(that.ui.phInput.val().length == 0) - { + PhoneNumber.init( this.ui.phCodeInput, this.ui.phInput, function( state ) { + switch( state ) { + case 'initial': that.setState('initial', 'info', 'initial'); - } - else if(!isok) - { + break; + case 'badnum': that.setState('badNum', 'error', 'badnum'); - } - else if(!isGoodDef) - { + break; + case 'unsupporteddef': that.setState('unsupporteddef', 'success', 'unsupporteddef'); - } - else - { + break; + default: that.setState('correctNum', 'info', 'correctnum'); if(initial) { var initState = smsNotifyInitialState || ""; @@ -90,10 +116,9 @@ that.checkVerification(initState); } } - } - initial = false; + break; } - }); + } ); this.ui.resendBtn.click(function() { @@ -112,20 +137,20 @@ { var resCode = $.trim(result.split(":")[1]); - switch( resCode ){ - case "invalid" : - that.setState('wrongcode', 'error', 'wrongcode'); - break; - case "outdated" : - that.setState('linkexpired', 'success', 'linkexpired'); - break; - case "ok" : - that.setState('verified', 'info', 'verified'); - break; - case "already_use": - that.setState('already_use', 'success', 'already_use'); - break; - } + switch( resCode ){ + case "invalid" : + that.setState('wrongcode', 'error', 'wrongcode'); + break; + case "outdated" : + that.setState('linkexpired', 'success', 'linkexpired'); + break; + case "ok" : + that.setState('verified', 'info', 'verified'); + break; + case "already_use": + that.setState('already_use', 'success', 'already_use'); + break; + } }); }); this.ui.verCode.input(function() { @@ -151,7 +176,7 @@ that.checkVerification(); break; case 'rate_limit_exceded': that.setState('totallimitexceeded', 'error', 'rateexceed'); break; - case 'already_use': that.setState('already_use', 'success', 'already_use'); break; + case 'already_use': that.setState('already_use', 'success', 'already_use'); break; } }); }, @@ -280,21 +305,21 @@ break; case 'wrongcode': this.skipReqSent = true; break; - case 'notasubscriber': resetUI(); + case 'notasubscriber': resetUI(); bt.attr('disabled', true); break; - case 'already_use': + case 'already_use': case 'unsupporteddef': resetUI(true); bt.attr('disabled', true); break; - case 'smssent' : bt.attr( 'disabled', false ); + case 'smssent' : bt.attr( 'disabled', false ); this.ui.resendBtn.parent('.b-manage-smsn-btn').css('display',''); this.ui.phVerification.removeAttr('style'); - break; - case 'linkexpired' : resetUI(); - bt.val( dict['resendsmsbut']) + break; + case 'linkexpired' : resetUI(); + bt.val( dict['resendsmsbut']) this.ui.phVerification.css('display','none') - break; + break; } }