function qty(q){
    $.ajax({
        type: "get",
        url: "WebServices/jquery.svc/UpdateCart",
        data: "id=" + q.parentNode.parentNode.sectionRowIndex + "&qty=" + q.options[q.selectedIndex].value,
        cache: false,
        dataType: 'json',
        success: changeQTY
    })
}

function changeQTY(result){
    if (result == '' || result.error != null) {
        window.location.reload();
        return;
    }
    
    updatePrice(result);
}

function updatePrice(price){
    if (price.P != "0") {
        var pricet = document.getElementById("orders").rows[price.Id].cells[3];
        pricet.innerHTML = price.P;
    }
    listPrice.html(price.LP);
    subTotal.html(price.ST);
    discount.html(" - " + price.CD);
    rankDiscount.html(" - " + price.RD);
    
}

function remove(s){
    $.ajax({
        type: "get",
        url: "WebServices/jquery.svc/RemoveCart",
        data: "id=" + s.parentNode.parentNode.sectionRowIndex,
        cache: false,
        dataType: 'json',
        success: removep
    })
}

function removep(result){
    if (result == '' || result.error != null) {
        return;
    }
    deleteRow(result.Id);
    updatePrice(result);
}

function deleteRow(id){
    var r = document.getElementById("orders");
    r.removeChild(r.rows[id]);
    //$("#orders").children("tr").eq(id).fadeOut("slow");
}

//coupon
function setCoupon(){
    $.ajax({
        type: "get",
        url: "WebServices/jquery.svc/SetCoupon",
        data: "c=" + $("#couponInput").val(),
        cache: false,
        dataType: 'json',
        success: setCouponp
    })
}

function setCouponp(result){
    if (result == '' || result.error != null) {
        return;
    }
    updateCoupon(result);
}

function removeCoupon(){
    $.ajax({
        type: "get",
        url: "WebServices/jquery.svc/RemoveCoupon",
        cache: false,
        dataType: 'json',
        success: removeCouponp
    })
}

function removeCouponp(result){
    if (result == '' || result.error != null) {
        return;
    }
    delCoupon(result);
}

function updateCoupon(coupon){
    closeaspStatus();
    var status = document.getElementById("status");
    if (!coupon.IS) {
        status.style.display = "";
        status.className = "status-red";
        status.innerHTML = coupon.I;
    }
    else {
        status.style.display = "";
        status.className = "status-green";
        status.innerHTML = coupon.I;
        subTotal.html(coupon.ST);
        if (discount != null) 
            discount.html(coupon.CD);
        couponSubmit.style.display = "none";
        couponRemove.style.display = "";
    }
}

function delCoupon(coupon){
    closeaspStatus();
    var status = document.getElementById("status");
    status.style.display = "none";
    subTotal.html(coupon.ST);
    if (discount != null) 
        discount.html(coupon.CD);
    couponSubmit.style.display = "";
    couponRemove.style.display = "none";
}

function closeaspStatus(){
    if (aspStatus != null) {
        aspStatus.style.display = "none";
    }
}


