﻿//********** Confrimation form**********************************
function ConfirmationDialog_check(titleText, message, width, height, callBackAction) {
    var msgw, msgh, bordercolor;
    msgw = width;
    msgh = height;
    titleheight = 20;
    bordercolor = "#336699";
    titlecolor = "#99CCFF";
    var sWidth, sHeight;
    sWidth = document.body.offsetWidth;
    sHeight = document.body.offsetHeight;
    if (sHeight < screen.height) {
        sHeight = screen.height;
    }
    var bgDiv = document.createElement("div");
    bgDiv.setAttribute('id', 'bgDiv');
    bgDiv.style.position = "absolute";
    bgDiv.style.top = "0";
    bgDiv.style.background = "#777";
    bgDiv.style.filter = "alpha(opacity=50)";
    bgDiv.style.opacity = "0.5";
    bgDiv.style.left = "0";
    bgDiv.style.width = sWidth + "px";
    bgDiv.style.height = sHeight + "px";
    bgDiv.style.zIndex = "10000";
    document.body.appendChild(bgDiv);

    var msgDiv = document.createElement("div")
    msgDiv.setAttribute("id", "msgDiv");
    msgDiv.setAttribute("align", "left");
    msgDiv.style.background = "white";
    msgDiv.style.border = "1px solid " + bordercolor;
    msgDiv.style.position = "absolute";

    var scrTop = document.body.scrollTop;
    if (scrTop == 0) { scrTop = document.documentElement.scrollTop; }

    var scrLeft = document.body.scrollLeft;
    if (scrLeft == 0) { scrLeft = document.documentElement.scrollLeft; }

    msgDiv.style.top = ((screen.height - msgh - 200) / 2 + scrTop) + "px"; //"50%";
    msgDiv.style.left = ((screen.width - msgw) / 2 + scrLeft) + "px"; // "50%";
    msgDiv.style.font = "12px Arial";
    //msgDiv.style.marginLeft =(-msgw/2) +"px" ;
    //msgDiv.style.marginTop = -75+document.documentElement.scrollTop+"px";
    msgDiv.style.width = msgw + "px";
    //msgDiv.style.height = msgh + "px";
    msgDiv.style.textAlign = "left";
    msgDiv.style.lineHeight = (msgh - titleheight) + "px";
    msgDiv.style.zIndex = "10001";

    var title = document.createElement("h4");
    title.setAttribute("id", "msgTitle");
    title.setAttribute("align", "left");
    title.style.margin = "0";
    title.style.padding = "3px";
    title.style.background = bordercolor;
    title.style.filter = "alpha(opacity=50);";
    title.style.opacity = "0.5";
    title.style.border = "1px solid " + bordercolor;
    title.style.height = "18px";
    title.style.font = "12px Arial";
    title.style.color = "white";
    title.style.cursor = "pointer";
    title.innerText = titleText;
    title.innerHTML = titleText;

    var divBtn = document.createElement("div");
    divBtn.setAttribute("id", "divBtn");
    divBtn.setAttribute("align", "center");
    divBtn.style.background = "white";
    divBtn.style.width = "100%";
    divBtn.style.textAlign = "center";

    var btnYes = document.createElement("input");
    btnYes.setAttribute("type", "button");
    btnYes.setAttribute("id", "btnYes");
    btnYes.value = 'Yes';
    btnYes.style.margin = "12px";
    btnYes.style.height = "24px";
    btnYes.style.width = "80px";
    btnYes.onclick = function () {
        document.body.removeChild(bgDiv);
        document.getElementById("msgDiv").removeChild(title);
        document.body.removeChild(msgDiv);
        callBackAction(true);
    };

    var btnNo = document.createElement("input");
    btnNo.setAttribute("type", "button");
    btnNo.setAttribute("id", "btnNo");
    btnNo.value = 'No';
    btnNo.style.margin = "12px";
    btnNo.style.height = "24px";
    btnNo.style.width = "80px";
    btnNo.onclick = function () {
        document.body.removeChild(bgDiv);
        document.getElementById("msgDiv").removeChild(title);
        document.body.removeChild(msgDiv);
        callBackAction(false);
    };


    var divTxt = document.createElement("div");
    divTxt.setAttribute("id", "divTxt");
    divTxt.setAttribute("align", "left");
    divTxt.style.background = "white";
    divTxt.style.width = (msgw - 48) + "px";
    divTxt.style.textAlign = "left";
    divTxt.style.paddingLeft = "24px";

    var lblText = document.createElement("label");
    lblText.setAttribute("id", "msgTxt");
    lblText.style.font = "12px Arial";
    lblText.innerHTML = "<br>" + message + "<br>";
    lblText.style.marginleft = "16px";


    //document.body.style.overflow = "hidden";
    document.body.appendChild(msgDiv);
    document.getElementById("msgDiv").appendChild(title);

    document.getElementById("msgDiv").appendChild(divTxt);
    document.getElementById("divTxt").appendChild(lblText);

    document.getElementById("msgDiv").appendChild(divBtn);

    document.getElementById("divBtn").appendChild(btnYes);
    document.getElementById("divBtn").appendChild(btnNo);
}

