function masterStart() {
  fixPrMenuSpanTagMenu();
  categoryPictureList();
  removeTrDelivery();

  if (document.getElementById('prCardWrapper')) {
    $(document).ready(function(){
      prcardDatepick();
      watchMouseOverBuy();
    })
  }
  if (window.location.pathname == '/shop/order1.html') {
    $(document).ready(function(){
      buyDatepicker();
    })
  }

}

function fixPrMenuSpanTagMenu() {
  if (document.getElementById('ProductMenu_Table')) {
    var tempSpan = document.getElementById('ProductMenu_Table').getElementsByTagName('SPAN');
    for (var i = 0; i < tempSpan.length; i++) {
      if (tempSpan[i].className == "ProductMenu_MenuItemBold") {
        tempSpan[i].parentNode.innerHTML += tempSpan[i].innerHTML;
        tempSpan[i].style.display = "none";
        tempSpan[i].innerHTML = "";
      }
    };
  }
}

function updatePgLogOutB2B() { // This function changes the LogOut function with and update so the PrMenu disappears
  var getTDTop = document.getElementById('ShopLayOut_Topmenu_TD');
  var getTDTopa = getTDTop.getElementsByTagName('A');

  for (var i = 0; i < getTDTopa.length; i++) {

    if (getTDTopa[i].innerHTML == "LogOut") {
      //getTDTopa[i].setAttribute('onClick','reloadPage()');
      //getTDTopa[i].setAttribute('href','javascript:reloadPage()');
    }
  }
}

function addSpaceTd() { // ADD SPACE TO TD IN ORDER
  var getTable = document.getElementsByTagName('TABLE');
  for (var i = 0; i < getTable.length; i++) {
    if (getTable[i].className == 'BackGroundColor1_OrderStep4') {
      var getTdTable = getTable[i];
    }
  }
  var getTableTwo = getTdTable.getElementsByTagName('TABLE');
  var getTableTwoTd = getTableTwo[0].getElementsByTagName('TD');

  for (var i = 0; i < getTableTwoTd.length; i++) {
    if (getTableTwoTd[i].width == 50) {
      getTableTwoTd[i].width = 120;
    }
  }
}

function removeCustomerType() { // REMOVES CUSTOMER ON ORDER
  var getTDClient = document.getElementsByTagName('TD');
  for (var i = 0; i < getTDClient.length; i++) {
    if (getTDClient[i].innerHTML == "Kundetype") {
      getTDClient[i].style.display = "none";
      getTDClient[i].nextSibling.style.display = "none";
      getTDClient[i].nextSibling.nextSibling.style.display = "none";
    }
  }
}

function reloadPage() {
}

function removePriceOrderNote() { //This function removes prices from ordresteps
  var searchArr = new Array("0,00", "<b>0,00&nbsp;DKK</b>", "0,00&nbsp;DKK", "Stk. pris", "Totalpris", "0,00&nbsp;DKK", "Stykpris", "Total eksl. moms<br>0,00&nbsp;DKK", "Total eksl. moms<BR>0,00&nbsp;DKK", "<br>0,00&nbsp;DKK", "Total inkl. moms<br>0,00&nbsp;DKK", "Total eksl. moms<BR>0,00&nbsp;DKK", "<BR>0,00&nbsp;DKK", "Total inkl. moms<BR>0,00&nbsp;DKK", ": <br>: ", ": <BR>: ", ":&nbsp;");
  if (document.getElementById('OrderStep3_TD')) {
    var getWrapper = document.getElementById('OrderStep3_TD');
  }
  if (document.getElementById('OrderStep4_TD')) {
    var getWrapper = document.getElementById('OrderStep4_TD');
  }
  var getWrTd = getWrapper.getElementsByTagName('TD');
  // search for TD innerHTML
  for (var i = 0; i < getWrTd.length; i++) {
    for (var x = 0; x < searchArr.length; x++) {
      if (getWrTd[i].innerHTML == searchArr[x]) {
        getWrTd[i].style.display = "none";
      }
    }
  }
  // search for TD b innerHTML
  var bTagged = document.getElementsByTagName('b');
  for (var y = 0; y < bTagged.length; y++) {

    for (var x = 0; x < searchArr.length; x++) {
      if (bTagged[y].innerHTML == searchArr[x]) {
        bTagged[y].style.display = "none";
      }
    }
  }
}

