var globalID = 0;
/*
function inCart(id, count)
{
    //var count = prompt("Добавить " + name + " в корзину. Количество: ", "");
    if (parseInt(count)) {
        var url = "/shop/cart.php?id=" + id + "&count=" + count + "&act=add";
        if (window.XMLHttpRequest) {
            req = new XMLHttpRequest();
            req.onreadystatechange = loadCart;
            req.open("GET", url, true);
            req.send(null);
        } else if (window.ActiveXObject) {
            req = new ActiveXObject("Microsoft.XMLHTTP");
            if (req) {
                req.onreadystatechange = loadCart;
                req.open("GET", url, true);
                req.send();
            }
        }
    }
}

function loadCart()
{
    if (req.readyState == 4) {
        if (req.status == 200) {
            xmlData = req.responseXML.documentElement;
            try {
                if (parseInt(xmlData.childNodes[2].firstChild.nodeValue)) {
                    alert(xmlData.childNodes[2].firstChild.nodeValue);
                    return 0;
                }
                document.getElementById('countInCart').innerHTML =
                    numberToString(xmlData.childNodes[0].firstChild.nodeValue);
                document.getElementById('priceInCart').innerHTML =
                    xmlData.childNodes[1].firstChild.nodeValue + " грн.";
            } catch (e) {
                alert("Ошибка обработки запроса. ");
            }
        } else {
            alert("Ошибка козины:\n" + req.statusText);
        }
    }
}
*/
function numberToString(number)
{
    if (number == 1) return "1 товар";
    if (number >= 11 && number <= 20) return number + " товаров";
    last = number.toString().substr(number.toString().length-1, 1);
    if (parseInt(last) == 0 || parseInt(last) >= 5) return number + " товаров";
    if (parseInt(last) == 1) return number + " товар";
    return number + " товара";
}

function deleteItem(id)
{
    var url = "/shop/cart.php?id=" + id + "&act=delete";
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = function() { editHtmlCart(req, id, 'delete'); }
        req.open("GET", url, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function() { editHtmlCart(req, id, 'delete'); }
            req.open("GET", url, true);
            req.send();
        }
    }
}

function editCart(id, action, param)
{
    if (action == 'editcount') {
        var count = param;
        if (count.length < 1) return 0;
        var url = "/shop/cart.php?id=" + id + "&count=" + count + "&act=" + action;
    }
    if (action == 'editcomment') {
        var comment = param;
        if (comment.length < 1) return 0;
        var url = "/shop/cart.php?id=" + id + "&act=" + action + "&comment=" + encodeURIComponent(comment);
    }
    if (action == 'add') {
        var count = param;
        var url = "/shop/cart.php?id=" + id + "&count=" + count + "&act=add";
    }
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = function() { editHtmlCart(req, id, action); }
        req.open("GET", url, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function() { editHtmlCart(req, id, action); }
            req.open("GET", url, true);
            req.send();
        }
    }
}

