﻿//check to see if pfd base oject is not null
if (typeof (pfd) == 'undefined') {
    pfd = function() { };
}
//add ajax for oject orientation
pfd.ajax = function() { };

pfd.ajax.persistCustomerEmail = function(emailAddress, promotionType, returnMessageDiv) {
    $.ajax({
        url: '/PersistCustomerEmail.asmx/PersistCustomerEmailToDatabase',
        type: 'POST',
        data: 'email=' + emailAddress + "&promotionType=" + promotionType,
        contentType: 'application/x-www-form-urlencoded',
        dataType: "xml",
        success: function(xml) {
            if ($(xml).find('string').text() == "Invalid Email Address") {
                if ($.cookie("couponEmail") == "initial") {
                    alert("Invalid Email Address. Please Try Again.");
                }
            } else {
                pfd.ajax.onSuccess(returnMessageDiv);
                $.cookie("coupon", "true");
             

            }
        },
        error: function(xml, status, error) {
            alert("There was a problem with this page:" + xml);
        }
    });
    pfd.ajax.onSuccess = function(returnMessageDiv) {
        pfd.display.showSuccessDiv(returnMessageDiv);
        pfd.utility.initializePage();
    }
}

pfd.ajax.PersistCustomerForSweepstakes = function (emailAddress, promotionType, siteSource, action, returnMessageDiv) {
    $.ajax({
        url: '/PersistCustomerEmail.asmx/PersistCustomerForSweepstakes',
        type: 'POST',
        data: 'email=' + emailAddress + "&promotionType=" + promotionType + "&siteSource=" + siteSource + "&action=" + action,
        contentType: 'application/x-www-form-urlencoded',
        dataType: "xml",
        success: function(xml) {
            if ($(xml).find('string').text() == "Invalid Email Address") {
//                if ($.cookie("couponEmail") == "initial") {
//                    alert("Invalid Email Address. Please Try Again.");
//                }
//            } else {
                pfd.ajax.onSuccess(returnMessageDiv);
                //$.cookie("coupon", "true");
             

            }
        },
        error: function(xml, status, error) {
            alert("There was a problem with this page:" + xml);
        }
    });
    pfd.ajax.onSuccess = function(returnMessageDiv) {
        pfd.display.showSuccessDiv(returnMessageDiv);
        pfd.utility.initializePage();
    }
}

pfd.ajax.addItemToCart = function (variantId, quantity, onComplete, onSuccess) {

    $.ajax({
        url: '/ClientSideOperations.asmx/AddItemToBasket',
        type: 'POST',
        data: 'variantId=' + variantId + "&quantity=" + quantity,
        contentType: 'application/x-www-form-urlencoded',
        dataType: "xml",
        complete: function (jqXHR, textStatus) {
            if (onComplete != null) { onComplete(textStatus == "success" || textStatus == "notmodified"); }
        },
        success: function (xml) {
            // will customize success process from caller
            if (onSuccess != null) {
                onSuccess(xml);
            } 
            // run default success process (i.e. redirect to cart/show BasketDisplay modal)
            else {

                var url = document.URL.toString();

                if (url.indexOf('cart.aspx') >= 0) {
                    window.location.href = "/cart.aspx";
                }
                else {
                    var ctrl = document.getElementById("cartCount");
                    var itemCount = xml.lastChild.lastChild.data;
                    ctrl.innerHTML = itemCount;

                    pfd.display.ShowPopup('BasketDisplay');
                }
            }
        },
        error: function (xml, status, error) {
            alert("error in web service");
        }
    });

}

pfd.ajax.addItemToAutoShip = function (variantId, quantity, orderGroupId, popupId) {
    $.ajax({
        url: '/ClientSideOperations.asmx/AddItemToAutoShip',
        type: 'POST',
        data: 'variantId=' + variantId + "&quantity=" + quantity + "&orderGroupId=" + orderGroupId,
        contentType: 'application/x-www-form-urlencoded',
        dataType: "xml",
        beforeSend: function () { pfd.display.ShowPopup('cartProgressBar'); },
        complete: function () { pfd.display.HidePopup('cartProgressBar'); },
        success: function (xml) {
            //var xml = $(xml)[0].lastChild.nodeTypedValue;
            //showBasketPopup();
            $("A.ViewMyAutoShipButton").attr("href", "/Profile/AutoShipDetailV2.aspx?o=" + orderGroupId);
//            $("A.ViewMyAutoShipButton").attr("href", "/Profile/AutoShipDetail.aspx");
            pfd.display.ShowPopup(popupId);

        },
        error: function (xml, status, error) {
            alert("error in web service");
        }
    });

}

function loadXMLString(txt) {
    if (window.DOMParser) {
        parser = new DOMParser();
        xmlDoc = parser.parseFromString(txt, "text/xml");
    }
    else // Internet Explorer
    {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(txt);
    }
    return xmlDoc;
}



