﻿$(document).ready(function() {

  $('a').focus(function() { $(this).blur(); });

  $('input[title]').each(function() {
    if (!$(this).val()) { $(this).val($(this).attr('title')); }
    $(this)
      .focus(function() { if ($(this).val() == $(this).attr('title')) { $(this).val(''); } })
      .blur(function() { if (!$(this).val()) { $(this).val($(this).attr('title')); } });
  });
  
  $('form.validate').submit(function() {
    $('input').blur();
    var do_submit = true;
    $(this).find('.needed').each(function() {
      if (!$(this).val() || $(this).val() == $(this).attr('title')) { do_submit = false; }
    });
    if (!do_submit) { return false; }
  });

  $('.pagename1').text(pagename1);
  
  if (loggedin) {
    $('.column1.content1 a.fancybox').hide();
    $("a.fancybox[title='Log ind']").closest("li,table").hide();
    $("a.fancybox[title='Bliv bruger']").closest("li,table").hide();
    $(".topnavigation ul").append("<li><a href='/Admin/Public/extranetlogoff.aspx' title='Log ud'>Log ud</a></li>");
  } else {
    $("a.fancybox[title='Log ind']").fancybox({
      'height' : 200,
      'width' : 380,
      'autoScale' : true,
      'transitionIn' : 'none',
      'transitionOut' : 'none',
      'type' : 'iframe',
      'overlayColor' : '#000',
      'titleShow' : false
    });
    $("a.parentfancybox[title='Log ind']").click(function() {
      parent.$.fancybox({
        'href' : $(this).attr("href"),
        'height' : 200,
        'width' : 380,
        'autoScale' : true,
        'transitionIn' : 'none',
        'transitionOut' : 'none',
        'type' : 'iframe',
        'overlayColor' : '#000',
        'titleShow' : false
      });
    });
    $("a.fancybox[title='Bliv bruger']").fancybox({
      'height' : 490,
      'width' : 380,
      'autoScale' : true,
      'transitionIn' : 'none',
      'transitionOut' : 'none',
      'type' : 'iframe',
      'overlayColor' : '#000',
      'titleShow' : false
    });
    $("a.parentfancybox[title='Bliv bruger']").click(function() {
      parent.$.fancybox({
        'href' : $(this).attr("href"),
        'height' : 490,
        'width' : 380,
        'autoScale' : true,
        'transitionIn' : 'none',
        'transitionOut' : 'none',
        'type' : 'iframe',
        'overlayColor' : '#000',
        'titleShow' : false
      });
    });
  }
    
  if($("h1").text() == "Kundeoplysninger" && loggedin === false) {
    $.fancybox({
      'href' : '/Default.aspx?ID=172&fancybox=1',
      'height' : 130,
      'width' : 380,
      'autoScale' : true,
      'transitionIn' : 'none',
      'transitionOut' : 'none',
      'type' : 'iframe',
      'overlayColor' : '#000',
      'titleShow' : false
    });
    $("input, textarea, select").attr("readonly", "readonly");
    $("a.NextStepButtonName").removeClass('NextStepButtonName').css({'color':'#999999'})
      .find("img").attr("src", "/Files/System/graphic/step_arrowrightdeactive.gif");
  }
  
  $('div.legend').each(function() {
    $(this).find('a.Legend').not(':last').after('&#160;&#160;&#62;&#160;');
  });
  
  //EcomCartPaymethodID($('*[name="customertype"]').val());
  
  //$('*[name="EcomCartPaymethodID"]').val($('*[name="EcomCartPaymethodIDClean"]').val());
  
  $('a.NextStepButtonName').click(function() {
    
    var do_submit = true; var do_focus = true;
    
    var obj = $('input[name="EcomOrderCustomerCompany"]');
    if (obj.length != 0 && !obj.val()) { obj.css({'border':'1px solid #9B0031'}); do_submit = false; if (do_focus) { obj.focus(); do_focus = false; } }
    else { obj.css({'border':'1px solid #BBBBBB'}); }
    
    var obj = $('input[name="EcomOrderCustomerName"]');
    if (obj.length != 0 && !obj.val()) { obj.css({'border':'1px solid #9B0031'}); do_submit = false; if (do_focus) { obj.focus(); do_focus = false; } }
    else { obj.css({'border':'1px solid #BBBBBB'}); }
    
    var obj = $('input[name="EcomOrderCustomerPhone"]');
    if (obj.length != 0 && !obj.val()) { obj.css({'border':'1px solid #9B0031'}); do_submit = false; if (do_focus) { obj.focus(); do_focus = false; } }
    else { obj.css({'border':'1px solid #BBBBBB'}); }
    
    var obj = $('input[name="EcomOrderCustomerAccepted"]');
    if (obj.length != 0 && !$(obj).is(':checked')) { alert('Du skal acceptere salgsbetingelserne.'); obj.focus(); do_submit = false; if (do_focus) { obj.focus(); do_focus = false; } }
    
    if (do_submit) {
      $('#PreviousStepButtonName').remove();
      $(this).closest('form').submit();
    } else { $("p.needed").css({'color':'#9B0031','font-weight':'bold'}); }
    
  });
  
  $('a.PreviousStepButtonName').click(function() {
    $('#NextStepButtonName').remove();
    $(this).closest('form').submit();
  });
  
  $('.UserManagementEditForm').click(function() {
    var do_submit = true; var do_focus = true;
    var reg_email = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   
    var obj = $('input[name="UserManagement_Form_Company"]');
    if (obj.length != 0 && !obj.val()) { obj.css({'border':'1px solid #9B0031'}); do_submit = false; if (do_focus) { obj.focus(); do_focus = false; } }
    else { obj.css({'border':'1px solid #BBBBBB'}); }
   
    var obj = $('input[name="UserManagement_Form_Name"]');
    if (obj.length != 0 && !obj.val()) { obj.css({'border':'1px solid #9B0031'}); do_submit = false; if (do_focus) { obj.focus(); do_focus = false; } }
    else { obj.css({'border':'1px solid #BBBBBB'}); }
      
    var obj = $('input[name="UserManagement_Form_Address"]');
    if (obj.length != 0 && !obj.val()) { obj.css({'border':'1px solid #9B0031'}); do_submit = false; if (do_focus) { obj.focus(); do_focus = false; } }
    else { obj.css({'border':'1px solid #BBBBBB'}); }
   
    var obj = $('input[name="UserManagement_Form_Zip"]');
    if (obj.length != 0 && !obj.val()) { obj.css({'border':'1px solid #9B0031'}); do_submit = false; if (do_focus) { obj.focus(); do_focus = false; } }
    else { obj.css({'border':'1px solid #BBBBBB'}); }
      
    var obj = $('input[name="UserManagement_Form_City"]');
    if (obj.length != 0 && !obj.val()) { obj.css({'border':'1px solid #9B0031'}); do_submit = false; if (do_focus) { obj.focus(); do_focus = false; } }
    else { obj.css({'border':'1px solid #BBBBBB'}); }
      
    var obj = $('input[name="UserManagement_Form_Country"]');
    if (obj.length != 0 && !obj.val()) { obj.css({'border':'1px solid #9B0031'}); do_submit = false; if (do_focus) { obj.focus(); do_focus = false; } }
    else { obj.css({'border':'1px solid #BBBBBB'}); }
      
    var obj = $('input[name="UserManagement_Form_Phone"]');
    if (obj.length != 0 && !obj.val()) { obj.css({'border':'1px solid #9B0031'}); do_submit = false; if (do_focus) { obj.focus(); do_focus = false; } }
    else { obj.css({'border':'1px solid #BBBBBB'}); }
      
    var obj = $('input[name="UserManagement_Form_Email"]');
    if (obj.length != 0 && !obj.val() || !reg_email.test(obj.val())) { obj.css({'border':'1px solid #9B0031'}); do_submit = false; if (do_focus) { obj.focus(); do_focus = false; } }
    else { obj.css({'border':'1px solid #BBBBBB'}); }
      
    var obj = $('input[name="UserManagement_Form_UserName"]');
    if (obj.length != 0 && !obj.val()) { obj.css({'border':'1px solid #9B0031'}); do_submit = false; if (do_focus) { obj.focus(); do_focus = false; } }
    else { obj.css({'border':'1px solid #BBBBBB'}); }
      
    var obj1 = $('input[name="UserManagement_Form_NewPassword"]');
    if (obj1.length != 0 && !obj1.val()) { obj1.css({'border':'1px solid #9B0031'}); do_submit = false; if (do_focus) { obj1.focus(); do_focus = false; } }
    else { obj1.css({'border':'1px solid #BBBBBB'}); }
      
    var obj2 = $('input[name="UserManagement_Form_NewPasswordConfirm"]');
    if (obj2.length != 0 && !obj2.val() || obj1.val() != obj2.val()) { obj2.css({'border':'1px solid #9B0031'}); do_submit = false; if (do_focus) { obj2.focus(); do_focus = false; } }
    else { obj2.css({'border':'1px solid #BBBBBB'}); }
      
    var obj = $('input[name="AcceptTerms"]');
    if (obj.length != 0 && !obj.is(':checked')) { obj.css({'border':'1px solid #9B0031'}); do_submit = false; if (do_focus) { alert('Du skal acceptere salgsbetingelserne.'); obj.focus(); do_focus = false; } }
    else { obj.css({'border':'1px solid #BBBBBB'}); }
    
    var obj3 = $('select[name="CustomField.AccessUser_AccessUser_customertype"] option:selected').val();
    if (obj3 == "Privat") { $('input[name="UserManagement_Form_CustomerNumber"]').val('710000@@SHOP1'); }

   
    if (do_submit) { $('form[name="UserManagementEditForm"]').submit(); }
    else { return false; }
  });
  
  $('input[readonly="readonly"]').click(function() { $(this).blur(); });
  
  $('.column2 .frontbox:nth-child(3n)').each(function() {
    var min_height = $(this).height();
    if (min_height < $(this).prev().height()) { min_height = $(this).prev().height(); };
    if (min_height < $(this).prev().prev().height()) { min_height = $(this).prev().prev().height(); };
    $(this).height(min_height).prev().height(min_height).prev().height(min_height);
  });
  
  /* update 4/10/2011 - CHR */
  $('select[name="EcomOrderCustomerCountry"]').change(function() {
    $('input[name="countrycode"]').val($(this).val());
  });
  
  /* update 10/10/2011 - CHR */
  $('div[class="user"]').each(function() {
    var w = ($('.topnavigation ul').width()+(2*10));
    $(this).css({'width':($(this).width()-(w*2))+'px','left':w+'px'});
  });
  
});

function EcomCartPaymethodID(val) {
  if (val=='Virksomhed') { /*$('*[name="EcomCartPaymethodID"] option[value="PAY1"]').attr("selected","selected");*/ }
  else if (val=='Privat') { $('*[name="EcomCartPaymethodID"] option[value="PAY1"]').remove(); }
}

function URLparse(name) {
  var regex = new RegExp("[\\?&]"+name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]")+"=([^&#]*)");
  var results = regex.exec(decodeURIComponent(window.location.href));
  if (results == null) {return ""; }
  else { return results[1]; }
}
