<script>
{% if zipManage and app.session.get(sessionModalOpen) %}
$(document).ready(function(){
zipUpdateSelect{{ number }}(true);
});
{% endif %}
// COMUNE
function cityModal{{ number }}(){
{% if hasOtherCity %}
document.getElementById('{{ formName }}_select_zips').style.display = 'block';
document.getElementById('other_zip').style.display = 'none';
{% endif %}
{% if modalToClose != '' %}
$('#{{ modalToClose }}').modal('hide');
{% endif %}
$('#modalCity{{ number }}').modal({backdrop: 'static', keyboard: false}).modal('show');
}
function cityFilter{{ number }}(){
var name = document.getElementById("city_filter_val_{{ number }}").value;
$.post("{{ path ('cities_update') }}", { name:name }, function(response){
if(response.code == 200 && response.success){
var tbody = document.getElementById("city_table_{{ number }}");
tbody.innerHTML = "";
var cities = JSON.parse(response.cities);
if(cities.length == 0){
var tr = document.createElement("tr");
var tdAlert = document.createElement("td");
var div1 = document.createElement("div");
var div2 = document.createElement("div");
div2.innerHTML = "<i class='icon-warning-sign'></i><strong>Attenzione!</strong> La ricerca non ha dato risultati; controlla il nome inserito!";
div2.className = "sb-msg";
div1.className = "style-msg alertmsg m_b_none";
div1.appendChild(div2);
tdAlert.colSpan = "3";
tdAlert.appendChild(div1);
tr.appendChild(tdAlert);
tbody.appendChild(tr);
}
else{
for(var i = 0; i < cities.length; i++){
var tr = document.createElement("tr");
var tdName = document.createElement("td");
var tdSelect = document.createElement("td");
tdName.innerHTML = cities[i]['name'] + ' (' + cities[i]['province'] + ')';
var aSelect = document.createElement("a");
aSelect.innerHTML = '<img class="icon_action icon_green" src="{{ asset (icon_select) }}" />';
aSelect.href = 'javascript: citySelect{{ number }}(' + cities[i]['id'] + ', "' + cities[i]['name'] + ' (' + cities[i]['province'] + ')' + '")';
tdSelect.className = "txt_a_c";
tdSelect.appendChild(aSelect);
tr.appendChild(tdName);
tr.appendChild(tdSelect);
tbody.appendChild(tr);
}
}
}
}
, "json");
}
function citySelect{{ number }}(id, name){
document.getElementById('{{ formName }}_cityId').value = id;
document.getElementById("{{ formName }}_cityName").value = name;
{% if zipManage %}
zipUpdateSelect{{ number }}(false);
{% endif %}
$('#modalCity{{ number }}').modal('hide');
{% if modalToClose != '' %}
$('#{{ modalToClose }}').modal({backdrop: 'static', keyboard: false}).modal('show');
{% endif %}
}
function cityDeselect{{ number }}(){
document.getElementById('{{ formName }}_cityId').value = null;
document.getElementById('{{ formName }}_cityName').value = '';
zipUpdateSelect{{ number }}(false);
$('#modalCity{{ number }}').modal('hide');
{% if modalToClose != '' %}
$('#{{ modalToClose }}').modal({backdrop: 'static', keyboard: false}).modal('show');
{% endif %}
}
{% if hasOtherCity %}
function cityInsert{{ number }}(){
document.getElementById('{{ formName }}_otherCity').value = document.getElementById('other_city_{{ number }}').value;
document.getElementById('{{ formName }}_cityName').value = document.getElementById('other_city_{{ number }}').value;
document.getElementById('{{ formName }}_select_zips').style.display = 'none';
document.getElementById('other_zip').style.display = 'block';
$('#modalCity{{ number }}').modal('hide');
{% if modalToClose != '' %}
$('#{{ modalToClose }}').modal({backdrop: 'static', keyboard: false}).modal('show');
{% endif %}
}
{% endif %}
function zipUpdateSelect{{ number }}(init){
var cityId = document.getElementById('{{ formName }}_cityId').value;
$.post("{{ path ('city_zips') }}", { cityId:cityId }, function(response){
if(response.code == 200 && response.success){
var select = document.getElementById('{{ formName }}_select_zips');
while(select.options.length){
select.remove(0);
}
if(response.zips.includes(',')){
// MULTI
var zs = response.zips.split(',');
for(var i=0; i < zs.length; i++){
var array = zs[i].split('-');
var opt = new Option(array[1], array[0]);
select.options.add(opt);
}
if(init){
var j = select.options.length;
for(var k=0; k < j; k++){
if(select.options[k].value == document.getElementById('{{ formName }}_zip').value){
select.selectedIndex = k;
break;
}
}
}
else{
document.getElementById('{{ formName }}_zip').value = '';
}
}
else{
var array = response.zips.split('-');
document.getElementById('{{ formName }}_zip').value = array[1];
var opt = new Option(array[1], array[0]);
select.options.add(opt);
}
select.removeAttribute('readonly');
zipSet{{ number }}();
}
}
, "json");
}
function zipSet{{ number }}(){
document.getElementById('{{ formName }}_zip').value = document.getElementById('{{ formName }}_select_zips').value;
}
function cityModalClose{{ number }}(){
$('#modalCity{{ number }}').modal('hide');
{% if modalToClose != '' %}
$('#{{ modalToClose }}').modal({backdrop: 'static', keyboard: false}).modal('show');
{% endif %}
}
</script>