summaryrefslogtreecommitdiff
blob: 2d030ac92509ad83b768b61dea36cbb8f71edbaf (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(a,b){var c=b[0].id.split("_"),d=c.length>1?c[c.length-1]:c[0],e=jQuery("#error_"+d+"_container",JSObject.DOMDoc);a.appendTo(e)},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(a){if(13==a.keyCode)return!1})},this.enableButton=function(a){jQuery(a).css("cursor","pointer"),jQuery(a).animate({opacity:1},100)},this.disableButton=function(a){jQuery(a).unbind("click"),jQuery(a).animate({opacity:.4},100),jQuery(a).css("cursor","default")},this.scrollToError=function(a){var b=jQuery("html,body",JSObject.DOMDoc),c=parseInt(jQuery("html,body").scrollTop())||parseInt(jQuery("body").scrollTop()),d=b.get(0).clientHeight;parseInt(b.offset().top);a<c?jQuery(b).animate({scrollTop:a-20},1e3):a>c+d&&jQuery(b).animate({scrollTop:c+d},1e3)},this.validate=function(){jQuery(this.form).validate().form();var a=[];for(var b in this.validator.invalid){var c=jQuery(this.form[b]);a.push(c.offset().top)}0==a.length?this.sendData():(a.sort(function(a,b){return a-b}),JSObject.scrollToError(a[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()},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(a){var a=Boolean(Number(String(a)));if(1==a)window.location.href=JSObject.redirectTo;else{WMPJSInterface.Loader.display({message:"We were unable to verify your API Key. Please contact support."}),JSObject.form.reset(),setTimeout(function(){var a=JSObject.form.elements;for(nElems=a.length,j=0;j<nElems;j++)a[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 a=JSObject.form.elements;for(nElems=a.length,j=0;j<nElems;j++)a[j].disabled=!1},300),JSObject.addButtonsActions())},error:function(a){WMPJSInterface.Preloader.remove(100),WMPJSInterface.Loader.display({message:"Verification endpoint is unreachable. Please contact support."})}})})}}