$(document).ready(function() { var paramsType, profileForm = $("#profile-form"), profileSearch = $("#searchAPI"), profileBossName = $("#id_boss_name", profileForm), profileBossSurname =$("#id_boss_surname", profileForm), profileBossMiddleName =$("#id_boss_midname", profileForm), profileInn = $("#id_inn", profileForm), profileKpp = $("#id_kpp", profileForm), profileOqrn = $("#id_ogrn", profileForm), profileAddress = $("#id_address", profileForm), profileLegalAddress = $("#id_jur_address", profileForm), profilePostalAddress = $("#id_real_address", profileForm), profileInputs = []; function confirmChangeDataPromise(inputArray) { var dlg_msg = $('#dialog-message'); var emptyInput = true; var defer = $.Deferred(); for (var i = inputArray.length - 1; i >= 0; --i) { if (inputArray[i].val()) { emptyInput = false; break; } } if (!emptyInput) { dlg_msg.dialog({ title: 'Изменить данные', buttons: {'Да': function(){ defer.resolve(true); $(this).dialog('close'); }, 'Нет': function(){ defer.resolve(false); $(this).dialog('close'); } } }).html('Имеются уже заполненные данные, они будут изменены, продолжить?'); dlg_msg.dialog('open'); } else { defer.resolve(true); } return defer.promise(); } function cleanSearchInput() { profileSearch.val(''); } function changeDataInInputs(data) { if (paramsType ==="LEGAL") { if (data.name) profileOrgName.val(join([data.opf && data.opf.short || "", data.name.short || data.name.full], " ")); if (data.name && data.name.full) profileOrgFullName.val(join([data.opf && data.opf.full || "", data.name.full], " ")); if (data.address) { var address = data.address.value; profileAddress.val(address); profileLegalAddress.val(address); profilePostalAddress.val(address); } if (data.management) { var management = data.management; if (management.post) {profileBossTitle.val(data.management.post)} if (management.name) { var fullName = management.name; profileBossSurname.val(getPartOfPersonName(fullName, 0)); profileBossName.val(getPartOfPersonName(fullName, 1)); profileBossMiddleName.val(getPartOfPersonName(fullName, 2)); } } } else { if (data.name && data.name.full) { var profileFullName = data.name.full; profileBossSurname.val(getPartOfPersonName(profileFullName, 0)); profileBossName.val(getPartOfPersonName(profileFullName, 1)); profileBossMiddleName.val(getPartOfPersonName(profileFullName, 2)); } if (data.address) { var address = data.address.value; profileAddress.val(address); profileLegalAddress.val(address); profilePostalAddress.val(address); } if (data.state && data.state.registration_date) { profileRegDate.val((new Date(data.state.registration_date)).toLocaleDateString()) } } profileInn.val(data.inn); profileKpp.val(data.kpp); profileOqrn.val(data.ogrn); } function showSuggestion(suggestion) { var data = suggestion.data; if (!data) return; confirmChangeDataPromise(profileInputs).then(function (answer) { if (answer) { changeDataInInputs(data) } cleanSearchInput(); }) ; } paramsType = getType(); if (paramsType === "LEGAL") { var profileOrgName = $('#id_name', profileForm), profileOrgFullName = $('#id_full_name', profileForm), profileBossTitle = $('#id_boss_title', profileForm); profileInputs = [ profileOrgName, profileOrgFullName, profileInn, profileKpp, profileOqrn, profileBossName, profileBossSurname, profileBossMiddleName, profileAddress, profileLegalAddress, profilePostalAddress ]; } else { var profileRegDate = $('#id_ip_reg_date', profileForm); profileInputs = [ profileBossName, profileBossSurname, profileBossMiddleName, profileInn, profileKpp, profileOqrn, profileRegDate, profileAddress, profileLegalAddress, profilePostalAddress ]; } profileSearch.suggestions({ token: daDataExternalApiKey, type: "PARTY", params: { type: paramsType }, count: 5, onSelect: showSuggestion }); });