$(document).ready(function() {
  //  initMode();
  //  initCity();
    
});
// Отображать / скрывать дополнительные параметры фильтров
    function ShowHide()
    {
        $("#ab_search_form_extras").animate({"height": "toggle"}, { duration: 10 });
    }



// Подгрузка Моделей выбранной марки
function abGetModel()
{
    $('#model').empty();
    $('#model').append(' <option value="">Будь-яка</option>');
    var mark_id = $('select[@name=mark] option:selected').val();
    if(mark_id == "")
    {
       $('#model').empty();
       $('#model').append(' <option value="">Будь-яка</option>');
       $('#model').removeClass("frm-sel w180").addClass("frm-sel w180 frm-dis");
        return true;
    }
   
    var option_str = '';
    $.ajax({      
        url:appURL+'fravtobazar/abGetAjaxModelSelect/',
        type:'POST',
        dataType: "json",
        data:'mark_id='+mark_id,      
       success: function(response) {
        
        for ( var i = 0 ; i < response.models.length ; i++ )
        {
            option_str += ' <option value="'+ response.models[i].model_id +'">'+ response.models[i].model_title +'</option>';
        }
        
        $('#model').append(option_str);      
        $('#model').removeClass("frm-sel w180 frm-dis").addClass("frm-sel w180");
        },//end success
        error: function() { alert('failure'); }
       
    });
    
}

// Дополнительная подгрузка моделей автомобиля
initMode = function() {
    
   var mark_id = $('select[@name=mark] option:selected').val();
   var model_id = $('#ajax_model').val();
   var option_str = '';  
    $.ajax({
        url:appURL+'fravtobazar/abGetAjaxModelSelect/',
        type:'POST',
        dataType: "json",
        data:'mark_id='+mark_id,
       success: function(response) {        
        for ( var i = 0 ; i < response.models.length ; i++ )
        {
            if(response.models[i].model_id == model_id)
            {
                option_str += ' <option value="'+ response.models[i].model_id +'" selected>'+ response.models[i].model_title +'</option>';
            }else{
                option_str += ' <option value="'+ response.models[i].model_id +'">'+ response.models[i].model_title +'</option>';
            }
        }

        $('#model').append(option_str);
        $('#model').css('display', '');
        },//end success
        /*error: function() { alert('failure1'); }*/
        error: function() {}

    });
 }



// Подгрузка городов выбранной марки
function abGetCityList()
{
    
   $('#usersubregion').empty();
   $('#usersubregion').append(' <option value=""> любой </option>');

    
    //var userregion = $('select[@name=userregion] option:selected').val();
    var cmp_value = document.getElementsByName('userregion');
    var userregion = cmp_value[0].value;   
    var option_string = '';

    $.ajax({
        url:appURL+'fravtobazar/abGetAjaxCitiesSelect/',
        type:'POST',
        dataType: "json",
        data:'userregion='+userregion,
        success: function(response) {       
        for ( var j = 0 ; j < response.city.length ; j++ )
        {
            option_string += ' <option value="'+ response.city[j].usersubregion_id +'">'+ response.city[j].usersubregion_title +'</option>';
        }

        $('#usersubregion').append(option_string);
        $('#usersubregion').css('display', '');

        },//end success
        error: function() { alert('failure'); }
        
    });
}

// Дополнительная подгрузка городов
initCity = function() {
    var cmp_value = document.getElementsByName('userregion');
    var userregion = cmp_value[0].value;
    var city = $('#ajax_city').val();


    var option_string = '';

    $.ajax({
        url:appURL+'fravtobazar/abGetAjaxCitiesSelect/',
        type:'POST',
        dataType: "json",
        data:'userregion='+userregion,
        success: function(response) {
        for ( var j = 0 ; j < response.city.length ; j++ )
        {
            if(response.city[j].usersubregion_id == city)
            {
                option_string += ' <option value="'+ response.city[j].usersubregion_id +'" selected >'+ response.city[j].usersubregion_title +'</option>';
            }else{
                option_string += ' <option value="'+ response.city[j].usersubregion_id +'">'+ response.city[j].usersubregion_title +'</option>';
            }
        }

        $('#usersubregion').append(option_string);
        $('#usersubregion').css('display', '');

        },//end success
     /*   error: function() { alert('failure2'); }*/
          error: function() {}
    });
 }

