/**
 * Funkcja uaktualniająca selecta w głównej wyszukiwarce opon.
 * 
 * @param {Object}
 *            strName
 * @param {Object}
 *            arrParams
 */
function updateTireSearcherSelect(strName,arrParams){
    var strData;
    strData = "a="+"gettsselect";
    strData += "&name="+strName;
    for(intIndex = 0; intIndex < arrParams.length; intIndex++ ){
        strData += "&arrparams["+intIndex+"]="+arrParams[intIndex];
    }
    $.ajax({
        type: "POST",
        url: "/index/get",
        data: strData,
        dataType: 'html',
        success: function(html){
            $("#"+strName).html(html);
            $("#"+strName).unbind();
            switch(strName){
                case 'selSTVehicle':
                    $("#"+strName).change(onChangeSTVehicle);
                    break;
                case 'selSTSize':
                    $("#"+strName).change(onChangeSTSize);
                    break;
            }
        }
    });
}

var intFlagBlock = 0;
function setFlag(){
    $("#selSTVehicle").attr("disabled",true);
    $("#selSTSeason").attr("disabled",true);
    intFlagBlock++;
}
function setUnFlag(){
    intFlagBlock--;
    if( intFlagBlock == 0 ){
        $("#selSTVehicle").attr("disabled",false);
        $("#selSTSeason").attr("disabled",false);
    }
}

/**
 * Akcja na wybranie nowego sezonu z paska wyszukiwania.
 */
function onChangeSTSeason(){
    /*
	 * if( $("#selSTSeason").val() == -1 ){
	 * $("#selSTVehicle").attr("disabled",true); $("#selSTVehicle").val(-1);
	 * $("#selSTSize").attr("disabled",true); $("#selSTSize").val(-1);
	 * $("#selSTProducer").attr("disabled",true); $("#selSTProducer").val(-1); }
	 * else{ $("#selSTSize").attr("disabled",true); $("#selSTSize").val(-1);
	 * $("#selSTProducer").attr("disabled",true); $("#selSTProducer").val(-1);
	 * $("#selSTVehicle").attr("disabled",false); var arrParams = new Array();
	 * arrParams[0] = $("#selSTSeason").val();
	 * updateTireSearcherSelect("selSTVehicle",arrParams); }
	 */	
    if( $("#selSTVehicle").val() != -1) {
        // pobranie danych dal rozmiaru i producenta
        var strParams = 'type=1';
        strParams += '&filter='+$("#selSTVehicle").val();
        strParams += '&filter2='+$("#selSTSeason").val();
        $("#selSTSize").attr("disabled",true);
        $("#selSTProducer").attr("disabled",true);
        setFlag();
        setFlag();
        $.post("/search/tiressel",strParams,function(data){
            $("#selSTProducer").html(data);
            $("#selSTSize").attr("disabled",false);
            $("#selSTSize").val(-1);
            $("#selSTProducer").attr("disabled",false);
            $("#selSTProducer").val(-1);
            setUnFlag();
        },"html");
        var strParams = 'type=0';
        strParams += '&filter='+$("#selSTVehicle").val();
        strParams += '&filter2='+$("#selSTSeason").val();
        $.post("/search/tiressel",strParams,function(data){
            $("#selSTSize").html(data);
            $("#selSTSize").attr("disabled",false);
            $("#selSTSize").val(-1);
            $("#selSTProducer").attr("disabled",false);
            $("#selSTProducer").val(-1);
            setUnFlag();
        },"html");
    // koniec: pobranie danych dal rozmiaru i producenta
    }
}

/**
 * Akcja na wybranie nowego typu samochodu z paska wyszukiwarki.
 */
