function checkAcceptState() {
  if (document.getElementById('conditions').checked) {
    document.getElementById('toPaymentButton').disabled=false;
  } else {
    document.getElementById('toPaymentButton').disabled=true;
  }
}

function Lightbox(formURL, strWidth, strHeight) {
var frameWidth = strWidth * 1 + 30;
var frameHeight = strHeight * 1 + 30;
$.modal('<iframe src="' + formURL + '" height="' + frameHeight + '" width="' + frameWidth + '" style="border:0" frameborder="0">', {
opacity:80,
overlayCss: {backgroundColor:"#000"},
containerCss:{
backgroundColor:"#fff",
borderColor:"#999",
height:frameHeight,
padding:0,
width:frameWidth 
},
overlayClose:true
});
} 


function updateVoucher() {
  
  // Did the user type anything other than spaces?
  if (jQuery.trim($('#voucherCodeId').val()) != '')
    {
      
      // put voucher codes together
    var x = jQuery.trim($('#voucherCodeId').val());
    x = x + ',';
    x = x + jQuery.trim($('#voucherCodesHiddenId').val());
      
      // remove double commas
    while (x.match(',,') == ',,'){
      var x = x.replace(',,', ',');
    }
      
    // remove last comma if at the end of string
    if (x.length == x.lastIndexOf(',')+1) {
      x = x.substring(0,x.length - 1);
    }
    // remove first comma if at the beginning of the string
    if (x.indexOf(',') == 0){
      x = x.substring(1, x.length);
    }
    
    // Set value of hidden input to contain all voucher codes
    $('#voucherCodesHiddenId').val(x);

    // Submit form      
    // Double submit to catch glitches
    $('#voucherForm').submit();
    $('#voucherFormSubmitId').trigger('click');
} else {
    alert('Indtast en kupon kode');
    }
}

function submitAddressForm(obj) {
  
  // Validate inputs in customer address form
  var gogogadgetform = true;

  if($('#firstName').val() == '') {
    gogogadgetform = false;
  }
  if($('#lastName').val() == '') {
    gogogadgetform = false;
  }
  if($('#line1').val() == '') {
    gogogadgetform = false;
  }
  if($('#postalCode').val() == '') {
    gogogadgetform = false;
  }
  if($('#city').val() == '') {
    gogogadgetform = false;
  }
  if($('#email').val() == '') {
    gogogadgetform = false;
  } else {
    var strTest = $('#email').val();
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if(reg.test(strTest))
      {} else {
        gogogadgetform = false;
      }
  }
  if (gogogadgetform){
    obj.submit();
  }  else {
    alert('Et eller flere felter er ikke udfyldt korrekt');
  }
}

$(document).ready(function() {
  // Submit search in mastertemplate if enter is pressed while input is in focus
  $('#idSearch').keyup(function(e) {
    if(e.keyCode == 13) {
      submitSearch();
    }
  });
  // Disable enter key in voucher input
  $('#voucherCodeId').bind("keypress", function(e) {
    if (e.keyCode == 13) return false;
  });
  
});


function submitSearch() {
  var url = '/soeg.aspx?search=' + $('#idSearch').val();
  window.location=url;
}

function submitSearchInternal() {
  var url = '/soeg.aspx?search=' + $('#idSearchword').val();
  window.location=url;
}

function updateInput(obj, txtStr) {
  if(obj.value == txtStr)
  {
    obj.value='';
  } else {
    if(obj.value == ''){
      obj.value=txtStr;
    }
  }
}

function replaceSearchLinks() {
      
      // replace link hrefs with /shop/[$catalog]/-/[$SKU].aspx
      $('#mainContentDiv').each(function(index) {   
        $(this).find('a').each(function(){
          var hrefurl = $(this).attr('href');
          var firstSplit = hrefurl.split('/shop/product.aspx?product=');
          var secondSplit = firstSplit[1].split('\&catalog=');
          var catalog = secondSplit[1];
          var sku = secondSplit[0];
          var newhref = '/shop/' + catalog + '/search/' + sku + '.aspx';
        $(this).attr('href', newhref);
        });
      });
}

function setActiveNav(activeId) {
  // set active nav1
  var selector = '#parentId'+activeId;
  $(selector).addClass('nav2Active');
}

  function insertCategoriesInNavigation(strHtml){
  // move content of #navlist to .nav2Active and add ULs 
  
    $('.nav2Active').each(function() {   
      var x=$(this).html();
      $(this).html(x + '<ul>' + strHtml + '</ul>');
    });
    //$('#navlist').remove();
}

$('document').ready(function(){
  $('ul#mostSold li').each(function(index){
    
  });
});

$('document').ready(function(){
  $('ul#mostSold>li').tsort();
});

  // SubmitOrderForm check for acceptance of conditions before submit
  
function submitOrderForm(){
  if ($('#conditions:checked').val()) {
    $('#submitForm').submit();
  } else {
    alert('Du skal acceptere leveringsbetingelserne før du kan komme videre.');
  };
};


