function send_xmlhttprequest(id, state_change, method, url, content, headers) {
    var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest() : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
    if (!xmlhttp) {
        return false;
    }
    xmlhttp.open(method, url);
    xmlhttp.onreadystatechange = function () {
        state_change(xmlhttp, id);
    };
    headers = headers || {};
    headers['X-Requested-With'] = headers['X-Requested-With'] || 'XMLHttpRequest';
    for (var key in headers) {
        xmlhttp.setRequestHeader(key, headers[key]);
    }
    xmlhttp.send(content);
    return true;
}



function add_basket(ean, id_skupiny, id) {

    if(document.getElementById("celbal_"+id)!=null)
    {
        celabaleni = document.getElementById("celbal_"+id).value;
    }
    else
    {
        celabaleni = '';
    }


    if(document.getElementById("zakaznik_cena_s_dph_"+id)!=null)
    {
        varianta = document.getElementById("zakaznik_cena_s_dph_"+id).options[document.getElementById("zakaznik_cena_s_dph_"+id).selectedIndex].value;
    }
    else
    {
        varianta = '';    
    }

    // odeslání požadavku na aktualizaci dat
    if (!send_xmlhttprequest(id, add_basket_obsluha, 'GET', 'ajax.php?action=katalog-pridej-ks-kosik&ID_SKUPINY=' + id_skupiny + '&EAN=' + ean + '&kusy=' + document.getElementById("ks_"+id).value + '&CELABALENI=' + celabaleni  + '&ZBOZI_VARIANTY=' + varianta)) {
        return false;
    }

    in_bas(ean, id_skupiny, id);
    
    return false;

}








function add_basket_obsluha(xmlhttp, id) {
    if (xmlhttp.readyState == 4) {
        var castka = xmlhttp.responseXML.getElementsByTagName('celkem_s_dph');
        document.getElementById('bas-box-castka').innerHTML = castka[0].firstChild.data;
        var msg_hlaseni=xmlhttp.responseXML.getElementsByTagName('hlaseni')[0].firstChild.data;
        msg_hlaseni=msg_hlaseni.replace(/@@MESSAGE@@/, '<div class="bubble-message">');
        msg_hlaseni=msg_hlaseni.replace(/@@_MESSAGE@@/, '</div>');
        msg_hlaseni=msg_hlaseni.replace(/@@CENA@@/, '<div class="bubble-cena">');
        msg_hlaseni=msg_hlaseni.replace(/@@_CENA@@/, '</div>');
        msg_hlaseni=msg_hlaseni.replace(/@@NAZEV@@/, '<div class="bubble-nazev">');
        msg_hlaseni=msg_hlaseni.replace(/@@_NAZEV@@/, '</div>');
        msg_hlaseni=msg_hlaseni.replace(/@@CENA_CELKEM@@/, '<span class="tucne">');
        msg_hlaseni=msg_hlaseni.replace(/@@_CENA_CELKEM@@/, '</span>');
        msg_hlaseni=msg_hlaseni.replace(/@@BR@@/, '<br />');
    
        var arVersion = navigator.appVersion.split("MSIE");
        var version = parseFloat(arVersion[1]);

        if(navigator.appName == "Microsoft Internet Explorer" && version <= 6)
        {
            if(document.getElementById("zakaznik_cena_s_dph_"+id)!=null)
            {
                document.getElementById("zakaznik_cena_s_dph_"+id).style.visibility = 'hidden';
            }
        }

        CatalogHelper.showInfo(document.getElementById("ks_"+id), msg_hlaseni);

        if(navigator.appName == "Microsoft Internet Explorer" && version <= 6)
        {
            if(document.getElementById("zakaznik_cena_s_dph_"+id)!=null)
            {
                window.setTimeout('zobraz_select_varianty(\''+id+'\')', 5000);
            }
        }

    }
}







function zobraz_select_varianty(id)
{

    document.getElementById("zakaznik_cena_s_dph_"+id).style.visibility = 'visible';
    //alert('ZDE:');

}







function hs_info(id, ean, id_skupiny)
{
    
    if(document.getElementById('hs-vypis2-'+id).innerHTML=='')
    {
        // odeslání požadavku na aktualizaci dat
        if (!send_xmlhttprequest(id, hs_info_obsluha, 'GET', 'ajax.php?action=hs-vypis2-nacti-info&EAN=' + ean + '&ID_SKUPINY=' + id_skupiny)) {
            return false;
        }
    }
    
    return false;

}







function hs_info_obsluha(xmlhttp, id) {

    if (xmlhttp.readyState == 4)
    {

        document.getElementById('hs-vypis2-'+id).innerHTML = xmlhttp.responseText;

    }

}





function in_bas(ean, id_skupiny, id)
{

    if(document.getElementById('in-bas-'+id)!=null)
    {

        sleep(500);

        // odeslání požadavku na aktualizaci dat
        if (!send_xmlhttprequest(id, in_bas_obsluha, 'GET', 'ajax.php?action=in-basket&EAN=' + ean + '&ID_SKUPINY=' + id_skupiny)) {
            return false;
        }

    }

}






function in_bas_obsluha(xmlhttp, id) {

    if (xmlhttp.readyState == 4)
    {

        if(xmlhttp.responseText!='')
        {
        
            document.getElementById('in-bas-'+id).innerHTML = xmlhttp.responseText;
            document.getElementById('in-bas-'+id).className='in-bas1';
            
        }

    }

}