function onChangeSTVehicle(){	
    if( $("#selSTVehicle").val() == -1 ){
        $("#selSTSize").attr("disabled",true);
        $("#selSTSize").val(-1);
        $("#selSTProducer").attr("disabled",true);
        $("#selSTProducer").val(-1);
    }
    else{
        // pobranie danych dal rozmiaru i producenta
        var strParams = 'type=1';
        strParams += '&filter='+$("#selSTVehicle").val();
        strParams += '&filter2='+$("#selSTSeason").val();
        $("#selSTSize").attr("disabled",true);
        $("#selSTProducer").attr("disabled",true);
        setFlag();
        setFlag();
        $.post("/search/tiressel",strParams,function(data){
            $("#selSTProducer").html(data);
            $("#selSTSize").attr("disabled",false);
            $("#selSTSize").val(intSizeTemp);
            $("#selSTProducer").attr("disabled",false);
            $("#selSTProducer").val(intProducerTemp);
            setUnFlag();
        },"html");
        var strParams = 'type=0';
        strParams += '&filter='+$("#selSTVehicle").val();
        strParams += '&filter2='+$("#selSTSeason").val();
        $.post("/search/tiressel",strParams,function(data){
            $("#selSTSize").html(data);
            $("#selSTSize").attr("disabled",false);
            $("#selSTSize").val(intSizeTemp);
            $("#selSTProducer").attr("disabled",false);
            $("#selSTProducer").val(intProducerTemp);
            setUnFlag();
        },"html");
    // koniec: pobranie danych dal rozmiaru i producenta
    }
}

/**
 * Akcja na wybranie nowej wielkości opon z paska wyszukiwarki.
 */
function onChangeSTSize(){
}


/*******************************************************************************
 * OBSŁUGA WYSZUKIWANIA PO MARCE
 ******************************************************************************/
/**
 * Zmiana selecta z producentem.
 */
function onChangeselSMTProducer(){
    if( $("#selSMTProducer").val() != -1 ){
        $("#selSMTModel").attr("disabled",false);
        var arrParams = new Array();
        arrParams[0] = $("#selSMTProducer").val();
        updateSMT("selSMTModel",arrParams);

    }
    else{
        $("#selSMTModel").attr("disabled",true);
        $("#selSMTVersion").attr("disabled",true);
        $("#selSMTSeason").attr("disabled",true);
        $("#selSMTModel").val(-1);
        $("#selSMTVersion").val(-1);
        $("#selSMTSeason").val(-1);
    }
}

/**
 * Zmiana selecta z modelem.
 */
function onChangeselSMTModel(){
    if( $("#selSMTModel").val() != -1 ){
        $("#selSMTVersion").attr("disabled",false);
        var arrParams = new Array();
        arrParams[0] = $("#selSMTProducer").val();
        arrParams[1] = $("#selSMTModel").val();
        updateSMT("selSMTVersion",arrParams);
    }
    else{
        $("#selSMTVersion").attr("disabled",true);
        $("#selSMTSeason").attr("disabled",true);
        $("#selSMTVersion").val(-1);
        $("#selSMTSeason").val(-1);
    }
}

/**
 * Zmiana selecta z wersją.
 */
function onChangeselSMTVersion(){
    if( $("#selSMTVersion").val() != -1 ){
        $("#selSMTSeason").attr("disabled",false);
        var arrParams = new Array();
        arrParams[0] = $("#selSMTProducer").val();
        arrParams[1] = $("#selSMTModel").val();
        arrParams[2] = $("#selSMTVersion").val();
        updateSMT("selSMTSeason",arrParams);
    }
    else{
        $("#selSMTSeason").attr("disabled",true);
        $("#selSMTSeason").val(-1);
    }

}

/**
 * Funkcja pobierająca selecty do wyszukiwarki.
 * 
 * @param {Object}
 *            strName
 * @param {Object}
 *            arrParams
 */
