﻿function AddCoupon(code, integrationcode, gametypeid, bettypeid, tdcode, livescore) {
    if (document.getElementById(tdcode) != undefined) {
        if (document.getElementById(tdcode).className == "tdBultenSelected" || document.getElementById(tdcode).className == "tdBultenFullSizeSelected") {
            DeleteCoupon(code, gametypeid, integrationcode, tdcode);
            return;
        }
    }

    $.ajax({
        type: "POST",
        url: "../CouponFunctions.aspx/Add",
        data: "{code: '" + code + "', gametypeid: " + gametypeid + ", integrationcode: '" + integrationcode + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            retval = msg.d;
            document.getElementById("aLink").click();
        },
        error: function(xmlHttpRequest, status, err) {
            //modalWarning('Oran Kupona Eklenemedi.');
        }
    });
}

function AddSpecialCoupon(code, integrationcode, gametypeid, bettypeid, tdcode, livescore) {
    if (tdcode != undefined) {
        if (document.getElementById(tdcode).className == "tdBultenSelected") {
            DeleteCoupon(code, gametypeid, integrationcode, tdcode);
            return;
        }
    }

    $.ajax({
        type: "POST",
        url: "CouponFunctions.aspx/AddSpecial",
        data: "{code: '" + code + "', gametypeid: " + gametypeid + ", integrationcode: '" + integrationcode + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            retval = msg.d;
            document.getElementById("couponfrm").src = "/IddaaKupon.aspx?ViewType=CouponSystem";

            if (retval.Status == 0) {
                if (livescore == undefined || livescore == "FullSize") {
                    if (tdcode != undefined) {
                        if (retval.FinalDouble) {
                            if (document.getElementById(retval.FinalDoubleId) != null) {
                                document.getElementById(retval.FinalDoubleId).className = "tdBulten";
                            }
                        }

                        if (document.getElementById(tdcode) != null) {
                            document.getElementById(tdcode).className = "tdBultenSelected";
                        }
                    }
                }
            }
            else {
                if (retval.Status == 5) {
                    location.reload();
                    return;
                }
                
                modalWarning(retval.ErrorMessage);
            }
        },
        error: function(xmlHttpRequest, status, err) {
            modalWarning('Oran Kupona Eklenemedi.');
        }
    });
}

function DeleteCoupon(code, gametypeid, integrationcode, tdcode) {
    $.ajax({
        type: "POST",
        url: "CouponFunctions.aspx/Delete",
        data: "{code: '" + code + "', gametypeid: " + gametypeid + ", integrationcode: '" + integrationcode + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            retval = msg.d;
            if (retval.Status == 5) {
                location.reload();
                return;
            }
        
            document.getElementById("couponfrm").src = "/IddaaKupon.aspx?ViewType=CouponSystem";

            if (tdcode != undefined) {
                if (document.getElementById(tdcode) != null) {
                    if (document.getElementById(tdcode).style.width == "52px") {
                        document.getElementById(tdcode).className = "tdBulten";
                    }
                    else {
                        document.getElementById(tdcode).className = "tdBultenFullSize";
                    }
                }
            }
        },
        error: function(xmlHttpRequest, status, err) {
            modalWarning('Oran Silinemedi.');
        }
    });
}

function findPos(obj) {
    var scrollTop = document.body.scrollTop;

    if (scrollTop == 0) {
        if (window.pageYOffset)
            scrollTop = window.pageYOffset;
        else
            scrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
    }

    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }

    curtop = curtop - scrollTop;
    return [curleft, curtop];
}

function disableWithAjax(id) {
    var pos = findPos(document.getElementById(id));

    document.getElementById('disablingDiv').style.top = pos[1] + "px";
    document.getElementById('disablingDiv').style.left = pos[0] + "px";
    document.getElementById('disablingDiv').divToHide = id;
    $('#disablingDiv').height($('#' + id).height());
    $('#disablingDiv').width($('#' + id).width());

    $('#disablingDiv').show();
}

function showWithAjax() {
    $('#disablingDiv').hide();
}

function AddPredefinedCoupon(couponid) {
    var oIframe = document.getElementById('couponfrm');
    var oDoc = (oIframe.contentWindow || oIframe.contentDocument);
    if (oDoc.document) oDoc = oDoc.document;

    if (oDoc.getElementById("couponBetCount") != undefined) {
        if (oDoc.getElementById("couponBetCount").value != "0") {
            if (!confirm('Kuponunuzdaki Etkinlikler Silinecek Emin Misiniz?')) {
                return;
            }
        } 
    }

    $.ajax({
        type: "POST",
        url: "CouponFunctions.aspx/AddPredefinedCoupon",
        data: "{couponid: " + couponid + " }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            retval = msg.d;
            if (retval.Status == 5) {
                location.reload();
                return;
            }
            
            document.getElementById("couponfrm").src = "/IddaaKupon.aspx?ViewType=CouponSystem";

            var obj = $(".tdBultenSelected");
            obj.removeClass("tdBultenSelected");
            obj.addClass("tdBulten");

            var objFullSize = $(".tdBultenFullSizeSelected");
            objFullSize.removeClass("tdBultenFullSizeSelected");
            objFullSize.addClass("tdBultenFullSize");
        },
        error: function(xmlHttpRequest, status, err) {
            enableCoupon('dCoupon');
            modalWarning('Hazır Kupon Eklenemedi.');
        }
    });
}
