function checkImport(){

    var defined_cat     = $('select[name=cat_id]').val();
    var data_struct     = $('#data_struct').val();
    var struct_has_cat  = (data_struct.indexOf('category_id')>=0 || data_struct.indexOf('category')>=0);

    //alert(struct_has_cat+' '+defined_cat);

    if (defined_cat==0 && struct_has_cat==false){
        alert('Вы указали что категория для импорта будет определена в файле, но не включили поле "Категория" в шаблон структуры данных.');
        return;
    }

    $('form#import').submit();

}

function addToCSVTemplate(select_id){

    var param   = $('#'+select_id).val();
    var struct  = $('#data_struct').val();

    if (struct.length > 0){
        $('#data_struct').val(struct + ', ' + param);
    } else {
        $('#data_struct').val(param);
    }

    if (param == 'category' || param == 'category_id'){
        $('select#cat_id').val(0);
    }

}

function sendShopForm(component_id, opt, object_id){

    var link = 'index.php?view=components&do=config&id='+component_id+'&opt='+opt;

    if (object_id && object_id.length>0) { link = link + '&obj_id='+ object_id; }

    var sel  = checked();

    if (sel){
        if (opt!='delete_item' || confirm('Удалить отмеченные объекты ('+sel+' шт.)?')){

            document.selform.action = link;
            document.selform.submit();

        }
    } else {
        alert('Нет отмеченных объектов');
    }

}

function moveItemsToPage(component_id, cat_id, to_page, page, perpage){

    var link = 'index.php?view=components&do=config&id='+component_id+'&opt=move_items_page&cat_id='+cat_id+'&to_page='+to_page+'&page='+page+'&perpage='+perpage;
    var sel  = checked();

    if (sel){
        document.selform.action = link;
        document.selform.submit();
    } else {
        alert('Нет отмеченных объектов');
    }

}

function renameCharGroup(current_name){

    var new_name = prompt('Укажите название группы:', current_name);

    if (new_name){
        $('#rename_form input[name=new_name]').val(new_name);
        $('#rename_form').submit();
    }

}

function deleteCharGroup(current_name){

    if (confirm('Удалить группу "'+current_name+'"?')){
        $('#delete_form').submit();
    }

}

function loadItemChars(component_id, cat_id, item_id){
    $('#item_chars').html('<p style="font-style:italic;color:#666;">Загрузка характеристик...</p>');
    $.ajax({
      type: "GET",
      url: "/admin/index.php",
      data: "view=components&do=config&id="+component_id+"&opt=load_chars&item_id="+item_id+"&cat_id="+cat_id,
      success: function(msg){
        $('#item_chars').html(msg);
      }
    });
}

function moveItem(item_id, dir){

    var component_id    = $('#filter_form input[name=id]').val();
    var cat_id          = $('#filter_form input[name=cat_id]').val();

	$.ajax({
		  type: "POST",
		  url: "/admin/index.php",
		  data: "view=components&do=config&id="+component_id+"&opt=move_item&item_id="+item_id+"&cat_id="+cat_id+"&dir="+dir,
		  success: function(msg){
            var trh = $('#listTable tr#'+item_id).html();
            if (dir == -1){
                $('#listTable tr#'+item_id).prev('tr').before('<tr id="'+item_id+'">'+trh+'</tr>').next('tr').remove();
            }
            if (dir == 1){
                $('#listTable tr#'+item_id).next('tr').after('<tr id="'+item_id+'">'+trh+'</tr>').prev('tr').remove();
            }
            $('#listTable tr').find('.move_item_up').show();
            $('#listTable tr').find('.move_item_down').show();
            $('#listTable tr').eq(1).find('.move_item_up').hide();
            $('#listTable tr').eq($('#listTable tr').length-1).find('.move_item_down').hide();
            $('#listTable tr#'+item_id).animate( { opacity:0.01 }, 200 ).animate( { opacity:1 }, 200 );
		  }
	});
}

function moveChar(item_id, dir){

    var component_id    = $('#bind_form input[name=id]').val();
    var cat_id          = $('#bind_form input[name=cat_id]').val();

	$.ajax({
		  type: "POST",
		  url: "/admin/index.php",
		  data: "view=components&do=config&id="+component_id+"&opt=move_char&item_id="+item_id+"&cat_id="+cat_id+"&dir="+dir,
		  success: function(msg){
            var trh = $('#listTable tr#'+item_id).html();
            if (dir == -1){
                $('#listTable tr#'+item_id).prev('tr').before('<tr id="'+item_id+'">'+trh+'</tr>').next('tr').remove();
            }
            if (dir == 1){
                $('#listTable tr#'+item_id).next('tr').after('<tr id="'+item_id+'">'+trh+'</tr>').prev('tr').remove();
            }
            $('#listTable tr').find('.move_item_up').show();
            $('#listTable tr').find('.move_item_down').show();
            $('#listTable tr').eq(1).find('.move_item_up').hide();
            $('#listTable tr').eq($('#listTable tr').length-1).find('.move_item_down').hide();
            $('#listTable tr#'+item_id).animate( { opacity:0.01 }, 200 ).animate( { opacity:1 }, 200 );
		  }
	});
}