function updateSMT(strName,arrParams){
    var strData;
    strData = "a="+"gettiresprod";
    strData += "&name="+strName;
    for(intIndex = 0; intIndex < arrParams.length; intIndex++ ){
        strData += "&arrparams["+intIndex+"]="+arrParams[intIndex];
    }
    $.ajax({
        type: "POST",
        url: "/index/get",
        data: strData,
        dataType: 'html',
        success: function(html){
            $("#"+strName).html(html);
            $("#"+strName).unbind();
            switch(strName){
                case 'selSMTModel':
                    $("#"+strName).change(onChangeselSMTModel);
                    break;
                case 'selSMTVersion':
                    $("#"+strName).change(onChangeselSMTVersion);
                    break;
            }
        }
    });
}

function onChangeTirechainSize(){
    if( $("#tirechainSizes").val() != -1 ){
        var strData = '&size='+$("#tirechainSizes").val();
        $.ajax({
            type: "POST",
            url: "/searcher/gettirechainproducers",
            data: strData,
            dataType: 'html',
            success: function(html){
                $('#tirechainProducers').empty().append(html);
            }
        });
    }
    else {
        $.ajax({
            type: "POST",
            url: "/searcher/gettirechainproducers",
            dataType: 'html',
            success: function(html){
                $('#tirechainProducers').empty().append(html);
            }
        });
    }
}

function onChangeAccumulatorVehicleType(){
    if( $("#accumulatorsVehicleTypes").val() != -1 ){
        var strData = '&vehicleType='+$("#accumulatorsVehicleTypes").val();
        $.ajax({
            type: "POST",
            url: "/searcher/getaccumulatorsproducer",
            data: strData,
            dataType: 'html',
            success: function(html){
                $('#accumulatorsProducers').removeAttr('disabled');
                $('#accumulatorsProducers').empty().append(html);
            }
        });
    }
    else {
        $('#accumulatorsProducers').empty().append('<option value="-1">wybierz</option>');
        $('#accumulatorsCapacity').empty().append('<option value="-1">wybierz</option>');
        $('#accumulatorsProducers').attr('disabled',true);
        $('#accumulatorsCapacity').attr('disabled',true);
    }
}

function onChangeAccumulatorProducer(){
    if( $("#accumulatorsProducers").val() != -1 && $("#accumulatorsVehicleTypes").val() != -1){
        var strData  = '&producer='+$("#accumulatorsProducers").val();
            strData += '&vehicleType='+$("#accumulatorsVehicleTypes").val();
        $.ajax({
            type: "POST",
            url: "/searcher/getaccumulatorscapacity",
            data: strData,
            dataType: 'html',
            success: function(html){
                $('#accumulatorsCapacity').removeAttr('disabled');
                $('#accumulatorsCapacity').empty().append(html);
            }
        });
    }
    else {
        $('#accumulatorsCapacity').empty().append('<option value="-1">wybierz</option>');
        $('#accumulatorsCapacity').attr('disabled',true);
    }
}

function onChangeAccumulatorsVehicles(){
	if( $("#vehicles").val() != -1 ){
		/*
		 * Wyzerowanie pojemności jeśli była wcześniej wybrana
		 */
        if( $("#vehicleProducers").val() != -1 ){
            $('#vehicleProducers').empty().append('<option value="-1">wybierz</option>');
            $('#vehicleProducers').attr('disabled',true);
        }
        /*
		 * Wyzerowanie pojemności jeśli była wcześniej wybrana
		 */
        if( $("#vehicleModels").val() != -1 ){
            $('#vehicleModels').empty().append('<option value="-1">wybierz</option>');
            $('#vehicleModels').attr('disabled',true);
        }
		/*
		 * Wyzerowanie pojemności jeśli była wcześniej wybrana
		 */
        if( $("#vehicleCapacity").val() != -1 ){
            $('#vehicleCapacity').empty().append('<option value="-1">wybierz</option>');
            $('#vehicleCapacity').attr('disabled',true);
        }
        /*
		 * Wyzerowanie roku produkcji
		 */
        $('#vehicleYearProduction').empty().append('<option value="-1">wybierz</option>');
        $('#vehicleYearProduction').attr('disabled',true);

        var strData = '&vehicles='+$("#vehicles").val();
        $.ajax({
            type: "POST",
            url: "/searcher/getvehicleproducers",
            data: strData,
            dataType: 'html',
            success: function(html){
                $('#vehicleProducers').removeAttr('disabled');
                $('#vehicleProducers').empty().append(html);
            }
        });
    }
    else {
    	$('#vehicleProducers').empty().append('<option value="-1">wybierz</option>');
        $('#vehicleCapacity').empty().append('<option value="-1">wybierz</option>');
        $('#vehicleModels').empty().append('<option value="-1">wybierz</option>');
        $('#vehicleProducers').attr('disabled',true);
        $('#vehicleCapacity').attr('disabled',true);
        $('#vehicleModels').attr('disabled',true);
        $('#vehicleYearProduction').empty().append('<option value="-1">wybierz</option>');
        $('#vehicleYearProduction').attr('disabled',true);
    }
}

