var tdbCustomForms={};jQuery().ready(function(){tdbCustomForms.init()});
(function(){tdbCustomForms={items:[],item:function(){this.formType=this.blockObj=this.uid="";this.formData=new FormData;this.customPostTitleField=this.cloudTplID=this.successURL=this.currentUserID=this.formGroupClass="";this.enablePostCreate=!1;this.tpl_id=this._nonce=this.higher_msg=this.lower_msg=this.valid_email_msg=this.blank_fields_msg=this.required_field_error=this.emailSubject=this.sendEmailToEmailFromField=this.sendEmailToCustomAddr=this.sendEmailToAuthor=this.sendEmailToAdmin=this.enableEmailSubmit=
this.makeChild=this.linkToPostID=this.postStatus=this.postFormat=this.postType=this.postID="";this._is_initialized=this._in_composer=!1},init:function(){tdbCustomForms.items=[];this._is_running=!1},_initialize_item:function(a){!0!==a._is_initialized&&(a.blockObj.find(".tdb-s-btn").on("click",function(e){e.preventDefault();e=a.blockObj.find(".tdb-s-form");var c=jQuery(a.formGroupClass+".tdb-s-form-group"),d={},b=!1;a.formData=new FormData;c.removeClass("tdb-s-fg-error");c.find(".tdb-s-fg-error-msg").remove();
e.find(".tdb-s-notif").remove();c=tdbCustomForms.getFormContentElements(a);d["content-fields"]=c.elements;c.hasErrors&&(b=c.hasErrors);c=tdbCustomForms.getFormFileElements(a);d["files-info"]=c.filesInfo;d["file-delete-fields"]=c.deleteElements;c.hasErrors&&(b=c.hasErrors);c=tdbCustomForms.getFormGalleryElements(a);d.galleries=c.elements;c.hasErrors&&(b=c.hasErrors);c=tdbCustomForms.getFormACFElements(a);d["acf-fields"]=c.elements;"post"===a.formType&&""!==c.postTitle&&(d["post-title"]=c.postTitle);
c.hasErrors&&(b=c.hasErrors);if("post"===a.formType){c=tdbCustomForms.getFormTaxonomyElements(a);d.taxonomies=c.elements;c.hasErrors&&(b=c.hasErrors);c=tdbCustomForms.getFormLocationElement(a);d["location-data"]=c.element;c.hasErrors&&(b=c.hasErrors);c=tdbCustomForms.getFormTitleElement(a);"undefined"===typeof d["post-title"]&&(d["post-title"]=c.postTitle);c.hasErrors&&(b=c.hasErrors);c=tdbCustomForms.getLinkToPostElement(a);var f=c.linkToPostID;c.hasErrors&&(b=c.hasErrors);""!==f&&(a.linkToPostID=
f,a.makeChild=c.makeChild)}if(b)e.prepend('<div class="tdb-s-notif tdb-s-notif-sm tdb-s-notif-error"><div class="tdb-s-notif-descr">'+a.blank_fields_msg+"</div></div>");else if(a.formData.append("formElements",JSON.stringify(d)),a.formData.append("enctype","multipart/form-data"),a.formData.append("_nonce",a._nonce),"user"===a.formType)a.formData.append("action","tdb_user_form_on_submit"),tdbCustomForms.userFormAjaxCall(a);else if("post"===a.formType)if(a.formData.append("action","tdb_posts_form_on_submit"),
a.formData.append("enablePostCreate",a.enablePostCreate),a.formData.append("postID",a.postID),a.formData.append("authorID",a.currentUserID),a.formData.append("postType",a.postType),a.formData.append("postFormat",a.postFormat),a.formData.append("postStatus",a.postStatus),a.formData.append("cloudTplID",a.cloudTplID),a.formData.append("linkToPostID",a.linkToPostID),a.formData.append("makeChild",a.makeChild),a.formData.append("cfInputEmailList",a.cfInputEmailList),a.formData.append("emailList",a.emailList),
a.formData.append("enableEmailSubmit",a.enableEmailSubmit),a.formData.append("sendEmailToAdmin",a.sendEmailToAdmin),a.formData.append("sendEmailToAuthor",a.sendEmailToAuthor),a.formData.append("sendEmailToCustomAddr",a.sendEmailToCustomAddr),a.formData.append("sendEmailToEmailFromField",a.sendEmailToEmailFromField),a.formData.append("emailSubject",a.emailSubject),a.formData.append("tpl_id",a.tpl_id),e=a.blockObj.find("#g-recaptcha"),e.length){var g=e.attr("data-sitekey"),l="";grecaptcha.ready(function(){grecaptcha.execute(g,
{action:"submit"}).then(function(b){l=b;a.formData.append("captcha",l);tdbCustomForms.postFormAjaxCall(a)})})}else tdbCustomForms.postFormAjaxCall(a)}),a._is_initialized=!0)},getFormContentElements:function(a){var e=[],c=!1;jQuery(a.formGroupClass+".tdb_form_content").each(function(){var d=jQuery(this),b=d.data("required"),f=d.data("custom-field"),g=d.data("form-type"),l=d.find(".tdb-s-form-group"),h=d.find(".tdb-s-form-label").text().replace(" *",""),m=d.find("textarea");d=d.find(".wp-editor-wrap");
var k="";g===a.formType&&(k=d.hasClass("tmce-active")?tinymce.get(m.attr("id")).getContent():m.val(),e.filter(function(a){return a.name===f}).length||(1===b&&""===k?(l.addClass("tdb-s-fg-error"),l.append('<span class="tdb-s-fg-error-msg">'+a.required_field_error+"</span>"),c=!0):(b=encodeURIComponent(k).replace(/%([0-9A-F]{2})/g,function(a,b){return String.fromCharCode("0x"+b)}),h={name:f,value:btoa(b),label:h},e.push(h))))});return{elements:e,hasErrors:c}},getFormFileElements:function(a){var e=[],
c=[],d=!1;jQuery(a.formGroupClass+".tdb_form_file_upload").each(function(){var b=jQuery(this),f=b.data("form-type"),g=b.data("required"),l=b.data("no-save"),h=b.data("custom-field"),m=b.find(".tdb-s-form-label").text().replace(" *",""),k=b.find(".tdb-s-form-group");b=b.find("input");var n=b.val();f===a.formType&&""!==h&&!1===l&&(1===g&&""===n?(k.addClass("tdb-s-fg-error"),k.append('<span class="tdb-s-fg-error-msg">'+a.required_field_error+"</span>"),d=!0):(a.formData.append(h,b.prop("files")[0]),
e.push({name:h,label:m}),""===n&&c.push(h)))});return{filesInfo:e,deleteElements:c,hasErrors:d}},getFormGalleryElements:function(a){var e=[],c=!1;jQuery(a.formGroupClass+".tdb_form_gallery").each(function(){var d=jQuery(this),b=d.data("form-type"),f=d.data("custom-field"),g=d.find(".tdb-s-form-label").text().replace(" *","");if(b===a.formType&&!e.filter(function(a){return a.name===f}).length){var l=d.data("required"),h=d.find(".tdb-s-form-group");b=d.find(".tdb-fg-item:not(.tdb-fg-item-add)").filter(function(){return""!=
jQuery(this).attr("data-img-id")});d=d.find(".tdb-fg-input");1!==l||d.length||b.length?(d.length&&d.each(function(b){var c=jQuery(this);a.formData.append("tdb_gallery_{"+f+"}_"+b,c.prop("files")[0])}),d=[],b.length&&(d=jQuery.map(b,function(a,b){return jQuery(a).data("image-id")})),e.push({source:f,label:g,existingImagesIDs:d})):(h.addClass("tdb-s-fg-error"),h.append('<span class="tdb-s-fg-error-msg">'+a.required_field_error+"</span>"),c=!0)}});return{elements:e,hasErrors:c}},getFormACFElements:function(a){var e=
[],c="",d=!1;jQuery(a.formGroupClass+".tdb-posts-form-acf-input").each(function(){var b=jQuery(this),f=b.data("form-type"),g=b.data("type"),l=b.data("required"),h=b.data("name"),m=b.find(".tdb-s-form-label").text().replace(" *",""),k=void 0;if(f===a.formType&&!e.filter(function(a){return a.name===h}).length)switch(g){case "text":case "textarea":case "url":case "select":k=b.find(".tdb-s-form-input");var n=k.val();1===l&&""===k.val()?(b.addClass("tdb-s-fg-error"),b.append('<span class="tdb-s-fg-error-msg">'+
a.required_field_error+"</span>"),d=!0):(m={name:h,value:n,label:m,type:g},"select"===g?(jQuery.isArray(n)?(n.forEach(function(a,b){n[b]={value:a,label:k.find('option[value="'+a+'"]').text()}}),m.type=g+"_multiple"):(n={value:n,label:k.find('option[value="'+n+'"]').text()},"post"===a.formType&&h===a.customPostTitleField&&(c=n)),m.value=n):("textarea"===g&&(m.value=encodeURI(n)),"post"===a.formType&&h===a.customPostTitleField&&(c=n)),e.push(m));break;case "date_picker":case "date_time_picker":case "time_picker":k=
b.find(".tdb-s-form-input.flatpickr-input");1===l&&""===k.val()?(b.addClass("tdb-s-fg-error"),b.append('<span class="tdb-s-fg-error-msg">'+a.required_field_error+"</span>"),d=!0):(g={name:h,value:k.val(),label:m,type:g},e.push(g),"post"===a.formType&&h===a.customPostTitleField&&(c=k.val()));break;case "email":f=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;k=b.find(".tdb-s-form-input");1===l&&""===k.val()?(b.addClass("tdb-s-fg-error"),b.append('<span class="tdb-s-fg-error-msg">'+a.required_field_error+
"</span>"),d=!0):k.val().match(f)||""===k.val()?(g={name:h,value:k.val(),label:m,type:g},e.push(g),"post"===a.formType&&h===a.customPostTitleField&&(c=k.val())):(b.addClass("tdb-s-fg-error"),b.append('<span class="tdb-s-fg-error-msg">'+a.valid_email_msg+"</span>"),d=!0);break;case "number":k=b.find(".tdb-s-form-input");f=k.attr("min");var p=k.attr("max");1===l&&""===k.val()?(b.addClass("tdb-s-fg-error"),b.append('<span class="tdb-s-fg-error-msg">'+a.required_field_error+"</span>"),d=!0):k.val()<f&&
""!==k.val()?(b.addClass("tdb-s-fg-error"),b.append('<span class="tdb-s-fg-error-msg">'+a.lower_msg+f+".</span>"),d=!0):k.val()>p&&""!==k.val()?(b.addClass("tdb-s-fg-error"),b.append('<span class="tdb-s-fg-error-msg">'+a.higher_msg+p+".</span>"),d=!0):(g={name:h,value:k.val(),label:m,type:g},e.push(g),"post"===a.formType&&h===a.customPostTitleField&&(c=k.val()));break;case "checkbox":if(1!==l||b.find("input:checked").length){var q=[];b.find("input:checked").each(function(){var a=jQuery(this);q.push({value:a.val(),
label:a.siblings(".tdb-fi-check-label").text()})});e.push({name:h,value:q,label:m,type:g})}else b.addClass("tdb-s-fg-error"),b.append('<span class="tdb-s-fg-error-msg">'+a.required_field_error+"</span>"),d=!0;break;case "radio":case "button_group":1!==l||b.find("input:checked").length?(b=b.find("input:checked"),l={value:b.val(),label:b.siblings(".tdb-fi-check-label").text()},e.push({name:h,value:l,label:m,type:g}),"post"===a.formType&&h===a.customPostTitleField&&(c=b.siblings(".tdb-fi-check-label").text())):
(b.addClass("tdb-s-fg-error"),b.append('<span class="tdb-s-fg-error-msg">'+a.required_field_error+"</span>"),d=!0)}});return{elements:e,postTitle:c,hasErrors:d}},getFormTaxonomyElements:function(a){var e=[],c=!1;jQuery(a.formGroupClass+".tdb_form_taxonomies").each(function(){var d=jQuery(this),b=d.data("tax-type"),f=d.data("required"),g=d.find('[data-term-hierarchy="parent"]'),l=g.data("display");d=d.find('[data-term-hierarchy="child"], [data-term-hierarchy="sub-child"]');var h=[];e.filter(function(a){return a.taxType===
b}).length||("dropdown"===l?(l=g.find("select"),1===f&&""===l.val()?(g.addClass("tdb-s-fg-error"),g.append('<span class="tdb-s-fg-error-msg">'+a.required_field_error+"</span>"),c=!0):""!==l.val()&&h.push(parseInt(l.val()))):(l=g.find("input:checked"),1!==f||l.length?l.each(function(){h.push(parseInt(jQuery(this).val()))}):(g.addClass("tdb-s-fg-error"),g.append('<span class="tdb-s-fg-error-msg">'+a.required_field_error+"</span>"),c=!0)),d.each(function(){var a=jQuery(this);"dropdown"===a.data("display")?
(a=a.find("select"),""!==a.val()&&h.push(parseInt(a.val()))):a.find("input:checked").each(function(){h.push(parseInt(jQuery(this).val()))})}),h.length&&e.push({terms:h,taxType:b}))});return{elements:e,hasErrors:c}},getFormLocationElement:function(a){var e=jQuery(a.formGroupClass+".tdb_location_finder").first(),c={},d=!1;if(e.length){var b=e.data("tax-type"),f=e.data("location-meta"),g=e.data("required"),l=e.find(".tdb-s-form-group-lf-search input"),h=l.closest(".tdb-s-form-group"),m=e.find(".tdb-s-form-group-lf-address input"),
k=e.find(".tdb-s-form-group-lf-postal-code input"),n=e.find(".tdb-s-form-group-lf-city input"),p=e.find(".tdb-s-form-group-lf-state input");e=e.find(".tdb-s-form-group-lf-country input");1===g&&""===l.val()?(h.addClass("tdb-s-fg-error"),h.append('<span class="tdb-s-fg-error-msg">'+a.required_field_error+"</span>"),d=!0):""!==n.val()&&""!==p.val()&&""!==e.val()&&(c={address:m.val(),postalCode:k.val(),city:n.val(),state:p.val(),country:e.val(),taxType:b,locationMeta:f})}return{element:c,hasErrors:d}},
getFormTitleElement:function(a){var e=jQuery(a.formGroupClass+".tdb_form_title input").first(),c="",d=!1;if(e.length){e=e.first();var b=e.closest(".tdb-s-form-group");""===e.val()?(b.addClass("tdb-s-fg-error"),b.append('<span class="tdb-s-fg-error-msg">'+a.required_field_error+"</span>"),d=!0):c=e.val()}return{postTitle:c,hasErrors:d}},getLinkToPostElement:function(a){var e=jQuery(a.formGroupClass+".tdb_form_link_post").first(),c="",d=!1,b=!1;if(e.length){var f=e.find("select"),g=e.find(".tdb-s-form-group");
1===e.data("required")&&""===f.val()?(g.addClass("tdb-s-fg-error"),g.append('<span class="tdb-s-fg-error-msg">'+a.required_field_error+"</span>"),b=!0):(c=f.val(),d=e.data("make-child"))}return{linkToPostID:c,makeChild:d,hasErrors:b}},userFormAjaxCall:function(a){var e=a.blockObj.find(".tdb-s-form"),c=jQuery(a.formGroupClass+".tdb-s-form-group"),d=a.blockObj.find(".tdb-s-btn");c.addClass("tdb-s-content-disabled");e.addClass("tdb-s-content-disabled");d.addClass("tdb-s-btn-saving");jQuery.ajax({method:"POST",
url:td_ajax_url,data:a.formData,contentType:!1,processData:!1,success:function(b){b=jQuery.parseJSON(b);""!==b.success&&e.prepend('<div class="tdb-s-notif tdb-s-notif-sm tdb-s-notif-success"><div class="tdb-s-notif-descr">'+b.success+"</div></div>");if(b.errors.length){var f='<div class="tdb-s-notif tdb-s-notif-sm tdb-s-notif-error">';f+='<ul class="tdb-s-notif-list">';jQuery.each(b.errors,function(a,b){f+="<li>"+b+"</li>"});f+="</ul>";f+="</div>";e.prepend(f)}d.addClass("tdb-s-btn-saved");e.removeClass("tdb-s-content-disabled");
setTimeout(function(){c.removeClass("tdb-s-content-disabled");d.removeClass("tdb-s-btn-saving").removeClass("tdb-s-btn-saved");setTimeout(function(){a.blockObj.find(".tdb-s-notif-success").remove();a.blockObj.find(".tdb-s-notif-error").remove()},4E3)},200)}})},postFormAjaxCall:function(a){var e=a.blockObj.find(".tdb-s-form"),c=jQuery(a.formGroupClass+".tdb-s-form-group"),d=a.blockObj.find(".tdb-s-btn");c.addClass("tdb-s-content-disabled");e.addClass("tdb-s-content-disabled");d.addClass("tdb-s-btn-saving");
jQuery.ajax({method:"POST",url:td_ajax_url,data:a.formData,contentType:!1,processData:!1,success:function(b){b=jQuery.parseJSON(b);if(""!==a.successURL&&""!==b.success.create_post&&""===a.postID)window.location.replace(a.successURL);else{if(""!==b.success.create_post||""!==b.success.email){var f='<div class="tdb-s-notif tdb-s-notif-sm tdb-s-notif-success"><ul class="tdb-s-notif-list">';""!==b.success.create_post&&(f+="<li>"+b.success.create_post+"</li>");""!==b.success.email&&(f+="<li>"+b.success.email+
"</li>");e.prepend(f+"</ul></div>")}if(""!==b.errors.create_post||""!==b.errors.email||""!==b.errors.permission)f='<div class="tdb-s-notif tdb-s-notif-sm tdb-s-notif-error"><ul class="tdb-s-notif-list">',""!==b.errors.create_post&&(f+="<li>"+b.errors.create_post+"</li>"),""!==b.errors.email&&(f+="<li>"+b.errors.email+"</li>"),""!==b.errors.permission&&(f+="<li>"+b.errors.permission+"</li>"),e.prepend(f+"</ul></div>");d.addClass("tdb-s-btn-saved");e.removeClass("tdb-s-content-disabled");""!==b.success.create_post&&
""===a.postID?setTimeout(function(){window.location.replace(window.location.pathname+window.location.search+window.location.hash)},1E3):setTimeout(function(){c.removeClass("tdb-s-content-disabled");d.removeClass("tdb-s-btn-saving").removeClass("tdb-s-btn-saved");setTimeout(function(){a.blockObj.find(".tdb-s-notif-success").remove();a.blockObj.find(".tdb-s-notif-error").remove()},4E3)},200)}}})},addItem:function(a){if("undefined"===typeof a.uid)throw"item.uid is not defined";tdbCustomForms.items.push(a);
tdbCustomForms._initialize_item(a)},deleteItem:function(a){for(var e=0;e<tdbCustomForms.items.length;e++)if(tdbCustomForms.items[e].uid===a)return tdbCustomForms.items.splice(e,1),!0;return!1},resetItems:function(){tdbCustomForms.items=[]}}})();
