function Commerce() {}

var showGoToButton = 1;

Commerce.prototype.AddToBasket = function(sitecoreID, interval, quantity) {
    showGoToButton = 1;
    ShowAjaxHourGlass('ajax_loading_basket');
    ShowAjaxHourGlass('ajax_loading_fbox');
    document.location = "#"; // jump to the top of the page
    if (quantity == "") quantity = 1;
    if (interval == "") interval = 0; /* for non-interval products */
    new Ajax.Request('/sitecore modules/commerce/dispatcher.aspx', { method: 'post', postBody: 'command=add&sitecoreID='+sitecoreID+'&interval='+interval+'&quantity='+quantity, onSuccess: commerce.UpdateBasketContainer, onFailure:commerce.ShowError });
    new Ajax.Request('/sitecore modules/commerce/updater.aspx', { method: 'post', postBody: 'command=update&actualcustomerno=', onSuccess: commerce.Update_FruitBoxes, onFailure:commerce.ShowError });
}

/* converts subscription type items to 'one time' orders */
Commerce.prototype.AddToBasketSingle = function(sitecoreID, quantity) {
    showGoToButton = 1;
    ShowAjaxHourGlass('ajax_loading_basket');
    document.location = "#"; // jump to the top of the page
    if (quantity == "") quantity = 1;
    new Ajax.Request('/sitecore modules/commerce/dispatcher.aspx', { method: 'post', postBody: 'command=addsingle&sitecoreID='+sitecoreID+'&interval=0&quantity='+quantity, onSuccess: commerce.UpdateBasketContainer, onFailure:commerce.ShowError });
}

Commerce.prototype.AddToBasketNoUpdate = function(sitecoreID, interval, quantity) {
    if (quantity == "") quantity = 1;
    if (interval == "") interval = 0; /* for non-interval products */
    new Ajax.Request('/sitecore modules/commerce/dispatcher.aspx', { method: 'post', postBody: 'command=add&sitecoreID='+sitecoreID+'&interval='+interval+'&quantity='+quantity, onFailure:commerce.ShowError });
}

Commerce.prototype.UpdateBasket = function(orderlineID, interval, quantity, showButton) {
    showGoToButton = showButton;
    ShowAjaxHourGlass('ajax_loading_basket');
    if (quantity == "") quantity = 0;
    new Ajax.Request('/sitecore modules/commerce/dispatcher.aspx', { method: 'post', postBody: 'command=update&orderlineID='+orderlineID+'&interval='+interval+'&quantity='+quantity, onSuccess: commerce.UpdateBasketContainer, onFailure:commerce.ShowError });
}

Commerce.prototype.RemoveFromBasket = function(sitecoreID, interval) {
    ShowAjaxHourGlass('ajax_loading_basket');
    ShowAjaxHourGlass('ajax_loading_fbox');
    new Ajax.Request('/sitecore modules/commerce/dispatcher.aspx', { method: 'post', postBody: 'command=remove&sitecoreID='+sitecoreID+'&interval='+interval, onSuccess: commerce.UpdateBasketContainer, onFailure:commerce.ShowError });
    new Ajax.Request('/sitecore modules/commerce/updater.aspx', { method: 'post', postBody: 'command=update&actualcustomerno=', onSuccess: commerce.Update_FruitBoxes, onFailure:commerce.ShowError });
}

Commerce.prototype.CreateOrder = function() {
    new Ajax.Request('/sitecore modules/commerce/dispatcher.aspx', { method: 'post', postBody: 'command=createorder', onSuccess: commerce.UpdateBasketContainer, onFailure:commerce.ShowError });
}

Commerce.prototype.UpdateBasketContainer = function(transport) {
    
    var basketContainer = $('BasketContainer');
    var frontsideCalculator = $('ChosenProductCountBoxes');
    if (basketContainer != null)
    {
        var requestString = '/Firmafrugt/Frontpage/ajax_Basket.aspx';
        if (!showGoToButton)
            requestString += '?nobtn=true';
        new Ajax.Request(requestString, { method: 'get', onSuccess: function(transport) { basketContainer.innerHTML = transport.responseText; }, onFailure:commerce.ShowError});
    }
    else if (frontsideCalculator != null)
        Commerce.setTimeout(RedirectIt(), 1000); // This delay isn't working..
}

function RedirectIt()
{
    document.location = "/Firmafrugt/Frugtkasserne/Bestil.aspx";
}

