summaryrefslogtreecommitdiff
blob: c25869ecd1bc66c46983ba3d53acc1882d6e13e4 (plain)
1
function WMP_CONNECT(){var JSObject=this;this.type="wmp_connect",this.form,this.DOMDoc,this.send_btn,this.submitURL,this.redirectTo,this.init=function(){WMPJSInterface=window.parent.WMPJSInterface,this.send_btn=jQuery("#"+this.type+"_send_btn",this.DOMDoc).get(0),this.form=this.DOMDoc.getElementById(this.type+"_form"),this.addButtonsActions(),null!=this.form&&this.initValidation()},this.initValidation=function(){this.validator=jQuery("#"+this.form.id,this.DOMDoc).validate({rules:{wmp_connect_apikey:{required:!0,alphanumeric:!0}},messages:{wmp_connect_apikey:{required:"This field is required."}},errorPlacement:function(e,t){var n=t[0].id.split("_"),r=n.length>1?n[n.length-1]:n[0],i=jQuery("#error_"+r+"_container",JSObject.DOMDoc);e.appendTo(i)},errorElement:"span"})},this.addButtonsActions=function(){jQuery(this.send_btn).unbind("click"),jQuery(this.send_btn).bind("click",function(){JSObject.disableButton(this),JSObject.validate()}),JSObject.enableButton(this.send_btn),jQuery("#"+JSObject.form.id,JSObject.DOMDoc).bind("keypress",function(e){return 13==e.keyCode?!1:void 0})},this.enableButton=function(e){jQuery(e).css("cursor","pointer"),jQuery(e).animate({opacity:1},100)},this.disableButton=function(e){jQuery(e).unbind("click"),jQuery(e).animate({opacity:.4},100),jQuery(e).css("cursor","default")},this.scrollToError=function(e){var t=jQuery("html,body",JSObject.DOMDoc),n=parseInt(jQuery("html,body").scrollTop())||parseInt(jQuery("body").scrollTop()),r=t.get(0).clientHeight;parseInt(t.offset().top);n>e?jQuery(t).animate({scrollTop:e-20},1e3):e>n+r&&jQuery(t).animate({scrollTop:n+r},1e3)},this.validate=function(){jQuery(this.form).validate().form();var e=[];for(var t in this.validator.invalid){var n=jQuery(this.form[t]);e.push(n.offset().top)}0==e.length?this.sendData():(e.sort(function(e,t){return e-t}),JSObject.scrollToError(e[0]),JSObject.addButtonsActions())},this.sendData=function(){WMPJSInterface.Preloader.start(),jQuery.post(ajaxurl,{action:"wmp_premium_save",api_key:jQuery("#"+JSObject.type+"_apikey",JSObject.DOMDoc).val()},function(response){jQuery.ajax({url:JSObject.submitURL,type:"get",data:{apiKey:jQuery("#"+JSObject.type+"_apikey",JSObject.DOMDoc).val(),settingsPath:jQuery("#"+JSObject.type+"_settings",JSObject.DOMDoc).val()},dataType:"jsonp",success:function(responseJSON){WMPJSInterface.Preloader.remove(100);var JSON=eval(responseJSON),status=Boolean(Number(String(JSON.status)));1==status?jQuery.post(ajaxurl,{action:"wmp_premium_connect",api_key:jQuery("#"+JSObject.type+"_apikey",JSObject.DOMDoc).val(),valid:1,config_path:JSON.config_path},function(e){var e=Boolean(Number(String(e)));if(1==e)window.location.href=JSObject.redirectTo;else{var t="We were unable to verify your API Key. Please contact support.";WMPJSInterface.Loader.display({message:t}),JSObject.form.reset(),setTimeout(function(){var e=JSObject.form.elements;for(nElems=e.length,j=0;j<nElems;j++)e[j].disabled=!1},300),JSObject.addButtonsActions()}}):(void 0!=JSON.message?WMPJSInterface.Loader.display({message:JSON.message}):WMPJSInterface.Loader.display({message:"We were unable to verify your API Key. Please contact support."}),JSObject.form.reset(),setTimeout(function(){var e=JSObject.form.elements;for(nElems=e.length,j=0;j<nElems;j++)e[j].disabled=!1},300),JSObject.addButtonsActions())},error:function(e){WMPJSInterface.Preloader.remove(100),WMPJSInterface.Loader.display({message:"Verification endpoint is unreachable. Please contact support."})}})})}}