$(document).ready(function(){
    $.get('/frontoffice/ajax/formashina.php',{},function(d){
        $("#rezina").html(d);
    })
})
$(document).ready(function(){
    $.get('/frontoffice/ajax/formadiski.php',{},function(d){
        $("#discs").html(d);
    })
})
function podbordisk(){
        var model=$("#podbordiskbrand").val()
        var J=$("#diskshirina").val()
        var R=$("#diskdiametr").val()
        var pcd=$("#pcd").val()
        var et=$("#diskvylet").val()
        var co=$("#centerotv").val()
        if ($("#vpm").is(":checked")) var dopusk=1 
        else var dopusk=0 
//        $.get('/frontoffice/ajax/diski.php',{model:model,J:J,R:R,pcd:pcd,et:et,co:co,dopusk:dopusk},function(d){alert(d)})
        $.getJSON('/frontoffice/ajax/diski.php',{model:model,J:J,R:R,pcd:pcd,et:et,co:co,dopusk:dopusk},function(d){
        if (d.length){
          var table='<table class="podbor" border cellpadding="2" cellspacing="0" align="center">'
        table+="<tr>"
        table+="<td>Производитель</td>"
        table+="<td>Модель</td>"
        table+="<td>J</td>"
        table+="<td>R</td>"
        table+="<td>Отверстий</td>"
        table+="<td>Диаметр</td>"
        table+="<td>Центр.отвертсие</td>"
        table+="<td>Вылет</td>"
        table+="<td>Цена</td>"
        table+="<td>Наличие</td>"
        table+="<td>&nbsp;</td>"
        table+="</tr>"
        $.each(d, function(k,v){
            table+="<tr>"
            table+='<td>'+v.brand+'</td>'
            table+='<td>'+v.model+'</td>'
            table+='<td>'+v.J+'</td>'
            table+='<td>'+v.R+'</td>'
            table+='<td>'+v.colotv+'</td>'
            table+='<td>'+v.diametr+'</td>'
            table+='<td>'+v.centerotv+'</td>'
            table+='<td>'+v.vylet+'</td>'
            var ps=parseFloat(v.price)
            table+='<td>'+ps+' руб.</td>'
            table+='<td>'+v.nal+'</td>'
            table+='<td width="60"><input type=text id="shina_col'+k+'" size=1 value=1 />\n\
<img src="/img/pictograms/basket.gif" onclick="in_cart('+k+','+v.id+','+v.price+',\'disk\')" style="cursor: pointer;"/></td>'
            table+="</tr>"
        })
        table+="</table>"
        $('#soderzh').html(table).show();
        }else{
           $('#soderzh').html("Данным параметрам не соотвествует ни один товар").show(); 
        }
        })
    }
    
function podborshina(){
//        $("#soderzh").html('<div id="result"></div').show();
        var model=$("#podborshinamarka").val()
        var vysota=$("#vysota").val()
        var profil=$("#profil").val()
        var diametr=$("#radius").val()
        var sezon=$("#podborshinasezon").val()
//        $.get('/frontoffice/ajax/shina.php',{model:model,vysota:vysota,profil:profil,diametr:diametr,sezon:sezon},function(d){
//            alert(d);
//        })
        $.getJSON('/frontoffice/ajax/shina.php',{model:model,vysota:vysota,profil:profil,diametr:diametr,sezon:sezon},function(d){
        if (d.length){
        var table='<table class="podbor" border cellpadding="2" cellspacing="0" align="center">'
        table+="<tr>"
        table+="<td>Производитель</td>"
        table+="<td>Модель</td>"
        table+="<td>Типоразмер</td>"
        table+="<td>Нагрузка</td>"
        table+="<td>Индекс скорости</td>"
        table+="<td>Цена</td>"
        table+="<td>Наличие</td>"
        table+="<td>&nbsp;</td>"
        table+="</tr>"
        $.each(d, function(k,v){
            table+="<tr>"
            table+='<td>'+v.brand+'</td>'
            table+='<td>'+v.model+'</td>'
            table+='<td>'+v.typorazmer+'</td>'
            table+='<td>'+v.nagruzka+'</td>'
            table+='<td>'+v.speedindex+'</td>'
            var ps=parseFloat(v.price)
            table+='<td>'+ps+' руб.</td>'
            table+='<td>'+v.nal+'</td>'
            table+='<td width="60"><input type=text id="shina_col'+k+'" size=1 value=1 />\n\
<img src="/img/pictograms/basket.gif" onclick=in_cart('+k+','+v.id+','+ps+',"shina") style="cursor: pointer;"/></td>'
            table+="</tr>"
        })
        table+="</table>"
        $('#soderzh').html(table);
        }else{
            $('#soderzh').html("Данному запросу не соотвествует ни один товар");
        }
        })
    }