// Сбор всех фильтров и  SUBMIT формы фильтров
function abFilters()
{
   /*
 * price_from
 * price_to
 * capacity_from
 * capacity_to
 * power_from
 * power_to
 * mileage_from
 * mileage_to
 * seats_from
 * seats_to
 * doors_from
 * doors_to
 * tonnage_from
 * tonnage_to
 */

 var lang = $('#lang').val();
 var price_from = $('#price_from').val();
 var price_to = $('#price_to').val();
 var capacity_from = $('#capacity_from').val();
 var capacity_to = $('#capacity_to').val();
 var power_from = $('#power_from').val();
 var power_to = $('#power_to').val();
 var mileage_from = $('#mileage_from').val();
 var mileage_to = $('#mileage_to').val();
 var seats_from = $('#seats_from').val();
 var seats_to = $('#seats_to').val();
 var doors_from = $('#doors_from').val();
 var doors_to = $('#doors_to').val();
 var tonnage_from = $('#tonnage_from').val();
 var tonnage_to = $('#tonnage_to').val();
/*-------------------------------------------*/
var productivity_from = $('#productivity_from').val();
var productivity_to = $('#productivity_to').val();

var capacity_body_from = $('#capacity_body_from').val();
var capacity_body_to = $('#capacity_body_to').val();

var admission_height_from = $('#admission_height_from').val();
var admission_height_to = $('#admission_height_to').val();

var arm_boom_from = $('#arm_boom_from').val();
var arm_boom_to = $('#arm_boom_to').val();

var capacity_bucket_from = $('#capacity_bucket_from').val();
var capacity_bucket_to = $('#capacity_bucket_to').val();


var lift_height_from = $('#lift_height_from').val();
var lift_height_to = $('#lift_height_to').val();

var dep_big_from = $('#dep_big_from').val();
var dep_big_to = $('#dep_big_to').val();

var laying_width_from = $('#laying_width_from').val();
var laying_width_to = $('#laying_width_to').val();

var caterpillars_width_from = $('#caterpillars_width_from').val();
var caterpillars_width_to = $('#caterpillars_width_to').val();

var dimension_length_from = $('#dimension_length_from').val();
var dimension_length_to = $('#dimension_length_to').val();

var dimension_width_from = $('#dimension_width_from').val();
var dimension_width_to = $('#dimension_width_to').val();

var dimension_height_from = $('#dimension_height_from').val();
var dimension_height_to = $('#dimension_height_to').val();

var dimension_weight_from = $('#dimension_weight_from').val();
var dimension_weight_to = $('#dimension_weight_to').val();

var palets_from = $('#palets_from').val();
var palets_to = $('#palets_to').val();

var displacement_from = $('#displacement_from').val();
var displacement_to = $('#displacement_to').val();

if(productivity_from !=undefined && productivity_from !='' && checkNumeric(productivity_from) == false){ alert("Только цифры"); return false}
if(productivity_to !=undefined && productivity_to !='' && checkNumeric(productivity_to) == false){ alert("Только цифры"); return false}
if(capacity_body_from !=undefined && capacity_body_from !='' && checkNumeric(capacity_body_from) == false){ alert("Только цифры"); return false}
if(capacity_body_to !=undefined && capacity_body_to !='' && checkNumeric(capacity_body_to) == false){ alert("Только цифры"); return false}
if(admission_height_from !=undefined && admission_height_from !='' && checkNumeric(admission_height_from) == false){ alert("Только цифры"); return false}
if(admission_height_to !=undefined && admission_height_to !='' && checkNumeric(admission_height_to) == false){ alert("Только цифры"); return false}
if(arm_boom_from !=undefined && arm_boom_from !='' && checkNumeric(arm_boom_from) == false){ alert("Только цифры"); return false}
if(arm_boom_to !=undefined && arm_boom_to !='' && checkNumeric(arm_boom_to) == false){ alert("Только цифры"); return false}
if(capacity_bucket_from !=undefined && capacity_bucket_from !='' && checkNumeric(capacity_bucket_from) == false){ alert("Только цифры"); return false}
if(capacity_bucket_to !=undefined && capacity_bucket_to !='' && checkNumeric(capacity_bucket_to) == false){ alert("Только цифры"); return false}
if(lift_height_from !=undefined && lift_height_from !='' && checkNumeric(lift_height_from) == false){ alert("Только цифры"); return false}
if(lift_height_to !=undefined && lift_height_to !='' && checkNumeric(lift_height_to) == false){ alert("Только цифры"); return false}
if(dep_big_from !=undefined && dep_big_from !='' && checkNumeric(dep_big_from) == false){ alert("Только цифры"); return false}
if(dep_big_to !=undefined && dep_big_to !='' && checkNumeric(dep_big_to) == false){ alert("Только цифры"); return false}
if(laying_width_from !=undefined && laying_width_from !='' && checkNumeric(laying_width_from) == false){ alert("Только цифры"); return false}
if(laying_width_to !=undefined && laying_width_to !='' && checkNumeric(laying_width_to) == false){ alert("Только цифры"); return false}
if(caterpillars_width_from !=undefined && caterpillars_width_from !='' && checkNumeric(caterpillars_width_from) == false){ alert("Только цифры"); return false}
if(caterpillars_width_to !=undefined && caterpillars_width_to !='' && checkNumeric(caterpillars_width_to) == false){ alert("Только цифры"); return false}
if(dimension_length_from !=undefined && dimension_length_from !='' && checkNumeric(dimension_length_from) == false){ alert("Только цифры"); return false}
if(dimension_length_to !=undefined && dimension_length_to !='' && checkNumeric(dimension_length_to) == false){ alert("Только цифры"); return false}
if(dimension_width_from !=undefined && dimension_width_from !='' && checkNumeric(dimension_width_from) == false){ alert("Только цифры"); return false}
if(dimension_width_to !=undefined && dimension_width_to !='' && checkNumeric(dimension_width_to) == false){ alert("Только цифры"); return false}
if(dimension_height_from !=undefined && dimension_height_from !='' && checkNumeric(dimension_height_from) == false){ alert("Только цифры"); return false}
if(dimension_height_to !=undefined && dimension_height_to !='' && checkNumeric(dimension_height_to) == false){ alert("Только цифры"); return false}
if(dimension_weight_from !=undefined && dimension_weight_from !='' && checkNumeric(dimension_weight_from) == false){ alert("Только цифры"); return false}
if(dimension_weight_to !=undefined && dimension_weight_to !='' && checkNumeric(dimension_weight_to) == false){ alert("Только цифры"); return false}
if(palets_from !=undefined && palets_from !='' && checkNumeric(palets_from) == false){ alert("Только цифры"); return false}
if(palets_to !=undefined && palets_to !='' && checkNumeric(palets_to) == false){ alert("Только цифры"); return false}
if(displacement_from !=undefined && displacement_from !='' && checkNumeric(displacement_from) == false){ alert("Только цифры"); return false}
if(displacement_to !=undefined && displacement_to !='' && checkNumeric(displacement_to) == false){ alert("Только цифры"); return false}
/*-------------------------------------------*/
 if(price_from !=undefined && price_from !='' && checkNumeric(price_from) == false){ alert("Только цифры"); return false}
 if(price_to !=undefined && price_to !='' && checkNumeric(price_to) == false){ alert("Только цифры"); return false}
 if(capacity_from !=undefined && capacity_from !='' && checkNumeric(capacity_from) == false){ alert("Только цифры"); return false}
 if(capacity_to !=undefined && capacity_to !='' && checkNumeric(capacity_to) == false){ alert("Только цифры"); return false}
 if(power_from !=undefined && power_from !='' && checkNumeric(power_from) == false){ alert("Только цифры"); return false}
 if(power_to !=undefined && power_to !='' && checkNumeric(power_to) == false){ alert("Только цифры"); return false}
 if(mileage_from !=undefined && mileage_from !='' && checkNumeric(mileage_from) == false){ alert("Только цифры"); return false}
 if(mileage_to !=undefined && mileage_to !='' && checkNumeric(mileage_to) == false){ alert("Только цифры"); return false}
 if(seats_from !=undefined && seats_from !='' && checkNumeric(seats_from) == false){ alert("Только цифры"); return false}
 if(seats_to !=undefined && seats_to !='' && checkNumeric(seats_to) == false){ alert("Только цифры"); return false}
 if(doors_from !=undefined && doors_from !='' && checkNumeric(doors_from) == false){ alert("Только цифры"); return false}
 if(doors_to !=undefined && doors_to !='' && checkNumeric(doors_to) == false){ alert("Только цифры"); return false}
 if(tonnage_from !=undefined && tonnage_from !='' && checkNumeric(tonnage_from) == false){ alert("Только цифры"); return false}
 if(tonnage_to !=undefined && tonnage_to  !='' && checkNumeric(tonnage_to ) == false){ alert("Только цифры"); return false}


/*=====================================================*/
        var filters = new Object();
    	var filters;
        
        filters['mark']             = $('#mark').val();
        filters['model']            = $('#model').val();
        filters['modify']           = $('#modify').val();
        filters['price_from']       = $('#price_from').val();
        filters['price_to']         = $('#price_to').val();
        filters['year_from']        = $('#year_from').val();
        filters['year_to']          = $('#year_to').val();

        filters['with_photo']       = $("input[name='with_photo']:checked").val();

        filters['userregion']       = $('#userregion').val();
        filters['usersubregion']    = $('#usersubregion').val();
        filters['period']           = $('#period').val();
        
        var engine                  = [];
        $("input[name='engine']:checked").each(function(i){
            engine[i] = $(this).val();
        });
        filters['engine']           = engine;
      
        
        var gearbox                 = [];
        $("input[name='gearbox']:checked").each(function(g){gearbox[g] = $(this).val();});
        filters['gearbox']          = gearbox;
        
        
        filters['capacity_from']    = $('#capacity_from').val();
        filters['capacity_to']      = $('#capacity_to').val();
        filters['power_from']       = $('#power_from').val();
        filters['power_to']         = $('#power_to').val();
       
        filters['mileage_from']     = $('#mileage_from').val();
        filters['mileage_to']       = $('#mileage_to').val();
        filters['seats_from']       = $('#seats_from').val();
        filters['seats_to']         = $('#seats_to').val();
        filters['doors_from']       = $('#doors_from').val();
        filters['doors_to']         = $('#doors_to').val();

        filters['productivity_from']        = $('#productivity_from').val();
        filters['productivity_to']          = $('#productivity_to').val();

        filters['capacity_body_from']       = $('#capacity_body_from').val();
        filters['capacity_body_to']         = $('#capacity_body_to').val();

        filters['admission_height_from']    = $('#admission_height_from').val();
        filters['admission_height_to']      = $('#admission_height_to').val();

        filters['arm_boom_from']            = $('#arm_boom_from').val();
        filters['arm_boom_to']              = $('#arm_boom_to').val();

        filters['capacity_bucket_from']     = $('#capacity_bucket_from').val();
        filters['capacity_bucket_to']       = $('#capacity_bucket_to').val();

        filters['lift_height_from']         = $('#lift_height_from').val();
        filters['lift_height_to']           = $('#lift_height_to').val();

        filters['dep_big_from']             = $('#dep_big_from').val();
        filters['dep_big_to']               = $('#dep_big_to').val();

        filters['laying_width_from']        = $('#laying_width_from').val();
        filters['laying_width_to']          = $('#laying_width_to').val();

        filters['caterpillars_width_from']  = $('#caterpillars_width_from').val();
        filters['caterpillars_width_to']    = $('#caterpillars_width_to').val();

        filters['dimension_length_from']    = $('#dimension_length_from').val();
        filters['dimension_length_to']      = $('#dimension_length_to').val();

        filters['dimension_width_from']     = $('#dimension_width_from').val();
        filters['dimension_width_to']       = $('#dimension_width_to').val();

        filters['dimension_height_from']    = $('#dimension_height_from').val();
        filters['dimension_height_to']      = $('#dimension_height_to').val();

        filters['dimension_weight_from']    = $('#dimension_weight_from').val();
        filters['dimension_weight_to']      = $('#dimension_weight_to').val();

        filters['palets_from']              = $('#palets_from').val();
        filters['palets_to']                = $('#palets_to').val();

        filters['displacement_from']        = $('#displacement_from').val();
        filters['displacement_to']          = $('#displacement_to').val();
    
        var statuses = [];
         $("input[name='statuses']:checked").each(function(s){statuses[s] = $(this).val();});
        filters['statuses']         = statuses;

        var drive = [];
        $("input[name='drive']:checked").each(function(d){drive[d] = $(this).val();});
        filters['drive']            = drive;

        var ecotype = [];
        $("input[name='ecotype']:checked").each(function(e){ecotype[e] = $(this).val();});
        filters['ecotype']          = ecotype;
        
        filters['tonnage_from']     = $('#tonnage_from').val();
        filters['tonnage_to']       = $('#tonnage_to').val();

        var axises = [];
        $("input[name='axises']:checked").each(function(a){axises[a] = $(this).val();});
        filters['axises']           = axises;
        
        var unloading = [];
        $("input[name='unloading']:checked").each(function(u){unloading[u] = $(this).val();});
        filters['unloading']        = unloading;

        var cabin = [];
        $("input[name='cabin']:checked").each(function(c){cabin[c] = $(this).val();});
        filters['cabin']            = cabin;

        var features = [];
        $("input[name='features']:checked").each(function(f){features[f] = $(this).val();});
        filters['features']         = features;

        filters['order_by'] = $('#ats_order').val();
        filters['node'] = $('#node').val();
    
    var search_str='';
  
     var j = 0;
    for(i in filters)
    {      
        var val = filters[i];
        if(val !='' && val !=undefined)
        {
            if(j==0)
            {
                search_str += ''+i+'/'+val;
            }else{
                search_str += '/'+i+'/'+val;
            }
        }
        
        j++;
    }

       var advert_type = 'technics';
       var send_string = appURL+lang+'/oldtechnicks/'+search_str;
       var newValue = send_string.replace(new RegExp( "//", "gi" ),"/");
       
       window.location = newValue;
      
}

/*Проверка вводимых данных*/
function checkNumeric(value){
    var anum=/(^\d+$)|(^\d+\.\d+$)/
    if (anum.test(value))
        return true;
		return false;
}


 