//**************************** Confirmation Form ENDS **********************************************

/*
** dependence function: 
** ConfirmationDialog(titleText, message, width, height, callBackAction)
** in scripts/General.js
** dependence on jQuery .
*/
function checkDrugsExclusion(catalogId, packageId, buyForm) {
    if (!buyForm || !buyForm.submit) {
        return;
    }
    var actionTo = "/ordering/addtocart/" + catalogId + "?packageid=" + packageId + "&forceAdd=";
    var m = null;
    var checkdrugURL = "/ordering/checkdrugsexclusion/" + catalogId + "?packageid=" + packageId + "&" + Math.random()

    $.ajax({ url: checkdrugURL,
        dataType: "json",
        async: false,
        type: "POST",
        success: function (c) {
            m = c;
            if (m != null && m.isExclusion) {
                //var msg = 'You can not order ' + m.bDrugName + ' with ' + m.aDrugName + '. Do you want to buy ' + m.aDrugName + '? ';
                //var msg = m.msg;
                var msg = "You can not order " + m.aDrugName + " with " + m.bDrugName + ". ";
                var toRemove = (m.bPackageName == "") ? m.bDrugName : m.bPackageName + " which include " + m.bDrugName;
                var toBuy = (m.aPackageName == "") ? m.aDrugName : "this package which include " + m.aDrugName;
                var msg2 = "Do you want to buy " + toBuy + "? <br /><br />Click Yes will remove the " + toRemove + " from cart."

                msg += msg2;

                ConfirmationDialog_check('Confirmation', msg, 400, 100, function (retValue) {
                    if (retValue) {
                        // remove drug B from cart, and add drug A to cart.

                        actionTo += true;

                        buyForm.action = actionTo;
                        buyForm.submit();
                    }
                    else {
                        // do nothing.
                        return true;
                    }
                });
            }
            else {
                buyForm.action = actionTo;
                buyForm.submit();
            }
        }
    });
}

function checkDrugsExclusionForRedirect(catalogId, dstUrl) {

    var actionTo = dstUrl + "&forceAdd=";
    var m = null;
    var checkdrugURL = "/ordering/checkdrugsexclusion/" + catalogId + "?" + Math.random();

    $.ajax({ url: checkdrugURL,
        dataType: "json", async: false, type: "POST", success: function (c) {
            m = c;
            if (m != null && m.isExclusion) {
                //var msg = 'You can not order ' + m.bDrugName + ' with ' + m.aDrugName + '. Do you want to buy ' + m.aDrugName + '? ';
                //var msg = m.msg;
                var msg = "You can not order " + m.aDrugName + " with " + m.bDrugName + ". ";
                var toRemove = (m.bPackageName == "") ? m.bDrugName : m.bPackageName + " which include " + m.bDrugName;
                var toBuy = (m.aPackageName == "") ? m.aDrugName : "this package which include " + m.aDrugName;
                var msg2 = "Do you want to buy " + toBuy + "? <br /><br />Click Yes will remove the " + toRemove + " from cart."

                msg += msg2;

                ConfirmationDialog_check('Confirmation', msg, 400, 100, function (retValue) {
                    if (retValue) {
                        // remove drug B from cart, and add drug A to cart.

                        actionTo += true;
                        window.location = actionTo;
                    }
                    else {
                        // do nothing.
                        return true;
                    }
                });
            }
            else {
                window.location = actionTo;
            }
        }
    });
}

/*  end of function checkDrugsExclusion
**********************************************/