/* Temp basket functionality */
Commerce.prototype.AddToTempBasket = function(sitecoreID, interval, quantity) {
    if (quantity == "") quantity = 1;
    if (interval == "") interval = 0; /* for non-interval products */
    new Ajax.Request('/sitecore modules/commerce/dispatcher.aspx', { method: 'post', postBody: 'command=addtotempbasket&sitecoreID='+sitecoreID+'&interval='+interval+'&quantity='+quantity, onSuccess: commerce.UpdateEditSubscriptions(), onFailure:commerce.ShowError });
}

/* If the EditSubscription xslt rendering updates qty, interval or stop_sub, then we only need to update this particular AJAX item*/
Commerce.prototype.AddSubscriptionToTempBasket = function(subscriptionNo, interval, quantity, stop, internControlUpdate) {
    ShowAjaxHourGlass('ajax_loading_eSub');
    if (!internControlUpdate)
        new Ajax.Request('/sitecore modules/commerce/dispatcher.aspx', { method: 'post', postBody: 'command=addsubscriptiontotempbasket&subscriptionNo='+subscriptionNo+'&interval='+interval+'&quantity='+quantity+'&stop='+stop, onSuccess: commerce.UpdateEditSubscriptions(), onFailure:commerce.ShowError });
    else
        new Ajax.Request('/sitecore modules/commerce/dispatcher.aspx', { method: 'post', postBody: 'command=addsubscriptiontotempbasket&subscriptionNo='+subscriptionNo+'&interval='+interval+'&quantity='+quantity+'&stop='+stop, onFailure:commerce.ShowError });
    new Ajax.Request('/sitecore modules/commerce/updater.aspx', { method: 'post', postBody: 'command=update&actualcustomerno=', onSuccess: commerce.Update_EditSubscriptions, onFailure:commerce.ShowError });
}

Commerce.prototype.RemoveFromTempBasket = function(subscriptionNo) {
    ShowAjaxHourGlass('ajax_loading_eSub');
    new Ajax.Request('/sitecore modules/commerce/dispatcher.aspx', { method: 'post', postBody: 'command=removefromtempbasket&subscriptionNo='+subscriptionNo, onSuccess: commerce.UpdateEditSubscriptions(), onFailure:commerce.ShowError });
}

Commerce.prototype.RemoveTempBasket = function(actualcustomeraddressno) {
    new Ajax.Request('/sitecore modules/commerce/dispatcher.aspx', { method: 'post', postBody: 'command=removetempbasket&actualcustomerno=', onSuccess: commerce.UpdateEditSubscriptions(), onFailure:commerce.ShowError });
    new Ajax.Request('/sitecore modules/commerce/updater.aspx', { method: 'post', postBody: 'command=update&actualcustomerno=', onSuccess: commerce.Update_EditSubscriptions, onFailure:commerce.ShowError });
}

/* Show Error */
Commerce.prototype.ShowError = function(transport) {
    alert("Failure: " + transport.responseText);
}

/* Update EditSubscriptions and (Subscription) FruitBoxes */
Commerce.prototype.UpdateEditSubscriptions = function() {
    ShowAjaxHourGlass('ajax_loading_fbox');
    ShowAjaxHourGlass('ajax_loading_eSub');
    new Ajax.Request('/sitecore modules/commerce/updater.aspx', { method: 'post', postBody: 'command=nothing', onSuccess: commerce.Update_EditSubscriptions, onFailure:commerce.ShowError });
    new Ajax.Request('/sitecore modules/commerce/updater.aspx', { method: 'post', postBody: 'command=nothing', onSuccess: commerce.Update_Boxes, onFailure:commerce.ShowError });
}

Commerce.prototype.Update_EditSubscriptions = function(transport) {    
    var container = $('_SectionEditContainer');
    new Ajax.Request('/Firmafrugt/Din Firmakonto/ajax_EditSubscriptions.aspx', { method: 'get', onSuccess: function(transport) { container.innerHTML = transport.responseText; container.innerHTML.evalScripts(); }, onFailure:commerce.ShowError});

    commerce.Update_SubscriptionChangedNotification(transport);
}

Commerce.prototype.Update_SubscriptionChangedNotification = function(transport) {
    var container = $('SubscriptionChangedUnsavedContainer');
    new Ajax.Request('/Firmafrugt/Din Firmakonto/ajax_SubscriptionChangedNotification.aspx', { method: 'get', onSuccess: function(transport) { container.outerHTML = transport.responseText; }, onFailure:commerce.ShowError});
}

Commerce.prototype.Update_Boxes = function(transport) {
    var container = $('BoxesContainer');
    new Ajax.Request('/Firmafrugt/Din Firmakonto/ajax_EditSubscriptionBoxes.aspx', { method: 'get', onSuccess: function(transport) { container.innerHTML = transport.responseText; } , onFailure:commerce.ShowError});
}

