<script>
$(document).ready(function(){
$("#countryId").live("change",function(){
var pk_c_code = $(this).val();
var url = '<?php echo osc_base_url(true)."?page=ajax&action=regions&countryId="; ?>' + pk_c_code;
var result = '';
if(pk_c_code != '') {
$("#regionId").attr('disabled',false);
$("#uniform-regionId").removeClass('disabled');
$("#cityId").attr('disabled',true);
$("#uniform-cityId").addClass('disabled');
$.ajax({
type: "POST",
url: url,
dataType: 'json',
success: function(data){
var length = data.length;
if(length > 0) {
result += '<option value=""><?php echo osc_esc_js(__('Select a region', 'zara')); ?></option>';
for(key in data) {
result += '<option value="' + data[key].pk_i_id + '">' + data[key].s_name + '</option>';
}
$("#region").before('<div class="selector" id="uniform-regionId"><span><?php echo osc_esc_js(__('Select a region', 'zara')); ?></span><select name="regionId" id="regionId" ></select></div>');
$("#region").remove();
$("#city").before('<div class="selector" id="uniform-cityId"><span><?php echo osc_esc_js(__('Select a city', 'zara')); ?></span><select name="cityId" id="cityId" ></select></div>');
$("#city").remove();
$("#regionId").val("");
$("#uniform-regionId").find('span').text('<?php echo osc_esc_js(__('Select a region', 'zara')); ?>');
} else {
$("#regionId").parent().before('<input placeholder="<?php echo osc_esc_js(__('Enter a region', 'zara')); ?>" type="text" name="sRegion" id="region" />');
$("#regionId").parent().remove();
$("#cityId").parent().before('<input placeholder="<?php echo osc_esc_js(__('Enter a city', 'zara')); ?>" type="text" name="sCity" id="city" />');
$("#cityId").parent().remove();
$("#city").val('');
}
$("#regionId").html(result);
$("#cityId").html('<option selected value=""><?php echo osc_esc_js(__('Select a city', 'zara')); ?></option>');
$("#uniform-cityId").find('span').text('<?php echo osc_esc_js(__('Select a city', 'zara')); ?>');
$("#cityId").attr('disabled',true);
$("#uniform-cityId").addClass('disabled');
}
});
} else {
// add empty select
$("#region").before('<div class="selector" id="uniform-regionId"><span><?php echo osc_esc_js(__('Select a region', 'zara')); ?></span><select name="regionId" id="regionId" ><option value=""><?php echo osc_esc_js(__('Select a region', 'zara')); ?></option></select></div>');
$("#region").remove();
$("#city").before('<div class="selector" id="uniform-cityId"><span><?php echo osc_esc_js(__('Select a city', 'zara')); ?></span><select name="cityId" id="cityId" ><option value=""><?php echo osc_esc_js(__('Select a city', 'zara')); ?></option></select></div>');
$("#city").remove();
if( $("#regionId").length > 0 ){
$("#regionId").html('<option value=""><?php echo osc_esc_js(__('Select a region', 'zara')); ?></option>');
} else {
$("#region").before('<div class="selector" id="uniform-regionId"><span><?php echo osc_esc_js(__('Select a region', 'zara')); ?></span><select name="regionId" id="regionId" ><option value=""><?php echo osc_esc_js(__('Select a region', 'zara')); ?></option></select></div>');
$("#region").remove();
}
if( $("#cityId").length > 0 ){
$("#cityId").html('<option value=""><?php echo osc_esc_js(__('Выбрать город', 'zara')); ?></option>');
} else {
$("#city").parent().before('<div class="selector" id="uniform-cityId"><span><?php echo osc_esc_js(__('Select a city', 'zara')); ?></span><select name="cityId" id="cityId" ><option value=""><?php echo osc_esc_js(__('Select a city', 'zara')); ?></option></select></div>');
$("#city").parent().remove();
}
$("#regionId").attr('disabled',true);
$("#uniform-regionId").addClass('disabled');
$("#uniform-regionId").find('span').text('<?php echo osc_esc_js(__('Select a region', 'zara')); ?>');
$("#cityId").attr('disabled',true);
$("#uniform-cityId").addClass('disabled');
$("#uniform-cityId").find('span').text('<?php echo osc_esc_js(__('Select a city', 'zara')); ?>');
}
});
$("#regionId").live("change",function(){
var pk_c_code = $(this).val();
var url = '<?php echo osc_base_url(true)."?page=ajax&action=cities®ionId="; ?>' + pk_c_code;
var result = '';
if(pk_c_code != '') {
$("#cityId").attr('disabled',false);
$("#uniform-cityId").removeClass('disabled');
$.ajax({
type: "POST",
url: url,
dataType: 'json',
success: function(data){
var length = data.length;
if(length > 0) {
result += '<option selected value=""><?php echo osc_esc_js(__('Выбрать город', 'zara')); ?></option>';
for(key in data) {
result += '<option value="' + data[key].pk_i_id + '">' + data[key].s_name + '</option>';
}
$("#city").before('<div class="selector" id="uniform-cityId"><select name="cityId" id="cityId" ></select></div>');
$("#city").remove();
$("#cityId").val("");
$("#uniform-cityId").find('span').text('<?php echo osc_esc_js(__('Select a city', 'zara')); ?>');
} else {
result += '<option value=""><?php echo osc_esc_js(__('No cities found', 'zara')); ?></option>';
$("#cityId").parent().before('<input type="text" placeholder="<?php echo osc_esc_js(__('Enter a city', 'zara')); ?>" name="sCity" id="city" />');
$("#cityId").parent().remove();
}
$("#cityId").html(result);
}
});
} else {
$("#cityId").attr('disabled',true);
$("#uniform-cityId").addClass('disabled');
$("#uniform-cityId").find('span').text('<?php echo osc_esc_js(__('Select a city', 'zara')); ?>');
}
});
if( $("#regionId").attr('value') == "") {
$("#cityId").attr('disabled',true);
$("#city").attr('disabled',true);
$("#uniform-cityId").addClass('disabled');
}
if($("#countryId").length != 0) {
if( $("#countryId").attr('value') == "") {
$("#regionId").attr('disabled',true);
$("#uniform-regionId").addClass('disabled');
}
}
$(".row").on('change', '#cityId, #regionId', function() {
$(this).parent().find('span').text($(this).find("option:selected" ).text());
});
$('.add_item .seller_info.logged input#contactName, .add_item .seller_info.logged input#contactEmail').prop('disabled', true);
});
</script>