function showcena(k){
    $('.uc').show();
    $('.cena').hide();
    $('#ucen'+k).hide();
    $('#cen'+k).show();
}

$(document).ready(function(){
    $.getJSON('/frontoffice/ajax/selectmarka.php',{},function(d){
        var marka='<select id="marka" size=1 style="width: 189px;" onchange="selyear()"><option value="">Выберите автомобиль</option>'
        $.each(d, function(k,v){
            marka+='<option value="'+v.marka+'">'+v.marka+'</option>'
        })
        marka+='</select>'
        $('#mashina_marka').html(marka)
    })
})

function selyear(){
    $('#mashina_year').show()
     $('#mashina_modify').hide();
    $('#mashina_specif').hide();
    $('#result').hide();
    var marka=$('#marka').val()
    $.getJSON('/frontoffice/ajax/selectyear.php',{marka:marka},function(d){
        var year='<select id="year" style="width: 189px;" size=1 onchange="selmodel(\''+marka+'\')"><option value="">Укажите год выпуска</option>'
         $.each(d, function(k,v){
                year+='<option value="'+v.year+'">'+v.year+'</option>'
             })
            year+='</select>'
            $('#mashina_year').html(year).show()
    })
}
function selmodel(marka){
    $('#mashina_modify').show();
    $('#mashina_specif').hide();
    $('#result').hide();
    var year=$("#year").val();
        $.getJSON('/frontoffice/ajax/selectmodel.php',{marka:marka,year:year},function(d){
             var model='<select id="model" style="width: 189px;" size=1 onchange="selmodify(\''+marka+'\',\''+year+'\')"><option value="">Выберите модель</option>'
             $.each(d, function(k,v){
                model+='<option value="'+v.model+'">'+v.model+'</option>'
             })
            model+='</select>'
            $('#mashina_model').html(model).show()
        })
    
}
function selmodify(marka,year){
    $('#mashina_modify').show();
    var model=$("#model").val();
    $.getJSON('/frontoffice/ajax/selectmodify.php',{marka:marka,model:model,year:year},function(d){
        var modify='<select id="modify" style="width: 189px;" size=1 onchange="showspec()"><option value="">Выберите модификацию</option>'
             $.each(d, function(k,v){
                if (v.manfstop==0) v.manfstop=' по настоящее время';
                modify+='<option value="'+v.id+'">'+v.engine+'('+v.power+'), '+v.gear+'</option>'
             })
            modify+='</select>'
            $("#mashina_modify").html(modify).show();
    })
    $('#mashina_specif').hide();
    $('#result').hide();
}
function showspec(){
    $('#soderzh').html('<div id="mashina_specif"></div><div id="result"></div').show();
    var id=$("#modify").val();
//    $.get("/frontoffice/ajax/selectspec.php",{id:id},function(d){
//        alert (d)
//    })
    $.getJSON("/frontoffice/ajax/selectspec.php",{id:id},function(d){
        $("#zagl").html('<h3>Возможные варианты шин и дисков на машину</h3>')
           var table='<table border class="podbor" cellpadding=5 width="650">'
           table+="</tr>"
           table+='<td class="th">Шина</td>'
           table+='<td class="th">Ширина</td>'
           table+='<td class="th">Относительная высота</td>'
           table+='<td class="th">Тип</td>'
           table+='<td class="th">Посадочный диаметр</td>'
           table+='<td class="th">Индекс скорости</td>'
           table+='<td class="th">Индекс нагрузки</td>'
           table+='<td class="th">Установка</td>'
           table+="</tr>"
           $.each(d, function(k,v){
              if (v.ustanovka=="тюнинг") var clas='tun'
              else var clas='st'
           table+='<tr>'
           table+='<td class="'+clas+'"><a class="pdbr" href="javascript:selshina('+v.profil+','+v.vysota+',\''+v.type+'\','+v.diametr+','+v.loadindex+',\''+v.speedindex+'\')">подобрать шины</a></td>'
           table+='<td class="'+clas+'">'+v.profil+'</td>'
           table+='<td class="'+clas+'">'+v.vysota+'</td>'
           table+='<td class="'+clas+'">'+v.type+'</td>'
           table+='<td class="'+clas+'">'+v.diametr+'</td>'
           table+='<td class="'+clas+'">'+v.speedindex+'</td>'
           table+='<td class="'+clas+'">'+v.loadindex+'</td>'
           table+='<td class="'+clas+'">'+v.ustanovka+'</td>'
           table+='</tr>'
           })
           table+='</table>'
       $('#mashina_specif').html('<p>'+table+'</p>').show();
    })
//    $.get("/frontoffice/ajax/selectspecdisk.php",{id:id},function(d){
//        alert(d);
//    })
    
     $.getJSON("/frontoffice/ajax/selectspecdisk.php",{id:id},function(d){
           var table='<table width="667" border cellpadding=5 class="podbor">'
           table+="</tr>"
           table+='<td class="th">Диск</td>'
           table+='<td class="th">Ширина</td>'
           table+='<td class="th">Диаметр внешний</td>'
           table+='<td class="th">Центральное отверстие</td>'
           table+='<td class="th">Вылет</td>'
           table+='<td class="th">Крепление к ступице</td>'
           table+='<td class="th">Крепеж</td>'
           table+='<td class="th">Установка</td>'
           table+="</tr>"
           $.each(d, function(k,v){
                if (v.ustanovka=="тюнинг") var clas='tun'
              else var clas='st'
           table+='<tr>'
           table+='<td class="'+clas+'"><a class="pdbr" href="javascript:seldisk('+v.J+','+v.R+','+v.numkrepotv+','+v.okrkrepotv+','+v.centralotv+','+v.vylet+')">подобрать диски</a></td>'
//           table+='<td><a href="#">подобрать диск</a></td>'
           table+='<td class="'+clas+'">'+v.J+'</td>'
           table+='<td class="'+clas+'">'+v.R+'</td>'
           table+='<td class="'+clas+'">'+v.centralotv+'</td>'
           table+='<td class="'+clas+'">'+v.vylet+'</td>'
           table+='<td class="'+clas+'">'+v.numkrepotv+'x'+v.okrkrepotv+'</td>'
           table+='<td class="'+clas+'">'+v.tipkrep+' '+v.rezba+'</td>'
           table+='<td class="'+clas+'">'+v.ustanovka+'</td>'
           table+='</tr>'
           })
           table+='</table>'
       $('#mashina_specif').append('<p>'+table+'</p>').show();
    })
}