Commerce.prototype.Update_FruitBoxes = function(transport) {
    var container = $('BoxesContainer');
    new Ajax.Request('/Firmafrugt/FrontPage/ajax_FruitBoxes.aspx', { method: 'get', onSuccess: function(transport) { container.outerHTML = transport.responseText; } , onFailure:commerce.ShowError});   
}

/* Update ViewSubscriptions */
Commerce.prototype.UpdateViewSubscriptions = function(actualcustomeraddressno) {
    ShowAjaxHourGlass('ajax_loading_sub');
    new Ajax.Request('/sitecore modules/commerce/updater.aspx', { method: 'post', postBody: 'command=update&actualcustomerno='+actualcustomeraddressno, onSuccess: commerce.Update_ViewSubscriptions, onFailure:commerce.ShowError });
}

Commerce.prototype.Update_ViewSubscriptions = function(transport) {
    var container = $('SectionBoxed');
    new Ajax.Request('/Firmafrugt/Din Firmakonto/ajax_ViewSubscriptions.aspx', { method: 'get', onSuccess: function(transport) { container.outerHTML = transport.responseText; }, onFailure:commerce.ShowError});
}

/* Update Subscriptions7DaysOverview */
Commerce.prototype.UpdateSubscriptions7DaysOverview = function(actualcustomeraddressno) {
    ShowAjaxHourGlass('ajax_loading_weekSub');
    new Ajax.Request('/sitecore modules/commerce/updater.aspx', { method: 'post', postBody: 'command=update&actualcustomerno='+actualcustomeraddressno, onSuccess: commerce.Update_Subscriptions7DaysOverview, onFailure:commerce.ShowError });
}

Commerce.prototype.Update_Subscriptions7DaysOverview = function(transport) {
    var container = $('_SectionSubscription7DaysOverview');
    new Ajax.Request('/Firmafrugt/Din Firmakonto/ajax_SubscriptionsWeeklyOverview.aspx', { method: 'get', onSuccess: function(transport) { container.innerHTML = transport.responseText; container.innerHTML.evalScripts(); }, onFailure:commerce.ShowError});
}

/* UPDATE Subscriptions */
Commerce.prototype.UPDATEsubscriptions = function() {
    ShowAjaxHourGlass('ajax_loading_eSub');
    new Ajax.Request('/sitecore modules/commerce/updater.aspx', { method: 'post', postBody: 'command=updatesubscriptions', onSuccess: function(transport) {document.location = "/Firmafrugt/Din firmakonto/Rediger abonnement/Abonnement aendret.aspx";}, onFailure:commerce.ShowError });
}

/* UPDATE Subscriptions */
Commerce.prototype.UPDATEsubscriptionEntries = function(subscriptionEntriesCollectionXml) {
    ShowAjaxHourGlass('ajax_loading_weekSub');
    new Ajax.Request('/sitecore modules/commerce/updater.aspx', { method: 'post', postBody: 'command=updatesubscriptionentries&xml='+subscriptionEntriesCollectionXml, onSuccess: showSubscriptionOverviewMessage, onFailure:commerce.ShowError });
}

Commerce.prototype.ReloadPage = function() {
    allowCancelChanges = 1;
    window.location.reload();
}

/* Update AcutalCustomerNo */
Commerce.prototype.UpdateActualCustomerNo = function(actualcustomeraddressno) {
    new Ajax.Request('/sitecore modules/commerce/updater.aspx', { method: 'post', postBody: 'command=update&actualcustomerno='+actualcustomeraddressno, onFailure:commerce.ShowError });
}

/* Update AccountInformation */
Commerce.prototype.UpdateAccountInformation = function(fromdate, todate) {
    ShowAjaxHourGlass('ajax_loading_ai');
    new Ajax.Request('/sitecore modules/commerce/updater.aspx', { method: 'post', postBody: 'command=updateaccountinfodateinterval&fromdate='+fromdate+'&todate='+todate, onSuccess: commerce.Update_AccountInformation, onFailure:commerce.ShowError });
}

Commerce.prototype.Update_AccountInformation = function(transport) {
    var container = $('fs_accountInformationPosts');
    new Ajax.Request('/Firmafrugt/Din Firmakonto/ajax_accountinformation.aspx', { method: 'get', onSuccess: function(transport) { container.outerHTML = transport.responseText; }, onFailure:commerce.ShowError});
}

var commerce = new Commerce();