function compare(id, qs, qs2, action, action2){
	$('img#compare'+id).attr('src', 'images/actions/loader.gif');
    $('a#comparelink'+id).attr('href', '');
	$.ajax({
		  type: "GET",
		  url: "index.php",
		  data: qs,
		  success: function(msg){
			$('img#compare'+id).attr('src', 'components/maps/images/'+action+'.gif');
			$('a#comparelink'+id).attr('href', 'javascript:compare('+id+', "'+qs2+'", "'+qs+'", "'+action2+'", "'+action+'");');
		  }
	});
}

function filter(id, qs, qs2, action, action2){
	$('img#filter'+id).attr('src', 'images/actions/loader.gif');
    $('a#filterlink'+id).attr('href', '');
	$.ajax({
		  type: "GET",
		  url: "index.php",
		  data: qs,
		  success: function(msg){
			$('img#filter'+id).attr('src', 'components/maps/images/'+action+'.gif');
			$('a#filterlink'+id).attr('href', 'javascript:filter('+id+', "'+qs2+'", "'+qs+'", "'+action2+'", "'+action+'");');
		  }
	});
}

function front(id, qs, qs2, action, action2){
	$('img#front'+id).attr('src', 'images/actions/loader.gif');
    $('a#frontlink'+id).attr('href', '');
	$.ajax({
		  type: "GET",
		  url: "index.php",
		  data: qs,
		  success: function(msg){
			$('img#front'+id).attr('src', 'components/maps/images/'+action+'.gif');
			$('a#frontlink'+id).attr('href', 'javascript:front('+id+', "'+qs2+'", "'+qs+'", "'+action2+'", "'+action+'");');
		  }
	});
}

function toggleBindAll(){
    if(document.addform.bind_all.checked){
		$('select#cats').attr('disabled', 'disabled');
	} else {
		$('select#cats').attr('disabled', '');
	}
}


function copyItem(com_id, item_id){
	var copies = prompt('Количество копий:', 1);
	if (copies>0){
		window.location.href='/admin/index.php?view=components&do=config&id='+com_id+'&opt=copy_item&item_id='+item_id+'&copies='+copies;
	}
}

function copyCat(com_id, item_id){
	var copies = prompt('Количество копий:', 1);
	if (copies>0){
		window.location.href='/admin/index.php?view=components&do=config&id='+com_id+'&opt=copy_cat&item_id='+item_id+'&copies='+copies;
	}
}

function xlsEditRow(){
    var r = $('input#title_row').val();
    $('input.row').val(r);
}

function xlsEditCol(){
    var c = Number($('input#title_col').val());

    $("input.col").each(function (i) {
        $(this).val(i+c+1);
    });
}

function ignoreRow(row){
    var r_id = 'row_'+row;
    var c_id = 'ignore_'+row;
    var checked = Number($('input:checkbox[@id='+c_id+']').attr('checked'));
    if(checked){
        $('tr#'+r_id+' input:text[@class!=other]').attr('disabled', 'disabled');
        $('tr#'+r_id+' input:text[@class=other]').attr('disabled', '');
    } else {
        $('tr#'+r_id+' input:text[@class!=other]').attr('disabled', '');
        $('tr#'+r_id+' input:text[@class=other]').attr('disabled', 'disabled');
    }
}

function toggleDiscountLimit(){
    var sign = Number($('select#sign').val());

    if (sign==3){ $('tr.if_limit').show(); }
    else { $('tr.if_limit').hide(); }
}

function checkGroupList(){

	if(document.addform.is_public.checked){
		$('select#showin').attr('disabled', '');
	} else {
		$('select#showin').attr('disabled', 'disabled');
	}

}

function showMapMarker(){
    var file = $('select[name=marker]').val();
    $('img#marker_demo').attr('src', '/components/maps/images/markers/'+file);
}

function checkGroupList(){

	if(document.addform.is_public.checked){
		$('select#showin').attr('disabled', '');
        $('input#can_edit').attr('disabled', '');
	} else {
		$('select#showin').attr('disabled', 'disabled');
        $('input#can_edit').attr('disabled', 'disabled');
        $('input#can_edit').attr('checked', '');
	}

}

function showMapCenter(){

    var mode = $('#map_mode').val();

    if(mode != 'city'){
        $('.city_center').hide();
    } else {
        $('.city_center').show();
    }

}

function showAPIKeys(){

    var engine = $('#maps_engine').val();

    if(engine != 'custom'){
        $('#ya_tr').hide();
    } else {
        $('#ya_tr').show();
    }

    if(engine == 'custom'){
        $('#custom_desc').show();
    } else {
        $('#custom_desc').hide();
    }

}