// JavaScript Document
var dostawy = new Object();
var stany_magazynu = new Object();

//czy dostawy maja byc pokazywane
var dostawy_pokaz = 1;

//identyfikator towaru dla którego są pobierane dostawy
var dostawy_towar_id = 0;

o_dostawy_xml_http = createXmlHttpRequestObject();


function get_dostawy( towar_id ){

    if( typeof(dostawy[towar_id]) == 'object' ){
        dostawy_print( dostawy[towar_id], stany_magazynu[towar_id] );
    }else{
        overlib('Czekaj trwa pobieranie danych!<br><center><img src=' + SZABLON_URL + '/images/loading.gif></center>');

        dostawy_towar_id = towar_id;

        server_connect(o_dostawy_xml_http, SUGGEST_URL + '?action=t&id='+towar_id+'&dostawy=1', { 'funkcja' : dostawy_handle_response, 'pomin_bledy' : true  } );
    }
}


function dostawy_print( a_dostawy, stan_magazynu ){
    //if( zalaczniki.length == 0) return;
    var i = 0;

    dostawy_info = 'Brak dostaw do wyświetlenia!';
    
    if( typeof(a_dostawy) == 'string'  ){
        dostawy_info = a_dostawy;
    }else if( typeof(a_dostawy) == 'object' && a_dostawy.length > 0 ){
        
        dostawy_info = '';
        dostawy_info += '<table width=100% align="center" cellpadding=2 cellspacing=0 class="grid" id="zalaczniki_tab_grid">';
        dostawy_info += '<tr>';
        dostawy_info += '  <th align=center width=100>Magzyn</th>';
        dostawy_info += '  <th align=center >Numer Dokumentu</th>';
        dostawy_info += '  <th align=center >Cena</th>';
        dostawy_info += '  <th align=center >Dostępność</th>';
        dostawy_info += '  <th align=center >Przydatność</th>';
        dostawy_info += '</tr>';

        for( i = 0; i < a_dostawy.length; i++ ){
            dostawy_info += '<tr class="wiersz' + ( (i%2) + 1 ) + '">';
            dostawy_info += '    <td align=center width=100>' + a_dostawy[i]['MAGAZYN_NUMER'] +  '</td>';
            dostawy_info += '    <td align=center width=100>' + a_dostawy[i]['NUMER_DOKUMENTU'] +  '</td>';
            dostawy_info += '    <td align=center width=100>' + currency_format( a_dostawy[i]['CENA_ZAKUPU'] )+  '</td>';
            dostawy_info += '    <td align=center width=100>' + parseFloat(a_dostawy[i]['ILOSC_DOSTEPNA'])  + '/' + parseFloat(a_dostawy[i]['ILOSC']) +  '</td>';
            dostawy_info += '    <td align=center width=100>' + a_dostawy[i]['TERMIN_PRZYD'] +  '</td>';
            dostawy_info += '</tr>';
        }

        dostawy_info += '<tr class="wiersz' + ( (i%2) + 1 ) + '">';
        dostawy_info += '    <td colspan=3 align=right> W sumie: </td>';
        dostawy_info += '    <td align=center>' +  parseFloat(stan_magazynu.ILOSC) + '/' + parseFloat(stan_magazynu.ILOSC_MAG) +  '</td>';
        dostawy_info += '    <td > &nbsp; </td>';
        dostawy_info += '</tr>';

        dostawy_info += '</table>\n';

    }


    if( dostawy_pokaz ){
        overlib( dostawy_info );
    }
}


function dostawy_handle_response( xmlRoot ){

    var o_towar_xml = xmlRoot.getElementsByTagName('towar').item(0);
    var o_dostawy_xml = xmlRoot.getElementsByTagName('dostawa');
    var o_dostawa_xml = null;
    var o_dostawa = new Object();

    var o_errors_xml = xmlRoot.getElementsByTagName('error');

    if( o_errors_xml && o_errors_xml.length > 0 ){

        var errors = [];

        var err_str = 'Wystąpiły następujące błędy: ';

        // przeszukuje w pętli wszystkie węzły XML, pobierając ich zawartość
        for(var i = 0; i < o_errors_xml.length; i++){
            errors[i] = o_errors_xml.item(i).getAttribute('str');
            err_str += errors[i] + "\n";
        }

        //alert( err_str );
        
        dostawy_print( err_str );
        
        return;
    }else{
        //zeruje dostawy
        towar_id = parseInt(o_towar_xml.getAttribute( 'ID' ));
        if( isNaN(towar_id) ){
            alert('Nie został zwrócony identyfikator towaru.');
            return;
        }

        var stan_magazynu = new Object();

        stan_magazynu.ILOSC = o_towar_xml.getAttribute( 'ILOSC' );
        stan_magazynu.ILOSC_MAG = o_towar_xml.getAttribute( 'ILOSC_MAGAZYN' );
        stan_magazynu.ILOSC_REZ = o_towar_xml.getAttribute( 'ILOSC_REZERWACJA' );
        stan_magazynu.MAGAZYN_ID = o_towar_xml.getAttribute( 'MAGAZYN_ID' );

        stany_magazynu[towar_id] = stan_magazynu;
        dostawy[towar_id] = new Array();

        /* pobieramy nowe nazwy funkcji z pliku XML w postaci tablicy */
        var atrybut, i, j;

        for( i = 0; i < o_dostawy_xml.length; i++){
            o_dostawa_xml = o_dostawy_xml.item(i);

            o_dostawa = new Object();

            for( j = 0; j < o_dostawa_xml.attributes.length; j++){
                atrybut = o_dostawa_xml.attributes.item(j);
                o_dostawa[atrybut.name] = atrybut.value;
            }

            dostawy[towar_id].push( o_dostawa );
        }

        if( towar_id == dostawy_towar_id && dostawy_pokaz == 1 )
            dostawy_print( dostawy[towar_id], stan_magazynu );

    }
}




