
var hostsList=(hostsList===undefined)?null:hostsList;var hostData=(hostData===undefined)?null:hostData;$(document).ready(function(){var hostname=(hostname===undefined)?null:$('.hostname-load-config').val();var selectedhostconfig=$("#selectedhostconfig").val();if(selectedhostconfig&&$("input[name=nextstep]").val()==3){$("input[name=operation]:not(:checked)").each(function(){$(this).prop('disabled',true);});if($("input[name=operation]:checked").val()=="update"){$('#setPassword').addClass('visually-hidden');$('#changePasswordOption').removeClass('visually-hidden');$('#password').removeAttr('required');updateConfigMessage();}
if($("input[name=operation]:checked").val()=="new"||$("input[name=operation]:checked").val()=="copy"){$('#changePasswordOption').addClass('visually-hidden');$('#setPassword').removeClass('visually-hidden');}
if($("input[name=operation]:checked").val()=="copy"){copyConfigMessage();}}
if($("input[name=operation]:checked").val()=="update"){$("input[name=operation]:checked").focus();updateConfigMessage();}
if($("input[name=operation]:checked").val()=="copy"){$("input[name=operation]:checked").focus();copyConfigMessage();}
$("#hostConfig").on('change',function(){selectedhostconfig=$("#hostConfig").val();$.get(ajaxUrl,{keyname:wizardname,hostname:selectedhostconfig},function(response){if(response!=""){hostData=response;loadValues(hostData,selectedhostconfig);}else{alert('Error, please try again.');}}).done(function(){}).fail(function(){}).always(function(){},'json')
$('#configWizardForm').append('<input type="hidden" name="selectedhostconfig" id="selectedhostconfig" value="'+selectedhostconfig+'" />');$('#configWizardForm').append('<input type="hidden" name="diffCount" id="diffCount" value="0" />');$("#hostConfig").val('');$("#hostConfig").keyup();switchingAction();});function switchingAction(){if(!selectedhostconfig){return;}
if($("input[name=operation]:checked").val()=="new"||$("input[name=operation]:checked").val()=="copy"){$('#changePasswordOption').addClass('visually-hidden');$('#setPassword').removeClass('visually-hidden');}
if($("input[name=operation]:checked").val()=="update"){$('#setPassword').addClass('visually-hidden');$('#changePasswordOption').removeClass('visually-hidden');updateConfigMessage();}
if($("input[name=operation]:checked").val()=="copy"){$('.hostname-load-config').val("");copyConfigMessage();}}
function updateConfigMessage(){$('#loadedConfigSuccess').text("Editing configuration '"+selectedhostconfig+"'");$('#loadConfigAlert').addClass('alert-info');$('#loadConfigAlert').removeClass('visually-hidden');}
function copyConfigMessage(){$('#loadedConfigSuccess').text("Editing a copy of configuration "+selectedhostconfig);$('#loadConfigAlert').addClass('alert-info');$('#loadConfigAlert').removeClass('visually-hidden');}
$("input[name=operation]").on("change",function(){if(this.value=="new"){hostData=null;$('#configWizardForm')[0].reset();$('#loadConfigAlert').addClass('visually-hidden');$('.hostname-load-config').prop('readonly',false);$('#hostConfig').prop('disabled',true);$('#hostConfig').children('option:first-child').prop('selected',true);}else{$('#hostConfig').prop('disabled',false);$('.hostname-load-config').prop('readonly',false);if(hostname){if($("input[name=operation]:checked").val()=="update"){updateConfigMessage();$('.hostname-load-config').val(hostname);$('.hostname-load-config').prop('readonly',true);}
if($("input[name=operation]:checked").val()=="copy"){$('#loadedConfigSuccess').text("Editing a copy of configuration "+hostname);$('#loadConfigAlert').addClass('alert-info');$('.hostname-load-config').val("");}}else if($(".copy-to-hostname").val()!==''){switchingAction();}}});$("#changePassword").change(function(){if($('#changePassword').is(':checked')){$('#setPassword').removeClass('visually-hidden');$('#password').attr('required');}else{$('#setPassword').addClass('visually-hidden');$('#password').val('');$('#password').removeAttr('required');}});var dataList=[];$("#hostConfigOptions").find("option").each(function(){dataList.push($(this).val());});$("#hostConfig").on("input",function(){if(dataList.indexOf($(this).val())!=-1){$('#loadFormBtn').parent().removeClass("visually-hidden");}});$('.copy-to-hostname').change(function(){if($("input[name=operation]:checked").val()!="update"){$('.initialized-by-ip').val($('.copy-to-hostname').val());}});$('.hostname-load-config').on('input paste blur',function(){validHostname();});function validHostname(){var key=$('.hostname-load-config').attr("id");if($("input[name=operation]:checked").val()!="update"){if(hostsList!=null&&hostsList[$('.hostname-load-config').val()]!=='undefined'&&hostsList.includes($('.hostname-load-config').val())){$('.hostname-load-config').addClass('invalid');$('.hostname-load-config').addClass('is-invalid');$('.hostname-load-config').addClass('is-invalid-hostname');$('.invalid-feedback').addClass('visually-hidden');$('.invalid-hostname').removeClass('visually-hidden');return false;}else{resetHostnameValidation();}}
return true;}
var diffCount=0;var differences=new Map();if(selectedhostconfig&&hostData!=null&&typeof($('#services_serial').val())!=="undefined"&&$('#config_serial').val()!=""){$('.monitor').each(function(){checkMonitor(this.id,this.value);});}
$('.monitor').on("input propertychange paste",function(){checkMonitor(this.id,this.value);});function checkMonitor(id,value){if(($('input[name=operation]').attr('type')=='radio'&&$('input[name=operation]:checked').val()=="new")||($('input[name=operation]').attr('type')=='hidden'&&$('input[name=operation]').val()=="new"&&$("input[name=nextstep]").val()==3)||hostData==null){return;}
if((($('input[name=operation]').attr('type')=='radio'&&$("input[name=operation]:checked").val()=="copy")||($('input[name=operation]').attr('type')=='hidden'&&$('input[name=operation]').val()=="copy"))&&id=='hostname'){return;}
var data=($("input[name=nextstep]").val()==3)?step1Data(id):step2Data(id);return;processMonitorData(data,id,value);}
function step1Data(id){var originalValue=(hostData!==null)?hostData[id]:null;var alertId='#'+id+'_Alert';return{"originalValue":originalValue,"alertId":alertId};}
function step2Data(id){var originalValue;var alertId;if(id.includes("serviceargs")){let idx=id;idx=idx.replace(/\[/g,"_");idx=idx.replace(/\]/g,"");idx=idx.replace(/serviceargs_/g,"");if(id.includes("process")){alertId='#serviceargs_'+idx+'_Alert-cust-sm';}else{alertId='#serviceargs_'+idx+'_Alert-sm';}
let indexes=id;indexes=indexes.replace(/\[/g," ");indexes=indexes.replace(/\]/g,"");indexes=indexes.split(" ");try{if(indexes.length==2){originalValue=hostData[indexes[0]][indexes[1]];}else if(indexes.length==3){originalValue=hostData[indexes[0]][indexes[1]][indexes[2]];}else if(indexes.length==4){originalValue=hostData[indexes[0]][indexes[1]][indexes[2]][indexes[3]];}else{alert("ERROR!! Deeper index, than supported");}}catch(e){originalValue=undefined;}}
return{"originalValue":originalValue,"alertId":alertId};}
function processMonitorData(data,id,value){var originalValue=data['originalValue'];var alertId=data['alertId'];if(value!=originalValue){differences.set(id,value);$(alertId).removeClass('visually-hidden');}else{differences.delete(id);$(alertId).addClass('visually-hidden');}
diffCount=differences.size;if(diffCount==0){noChanges();}else{changes();}}
function changes(){$('#submitButton2Alert').removeClass('visually-hidden');}
function noChanges(){$('#submitButton2Alert').addClass('visually-hidden');}
$('.btn-show-secret').off('click').on('click',function(e){var password=$(this).prev().attr("type");const type=password==="password"?"text":"password";$(this).prev().attr("type",type);var icon=$(this).find('span.material-symbols-outlined');if(icon.length>0){icon.html(type==="password"?'visibility':'visibility_off');}else{var faIcon=$(this).find('i.fa-eye, i.fa-eye-slash');if(faIcon.length>0){if(type==="password"){faIcon[0].classList.remove('fa-eye-slash');faIcon[0].classList.add('fa-eye');}else{faIcon[0].classList.remove('fa-eye');faIcon[0].classList.add('fa-eye-slash');}}}
var newTitle=type==="password"?_("Show"):_("Hide");$(this).attr('title',newTitle);$(this).attr('data-original-title',newTitle);var tooltip_id=$(this).attr('aria-describedby');if(tooltip_id){$('#'+tooltip_id+' .tooltip-inner').text(newTitle);}});(function(){'use strict'
var forms=document.querySelectorAll('.needs-validation');Array.prototype.slice.call(forms).forEach(function(form){form.addEventListener('submit',processSubmit,false);form.addEventListener('keydown',processSubmit,false);})})();function processSubmit(event){if($(document.activeElement).attr('name')=='submitButton2'||(event.key==='Enter'&&!$(document.activeElement).is('textarea'))||(event.type=='submit'&&event.submitter.name=='submitButton2')){if(event.key=='Tab'){return;}
let formvalid=this.checkValidity();let hostvalid=validHostname();if(!formvalid||!hostvalid){event.preventDefault();event.stopPropagation();}else{whiteout();show_throbber();}}else if(event.key!=='undefined'){return;}else{whiteout();show_throbber();}
this.classList.add('was-validated');var invalidElem=$('#configWizardForm').find(':invalid').first();if(invalidElem!=null&&invalidElem.length>0){$(invalidElem)[0].scrollIntoView({behavior:"smooth",block:"center",inline:"nearest"});}}});var formId='#configWizardForm';function loadValues(obj,hostname){resetHostnameValidation();for(var key in obj){if(obj[key]instanceof Object){loadValues(obj[key]);}else{var $ctrl=$('#'+key,formId);if(document.getElementById(key)==null){var newkey=obj[key];if(document.getElementById(newkey)!==null){$ctrl=$('#'+newkey,formId);}}
if($ctrl.is('select')){$("option",$ctrl).each(function(){if(this.value==obj[key]){this.selected=true;}});}else{switch($ctrl.attr("type")){case"text":case"hidden":case"textarea":$ctrl.val(unescapeSlashes(obj[key]));$ctrl.prop("disabled",false);break;case"radio":case"checkbox":$ctrl.each(function(){if($(this).attr('value')==obj[key]){$(this).attr("checked",true);}});break;}}
if($("input[name=operation]:checked").val()=="update"){$('.hostname-load-config').prop('readonly',true);$('#password').removeAttr('required');}else{$('#password').attr('required');}
if($("input[name=operation]:checked").val()=="copy"){$('.hostname-load-config').val("");}}}};function resetHostnameValidation(){$('.hostname-load-config').removeClass('invalid');$('.hostname-load-config').removeClass('is-invalid');$('.hostname-load-config').removeClass('is-invalid-hostname');$(".invalid-hostname").addClass('visually-hidden');$(".invalid-feedback").removeClass('visually-hidden');}
function unescapeSlashes(str){if(!str){return str;}
let parsedStr=str.replace(/(^|[^\\])(\\\\)*\\$/,"$&\\");parsedStr=parsedStr.replace(/(^|[^\\])((\\\\)*")/g,"$1\\$2");try{parsedStr=JSON.parse(`"${parsedStr}"`);}catch(e){return str;}
return parsedStr;}