

/**
 * vyber barvy ze selectboxu podle kodu
 */
function shop_set_barvy(shopid, v) {
    var opt = document.getElementById('form_'+shopid)['attrib_'+shopid+'_barvy'];
    if (opt.options) {
        for (var i=0; i<opt.options.length; i++) {
            if (opt.options[i].value == v) {
                opt.selectedIndex = i;
                break;
            }
        }
    }
}

/**
 * vyber velikosti ze selectboxu podle kodu
 */
function shop_set_velikosti(shopid, v) {
    var opt = document.getElementById('form_'+shopid)['attrib_'+shopid+'_velikosti'];
    if (opt.options) {
        for (var i=0; i<opt.options.length; i++) {
            if (opt.options[i].value == v) {
                opt.selectedIndex = i;
                break;
            }
        }
    }
}

/**
 * zkopiruje obrazek z varianty do hlavniho img
 */
function shop_set_barvy_img(shopid, kod_barvy) {
    var i = shopid+';'+kod_barvy;
    
    // offer
    var img = document.getElementById('img_offer_'+shopid);
    if (img && shop_varianty[i]) {
        img.src = shop_varianty[i]['small'];
    }

    // detail
    var img = document.getElementById('img_detail_'+shopid);
    if (img && shop_varianty[i]) {
        img.src = shop_varianty[i]['big'];
        //alert(img.onclick);
        var onc = new String(img.onclick);
        img.onclick = onc.replace(/picture\/shop\/zbozi\/upload\/[^\.]+\.jpg/, shop_varianty[i]['upload']);
        img.parentNode.href = img.parentNode.href.replace(/picture\/shop\/zbozi\/upload\/[^\.]+\.jpg/, shop_varianty[i]['upload']);
        //picture/shop/zbozi/upload/T45.jpg
    }
    // zmena odkazu pod obrazkem
    var imglink = document.getElementById('img_detail_link_'+shopid);
    if (imglink && shop_varianty[i]) {
        var onc = new String(imglink.onclick);
        imglink.onclick = onc.replace(/picture\/shop\/zbozi\/upload\/[^\.]+\.jpg/, shop_varianty[i]['upload']);
        imglink.href = imglink.href.replace(/picture\/shop\/zbozi\/upload\/[^\.]+\.jpg/, shop_varianty[i]['upload']);
    }
}

/**
 * vola se pri zmene atributu
 * - zjistime, zda jsou vybrany vsechny a pokud ano, pak nakopirujeme z globalniho pole udaje o podpolozce na urcita mista na strance (nazev/kod/fotka/...)
 */