function seldisk(J,R,colotv,diametr,centerotv,vylet){
//   $.get('/frontoffice/ajax/select_disk.php',{J:J,R:R,colotv:colotv,diametr:diametr,centerotv:centerotv,vylet:vylet}, function(d){
//       alert(d);
//   })
    $.getJSON('/frontoffice/ajax/select_disk.php',{J:J,R:R,colotv:colotv,diametr:diametr,centerotv:centerotv,vylet:vylet}, function(d){
        var table='<table width="667" border cellpadding=5 cellspacing=0 class=podbor>'
        table+="<tr>"
        table+='<td class="th">Производитель</td>'
        table+='<td class="th">Модель</td>'
        table+='<td class="th">J</td>'
        table+='<td class="th">R</td>'
        table+='<td class="th">Отверстий</td>'
        table+='<td class="th">Диаметр</td>'
        table+='<td class="th">Центр.отвертсие</td>'
        table+='<td class="th">Вылет</td>'
        table+='<td class="th">Цена</td>'
        table+='<td class="th">Наличие</td>'
        table+='<td class="th">&nbsp;</td>'
        table+='</tr>'
        $.each(d, function(k,v){
            table+="<tr>"
            table+='<td>'+v.brand+'</td>'
            table+='<td>'+v.model+'</td>'
            table+='<td>'+v.J+'</td>'
            table+='<td>'+v.R+'</td>'
            table+='<td>'+v.colotv+'</td>'
            table+='<td>'+v.diametr+'</td>'
            table+='<td>'+v.centerotv+'</td>'
            table+='<td>'+v.vylet+'</td>'
            var ps=parseFloat(v.price)
            table+='<td>'+ps+' руб.</td>'
            table+='<td>'+v.nal+'</td>'
            table+='<td><input type=text id="shina_col'+k+'" size=1 value=1 />\n\
<img src="/img/pictograms/basket.gif" onclick="in_cart('+k+','+v.id+','+v.price+',\'disk\')" style="cursor: pointer;"/></td>'
            table+="</tr>"
        })
        table+="</table>"
        $("#zagl").html('<h3>Результат подбора дисков</h3>')
        $('#result').html('<p>'+table+'</p>').show();
    })
}


