
function alertElProps(el){
    var str = '';
    for(var key in el){
        str += String(key) + ":" + String(el[key]) + "\n";
    }
    alert(str);
}

function goToSubCat(subCatId){
    var ulEl = document.getElementById('catmenuul');
    var nodes = ulEl.childNodes;
    for ( var iNode = 0; iNode < nodes.length; iNode++ ){
        if(nodes[iNode].className = 'catlichoosed'){
            nodes[iNode].className = 'catlipath';
        }
    }
    var contentSpan = document.getElementById('catcontent');
    //contentSpan.innerHTML = '';
    var curLi = document.getElementById('cli_'+String(subCatId));
    curLi.className = 'catlichoosed';
    var curLiImg = document.getElementById('cimg_'+String(subCatId));
    curLiImg.src = "img/menu_small_arrow_down.gif";
    contentSpan = '';
    JsHttpRequest.query(
        'catajax.php',
        {
            'category': ''+subCatId
        },

        function(result, errors) {
            if (result['basketres']=='true'){
                if(result['hasprods']=='true'){
                    window.location.href = 'catalog.php?id=' + result['catid'];    
                }else{
                    var choosedLi = document.getElementById("cli_"+result['catid']);
                    var newPadding = String(Number(choosedLi.style.paddingLeft.substr(0,choosedLi.style.paddingLeft.length-2))+15) + "px";
                    var contentSpan = document.getElementById('catcontent');
                    var pathDiv = document.getElementById('catpath');
                    var arrSubCats = result['childs'].split('>');
                    var ulEl = document.getElementById('catmenuul');
                    var newLiNodes = [];
                    var resContent = '';
                    for( var iSCat = 0; iSCat < arrSubCats.length/2; iSCat++){
                        var catName = String(arrSubCats[iSCat*2+1]);
                        var catID = String(arrSubCats[iSCat*2]);
                        var elNum = newLiNodes.length;
                        newLiNodes[elNum] = document.createElement("LI");
                        newLiNodes[elNum].innerHTML = '<img src="img/menu_small_arrow.gif" width="8" height="18" id="cimg_'+catID+'"><a href="catalog.php?id='+catID+'">'+catName+'</a>';
                        newLiNodes[elNum].className = 'catli';
                        newLiNodes[elNum].id = 'cli_' + catID;
                        newLiNodes[elNum].style.paddingLeft = newPadding;
                        resContent += '<a href="javascript:goToSubCat('+catID+')">'+catName+'</a><br>';
                    }
                    var ulChildsCount = ulEl.childNodes.length;
                    var parentLiPos = -1;
                    var newNodesCount = newLiNodes.length;
                    
                    // find choosen element position
                    for( var iSCat = 0; iSCat < ulChildsCount; iSCat++){
                        if(ulEl.childNodes[iSCat] == choosedLi){
                            parentLiPos = iSCat;
                            break;
                        }
                    }
                    
                    // remove old LIs
                    var arrTmpChilds = [];
                    for (var iLi = parentLiPos+1; iLi < ulChildsCount; iLi++){
                        arrTmpChilds[arrTmpChilds.length] = ulEl.removeChild(ulEl.lastChild);
                    }
                    
                    // append new LIs
                    for(var iNode = 0; iNode < newNodesCount; iNode++){
                        ulEl.appendChild(newLiNodes[iNode]);
                    }
                    
                    // append old LIs back
                    for (var iLi = arrTmpChilds.length-1; iLi >= 0; iLi--){
                        ulEl.appendChild(arrTmpChilds[iLi]);
                    }
                    
                    // replace page content
                    contentSpan.innerHTML = resContent;
                    pathDiv.innerHTML += '<img src="img/arrowtitle.gif" width="9" height="10"> <a href="catalog.php?id='+result['catid']+'">'+result['catname']+'</a>';
                }
            }else{
                alert(errors);
            }
        },
        true
    );
}

function addtobasket(productID){
    var button = document.getElementById('prodbutt_'+productID);
    button.value = "";
    button.style.backgroundImage = 'url("img/cart_but_request.gif")';
    button.style.backgroundPosition = 'center';
    button.style.paddingLeft = '0';
    JsHttpRequest.query(
        'basketajax.php',
        {
            'product': ''+productID,
            'some': 'some1'
        },

        function(result, errors) {
            if (result['basketres']=='true'){
                document.getElementById('prodspan_'+result['productid']).innerHTML = strProdPlacedInCart;
                document.getElementById('cartitemscount').innerHTML = result['newcount'];
                document.getElementById('cartitemscost').innerHTML = result['newcost'];
            }else{
                alert(errors);
            }
        },
        true
    );
}