function removeTrDelivery() { // Function for hiding false inputfields in approve for B2B member
  if (window.location.pathname.match(new RegExp('^/shop/addb2b.html'))) {
    if (!document.getElementById('Field10_0'))
      return false;
    var getTr = document.getElementById('Field10_0');
    var getTrTD = getTr.getElementsByTagName('TD');
    var getTrTDField = getTrTD[1].getElementsByTagName('INPUT');
    getTrTDField[0].setAttribute("value", " ");
    getTr.style.display = "none";

    document.getElementById('Field5_0').style.display = "none";
    document.getElementById('Field9_0').style.display = "none";

    // Find td and hide tr parent		
    var getTd = document.getElementById("Field0_0");
    getTd.previousSibling.style.display = "none";
  }
}

function categoryPictureList() {
  var imgs = {}; // Map img tag from url to img object
  // Find all images in menu, swap their a hrefs title into the place of the image
  if (document.getElementById("ProductMenu_Table")) {
    var productMenu = document.getElementById("ProductMenu_Table");
    var img = productMenu.getElementsByTagName("IMG");
    for (var i = 0; i < img.length; i++) {
      var parent = img[i].parentNode; // The a tag
      var title = (parent.tagName == "B" ? parent.parentNode.title : parent.title); // title of a tag, which becomes link text
      var href = (parent.tagName == "B" ? parent.parentNode.href : parent.href)
      if (title) {
        imgs[href] = img[i];
        parent.innerHTML += title;
      }
    }
    // Find product list and move the image into that position
    var a = document.getElementsByTagName("A");
    for (var i = 0; i < a.length; i++) {
      if ((a[i].className == "SubCats_Prodlink")) {
        var img = imgs[a[i].href];
        if (img) {
          var innerHTML = a[i].innerHTML;
          a[i].innerHTML = "";
          a[i].appendChild(img);
          a[i].innerHTML += "<div class=\"catMoveName\">" + innerHTML + "</div>";
        }
      }
    }
  }
}

function createCookie(name, value, days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    var expires = "; expires=" + date.toGMTString();
  }
  else
    var expires = "";
  document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for (var i = 0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) == ' ')
      c = c.substring(1, c.length);
    if (c.indexOf(nameEQ) == 0)
      return c.substring(nameEQ.length, c.length);
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name, "", -1);
}

var basketFunction = {
  init: function () {
    if (document.getElementById('name')) {
      this.insertContact();
    }
  }
  , insertContact: function () {

    var _Name = document.getElementById('name').value;
    var _FindTDs = document.getElementsByTagName('TD');


    for (var i = 0, max = _FindTDs.length; i < max; i++) {
      if (_FindTDs[i].innerHTML == "Kontaktperson") {
        var _TempVal = _FindTDs[i].nextSibling.getElementsByTagName('INPUT')[1];
        if (_TempVal.value == "") {
          _TempVal.value = _Name;
        }
      }
    }
  }
  , acceptTermsBit: function () {
    document.getElementById('acceptTermsBit').checked = true;
  }
}

function prcardDatepick() {
  var inputs = $('.Variants input');
  $.each(inputs, function () {
    if ($(this).attr('value') == 'Leveringsdato') {
      $(this).siblings('.TextInputField_ProductInfo').attr('maxlength', '10');
      $(this).siblings('.TextInputField_ProductInfo').datepicker({
        minDate: 0,
        onSelect: function () {
          SimpleVariantsValidate('0', '0', '', '', '', this)
        }
      });
    }
    if ($(this).attr('value') == 'Kommunekode') {
      $(this).siblings('.TextInputField_ProductInfo').attr('maxlength', '3');
    }
    if ($(this).attr('value') == 'CPR/reference') {
      $(this).siblings('.TextInputField_ProductInfo').attr('maxlength', '10');
    }
  })
}

function watchMouseOverBuy() {
  myform.onsubmit = validateDate;
}

function validateDate() {
  var validated = true;
  var inputs = $('#prCardWrapper .Variants input');
  $.each(inputs, function () {
    if ($(this).attr('value') == 'Leveringsdato') {
      if ($(this).siblings('.TextInputField_ProductInfo').attr('value').search(/\d\d-\d\d-\d\d\d\d/i) == -1) {
        validated = false;
      }
    }
  })
  if (validated == false) {
    alert('Ønsket leveringsdato skal være i formatet dd-mm-yyyy');
    return false;
  }
}

function buyDatepicker() {
    $('#Field10_0 input').datepicker({
        minDate: 0
      });
}
