text: /*
   2017-2021 (c) Sahana Software Foundation
   @license MIT
  
   requires jQuery 1.9.1+
   requires jQuery UI 1.10 widget factory
  */
  (function(e,q){var u=function(a,b){var c=function(k,l){return l?encodeURIComponent(k)+"="+encodeURIComponent(l):""};a=a.split("?");a=[a[0]].concat((a[1]||"").split("#"));var d=a[1],f,h={},g;d=d?d.split("&").map(function(k){f=decodeURIComponent(k.split("=")[0]);return b.hasOwnProperty(f)?(h[f]=!0,c(f,b[f])):k}).filter(function(k){return k}):[];for(f in b)h[f]||(g=c(f,b[f]))&&d.push(g);g=a[0];d.length&&(g+="?"+d.join("&"));a[2]&&(g+="#"+a[2]);return g};S3.addPersonWidgetReady=function(a){var b=new jQuery.Deferred,
  c=e("#"+a);if(c.data("lookup_contact")!==q)b.resolve(c);else c.on("addPersonReady",function(){b.resolve(c)});return b.promise()};var t=0;e.widget("s3.addPerson",{options:{lookupDuplicates:!1,separateNameFields:!1,trigger:"full_name",c:"pr",f:"person",editableFields:[],tags:[],chars:2,delay:800,downIcon:"fa fa-caret-down",yesIcon:"fa fa-check",noIcon:"fa fa-remove"},_create:function(){this.id=t;t+=1;this.eventNamespace=".addPerson"},_init:function(){var a=e(this.element),b=this.options;this.fieldName=
  a=a.attr("id");this.selector="#"+a;this.data={};this.edited=!1;a=e(this.selector+"__row").hide();a.hasClass("form-row")||a.hasClass("control-group")?this.formStyle="div":this.formStyle="table";this.topFields=["organisation_id","pe_label"];a=e(this.selector+"_pe_label");a.length&&(this.idLabel=a);a=b.trigger;if(b.separateNameFields)if("first_name"==a)var c=["first_name","middle_name","last_name"];else a="last_name",c=["last_name","middle_name","first_name"];else a="full_name",c=["full_name"];b.trigger=
  a;this.triggerRow=e(this._rowSelector(a));this.trigger=e(this.selector+"_"+a);this.nameFields=c;var d="father_name grandfather_name year_of_birth date_of_birth gender occupation mobile_phone home_phone email".split(" ");b.tags.forEach(function(f){-1==d.indexOf(f)&&d.push(f)});this.personFields=d;this._arrangeFormRows();this.refresh()},_destroy:function(){e.Widget.prototype.destroy.call(this)},refresh:function(){var a=e(this.element),b=this.selector,c=this.options;this._unbindEvents();this.pendingAC=
  !1;var d=this.throbber;d&&d.remove();d=e('<div class="throbber input_throbber"></div>');d.hide().insertAfter(this.trigger);this.throbber=d;var f=this.idLabel;f&&(d=e('<div class="throbber input_throbber"></div>'),d.hide().insertAfter(f),this.idLabelThrobber=d);(d=a.val())?(isNaN(d-0)||(this.data={id:d},this._serialize()),this.data.id?(this._disableInputs(),this._toggleActions({undo:!1})):(this._enableAutocomplete(),this._toggleActions({edit:!1,clear:!1}))):(this._serialize(),this._enableAutocomplete(),
  this._toggleActions({edit:!1,clear:!1}));e(b+"_edit_bar").removeClass("hide").show();c.lookupDuplicates&&(c=this.fieldName+"_duplicates",e("#"+c).length||(c='<div id="'+c+'" class="req"></div>',"div"==this.formStyle?e(b+"_box_bottom").before(c):e(b+"_box_bottom1").before(c)));this._bindEvents();a.trigger("addPersonReady")},_arrangeFormRows:function(){var a=e(this.element),b=this.selector,c=e(b+"__row").hide();if("div"==this.formStyle){e(b+"_box_bottom").removeClass("hide").show().insertAfter(c);var d=
  this,f=this.topFields.concat(this.nameFields).concat(this.personFields).reverse();f.forEach(function(h){c.after(e(d._rowSelector(h)))});e(f.map(this._rowSelector,this).join(",")).removeClass("hide").show()}else e(b+"__row1").hide();b=e(b+"_title__row");a=a.next(".error_wrapper");c.after(a).after(b.removeClass("hide").show())},_allFields:function(){return this.topFields.concat(this.nameFields).concat(this.personFields)},_fieldMap:function(){var a={pe_label:"pe_label",email:"email",mobile_phone:"mphone",
  home_phone:"hphone",gender:"sex",date_of_birth:"dob",year_of_birth:"year_of_birth",father_name:"father_name",grandfather_name:"grandfather_name",occupation:"occupation",organisation_id:"org_id"};this.options.separateNameFields&&(a.first_name="first_name",a.middle_name="middle_name",a.last_name="last_name");this.options.tags.forEach(function(b){a.hasOwnProperty(b)||(a[b]=b)});return a},_rowSelector:function(a){return this.selector+"_"+a+"__row"},_readInputs:function(){var a=this.selector,b={},c;this._allFields().forEach(function(d){c=
  e(a+"_"+d);c.length&&(b[d]=c.val())});this.data=b},_updateInputs:function(){var a=this.selector,b=this.data,c;this._allFields().forEach(function(d){c=e(a+"_"+d);c.length&&b.hasOwnProperty(d)&&c.val(b[d])})},_serialize:function(){e(this.element).val(JSON.stringify(this.data))},_deserialize:function(){var a=e(this.element).val(),b=a-0;this.data=isNaN(b)?JSON.parse(a):{id:b}},_toggleActions:function(a){var b={edit:".edit-action",clear:".clear-action",undo:".undo-action"},c=this.selector,d;for(d in a){var f=
  e(c+"_edit_bar "+b[d]);a[d]?f.removeClass("hide").show():f.hide()}},_toggleDatePickerTrigger:function(a){var b=e(this.selector+"_date_of_birth__row .calendar-clear-btn"),c=e(this.selector+"_date_of_birth__row .ui-datepicker-trigger");a?(c.removeClass("hide").show(),b.removeClass("hide").show()):(c.hide(),b.hide())},_enableInputs:function(a){var b=this.selector;(a?this.options.editableFields:this._allFields()).forEach(function(c){e(b+"_"+c).prop("disabled",!1)});this._toggleDatePickerTrigger(!0)},
  _disableInputs:function(){var a=this.selector;this._allFields().forEach(function(b){e(a+"_"+b).prop("disabled",!0)});this._toggleDatePickerTrigger(!1);this._toggleActions({edit:!0,clear:!0,undo:!1})},_reset:function(a){var b=this.topFields.concat(this.personFields);a||(b=b.concat(this.nameFields));var c=this.selector;b.forEach(function(d){e(c+"_"+d).prop("disabled",!1).val("")});this._toggleDatePickerTrigger(!0);a?this._readInputs():this.data={};this._serialize()},_clear:function(){this.previousSelection=
  e.extend({},this.data);this._reset();this._enableAutocomplete();this._toggleActions({edit:!1,clear:!1,undo:!0})},_edit:function(){this.previousSelection=e.extend({},this.data);this._enableInputs(!0);this.trigger.autocomplete("destroy");this.edited=!0;this._toggleActions({edit:!1,clear:!1,undo:!0})},_undo:function(){var a=this.previousSelection;this.pendingAC&&(this.pendingAC.abort(),this.pendingAC=!1,this.throbber.hide());this.edited=!1;a&&Object.values(a).length?(this.data=e.extend({},this.previousSelection),
  this._serialize(),this._updateInputs(),this._disableInputs()):this._reset();this._clearDuplicates()},_clearError:function(){e(this.selector+"_title__row").next(".error_wrapper").fadeOut("slow",function(){S3.hideAlerts()})},_fullName:function(a){return a.label!==q?a.label:a.name},_autocompleteLabel:function(a){if(a.label!==q)return a.label;var b=a.name,c=[a.job,a.org].filter(function(d){return d});c.length&&(b+=" ("+c.join(",")+")");this.idLabel&&(a=a.pe_label)&&(b+=' <span class="pe-label">['+a+"]</span> ");
  return b},_enableAutocomplete:function(){var a=this.trigger;if(!a.attr("autocomplete")){var b=this.options,c=this;this.acURL=S3.Ap.concat("/"+b.c+"/"+b.f+"/search_ac");this.idLabel&&(this.acURL+="?label=1");var d=a.autocomplete({delay:b.delay,minLength:b.chars,source:function(f,h){c.pendingAC&&c.pendingAC.abort();c.pendingAC=e.ajax({url:c.acURL,data:{term:f.term}}).done(function(g){0===g.length?delete c.data.id:g.push({id:0,value:"",label:i18n.none_of_the_above});h(g)})},search:function(){c.throbber.show();
  return!0},response:function(f,h,g){c.throbber.hide();return g},focus:function(){return!1},select:function(f,h){f=h.item;f.id?(b.separateNameFields||a.val(c._fullName(f)),c._selectPerson(f.id)):delete c.data.id;return!1}}).data("ui-autocomplete");d&&(d._renderItem=function(f,h){var g=c._autocompleteLabel(h);return e("<li>").data("item.autocomplete",h).append("<a>"+g+"</a>").appendTo(f)})}},_selectPerson:function(a){var b=this.options,c=this.selector,d=b.trigger,f;this.nameFields.forEach(function(l){f=
  e(c+"_"+l).prop("disabled",!1);l!=d&&f.val("")});this._reset(!0);var h=this,g=this.trigger,k=S3.Ap.concat("/"+b.c+"/"+b.f+"/"+a+"/lookup.json");this.idLabel&&(k+="?label=1");this.throbber.show();g.val()||g.attr("placeholder",i18n.loading+"...");e.getJSONS3(k,function(l){try{h.data.id=a,b.separateNameFields||(h.data.full_name=g.val()),h._processReponse(l)}catch(r){h._reset(!0)}h.throbber.hide();g.removeAttr("placeholder")})},_processReponse:function(a){var b=this._fieldMap(),c=this.data,d;for(d in b){var f=
  b[d]||d;a.hasOwnProperty(f)&&(c[d]=a[f])}this._serialize();this.previousSelection=e.extend({},this.data);this._updateInputs();this._disableInputs()},lookupIDLabel:function(){var a=this.idLabel;if(a){var b=a.val();if(b){var c=a.data("lookup");c&&(c.abort(),a.data("lookup",null));var d=this.options;c="";d.separateNameFields||(c="&name=1");var f=this,h=this.idLabelThrobber.show();b=S3.Ap.concat("/"+d.c+"/"+d.f+"/lookup.json?search=1&label=1"+c+"&~.pe_label="+b);c=e.getJSONS3(b,function(g){g.pe_label&&
  (f.data.id=g.id,d.separateNameFields||(f.data.full_name=g.name),f._processReponse(g));a.data("lookup",null);h.hide()});a.data("lookup",c)}}},lookupContact:function(a){this._reset();var b=this.options,c=this.trigger,d=this;a=S3.Ap.concat("/org/site/"+a+"/site_contact_person");this.throbber.show();c.val()||c.attr("placeholder",i18n.loading+"...");e.getJSONS3(a,function(f){try{d.data.id=f.id;if(!b.separateNameFields){var h=f.name;c.val(h);d.data.full_name=h}d._processReponse(f)}catch(g){d._reset()}d.throbber.hide();
  c.removeAttr("placeholder")})},_checkDuplicates:function(){var a=this.options,b=this._fieldMap(),c=this.selector,d=this.data,f={};if(!d.id){if(a.separateNameFields){if(!d.first_name)return;this.nameFields.forEach(function(k){(h=d[k])&&(f[b[k]||k]=h)})}else{var h=d.full_name;if(!h)return;f.name=h}this.personFields.forEach(function(k){(h=d[k])&&(f[b[k]||k]=h)});var g=S3.Ap.concat("/pr/person/check_duplicates");e.ajaxS3({url:g,data:f,type:"POST",dataType:"json",success:function(k){e(c+"_results").remove();
  if(k.length){var l=i18n.dupes_found.replace("_NUM_",k.length)+' <i class="'+a.downIcon+'"> </i>';e(c+"_duplicates").html(l).data("results",k).show()}else e(c+"_duplicates").data("results",[]).hide()}})}},_displayDuplicates:function(){var a=this.options,b=this.selector;if(!e(b+"_results").length){var c=function(m,n){var p=e("<div>").addClass("card_1_line");n&&p.append(e("<label>").html(n));return p.append(m)},d=function(m,n){var p=e('<button type="button" class="fright">');n&&p.append(e('<i class="'+
  n+'">'));return p.append(m)};b=e(b+"_duplicates");var f=b.data("results"),h=e('<div id="'+this.fieldName+'_results">'),g,k,l,r;f.forEach(function(m){k=m.name;g=e('<div class="dl-item">').data({id:m.id,name:k});l=e('<img width="50" height="50" class="media-object">');r=m.image?"/default/download/"+m.image:"/static/img/blank-user.gif";l.attr("src",S3.Ap.concat(r));g.append(e('<a class="fleft">').append(l));m.org&&g.append(c(m.org));g.append(c(k));m.father_name&&i18n.father_name_label&&g.append(c(m.father_name,
  i18n.father_name_label));m.grandfather_name&&i18n.grandfather_name_label&&g.append(c(m.grandfather_name,i18n.grandfather_name_label));["year_of_birth","dob","email","phone"].forEach(function(n){(n=m[n])&&g.append(c(n))});g.append(d(i18n.Yes,a.yesIcon));h.append(g)});f=e('<div class="dl-item">').append(d(i18n.No,a.noIcon));h.append(f);b.after(h);e("html,body").animate({scrollTop:b.offset().top},250)}},_selectDuplicate:function(a,b){var c=this.selector,d=e(c+"_duplicates");a&&b?(e(c+"_full_name").val(b),
  this._clearDuplicates(),d.data("submit")?(this.data={id:a},this._serialize(),d.closest("form").off(this.eventNamespace).submit()):(this.options.separateNameFields&&this.trigger.val(""),this._selectPerson(a))):(this._clearDuplicates(),d.data("submit")&&d.closest("form").off(this.eventNamespace).submit())},_clearDuplicates:function(){var a=this.selector;e(a+"_duplicates").data("results",[]).empty();e(a+"_results").remove()},_onFormSubmit:function(a){var b=this.selector;if(this.options.lookupDuplicates&&
  (b=e(b+"_duplicates"),b.data("results").length))return b.data("submit",!0),this._displayDuplicates(),!1;if(this.edited){var c=this;b=this.data.id;this._readInputs();this.data.id=b;this.nameFields.forEach(function(d){delete c.data[d]})}else this.data.id?this.data={id:this.data.id}:this._readInputs();this._serialize();e(a).off(this.eventNamespace).submit();return!0},_bindEvents:function(){var a=e(this.element),b=this.options,c=this.selector,d=this.eventNamespace,f=this;a.closest("form").on("submit"+
  d,function(g){g.preventDefault();return f._onFormSubmit(this)});e(c+"_edit_bar .edit-action").on("click"+d,function(){f._clearError();f._edit()});e(c+"_edit_bar .clear-action").on("click"+d,function(){f._clearError();f._clear()});e(c+"_edit_bar .undo-action").on("click"+d,function(){f._clearError();f._undo()});e(c+"_organisation_id").change(function(){f.acURL=u(f.acURL,{"~.organisation_id":e(this).val()})});e(c+"_title__row").next(".error_wrapper").one("click"+d,function(){f._clearError();return!1});
  var h=this.nameFields.concat(this.personFields);e(h.map(function(g){return c+"_"+g}).join(",")).on("change"+d,function(){f.data.id||(f._readInputs(),f._serialize(),b.lookupDuplicates&&f._checkDuplicates())});b.lookupDuplicates&&(e(c+"_duplicates").data("results",[]).on("click"+d,function(){f._displayDuplicates()}),a.closest("form").on("click"+d,c+"_results .dl-item",function(){var g=e(this),k=g.data("id");g=g.data("name");f._selectDuplicate(k,g)}),a.on("change"+d,function(){a.val()&&f._clearDuplicates()}));
  if(h=this.idLabel)h.on("input"+d,function(){var g=e(this),k=g.data("lookupTimeout");k&&clearTimeout(k);k=setTimeout(function(){f.lookupIDLabel()},b.delay);g.data("lookupTimeout",k)}),h.on("blur"+this.eventNamespace,function(){var g=e(this),k=g.data("lookup"),l=g.data("lookupTimeout");k&&(k.abort(),g.data("lookup",null),f.idLabelThrobber.hide());l&&clearTimeout(l)});return!0},_unbindEvents:function(){var a=e(this.element),b=this.selector,c=this.eventNamespace;a.off(c);a.closest("form").off(c);e(b+
  "_edit_bar .edit-action").off(c);e(b+"_edit_bar .clear-action").off(c);e(b+"_edit_bar .undo-action").off(c);e(b+"_duplicates").off(c);a=this.nameFields.concat(this.personFields);e(a.map(function(d){return b+"_"+d}).join(",")).off(c);(a=this.idLabel)&&a.off(c);return!0}})})(jQuery);
  