function isAuthenticated(n){return n.getAllResponseHeaders().indexOf('"status":401')===-1}function updateYearShares(n,t){$.post(siteBase+"/Projects/UpdateYearShares/",{id:n},function(n,i,r){isAuthenticated(r)?$("#divPojectYears").load(updateYearsUrl,function(){WireUpUpdateProjYears();$("#yearTimingsValidation").html(t)}):alert("not authenticated")}).fail(function(n,t,i){alert("fail"+n+"|"+t+"|"+i.getAllResponseHeaders())}).error(function(n,t,i){alert("error"+n+"|"+t+"|"+i.getAllResponseHeaders())})}function updateYearShare(n,t,i){return $.post(siteBase+"/Projects/UpdateYearShare/",{id:n,yearId:t,percentage:i},function(t,i,r){if(isAuthenticated(r)){var u=jQuery.parseJSON(t);$("#yearTimingsValidation").html("");u.result=="Success"?updateYearShares(n,""):u.result=="Warning"?updateYearShares(n,u.error):u.result=="Error"&&$("#yearTimingsValidation").html(u.error)}else alert("not authenticated")}).fail(function(n,t,i){alert("fail"+n+"|"+t+"|"+i.getAllResponseHeaders())}).error(function(n,t,i){alert("error"+n+"|"+t+"|"+i.getAllResponseHeaders())}),!1}function WireUpUpdateProjYears(){$("a.updateProjYears").click(function(){var n=$("#StartingDcpYearId").val(),t=$("#EndingDcpYearId").val();return $.post(siteBase+"/Projects/UpdateYears/",{id:projectId,startYearId:n,endYearId:t},function(n,t,i){isAuthenticated(i)?$("#divPojectYears").load(updateYearsUrl,function(){WireUpUpdateProjYears()}):alert("not authenticated")}).fail(function(n,t,i){alert("fail"+n+"|"+t+"|"+i.getAllResponseHeaders())}).error(function(n,t,i){alert("error"+n+"|"+t+"|"+i.getAllResponseHeaders())}),!1});$("a.shareProjYears").click(function(){return updateYearShares(projectId,""),!1});$(".pyAutoCb").click(function(){var n=$(this).parent().prev();$(this).is(":checked")?(n.addClass("disabled"),n.prop("readonly",!0),updateYearShare(projectId,n.prop("id").substring(5),-1)):(n.removeClass("disabled"),n.prop("readonly",!1),updateYearShare(projectId,n.prop("id").substring(5),n.val()))});$(".pyAutoText").change(function(){var n=$(this).val().replace(/^\s\s*/,"").replace(/\s\s*$/,"");/^\d+(\.\d+)?$/.test(n)?n>100?alert("Value must be less than or equal to 100"):updateYearShare(projectId,$(this).prop("id").substring(5),$(this).val()):alert("Value must be a number between 0 and 100")});$("div.chargeArea").click(function(){$(this).hasClass("active")?($(this).removeClass("active"),$(this).addClass("inactive")):($(this).removeClass("inactive"),$(this).addClass("active"));$.post(siteBase+"/Projects/ToggleChargeAreaById/",{id:projectId,dcpId:dcpId,chargeAreaId:$(this).data("chargeareaid")},function(n,t,i){var u;if(isAuthenticated(i)){if(u=jQuery.parseJSON(n),u.result==="Success"){var r=jQuery("#caMap").attr("src"),f="&ts="+Math.random(),e=r.indexOf("&ts=");e<0?r+=f:r=r.substring(0,e)+f;jQuery("#caMap").attr("src",r)}}else alert("not authenticated")}).fail(function(n,t,i){alert("fail"+n+"|"+t+"|"+i.getAllResponseHeaders())}).error(function(n,t,i){alert("error"+n+"|"+t+"|"+i.getAllResponseHeaders())})});$("#caMap").click(function(n){var i=$(this).width(),r=$(this).height(),t=$(this).offset(),u=n.pageX-t.left,f=n.pageY-t.top;$.post(siteBase+"/Projects/ToggleChargeArea/",{id:projectId,width:i,height:r,x:u,y:f},function(n,t,i){var u;if(isAuthenticated(i)){var f=jQuery.parseJSON(n),r=jQuery("#caMap").attr("src"),e="&ts="+Math.random(),o=r.indexOf("&ts=");o<0?r+=e:r=r.substring(0,o)+e;jQuery("#caMap").attr("src",r);u=$('[data-chargeareaid="'+f.values.caId+'"]');f.values.state==="1"?(u.removeClass("inactive"),u.addClass("active")):(u.removeClass("active"),u.addClass("inactive"))}else alert("not authenticated")}).fail(function(n,t,i){alert("fail"+n+"|"+t+"|"+i.getAllResponseHeaders())}).error(function(n,t,i){alert("error"+n+"|"+t+"|"+i.getAllResponseHeaders())})})}function LoadAssignedUsers(){$("#divAssignedUsers").load(assignedUsersUrl,function(){WireupOrgUserAdmin()})}function WireupOrgUserAdmin(){$("a.assignUser").click(function(){return $("#Email").closest("form").valid()&&$.post(siteBase+"/Organisations/AssignUser/",{id:orgId,email:$("#Email").val()},function(n,t,i){var u,r,f;isAuthenticated(i)?(u=jQuery.parseJSON(n),u.result==="Success"?LoadAssignedUsers():(r=$(".validation-summary-valid,validation-summary-errors"),f=r.find("li"),r.removeClass("validation-summary-valid"),r.addClass("validation-summary-errors"),f.html(u.error),f.show())):alert("not authenticated")}).fail(function(n,t,i){alert("fail"+n+"|"+t+"|"+i.getAllResponseHeaders())}).error(function(n,t,i){alert("error"+n+"|"+t+"|"+i.getAllResponseHeaders())}),!1});$("a.removeUser").click(function(){return confirm("Are you sure you want to remove this user from the organisation?")&&$.post(siteBase+"/Organisations/RemoveUser/",{id:$(this).data("org"),userId:$(this).data("userid")},function(n,t,i){if(isAuthenticated(i)){var r=jQuery.parseJSON(n);r.result==="Success"?LoadAssignedUsers():alert(r.result+": "+r.error)}else alert("not authenticated")}).fail(function(n,t,i){alert("fail"+n+"|"+t+"|"+i.getAllResponseHeaders())}).error(function(n,t,i){alert("error"+n+"|"+t+"|"+i.getAllResponseHeaders())}),!1});$("input.roleUpdate").change(function(){var n="";n=$(this).is(":checked")?"AddUserToRole":"RemoveUserFromRole";$.post(siteBase+"/Organisations/"+n+"/",{id:$(this).data("org"),userId:$(this).data("userid"),roleName:$(this).data("rolename")},function(n,t,i){if(isAuthenticated(i)){var r=jQuery.parseJSON(n);r.result==="Success"||alert(r.result+": "+r.error)}else alert("not authenticated")}).fail(function(n,t,i){alert("fail"+n+"|"+t+"|"+i.getAllResponseHeaders())}).error(function(n,t,i){alert("error"+n+"|"+t+"|"+i.getAllResponseHeaders())})});$("input.roleUpdateOrgUser").change(function(){var i="",r="",t=$(this),n;t.is(":checked")?(n="input:checkbox[name='"+t.attr("name")+"']",$(n).prop("checked",!1),r="RemoveUserFromRole",$.post(siteBase+"/Organisations/"+r+"/",{id:$(n).not(this).data("org"),userId:$(n).not(this).data("userid"),roleName:$(n).not(this).data("rolename")},function(n,t,i){if(isAuthenticated(i)){var r=jQuery.parseJSON(n);r.result==="Success"||alert(r.result+": "+r.error)}else alert("not authenticated")}).fail(function(n,t,i){alert("fail"+n+"|"+t+"|"+i.getAllResponseHeaders())}).error(function(n,t,i){alert("error"+n+"|"+t+"|"+i.getAllResponseHeaders())}),t.prop("checked",!0),i="AddUserToRole"):(t.prop("checked",!1),i="RemoveUserFromRole");$.post(siteBase+"/Organisations/"+i+"/",{id:$(this).data("org"),userId:$(this).data("userid"),roleName:$(this).data("rolename")},function(n,t,i){if(isAuthenticated(i)){var r=jQuery.parseJSON(n);r.result==="Success"||alert(r.result+": "+r.error)}else alert("not authenticated")}).fail(function(n,t,i){alert("fail"+n+"|"+t+"|"+i.getAllResponseHeaders())}).error(function(n,t,i){alert("error"+n+"|"+t+"|"+i.getAllResponseHeaders())})});$(".cbShowOnDash").click(function(){var n=$(this).data("org"),t=$(this).data("dcp");dcpOnDd(n,t,$(this).is(":checked"))})}function WireupRatiosAdmin(){$(document).ready(function(){$(".updateRatios").click(function(){return $(".updateRatio").each(function(){var n=$(this),r=$(this).data("rowkey"),u=$(this).parent().parent().find(".prev-ratio").text(),t=$(document.getElementById(r)),i=n.val();$.post(siteBase+"/InfraDevRatios/Update/",{id:n.data("dcpid"),devTypeId:n.data("devtypeid"),infraTypeId:n.data("infratypeid"),value:n.val()},function(n,r,f){if(isAuthenticated(f)){var e=jQuery.parseJSON(n);e.result==="Success"?(Math.floor(i)===parseInt(i)&&parseInt(i)===0?t.addClass("ratioNotSet"):t.removeClass("ratioNotSet"),parseInt(u)!=parseInt(i)&&(t.find(".updateInfo").html("updated"),window.setTimeout(function(){t.find(".updateInfo").html("")},5e3),t.find(".prev-ratio").html(parseInt(i).toFixed(2)))):alert(e.result+": "+e.error)}else alert("not authenticated")}).fail(function(n,t,i){alert("fail"+n+"|"+t+"|"+i.getAllResponseHeaders())}).error(function(n,t,i){alert("error"+n+"|"+t+"|"+i.getAllResponseHeaders())})}),!1})})}function WireupDemands(){$("input.demandEdit").change(function(){var t=$(this).val().replace(/^\s\s*/,"").replace(/\s\s*$/,""),n;/^\d+(\.\d+)?$/.test(t)?(n=$(this),$.post(siteBase+"/Demands/Update/",{id:n.data("dcpid"),devTypeId:n.data("devtypeid"),chargeAreaId:n.data("chargeareaid"),dcpYearId:n.data("dcpyearid"),value:n.val()},function(t,i,r){if(isAuthenticated(r)){var u=jQuery.parseJSON(t);u.result==="Success"?(n.val(u.values.newValue),n.data("initial",u.values.newValue),n.addClass("updated"),window.setTimeout(function(){n.removeClass("updated")},1e3)):alert(u.result+": "+u.error)}else alert("not authenticated")}).fail(function(n,t,i){alert("fail"+n+"|"+t+"|"+i.getAllResponseHeaders())}).error(function(n,t,i){alert("error"+n+"|"+t+"|"+i.getAllResponseHeaders())})):alert("Value must be a number")});$("input.demandEdit").click(function(){$(this).val($(this).val().replace(/,/,""))});$("input.demandEdit").blur(function(){$(this).val()==$(this).data("initial").replace(/,/,"")&&$(this).val($(this).data("initial"))});$("input.rbSelectHeaderRow").click(function(){$("tr.trExcelHeaderRow").removeClass("activeRow");$(this).closest("tr").addClass("activeRow")})}function WireupSharedFunctions(){$("input.noSpaces").on({keydown:function(n){if(n.which===32)return!1},change:function(){this.value=this.value.replace(/\s/g,"")}});$("input.numbersOnly").on({keydown:function(n){return n.which>=48&&n.which<=57?!0:n.which>=96&&n.which<=105?!0:n.which===9||n.which===10||n.which===190||n.which===8||n.which===46||n.which===37||n.which===39?!0:!1},change:function(){this.value=this.value.replace(/\s/g,"")}});$("a.hidableButton").click(function(){return $(this).hasClass("showing")?($(".hidable").hide(),$(this).removeClass("showing"),$(this).html($(this).data("show-message"))):($(".hidable").show(),$(this).addClass("showing"),$(this).html($(this).data("hide-message"))),!1})}function dcpOnDd(n,t,i){$.post(siteBase+"/dcp/ShowHideOnDb/",{id:t,orgId:n,status:i},function(n,t,i){isAuthenticated(i)||alert("not authenticated")}).fail(function(n,t,i){alert("fail"+n+"|"+t+"|"+i.getAllResponseHeaders())}).error(function(n,t,i){alert("error"+n+"|"+t+"|"+i.getAllResponseHeaders())})}$(document).ready(function(){WireUpUpdateProjYears();WireupOrgUserAdmin();WireupRatiosAdmin();WireupDemands();WireupSharedFunctions()})