dojo.provide("atg.b2cblueprint.shipping");

atg.b2cblueprint.shipping={

  /*
    //////////////////////////////////////////////////////////////////////////
        Methods used for 'Shipping Calculation' popup from the cart page
    //////////////////////////////////////////////////////////////////////////
  */
  
  openCalculateOrderShippingPopup: function(pEventCodeTracking ) {
      var trigger = dojo.widget.byId("richCalculateOrderShippingTrigger");
      trigger.toggleCalculateShipping(pEventCodeTracking);
  },
  
  
  getShippingCostForOrder: function() {

    var calculateShippingWidjet = dojo.widget.byId("calculateOrderShipping");

    var validForSaturdayDelivery = document.getElementById("validForSaturdayDelivery");
    
    if (validForSaturdayDelivery.value = 'true') {
      var saturdayDeliveryFlag = calculateShippingWidjet.saturdayDeliveryFlag;
      var saturdayDeliveryToSend = dojo.byId("deliveryOnSaturday");
      saturdayDeliveryToSend.value = saturdayDeliveryFlag.checked;
    }
    
    dojo.io.bind({
    load: function(type, data, evt){

      var results = dojo.byId("atg_mff_calculateOrderShippingResults");
      results.innerHTML = data;
      var shippingTotal = dojo.byId("shippingTotalForOrder");
      
      calculateShippingWidjet.calculateShippingTotal.innerHTML = shippingTotal.value;
      var shippingOnCart = dojo.byId("calculateShippingId");
      
      shippingOnCart.innerHTML = shippingTotal.value;
    },
    formNode: dojo.byId("calculateOrderShippingResultsForm")
    });
  },
  
  
  changeMethodOnPopup: function(pSelect,pSourceWidgetId) {
    var methodToSubmit = dojo.byId(pSelect.id + "_method");
    
    methodToSubmit.value = pSelect.options[pSelect.selectedIndex].value;
    
    var calculateShippingWidjet = dojo.widget.byId(pSourceWidgetId);
    var saturdayDeliveryFlag = calculateShippingWidjet.saturdayDeliveryFlag;
    var syncDelivery = true;
    if (pSelect.id == 'defaultShippingGroup' && pSelect.options[pSelect.selectedIndex].value == 'Standard 4 to 7 Business Day Delivery') {
      syncDelivery = false;
    }
    
    atg.b2cblueprint.shipping.checkMethod(pSelect,saturdayDeliveryFlag, syncDelivery);
  },
  
  
  initSaturdayDelivery: function (pCalculateShippingWidjet){
  
    var validForSaturdayDelivery = document.getElementById("validForSaturdayDelivery");
    
    if (validForSaturdayDelivery.value == 'true') {
      var saturdayDeliveryMethod = document.getElementById("saturdayDeliveryMethod").value;
      var deliveryOnSaturday = pCalculateShippingWidjet.saturdayDeliveryFlag;
      deliveryOnSaturday.disabled = true;
      
      if (saturdayDeliveryMethod == "Express 1 Day Delivery") {
        var currentSelection = document.getElementById("auxiliaryExpressExpediteShippingGroup");
        if (currentSelection && currentSelection.options[currentSelection.selectedIndex].value == 'Express 1 Day Delivery') {
          deliveryOnSaturday.disabled = false;
          return;
        }
        currentSelection = document.getElementById("defaultShippingGroup");
        if (currentSelection && currentSelection.options[currentSelection.selectedIndex].value == 'Express 1 Day Delivery') {
          deliveryOnSaturday.disabled = false;
          return;
        }

      } else if (saturdayDeliveryMethod == "Expedite 2 to 3 Business Day Delivery") {
        var currentSelection = document.getElementById("auxiliaryExpressExpediteShippingGroup");
        if (currentSelection && currentSelection.options[currentSelection.selectedIndex].value == 'Expedite 2 to 3 Business Day Delivery') {
          deliveryOnSaturday.disabled = false;
          return;
        }
        currentSelection = document.getElementById("defaultShippingGroup");
        if (currentSelection && currentSelection.options[currentSelection.selectedIndex].value == 'Expedite 2 to 3 Business Day Delivery') {
          deliveryOnSaturday.disabled = false;
          return;
        }
      }
    }
  },

  checkMethod: function (pSelect, pSaturdayDeliveryFlag, pSyncDelivery) {

    var validForSaturdayDelivery = document.getElementById("validForSaturdayDelivery");
    if (validForSaturdayDelivery.value == 'true') {
      var saturdayDeliveryMethod = document.getElementById("saturdayDeliveryMethod").value;
      var deliveryOnSaturday = pSaturdayDeliveryFlag;
      
      if (pSyncDelivery) {
        atg.b2cblueprint.shipping.syncDelivery(pSelect);
      }
      
      
      if (pSelect.options[pSelect.selectedIndex].value == saturdayDeliveryMethod) {
        deliveryOnSaturday.disabled = false;
        
      } else {
        if (!atg.b2cblueprint.shipping.isAnotherGroupHasSelection(saturdayDeliveryMethod)) {
          deliveryOnSaturday.disabled = true;
        }
      }
      
    } else {
      if (pSyncDelivery) {
        atg.b2cblueprint.shipping.syncDelivery(pSelect);
      }
    }
    
    
  }, 

  isAnotherGroupHasSelection: function (pSaturdayDeliveryMethod) {
    var anotherSelection = document.getElementById("auxiliaryExpressExpediteShippingGroup");
    if (anotherSelection && anotherSelection.options[anotherSelection.selectedIndex].value == pSaturdayDeliveryMethod) {
      return true;
    }
    return false;
  },

  syncDelivery: function (pSelect) {
    
    var method = pSelect.options[pSelect.selectedIndex].value
    
    if (method == "Express 1 Day Delivery") {
      var anotherSelection = document.getElementById("auxiliaryExpressExpediteShippingGroup");
      if (anotherSelection && anotherSelection.options[anotherSelection.selectedIndex].value != method) {
        anotherSelection.options[1].selected  = true;
        var methodToSubmit = dojo.byId(anotherSelection.id + "_method");
        methodToSubmit.value = anotherSelection.options[anotherSelection.selectedIndex].value;
      }
      anotherSelection = document.getElementById("defaultShippingGroup");
      if (anotherSelection && anotherSelection.options[anotherSelection.selectedIndex].value != method) {
        anotherSelection.options[2].selected  = true;
        var methodToSubmit = dojo.byId(anotherSelection.id + "_method");
        methodToSubmit.value = anotherSelection.options[anotherSelection.selectedIndex].value;
      }

    } else if (method == "Expedite 2 to 3 Business Day Delivery") {
      var anotherSelection = document.getElementById("auxiliaryExpressExpediteShippingGroup");
      if (anotherSelection && anotherSelection.options[anotherSelection.selectedIndex].value != method) {
        anotherSelection.options[0].selected  = true;
        var methodToSubmit = dojo.byId(anotherSelection.id + "_method");
        methodToSubmit.value = anotherSelection.options[anotherSelection.selectedIndex].value;
      }
      anotherSelection = document.getElementById("defaultShippingGroup");
      if (anotherSelection  && anotherSelection.options[anotherSelection.selectedIndex].value != method) {
        anotherSelection.options[1].selected  = true;
        var methodToSubmit = dojo.byId(anotherSelection.id + "_method");
        methodToSubmit.value = anotherSelection.options[anotherSelection.selectedIndex].value;
      }
    }
  },
  
  /*
    ///////////////////////////////////////////////
        Methods used on the 'Shipping' step
    ///////////////////////////////////////////////
  */
  
  changeMethodOnShippingStep: function(pRadio, pShippingGroupName, pSyncDelivery, pSourceWidgetId)
  {
    var shippingOnCart = dojo.byId("orderShippingCost");
    shippingOnCart.innerHTML = "<font style=\"font-size: 0.8em\">Calculating...</font>";

    var totalOnCart = dojo.byId("orderTotalCost");
    totalOnCart.innerHTML = "<font style=\"font-size: 0.8em\">Calculating...</font>";

    var methodToSubmit = dojo.byId(pShippingGroupName + "_method");
    methodToSubmit.value = pRadio.value;
    
    atg.b2cblueprint.shipping.checkMethodOnShippingStep(pRadio, pSyncDelivery);
    
    var validForSaturdayDelivery = document.getElementById("validForSaturdayDelivery");

    if (validForSaturdayDelivery.value == 'true') {
      var saturdayDeliveryFlag = dojo.byId("deliveryOnSaturdayFlag");
      var saturdayDeliveryToSend = dojo.byId("deliveryOnSaturday");
      saturdayDeliveryToSend.value = saturdayDeliveryFlag.checked;
    }
    
    dojo.io.bind({
    load: function(type, data, evt){

      var results = dojo.byId("atg_mff_calculateOrderShippingResults");
      results.innerHTML = data;
      var shippingTotal = dojo.byId("shippingTotalForOrder");
      var orderTotal = dojo.byId("orderTotalForOrder");
      
      shippingOnCart.innerHTML = shippingTotal.value;
      totalOnCart.innerHTML = orderTotal.value;

      },
    formNode: dojo.byId("calculateOrderShippingResultsForm")
    });
  },

  changeSaturdayDeliveryCheckboxOnShippingStep: function(pSaturdayDeliveryFlag)
  {
    var shippingOnCart = dojo.byId("orderShippingCost");
    shippingOnCart.innerHTML = "<font style=\"font-size: 0.8em\">Calculating...</font>";

    var totalOnCart = dojo.byId("orderTotalCost");
    totalOnCart.innerHTML = "<font style=\"font-size: 0.8em\">Calculating...</font>";

    var saturdayDeliveryToSend = dojo.byId("deliveryOnSaturday");
    saturdayDeliveryToSend.value = pSaturdayDeliveryFlag.checked;
    
    dojo.io.bind({
    load: function(type, data, evt){

      var results = dojo.byId("atg_mff_calculateOrderShippingResults");
      results.innerHTML = data;
      var shippingTotal = dojo.byId("shippingTotalForOrder");
      var orderTotal = dojo.byId("orderTotalForOrder");
      
      shippingOnCart.innerHTML = shippingTotal.value;
      totalOnCart.innerHTML = orderTotal.value;

      },
    formNode: dojo.byId("calculateOrderShippingResultsForm")
    });
  },
  
  initSaturdayDeliveryOnShippingStep: function () {
  
    var validForSaturdayDelivery = document.getElementById("validForSaturdayDelivery");
    
    if (validForSaturdayDelivery.value == 'true') {
      var saturdayDeliveryMethod = document.getElementById("saturdayDeliveryMethod").value;
      var deliveryOnSaturday = document.getElementById("deliveryOnSaturdayFlag");
      deliveryOnSaturday.disabled = true;
      
      if (saturdayDeliveryMethod == "Express 1 Day Delivery") {
        var currentSelection = document.getElementById("shippingMethod_auxExpExp_expressDelivery");
        if (currentSelection && currentSelection.checked) {
          deliveryOnSaturday.disabled = false;
          return;
        }
        currentSelection = document.getElementById("shippingMethod_default_express");
        if (currentSelection && currentSelection.checked) {
          deliveryOnSaturday.disabled = false;
          return;
        }

      } else if (saturdayDeliveryMethod == "Expedite 2 to 3 Business Day Delivery") {
        var currentSelection = document.getElementById("shippingMethod_auxExpExp_expediteDelivery");
        if (currentSelection && currentSelection.checked) {
          deliveryOnSaturday.disabled = false;
          return;
        }
        currentSelection = document.getElementById("shippingMethod_default_expedite");
        if (currentSelection && currentSelection.checked) {
          deliveryOnSaturday.disabled = false;
          return;
        }
      }
    }
  },


  checkMethodOnShippingStep: function (pRadio, pSyncDelivery) {

    var validForSaturdayDelivery = document.getElementById("validForSaturdayDelivery");
    if (validForSaturdayDelivery.value == 'true') {
      var saturdayDeliveryMethod = document.getElementById("saturdayDeliveryMethod").value;
      var deliveryOnSaturday = document.getElementById("deliveryOnSaturdayFlag");
      
      if (pSyncDelivery) {
        atg.b2cblueprint.shipping.syncDeliveryOnShippingStep(pRadio);
      }
      
      
      if (pRadio.value == saturdayDeliveryMethod) {
        deliveryOnSaturday.disabled = false;
        
      } else {
        if (!atg.b2cblueprint.shipping.isAnotherGroupHasSelectionOnShippingStep(saturdayDeliveryMethod)) {
          deliveryOnSaturday.disabled = true;
        }
      }
      
    } else {
      if (pSyncDelivery) {
        atg.b2cblueprint.shipping.syncDeliveryOnShippingStep(pRadio);
      }
    }
    
  }, 

  isAnotherGroupHasSelectionOnShippingStep: function (pSaturdayDeliveryMethod) {
    if (pSaturdayDeliveryMethod == "Express 1 Day Delivery") {
      var anotherSelection = document.getElementById("shippingMethod_auxExpExp_expressDelivery");
      if (anotherSelection && anotherSelection.checked) {
        return true;
      }
    } else if (pSaturdayDeliveryMethod == "Expedite 2 to 3 Business Day Delivery") {
      var anotherSelection = document.getElementById("shippingMethod_auxExpExp_expediteDelivery");
      if (anotherSelection && anotherSelection.checked) {
        return true;
      }
    }
    return false;
  },

  syncDeliveryOnShippingStep: function (pRadio) {
  
    var method = pRadio.value;
    if (method == "Express 1 Day Delivery") {
      var anotherSelection = document.getElementById("shippingMethod_auxExpExp_expressDelivery");
      if (anotherSelection) {
        anotherSelection.checked = true;
        
        var methodToSubmit = dojo.byId("auxiliaryExpressExpediteShippingGroup_method");
        methodToSubmit.value = anotherSelection.value;
      }
      anotherSelection = document.getElementById("shippingMethod_default_express");
      if (anotherSelection) {
        anotherSelection.checked = true;

        var methodToSubmit = dojo.byId("defaultShippingGroup_method");
        methodToSubmit.value = anotherSelection.value;
      }
    } else if (method == "Expedite 2 to 3 Business Day Delivery") {
      var anotherSelection = document.getElementById("shippingMethod_auxExpExp_expediteDelivery");
      if (anotherSelection) {
        anotherSelection.checked = true;

        var methodToSubmit = dojo.byId("auxiliaryExpressExpediteShippingGroup_method");
        methodToSubmit.value = anotherSelection.value;
      }
      anotherSelection = document.getElementById("shippingMethod_default_expedite");
      if (anotherSelection) {
        anotherSelection.checked = true;

        var methodToSubmit = dojo.byId("defaultShippingGroup_method");
        methodToSubmit.value = anotherSelection.value;
      }
    }
  },

  /*
    ///////////////////////////////////////////////////
        Shipping calculation from the product page
    ///////////////////////////////////////////////////
  */
  
  openCalculateProductShippingPopup: function(pEventCodeTracking) {
      var trigger = dojo.widget.byId("richCalculateShippingTrigger");
      trigger.toggleCalculateShipping(pEventCodeTracking);
  },
  
  
  submitRefreshForm: function() {
    dojo.io.bind({
    load: function(type, data, evt){
      var divColorPicker = dojo.byId("atg_b2cblueprint_calculateShippingResults");
      divColorPicker.innerHTML = data;
      },
    formNode: dojo.byId("calculateShippingResultsForm")
    });
  },
  
  initSaturdayDeliveryOnProductDetails: function(pCalculateShippingWidjet, pCalculateShippingSelect) {
    var saturdayDeliveryMethod = dojo.byId("hShippingMethodForSaturdayDelivery");

    if (saturdayDeliveryMethod.value != '' ) {

      var saturdayDeliveryFlag = pCalculateShippingWidjet.saturdayDeliveryFlag;//dojo.byId("saturdayDeliveryFlag");
      
      var selectedMethod = pCalculateShippingSelect.options[0];
      if (selectedMethod.value == saturdayDeliveryMethod.value) {
        saturdayDeliveryFlag.disabled = false;
      } else {
        saturdayDeliveryFlag.disabled = true;
        saturdayDeliveryFlag.checked = false;
      }
    } else {
      dojo.html.hide(pCalculateShippingWidjet.saturdayDelivery);
    }
  },
  
  checkSaturday: function(pSelect,pSourceWidgetId) {
    
    var calculateShippingWidjet = dojo.widget.byId(pSourceWidgetId);
    var saturdayDeliveryMethod = dojo.byId("hShippingMethodForSaturdayDelivery");
    
    var saturdayDeliveryFlag = calculateShippingWidjet.saturdayDeliveryFlag;//dojo.byId("saturdayDeliveryFlag");
    
    var selectedMethod = pSelect.options[pSelect.selectedIndex];
    if (saturdayDeliveryMethod.value != '' && selectedMethod.value == saturdayDeliveryMethod.value) {
      saturdayDeliveryFlag.disabled = false;
    } else {
      saturdayDeliveryFlag.disabled = true;
      saturdayDeliveryFlag.checked = false;
    }
  },
  
  getShippingCost: function() {
    var calculateShippingWidjet = dojo.widget.byId("calculateShipping");
    
    var quantityToSend = dojo.byId("hQuantity");
    var selectedQuantity = dojo.byId("atg_b2cblueprint_quantityField");
    
    quantityToSend.value = selectedQuantity.value;
    
    var calculateShippingSelect = calculateShippingWidjet.calculateShippingSelect;//dojo.byId("calculateShippingSelect");
    var methodToSend = dojo.byId("hShippingMethod");
    var selectedOption = calculateShippingSelect.options[calculateShippingSelect.selectedIndex];
    methodToSend.value = selectedOption.value;

    var saturdayDeliveryFlag = calculateShippingWidjet.saturdayDeliveryFlag;//dojo.byId("saturdayDeliveryFlag");
    var saturdayDeliveryToSend = dojo.byId("hDeliveryOnSaturday");
    saturdayDeliveryToSend.value = saturdayDeliveryFlag.checked;
    
    dojo.io.bind({
    load: function(type, data, evt){
      var divColorPicker = dojo.byId("atg_b2cblueprint_calculateShippingResults");
      divColorPicker.innerHTML = data;
      var shippingTotal = dojo.byId("hShippingTotal");
      
      calculateShippingWidjet.selectedMethod.innerHTML = selectedOption.text;
      calculateShippingWidjet.saturdayDeliveryShow.innerHTML ="";
      if(calculateShippingWidjet.saturdayDeliveryFlag.checked){
        calculateShippingWidjet.saturdayDeliveryShow.innerHTML = '(' + calculateShippingWidjet.i18n.showSaturdayDeliveryText + ')';
      }
      calculateShippingWidjet.calculateShippingTotal.innerHTML = '- ' + shippingTotal.value;
      },
    formNode: dojo.byId("calculateShippingResultsForm")
    });
  }
};
