var prices = new Array();
var priceFactors = new Array();

var addedIds = new Object();
var itemsAddedCount = 0;

function processOnchangeSelect(selectControl)
{
    //debugger;
    var id = (selectControl.options[selectControl.selectedIndex]).value;

    var qControl = document.getElementById((selectControl.id).replace(/select/gi, "input"));
    //var q = (!isNaN(qControl.value) ? qControl.value : 0);

    if(addedIds[selectControl.id] == null)
    {
        addedIds[selectControl.id] = 0;
    }
    if (id != null && id != "")
    {
        addedIds[selectControl.id] = id;
    }
    else
    {
        addedIds[selectControl.id] = 0;
        qControl.value = 0;
    }

    countTotal();
}

function processOnchangeInput(qControl)
{
    countTotal();
}

function countTotal()
{
    //debugger;

    var totalValue = 0;

    for(selectControlName in addedIds)
    {
        if (addedIds[selectControlName] == 0)
        {
            continue;
        }
    	//res += i + ": " + addedIds[i] + "\r\n";
    	var selectControl = document.getElementById(selectControlName);
    	var qControl = document.getElementById((selectControl.id).replace(/select/gi, "input"));
        var q = (!isNaN(qControl.value) ? qControl.value : 0);
        if (selectControl.id.indexOf("minus") > -1)
        {
            q = -q;
        }
        totalValue += prices[addedIds[selectControlName]] * q;
    }

    totalControl.innerHTML = "Èòîãî: " + totalValue + " ð.";
}

function dublicate(node)
{
    //debugger;
    var newNode = node.cloneNode(true);
    renewIds(newNode);
    node.parentNode.insertBefore(newNode, totalControl);
}
function dublicate2(node)
{
    var newNode = node.cloneNode(true);
    renewIds(newNode);
    node.parentNode.insertBefore(newNode, node.nextSibling);
}

function renewIds(node)
{
//    if (node.hasAttribute("id"))
//    {
//        node.setAttribute("id", node.getAttribute("id") + "_");
//    }
    if (node.id!= null && node.id != "")
    {
        node.id = node.id + "_";
    }
    if (node.style != null && node.style.display == "none")
    {
       node.style.display = "inline";
    }
    if (node.tagName != null)
    {
        if (node.tagName.toLowerCase() == "input" && (node.type.toLowerCase() == "textbox" || node.type.toLowerCase() == "text"))
        {
           node.value = "0";
        }
        if (node.tagName.toLowerCase() == "select")
        {
           node.selectedIndex = 0;
        }
    }
    var children = node.childNodes;
    if (children != null)
    {
        for(var i = 0, len = children.length; i < len; i++)
        {
            renewIds(children.item(i));
        }
    }
    //return node;
}

function drop(node)
{
    //debugger;
    node.parentNode.removeChild(node);
}

function stToBasket()
{
    document.getElementById("stToBasket").disabled = true;
    document.getElementById("stToBasket").src = "images/ProcessImage.gif";

    itemsAddedCount = 0;

    for(selectControlName in addedIds)
    {
        if (addedIds[selectControlName] == 0)
        {
            continue;
        }
    	var selectControl = document.getElementById(selectControlName);
    	var qControl = document.getElementById((selectControl.id).replace(/select/gi, "input"));
        var q = (!isNaN(qControl.value) ? qControl.value : 0);
        if (q == 0)
        {
            continue;
        }
        if (selectControl.id.indexOf("minus") > -1)
        {
            q = -q;
        }

        var kw = {
            url:    "a_can.php",
            load:   afterStBuy,
            content: {q: q, itemId: addedIds[selectControlName], priceFactorId: priceFactors[addedIds[selectControlName]], action: ADD_ACTION},
            method: "GET",
            error: errorHandler,
            timeoutSeconds:10,
            timeout: onTimeout
        };
        dojo.io.bind(kw);
        itemsAddedCount ++;
    }

    if (itemsAddedCount < 1)
    {
        var button = document.getElementById("stToBasket");
        if(button != null)
        {
        	button.disabled = false;
        	button.src = "images/"+ADD_ACTION+".gif";
        }
    }
}

function afterStBuy(type, data, evt)
{
	//debugger;
    var xmlDoc = dojo.dom.createDocumentFromText(data);
    var buyNode = dojo.dom.firstElement(xmlDoc, "Buy");

    var tmp =  dojo.dom.firstElement(buyNode, "Action");
    var action = tmp.firstChild.data;
    tmp = dojo.dom.firstElement(buyNode, "ItemId");
    var itemId = tmp.firstChild.data;
    tmp = dojo.dom.firstElement(buyNode, "PriceFactorId");
    var priceFactorId = tmp.firstChild.data;
    tmp = dojo.dom.firstElement(buyNode, "Result");
    var result = tmp.firstChild.data;

    if(result == 1)
    {
        itemsAddedCount--;
        if (itemsAddedCount == 0)
        {
            var button = document.getElementById("stToBasket");
            if(button != null)
            {
            	button.disabled = false;
            	button.src = "images/"+action+".gif";
            }
            alert(successActions[action]);
        }
    }
    else
    {
        alert(failActions[action]);
    }

    switch (action)
    {
        case ADD_ACTION:
        case EDIT_ACTION:
            refreshBasketPreview();
            break;
        case DEL_ACTION:
            document.location.reload();
            break;
    }
}

