﻿var assignNavigationBasket = {
  // utility function which assigns navigation in the basket where none is placed from the system
  init: function () {
    $("#OrderStep1_TD input[name='Search']").css({
      "float": "right",
      "position": "relative",
      "top": "12px"
    });
    $("#OrderStep2_TD input[name='Search']").css({
      "float": "right",
      "position": "relative",
      "top": "32px"
    });
  }
}


var cookieMgr = {
  getExpDate: function (days, hours, minutes) {
    var expDate = new Date();
    if (typeof days == "number" && typeof hours == "number" &&
typeof minutes == "number") {
      expDate.setDate(expDate.getDate() + parseInt(days));
      expDate.setHours(expDate.getHours() + parseInt(hours));
      expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
      return expDate.toGMTString();
    }
  },
  getCookieVal: function (offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) {
      endstr = document.cookie.length;
    }
    return decodeURI(document.cookie.substring(offset, endstr));
  },
  getCookie: function (name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
      var j = i + alen;
      if (document.cookie.substring(i, j) == arg) {
        return this.getCookieVal(j);
      }
      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0) break;
    }
    return "";
  },
  setCookie: function (name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + encodeURI(value) +
((expires) ? "; expires=" + expires : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
  },
  deleteCookie: function (name, path, domain) {
    if (this.getCookie(name)) {
      document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
  }
};

var incomedata; //VAR FOR COOKIE RETURN


var b2bScripts = {
  init: function () {

    if (document.getElementById('b2blogout')) {
      var html = '<p class="topMenuB2Bmessage">Du er nu logget ind i kundeklubben - god fornøjelse</p>';
      document.getElementById('topB2BWrapper').innerHTML += html;
    }
    if (document.getElementById('b2bAdded')) {
      document.getElementById('b2bAddedContent').style.display = "block";
    }



  }
}




init();

function init() {
  checkforBasketShipCookie();
  // MOVE PICTURE FROM SHORT DESCRIPTION UP TO NEWSPIC DIV
  $('#prCardNewProductImageInsert').replaceWith($('#prCardShortDescipr').find('IMG'));

  assignNavigationBasket.init();
  b2bScripts.init();
}


// BEGIN COOKIE BASKET
function checkforBasketShipCookie() {
  if (document.getElementById('basketTotPrice')) {
    if (navigator.cookieEnabled) {
      incomedata = cookieMgr.getCookie("checkforBasketShipCookie");

      if (incomedata == "") {
        // DO NOTHING 
      }
      if (incomedata == "deliverByStore") {
        commandStyling();
      }
    }

  } else if (!navigator.cookieEnabled) {
    //  USER HAS NO COOKIE HIDE FUNCTION
    document.getElementById('basketAltShipChBoxDIV').style.display = "none";
  }

  //ASSIGN SHIPPING METHOD IF COOKIE IS TRUE
  if (document.getElementById('ShippingMethodsContainer')) {

    if (navigator.cookieEnabled) {
      incomedata = cookieMgr.getCookie("checkforBasketShipCookie");


      if (incomedata == "") {
        // DO NOTHING BUT ASSIGN CLICK HANDLERS
        if (document.getElementById('ShippingMethodID48')) {
          document.getElementById('ShippingMethodID48').onclick = deleteCookieOnFreightMethod;
        }
        if (document.getElementById('ShippingMethodID56')) {
          document.getElementById('ShippingMethodID56').onclick = setCookieOnFreightMethod;
        }
      }
      if (incomedata == "deliverByStore") {

        if (document.getElementById('ShippingMethodID48')) {
          document.getElementById('ShippingMethodID48').onclick = deleteCookieOnFreightMethod;
          if (document.getElementById('ShippingMethodID48').checked == true) { document.getElementById('ShippingMethodID48').checked = false }

        }
        // AFHENT I BUTIK
        if (document.getElementById('ShippingMethodID56')) {
          document.getElementById('ShippingMethodID56').onclick = setCookieOnFreightMethod;
          if (document.getElementById('ShippingMethodID56').checked == false) { document.getElementById('ShippingMethodID56').checked = true }

        }



      }
    }

  }
}
function setCookieOnFreightMethod() {
  var data = "deliverByStore";
  var expDate = cookieMgr.getExpDate(2, 0, 0);
  cookieMgr.setCookie("checkforBasketShipCookie", data, expDate);
  window.location = "/shop/order2.html";
}
function deleteCookieOnFreightMethod() {
  var data = "";
  var expDate = cookieMgr.getExpDate(0, 0, 0);
  cookieMgr.setCookie("checkforBasketShipCookie", data, expDate);
  window.location = "/shop/order2.html";
}

function commandStyling() {
  var shippingFeeVar = (document.getElementById('basketShippingFee').innerHTML);
  var basketTotalPrice = (document.getElementById('basketTotPrice').innerHTML);

  shippingFeeVar = shippingFeeVar.replace(/\./ig, "");
  shippingFeeVar = shippingFeeVar.replace(/\,/ig, ".");
  basketTotalPrice = basketTotalPrice.replace(/\./ig, "");
  basketTotalPrice = basketTotalPrice.replace(/\,/ig, ".");
  parseFloat(basketTotalPrice);
  parseFloat(shippingFeeVar);

  document.getElementById('basketTotPrice').innerHTML = (basketTotalPrice - shippingFeeVar).toFixed(2);
  document.getElementById('basketShippingFee').innerHTML = "0,00";
  document.getElementById('basketAltShipChBox').checked = true;

  var data = "deliverByStore";
  var expDate = cookieMgr.getExpDate(1, 0, 0);
  cookieMgr.setCookie("checkforBasketShipCookie", data, expDate);

}

function shippingFeeChecked() {
  if (basketAltShipChBox.checked == true) {
    commandStyling();
  }
  if (basketAltShipChBox.checked == false) {
    deleteCookie();
    window.location = "/shop/showbasket.html";
  }
}



function deleteCookie() {
  var data = "";
  var expDate = cookieMgr.getExpDate(0, 0, 0);
  cookieMgr.setCookie("checkforBasketShipCookie", data, expDate);
}

// END COOKIE BASKET