function onChangeVehicleProducer(){
    if( $("#vehicles").val() != -1 && $("#vehicleProducers").val() != -1 ){
        /*
		 * Wyzerowanie pojemności jeśli była wcześniej wybrana
		 */
            $('#vehicleCapacity').empty().append('<option value="-1">wybierz</option>');
            $('#vehicleCapacity').attr('disabled',true);
        /*
		 * Wyzerowanie roku produkcji
		 */
            $('#vehicleYearProduction').empty().append('<option value="-1">wybierz</option>');
            $('#vehicleYearProduction').attr('disabled',true);

        var strData = '&vehicles='+$("#vehicles").val()+'&vehicleProducer='+$("#vehicleProducers").val();
        $.ajax({
            type: "POST",
            url: "/searcher/getvehiclemodels",
            data: strData,
            dataType: 'html',
            success: function(html){
                $('#vehicleModels').removeAttr('disabled');
                $('#vehicleModels').empty().append(html);
            }
        });
    }
    else {
        $('#vehicleCapacity').empty().append('<option value="-1">wybierz</option>');
        $('#vehicleModels').empty().append('<option value="-1">wybierz</option>');
        $('#vehicleCapacity').attr('disabled',true);
        $('#vehicleModels').attr('disabled',true);
        $('#vehicleYearProduction').empty().append('<option value="-1">wybierz</option>');
        $('#vehicleYearProduction').attr('disabled',true);
    }
}

function onChangeVehicleModel(){
    if( $("#vehicles").val() != -1 && $("#vehicleProducers").val() != -1 && $("#vehicleModels").val() != -1){
        /*
		 * Wyzerowanie roku produkcji
		 */
        $('#vehicleYearProduction').empty().append('<option value="-1">wybierz</option>');
        $('#vehicleYearProduction').attr('disabled',true);
        var strData  = '&vehicles='+$("#vehicles").val()+'&vehicleProducer='+$("#vehicleProducers").val();
            strData += '&vehicleModel='+$("#vehicleModels").val();
        $.ajax({
            type: "POST",
            url: "/searcher/getvehiclecapacity",
            data: strData,
            dataType: 'html',
            success: function(html){
                $('#vehicleCapacity').removeAttr('disabled');
                $('#vehicleCapacity').empty().append(html);
            }
        });
    }
    else {
        $('#vehicleCapacity').empty().append('<option value="-1">wybierz</option>');
        $('#vehicleCapacity').attr('disabled',true);
        $('#vehicleYearProduction').empty().append('<option value="-1">wybierz</option>');
        $('#vehicleYearProduction').attr('disabled',true);
    }
}

function onChangeVehicleCapacity(){
    if( $("#vehicles").val() != -1 && $("#vehicleProducers").val() != -1 && $("#vehicleModels").val() != -1  && $("#vehicleCapacity").val() != -1){
        var strData  = '&vehicles='+$("#vehicles").val()+'&vehicleProducer='+$("#vehicleProducers").val();
            strData += '&vehicleModel='+$("#vehicleModels").val();
            strData += '&vehicleCapacity='+$('#vehicleCapacity').val();
        $.ajax({
            type: "POST",
            url: "/searcher/getyearproduction",
            data: strData,
            dataType: 'html',
            success: function(html){
                $('#vehicleYearProduction').removeAttr('disabled');
                $('#vehicleYearProduction').empty().append(html);
            }
        });
    }
    else {
        $('#vehicleYearProduction').empty().append('<option value="-1">wybierz</option>');
        $('#vehicleYearProduction').attr('disabled',true);
    }
}

