(function($){$.fn.initRegistration=function(options){var _this=this,settings=$.extend({},$.fn.initRegistration.defaults,options);$(_this).find('button[type=submit]').removeAttr('disabled');$(_this).on('submit',function(e){e.preventDefault();e.stopImmediatePropagation();var $btnSubmit=$(_this).find('button[type=submit]');if(settings.validatorForm(_this)===false){return false;}
if($(_this).attr('data-soipending-url')&&!settings.soiPending){eval($(_this).attr('data-soipending-onerror'));settings.soiPending={};settings.soiPending.url=$(_this).attr('data-soipending-url');settings.soiPending.delay=$(_this).attr('data-soipending-delay');settings.soiPending.onError=soiPendingOnError;}
if(typeof settings.soiPending!=='undefined'&&settings.soiPending!=''){settings.onPending(_this,settings.soiPending);return false;}
$.ajax({url:$(_this).attr('action'),type:'POST',dataType:'json',data:$(_this).serialize(),beforeSend:function(){settings.beforeSubmit();_loading();$btnSubmit.attr('disabled','disabled');},success:function(response){settings.afterSubmit();_removeLoading();$btnSubmit.removeAttr('disabled');if(response.success===true){settings.onComplete(_this,response);return;}
settings.handlingServerReponseError(_this,response);}});});};var _validatorForm=function(form){if(typeof Validator==='undefined'){return true;}
var validator=new Validator($(form),{});if(validator.checkForm()===true){return false;}
return true;};var _handlingServerReponseError=function(form,response){$.each(response.errors,function(element,errors){var errorKey=Object.keys(errors)[0],errorMsg=errors[errorKey],validator=new Validator(),$form=$(form),$btnSubmit=$form.find('button[type=submit]');if(element==='unknown'){var customMsg=$btnSubmit.attr('data-'+errorKey+'-message');if(typeof customMsg!='undefined'){errorMsg=customMsg;}
_addUnknowErrorMessage($btnSubmit,errorMsg);}
else{var customMsg=$('#'+element).attr('data-'+errorKey+'-message');if(typeof customMsg!='undefined'){errorMsg=customMsg;}
validator.addError('#'+element,errorMsg,$form);}});};var _loading=function(){$(document.body).find('.modal-overlay').addClass('hidden');$(document.body).append('<div class="lds-ripple"><span></span></div>');};var _removeLoading=function(){$(document.body).find('.modal-overlay').removeClass('hidden');$(document.body).find('.lds-ripple').remove();};var _addUnknowErrorMessage=function(obj,msg){if(obj.hasClass('js-add-unknow-msg-after')){obj.next('.unknown-error-msg').remove();obj.after('<span class="unknown-error-msg">'+msg+'</span>');}
else{obj.prev('.unknown-error-msg').remove();obj.before('<span class="unknown-error-msg">'+msg+'</span>');}};var _sendUserdata=function(form,soiPending){var delay=typeof soiPending.delay?soiPending.delay:1000;setTimeout(function(){$.ajax({url:$(form).attr('action'),type:'POST',dataType:'json',data:$(form).serialize(),success:function(response){if(response.success===true&&typeof response.result!='undefined'&&typeof response.result.deeplink!='undefined'&&response.result.deeplink!=''){window.location.href=response.result.deeplink;}else{if(typeof soiPending.onError==='function'){soiPending.onError();}}}});},delay);};var _onPending=function(form,soiPending){var soiPendingUrl=soiPending;if(typeof soiPending==='object'){soiPendingUrl=soiPending.url;}
if(typeof soiPendingUrl==='undefined'||soiPendingUrl===''){return false;}
$.ajax({url:soiPendingUrl,type:'GET',dataType:'html',beforeSend:function(){_loading();},success:function(response){var matched=response.match(/<body[^>]*>([\w|\W]*)<\/body>/im);if(typeof matched[1]!=='undefined'){$(document.body).append(matched);_sendUserdata(form,soiPending);}}});};$.fn.initRegistration.defaults={validatorForm:function(form){return _validatorForm(form);},onPending:function(form,soiPending){_onPending(form,soiPending);},handlingServerReponseError:function(form,response){_handlingServerReponseError(form,response);},beforeSubmit:function(){},afterSubmit:function(){},onComplete:function(){},};}(jQuery));