function editHtmlCart(request, pid, action) {
//    alert(id + " - " + count);
    if (request.readyState == 4) {
        if (request.status == 200) {
            xmlData = request.responseXML.documentElement;
            try {
                if (parseInt(xmlData.childNodes[2].firstChild.nodeValue)) {
                    alert(xmlData.childNodes[2].firstChild.nodeValue);
                    return 0;
                }
                if (action == 'editcount') {
                    document.getElementById(action + '-' + pid).innerHTML =
                        parseInt(xmlData.childNodes[0].firstChild.nodeValue);
                    document.getElementById('summ').innerHTML =
                        parseInt(xmlData.childNodes[1].firstChild.nodeValue);
                }
                if (action == 'delete') {
                    document.getElementById(action + '-' + pid).parentNode.
                        parentNode.removeChild(document.getElementById('prod' + pid + 'i'));
                    document.getElementById(action + '-' + pid).parentNode.
                        parentNode.removeChild(document.getElementById('prod' + pid));
                    document.getElementById('summ').innerHTML =
                        parseInt(xmlData.childNodes[1].firstChild.nodeValue);
                }
                if (action == 'editcomment') {
                    document.getElementById(action + '-' + pid).innerHTML =
                        decodeURIComponent(xmlData.childNodes[0].firstChild.nodeValue);
                }
                if (action == 'add') {
                    if (parseInt(xmlData.childNodes[2].firstChild.nodeValue)) {
                        alert(xmlData.childNodes[2].firstChild.nodeValue);
                        return 0;
                    }
                    document.getElementById('countInCart').innerHTML =
                        numberToString(xmlData.childNodes[0].firstChild.nodeValue);
                    document.getElementById('priceInCart').innerHTML =
                        xmlData.childNodes[1].firstChild.nodeValue + " грн.";
                }
//                if (parseInt(xmlData.childNodes[2].firstChild.nodeValue)) {
//                    alert(xmlData.childNodes[2].firstChild.nodeValue);
//                    return 0;
//                }
//                document.getElementById('countInCart').innerHTML =
//                    numberToString(xmlData.childNodes[0].firstChild.nodeValue);
//                document.getElementById('priceInCart').innerHTML =
//                    xmlData.childNodes[1].firstChild.nodeValue + " грн.";
            } catch (e) {
                alert("Ошибка обработки запроса. ");
            }
        } else {
            alert("Ошибка козины:\n" + request.statusText);
        }
    }
}

function doPrint()
{
    try {
        window.open("q", "kamelot-window", "width=700,height=600");
    } catch (e) {}
    document.getElementById("request").value = 'print';
    document.getElementById("appform").submit();
}

function doSend()
{
    try {
        window.open("", "kamelot-window", "width=700,height=600");
    } catch (e) {}
    document.getElementById("request").value = 'send';
    document.getElementById("appform").submit();
}

function appendPromptDialog()
{
    var dialog = document.createElement('div');
    dialog.id = 'dialogBox';
    dialog.innerHTML = "<span id='dbMsg'>&nbsp;</span><br><input type='text' id='dbValue'><br><center><input type='button' id='dbOK' value='ОК'><input type='button' id='dbCancel' value='Отмена'></center>";
//    var dialog = document.createElement("<div id='dialogBox'><span id='dbMsg'>&nbsp;</span><br><input type='text' id='dbValue'><input type='button' id='dbOK' value='ОК'><input type='button' id='dbCancel' value='Отмена'></div>");
    document.getElementsByTagName('body')[0].appendChild(dialog);
    document.getElementById('dialogBox').style.position = 'absolute';
    document.getElementById('dialogBox').style.top = (screen.availHeight/2 - document.getElementById('dialogBox').offsetHeight/2 - 100) + 'px';
    document.getElementById('dialogBox').style.left = (screen.availWidth/2 - document.getElementById('dialogBox').offsetWidth/2) + 'px';
    document.getElementById('dialogBox').style.display = 'none';
//    alert(screen.availWidth/2 + " - " + document.getElementById('dialogBox').clientWidth/2);
    document.getElementById('dbCancel').onclick = function (e) {
        document.getElementById('dialogBox').style.display = 'none';
    }
    document.getElementById('dialogBox').onkeypress = function (e) {
        var keynum;
        if(window.event) {
            keynum = e.keyCode;
        } else if(e.which) {
            keynum = e.which;
        }
        if (keynum == 27) document.getElementById('dialogBox').style.display = 'none';
    }
}

function showPromptDialog(id, action)
{
    document.getElementById('dialogBox').style.display = 'block';
    if (action == 'editcount') {
        document.getElementById('dbMsg').innerHTML = 'Другое количество: ';
    }
    if (action == 'editcomment') {
        document.getElementById('dbMsg').innerHTML = 'Примечание к выбранной позиции: ';
    }
    if (action == 'add') {
        document.getElementById('dbMsg').innerHTML = 'Количество материала: ';
    }
    document.getElementById('dbValue').value = '';
    document.getElementById('dbValue').focus();
    document.getElementById('dbOK').onclick = function (e) {
        //alert('test');
        var param = document.getElementById('dbValue').value;
        if (param) {
            editCart(id, action, param);
        }
        document.getElementById('dialogBox').style.display = 'none';
    }
}