function shop_attribs_change (shopid, attrib_name_onchange) {
    var r = true;
    var opt = document.getElementById('form_'+shopid);
    var attribs = '';
    var kod_barvy = false; // kod barvy potrebujeme pro zobrazeni obrazku s variantou
    var el_kod = false;
    var el_velikost = false;

    if (opt) {
        attribs = shopid+';';
        for (e in opt) {
            var el = opt[e];
            if (el && typeof(el)=='object' && el.nodeName == 'SELECT' && el.name && el.name.substr(0, 6) == 'attrib') {
                
                // ulozime si pro vytvoreni klice do glob. pole s atributy jednotlivych comodit
                attribs += el.options[el.selectedIndex].value + ';';
                // zjistime kod barvy
                if (el.name == ('attrib_'+shopid+'_barvy')) {
                    kod_barvy = el.options[el.selectedIndex].value;
                    el_kod = el;
                }
                // zjistime element velikosti
                if (el.name == ('attrib_'+shopid+'_velikosti')) {
                    el_velikost = el;
                }

            // pokud neni select, tak muze byt hidden
            } else  if (el && typeof(el)=='object' && el.nodeName == 'INPUT' && el.name && el.name.substr(0, 6) == 'attrib') {
                // ulozime si pro vytvoreni klice do glob. pole s atributy jednotlivych comodit
                attribs += el.value + ';';
                // zjistime kod barvy
                if (el.name == ('attrib_'+shopid+'_barvy')) {
                    kod_barvy = el.value;
                }
            }
        }
    }
    
    // pokud neni barva, pak zdisablujeme velikost
    // a omezime jen platne velikosti
    // delame jen pokud se prave nevybira velikost - protoze jinak by nam to skocilo na prvni
    if (attrib_name_onchange!='velikosti' && el_kod && el_velikost) {
        // poprve si zazalohujeme vsechny velikosti
        if (!shop_attribs_options_velikosti_arr) {
            shop_attribs_options_velikosti_arr = new Array();
            for (var ii=0; ii<el_velikost.options.length; ii++) {
                shop_attribs_options_velikosti_arr[ii] = new Array(el_velikost.options[ii].value, el_velikost.options[ii].innerHTML);
            }
        }
        
        if (!kod_barvy) {
            el_velikost.selectedIndex = 0;
            el_velikost.disabled = true;
        } else  {
            // musime najit jen platne kombinace
            el_velikost.innerHTML = '';
            var o = document.createElement('option');
            o.value = shop_attribs_options_velikosti_arr[0][0];
            o.text = shop_attribs_options_velikosti_arr[0][1];            
            el_velikost.options.add(o);
            for (var ii=0; ii<shop_attribs_options_velikosti_arr.length; ii++) {
                var index = shopid+';'+kod_barvy+';'+shop_attribs_options_velikosti_arr[ii][0]+';';
                if (shop_attribs[index]) {
                    var o = document.createElement('option');
                    o.value = shop_attribs_options_velikosti_arr[ii][0];
                    o.text = shop_attribs_options_velikosti_arr[ii][1];            
                    el_velikost.options.add(o);
                } 
            }
            el_velikost.disabled = false;
        }
    }

    if (kod_barvy && attribs && shop_attribs && shop_attribs[attribs]) {

        if (shop_attribs[attribs]['sys_kod']) {
            var elem = document.getElementById('shop_sys_kod_'+shopid)
            if (elem) {
                elem.innerHTML = shop_attribs[attribs]['sys_kod'];
            }
        }
        if (shop_attribs[attribs]['nazev']) {
            var elem = document.getElementById('shop_nazev_'+shopid)
            if (elem) {
                elem.innerHTML = shop_attribs[attribs]['nazev'];
            }
        }
        if (shop_attribs[attribs]['dostupnost']) {
            var elem = document.getElementById('shop_dostupnost_'+shopid)
            if (elem) {
                elem.innerHTML = shop_attribs[attribs]['dostupnost'];
                // pokud neni skladem a neni to zbozi Exxx, tak disablujeme nakup
                // kulheim - docasne povoleno
                if (opt['pocet_'+shopid]) {
                        opt['pocet_'+shopid].disabled = false;
                        opt['buy_'+shopid].disabled = false;

                    /*if(opt['pocet_'+shopid] && opt['buy_'+shopid]) {
                        if (shop_attribs[attribs]['dostupnost'] != 'skladem' && shop_attribs[attribs]['sys_kod'].substr(0, 1)!='E') {
                            opt['pocet_'+shopid].disabled = true;
                            opt['buy_'+shopid].disabled = true;
                        } else {
                            opt['pocet_'+shopid].disabled = false;
                            opt['buy_'+shopid].disabled = false;
                        }
                    }*/
                }
            }
        }
        if (shop_attribs[attribs]['cenabeznabezdph']) {
            var elem = document.getElementById('shop_cenabeznabezdph_'+shopid);
            if (elem) {
                elem.innerHTML = shop_attribs[attribs]['cenabeznabezdph'];
            }
        }
        if (shop_attribs[attribs]['sleva']) {
            var elem = document.getElementById('shop_sleva_'+shopid);
            if (elem) {
                elem.innerHTML = shop_attribs[attribs]['sleva'];
            }
        }
        if (shop_attribs[attribs]['cenabezdph']) {
            var elem = document.getElementById('shop_cenabezdph_'+shopid);
            if (elem) {
                elem.innerHTML = shop_attribs[attribs]['cenabezdph'];
            }
        }
        if (shop_attribs[attribs]['cenasdph']) {
            var elem = document.getElementById('shop_cenasdph_'+shopid);
            if (elem) {
                elem.innerHTML = shop_attribs[attribs]['cenasdph'];
            }
        }
        
    }
    
    // pokud je zvolena barva, pak zkusime najit variantu
    if(kod_barvy) {
        shop_set_barvy_img(shopid, kod_barvy);
    }
    
    return r;
}

