﻿function AddCoupon(obj) {
    AjaxRequest(ajaxServiceURL, 
        "cashdesk", 
        "cpn:" + obj.value + ";mth:add;", 
        RefreshDesk);
}
function DeleteCoupon(obj) {
    AjaxRequest(ajaxServiceURL, 
        "cashdesk", 
        "cpn:" + obj + ";mth:del;", 
        RefreshDesk);
}

function RefreshDesk() {
    if ((ajaxRequest1.readyState == 4) && (ajaxRequest1.status == 200)) {
        var nodes = ajaxRequest1.responseXML.getElementsByTagName("coupon").item(0);
        var msgElem = document.getElementById('msgDesk');
        msgElem.style.visibility = "hidden";
        msgElem.style.display = "none";
        for (var i = 0; i < nodes.childNodes.length; i++) 
        {
            var node = nodes.childNodes[i];
            var messageText = "";
            
            switch(node.nodeName.toUpperCase())
            {
                case "ADD":
                    var tabCart = document.getElementById('totOrder');
                    var cartBody = null;
                    if(tabCart.childNodes[0].nodeName.toUpperCase() == "TBODY") {
                        cartBody = tabCart.childNodes[0];
                    }
                    var trCartDis = document.getElementById("trCartDis");
                    var trCartPay = document.getElementById("trCartPay");
                    var trCartVat = document.getElementById("trCartTotVat");
                    var trOrderTot = document.getElementById("trOrderTot");
                    var trTotRounding = document.getElementById("trCartTotRounding");
                    trCartDis.parentNode.removeChild(trCartDis);
                    trCartPay.parentNode.removeChild(trCartPay);
                    trOrderTot.parentNode.removeChild(trOrderTot);
                    if(trCartVat != null) {
                        trCartVat.parentNode.removeChild(trCartVat);
                    }
                    if(trTotRounding != null) {
                        trTotRounding.parentNode.removeChild(trTotRounding);
                    }
                    
                    var trCoupon = document.createElement("tr");
                    trCoupon.setAttribute("id", "trcc_" + node.getAttribute("code"));
                    var tcCouponQnt = document.createElement("td");
                    var tcCouponTxt = document.createElement("td");
                    var tcCouponSpc = document.createElement("td");
                    var tcCouponPrice = document.createElement("td");
                    tcCouponPrice.align = "right";
                    
                    tcCouponQnt.appendChild(document.createTextNode("1"));
                    tcCouponQnt.appendChild(document.createTextNode(" "));
                        var img = new Image ();
                            img.src = "../App_Themes/" + theme + "/gfx/icn_delete.gif";
                            img.setAttribute("onclick", "DeleteCoupon('" + node.getAttribute("code") + "');");
                            img.style.cursor = "pointer";
                    if (img.addEventListener) {
                        img.addEventListener("onclick", function (){ DeleteCoupon(node.getAttribute("code")); }, false);
                    } else if (img.attachEvent) {
                        img.attachEvent("onclick", function (){ DeleteCoupon(node.getAttribute("code")); });
                    } else {
                        img.onclick = DeleteCoupon;
                    }
                    tcCouponQnt.appendChild(img);
                    tcCouponTxt.appendChild(document.createTextNode(node.getElementsByTagName("desc").item(0).getAttribute("txt")));
                    tcCouponSpc.appendChild(document.createTextNode(" "));
                    
                    var divPrice = document.createElement("div");
                    divPrice.setAttribute("id", "dP_" + node.getAttribute("code"));
                    divPrice.appendChild(document.createTextNode(node.getAttribute("val")));
                    tcCouponPrice.appendChild(divPrice);
                    
                    trCoupon.appendChild(tcCouponQnt);
                    trCoupon.appendChild(tcCouponTxt);
                    trCoupon.appendChild(tcCouponSpc);
                    trCoupon.appendChild(tcCouponPrice);
                    
                    var parentTag = cartBody;
                    if (cartBody == null) {
                        parentTag = tabCart;
                    }
                    parentTag.appendChild(trCoupon);
                    parentTag.appendChild(trCartDis);
                    parentTag.appendChild(trCartPay);
                    if (trTotRounding != null) {
                        parentTag.appendChild(trTotRounding);
                    }
                    if (trCartVat != null) {
                        parentTag.appendChild(trCartVat);
                    }
                    parentTag.appendChild(trOrderTot);
                    
                    messageText = node.getElementsByTagName("msg").item(0).getAttribute("txt");
                    break;
                case "ERR":
                    messageText = node.getElementsByTagName("msg").item(0).getAttribute("txt");
                    break;
                case "UPD":
                    var divPrice = document.getElementById("dP_" + node.getAttribute("code"));
                    divPrice.innerHTML = node.getAttribute("val");
                    break;
                case "MSG":
                    messageText = node.getAttribute("txt");
                    break;
                case "DEL":
                    var trRem = document.getElementById("trcc_" + node.getAttribute("code"));
                    trRem.parentNode.removeChild(trRem);
                    messageText = node.getAttribute("txt");
                    break;
                default:
                    break;
            }
            //while ( msgElem.childNodes.length >= 1 )
            //{
            //    msgElem.removeChild(msgElem.firstChild );       
            //} 
            //msgElem.appendChild(document.createTextNode(messageText))
            if (messageText != "")
            {
                msgElem.innerHTML = messageText;
                msgElem.style.visibility = "visible";
                msgElem.style.display = "block";
            }
        }
        
        document.getElementById("ctl00_cntMain_tCoupon").value = "";
        CalcDesk(ajaxRequest1.responseXML.getElementsByTagName("cartsummary").item(0));
    }
}

function CalcDesk(nodes) {
    // var amountPos = parseFloat(document.getElementById('tPosAnt').innerHTML);
    //var dis = document.getElementsByName('rDis');
    //var pay = document.getElementsByName('rPay');
    var rndDiff = nodes.getElementsByTagName("sumRDiff").item(0).firstChild.nodeValue;
    document.getElementById('sumCart').innerHTML = nodes.getElementsByTagName("sumCrt").item(0).firstChild.nodeValue;
    document.getElementById('sumDis').innerHTML = nodes.getElementsByTagName("sumDis").item(0).firstChild.nodeValue;
    document.getElementById('sumPay').innerHTML = nodes.getElementsByTagName("sumPay").item(0).firstChild.nodeValue;
    if (document.getElementById('sumVat') != null) {
        document.getElementById('sumVat').innerHTML = nodes.getElementsByTagName("sumVat").item(0).firstChild.nodeValue;
    }
    document.getElementById('sumTot').innerHTML = nodes.getElementsByTagName("sum").item(0).firstChild.nodeValue;

    var elemRoundDiff = document.getElementById('trCartTotRounding');
    if (elemRoundDiff != null) {
        document.getElementById('sumRounding').innerHTML = rndDiff;
        if (rndDiff != "0.00" && rndDiff != "0,00") 
        {
            elemRoundDiff.style.visibility = "visible";
            elemRoundDiff.style.display = "";
        }
        else
        {
            elemRoundDiff.style.visibility = "hidden";
            elemRoundDiff.style.display = "none";
        }
    }
}