function selshina(profil,vysota,typ,diametr,loadindex,speedindex){
//   $.get('/frontoffice/ajax/select_shina.php',{profil:profil,vysota:vysota,typ:typ,diametr:diametr,loadindex:loadindex,speedindex:speedindex}, function(d){
//       alert(d);
//   })
    $.getJSON('/frontoffice/ajax/select_shina.php',{profil:profil,vysota:vysota,typ:typ,diametr:diametr,loadindex:loadindex,speedindex:speedindex}, function(d){
        var table='<table border cellpadding=5 cellspacing=0 class="podbor">'
        table+='<tr>'
        table+='<td class="th">Произв.</td>'
        table+='<td class="th">Модель</td>'
        table+='<td class="th">Размер</td>'
        table+='<td class="th">Инд. нагр.</td>'
        table+='<td class="th">Инд. скор.</td>'
        table+='<td class="th">Цена</td>'
        table+='<td class="th">Наличие</td>'
        table+='<td class="th">&nbsp;</td>'
        table+='<td class="th">Огранич.</td>'
        table+='</tr>'
        $.each(d, function(k,v){
            table+="<tr>"
            table+='<td>'+v.brand+'</td>'
            table+='<td>'+v.model+'</td>'
            table+='<td>'+v.typorazmer+'</td>'
            table+='<td>'+v.nagruzka+'</td>'
            table+='<td>'+v.speedindex+'</td>'
            var ps=parseFloat(v.price)
            table+='<td>'+ps+' руб.</td>'
            table+='<td>'+v.nal+'</td>'
            table+='<td><input type=text id="shina_col'+k+'" size=1 value=1 />\n\
<img src="/img/pictograms/basket.gif" onclick=in_cart('+k+','+v.id+','+ps+',"shina") style="cursor: pointer;"/></td>'
            table+='<td>'+v.limits+'</td>'
            table+="</tr>"
        })
        table+="</table>"
        $("#zagl").html('<h3>Результат подбора шин</h3>')
        $('#result').html('<p>'+table+'</p>').show();
    })
}