$(document).ready(function(){
    /*
	 * Konfigurator Łańcuchów
	 */
    $('#tirechainSizes').change(onChangeTirechainSize);

    /*
	 * Konfigurator akumulatorów
	 */

    $('#accumulatorsVehicleTypes').change(onChangeAccumulatorVehicleType);
    $('#accumulatorsProducers').change(onChangeAccumulatorProducer);
    /*
     * Konfigurator akumualtorów wg typu pojazdu, marki, modelu itd...
     */
    $('#vehicles').change(onChangeAccumulatorsVehicles);

    /*
	 * Konfigurator akumulatorów
	 */
    $('#vehicleProducers').change(onChangeVehicleProducer);
    $('#vehicleModels').change(onChangeVehicleModel);
    $('#vehicleCapacity').change(onChangeVehicleCapacity);

    /*
	 * Opony. Wybranie sezonu.
	 */
    $("#selSTSeason").change(onChangeSTSeason);

    /*
	 * Opony. Wybranie rodzaju.
	 */
    $("#selSTVehicle").change(onChangeSTVehicle);
	
    /*
	 * Opony. Wybranie rozmiaru.
	 */
    $("#selSTSize").change(onChangeSTSize);
	
    /*
	 * Ustawienie wyboru sezonu na pozycje 'Wybierz'.
	 */
    if( $("#intTireSearcherReset").val() == 0 )
        $("#selSTSeason").val(-1);
	
    /*
	 * Wybór paczki z wynikami dla opon.
	 */
    $(".cPackLink").click(function(){
        var intId = $(this).attr("id");
        intId = intId.split("_");
        intId = intId[1];
        $("#intStartTires").val(intId);
        $("#sklep_50").submit();
    });
    $(".cPackLinkS").click(function(){
        var intId = $(this).attr("id");
        intId = intId.split("_");
        intId = intId[1];
        $("#intStartSrims").val(intId);
        $("#sklep_503").submit();
    });
    $(".cPackLinkA").click(function(){
        var intId = $(this).attr("id");
        intId = intId.split("_");
        intId = intId[1];
        $("#intStartArims").val(intId);
        $("#sklep_504").submit();
    });
	
    /*
	 * Zmiana sortowania wyników.
	 */
    $("#selSortTires").change(function(){
        $("#intSortTires").val($(this).val());
        $("#sklep_50").submit();
    });
	
    $("#selSortSrims").change(function(){
        $("#intSortSrims").val($(this).val());
        $("#sklep_503").submit();
    });

    $("#selSortArims").change(function(){
        $("#intSortArims").val($(this).val());
        $("#sklep_504").submit();
    });
	
    /***************************************************************************
	 * OBSŁUGA WYSZUKIWANIA PO MARCE
	 **************************************************************************/
    $("#selSMTProducer").change(onChangeselSMTProducer);
    $("#selSMTModel").change(onChangeselSMTModel);
    $("#selSMTVersion").change(onChangeselSMTVersion);
    /***************************************************************************
	 * Koniec: OBSŁUGA WYSZUKIWANIA PO MARCE
	 **************************************************************************/
	
    /***************************************************************************
	 * Sprawdzanie poprawności pola ilości produktów oraz Obliczanie wartości
	 * przesyłki w wynikach
	 **************************************************************************/
    $(".cTireCounter").change(function(){
        var intValue = parseInt( $(this).val() );
        if (isNaN(intValue) || intValue == 'NaN' || intValue < 1 || intValue > 20) {
            intValue = 4;
        }
        else {
            $(this).val(intValue);
        }
		
        var intId = $(this).attr('id');
        intId = intId.split('_');
        intId = intId[1];
        // uaktualnienie ukrytego inputa z liczbą sztuk
        $('#txtTireCounter_'+intId).val(intValue);
        // uaktualnienie wartości opisu
        var strData;
        strData = "a="+"shipmentcosttire";
        strData += "&oid="+intId;
        strData += "&counter="+intValue;
        $.ajax({
            type: "POST",
            url: "/searcher/get",
            data: strData,
            dataType: 'json',
            success: function(json){
                if( json.DATA > 0 ){
                    $("#spanShipmentCost_"+intId).html('Koszt dostawy: ' +json.DATA + ' zł');
                }
                else{
                    $("#spanShipmentCost_"+intId).html('Koszt dostawy ustalany indywidualnie');
                }
            }
        });
    });

    /*
	 * Obliczanie ceny dostawy dla akumulatorów
	 */

    $('.cAccumulatorCounter').change(function(){
        var intValue = parseInt( $(this).val() );
        if (isNaN(intValue) || intValue == 'NaN' || intValue < 1 || intValue > 20) {
            intValue = 1;
        }
        else {
            $(this).val(intValue);
        }
        var intId = $(this).attr('id');
        intId = intId.split('_');
        intId = intId[1];
        // uaktualnienie ukrytego inputa z liczbą sztuk
        $('#txtAccumulatorCounter_'+intId).val(intValue);
        // uaktualnienie wartości opisu
        var strData;
        strData = "a=shipmentcostaccumulator";
        strData += "&oid="+intId;
        strData += "&counter="+intValue;
        $.ajax({
            type: "POST",
            url: "/searcher/get",
            data: strData,
            dataType: 'json',
            success: function(json){
                $("#spanShipmentCost_"+intId).html('Koszt dostawy: ' + json.DATA + ' zł');
            }
        });
    });

    $('.cTirechainCounter').change(function(){
        var intValue = parseInt( $(this).val() );
        if (isNaN(intValue) || intValue == 'NaN' || intValue < 1 || intValue > 20) {
            intValue = 1;
        }
        else {
            $(this).val(intValue);
        }
        var intId = $(this).attr('id');
        intId = intId.split('_');
        intId = intId[1];
        // uaktualnienie ukrytego inputa z liczbą sztuk
        $('#txtTirechainCounter_'+intId).val(intValue);
        // uaktualnienie wartości opisu
        var strData;
        strData = "a=shipmentcosttirechain";
        strData += "&oid="+intId;
        strData += "&counter="+intValue;
        $.ajax({
            type: "POST",
            url: "/searcher/get",
            data: strData,
            dataType: 'json',
            success: function(json){
                $("#spanShipmentCost_"+intId).html('Koszt dostawy: ' + json.DATA + ' zł');
            }
        });
    });

    /***************************************************************************
	 * Obliczanie wartości przesyłki w wynikach
	 **************************************************************************/

    /**
	 * Wyszukiwanie felgi aluminiowej
	 */
    $("#submitArims").click(function(){
        $("#intStartArims").val(0);
        $("#sklep_504").submit();
    });

    /**
	 * Wyszukiwanie felgi stalowej
	 */
    $("#submitSrims").click(function(){
        $("#intStartSrims").val(0);
        $("#sklep_503").submit();
    });

    /**
	 * Wyszukiwanie opony
	 */
    $("#submitTires").click(function(){
        $("#intStartTires").val(0);
        $("#sklep_50").submit();
    });

    /**
	 * Wyszukiwanie opony producenci
	 */
    $("#submitTiresProducers").click(function(){
        $("#intStartTires").val(0);
        $("#sklep_502").submit();
    });

    $('#submitTirechains').click(function(){
        $("#intStartTirechains").val(0);
        $('#tirechainConfigurator').submit();
    });

    onChangeSTVehicle();
});