
function onInvalidForm() {


  if($('#barn').hasClass('invalid'))
    $('#barn-button').addClass('invalid');
  else
    $('#barn-button').removeClass('invalid');

  if($('#land').hasClass('invalid'))
    $('#land-button').addClass('invalid');
  else
    $('#land-button').removeClass('invalid');
  
  if($('#payment_method').hasClass('invalid'))
    $('#payment_method-button').addClass('invalid');
  else
    $('#payment_method-button').removeClass('invalid');
  
  if($('#payment_date').hasClass('invalid'))
    $('#payment_date-button').addClass('invalid');
  else
    $('#payment_date-button').removeClass('invalid');
  
  if($('#payment_recurring').hasClass('invalid'))
    $('#payment_recurring-button').addClass('invalid'); 
  else
    $('#payment_recurring-button').removeClass('invalid'); 

  $('#popup-inner-error').html("<h3>Ups, her manglet det visst noe...</h3><p>Se over felt merket i rødt for å fylle inn på nytt.</p><p>Hvis du har problemer, eller opplever noen feil, vennligst ta kontakt med oss på mail: info@misjonsalliansen.no eller på telefon: 22 94 26 01</p>");
  $('#popup-outer-error').fadeIn(10);

}

function onValidForm() {

    $('#barn-button').addClass('valid');
    $('#land-button').addClass('valid');
    $('#payment_method-button').addClass('valid');
    $('#payment_date-button').addClass('valid');
    $('#payment_recurring-button').addClass('valid');

    $.ajax({
      type: 'POST',
      url: '/api/fadder',
      data: $('#sign').serialize(),
      success: showSuccessPopup,
      error: function (xhr, options, error) {
        var text = getHeaderJSON(xhr);
        alert('Feil i kontrolltekstfeltet. Skriv inn teksten i bildet i feltet merket "Kontrolltekst"');
        $("#captcha-image").attr("src", "http://blifadder.no/securimage/securimage_show.php?"+Math.random());
        // location.reload(true);
        // $("#captcha-image").attr("src", "http://blifadder.no/securimage/securimage_show.php?"+Math.random());
      }
    });

}

function getHeaderJSON(xhr) 
{
    var data;

    try { data = xhr.getResponseHeader('X-Json') }
    catch(e) {}

    if (data) return data;
}

function showSuccessPopup() {

    $('span#receipt-name').text($('input[name="navn"]').val());
    $('span#receipt-address').text($('input[name="adresse"]').val());
    $('span#receipt-postal-place').text($('input[name="postnummer"]').val() + ' ' + $('input[name="sted"]').val());
    $('span#receipt-telephone').text($('input[name="telefon"]').val());
    $('span#receipt-email').text($('input[name="e_post"]').val());
    $('span#receipt-birth').text($('input[name="dato"]').val());
    $('span#receipt-person-number').text($('input[name="personnummer"]').val());

    $('span#receipt-children').text("Antall barn: " + $('select[name="barn"] option:selected').val());
    $('span#receipt-country').text("Land: " + $('select[name="land"] option:selected').val());
    $('span#receipt-payment-method').text("Betalingsmåte: " + $('select[name="payment_method"] option:selected').val());
    $('span#receipt-payment-date').text("Betalingsdato: " + $('select[name="payment_date"] option:selected').val());
    $('span#receipt-payment-recurring').text("Betalingshyppighet:" + $('select[name="payment_recurring"] option:selected').val());

    $('#popup-outer-receipt').fadeIn(10);
}

$(document).ready(function() {

  $('#sign select').selectmenu();

  $('a#why').click(function() {
    
    $('.menuitem').removeClass('menuitem-active');
    $(this).addClass('menuitem-active');

    $('.info:not(#info-why)').fadeOut('slow', function() {
      $('#info-why').fadeIn('slow');
    });
	
	/*
    setTimeout(function() {
    $('#background').fadeOut('fast', function() {
      // $('#background').css('background', 'url("/images/img_02.jpg") top center fixed no-repeat');
	  $('#background').css('background', 'url("/images/img_split.jpg") top center fixed no-repeat');
      $('#background').fadeIn('fast');
    })},
    500);
	*/

  });
  
  $('a#about').click(function() {

    $('.menuitem').removeClass('menuitem-active');
    $(this).addClass('menuitem-active');

    $('.info:not(#info-about)').fadeOut('slow', function() {
      $('#info-about').fadeIn('slow');
    }); 
    
	/*
    setTimeout(function() {
    $('#background').fadeOut('fast', function() {
      // $('#background').css('background', 'url("/images/img_03.jpg") top center fixed no-repeat');
	  $('#background').css('background', 'url("/images/img_split.jpg") top center fixed no-repeat');
      $('#background').fadeIn('fast');
    })},
    500);
	*/

  });
  
  $('a#what').click(function() {

    $('.menuitem').removeClass('menuitem-active');
    $(this).addClass('menuitem-active');

    $('.info:not(#info-what)').fadeOut('slow', function() {
      $('#info-what').fadeIn('slow');
    });
	
	/*
    setTimeout(function() {
    $('#background').fadeOut('fast', function() {
      // $('#background').css('background', 'url("/images/img_01.jpg") top center fixed no-repeat');
	  $('#background').css('background', 'url("/images/img_split.jpg") top center fixed no-repeat');
      $('#background').fadeIn('fast');
    })},

    500);
	*/
  });

  $('#logo, #front').click(function() {

    $('.menuitem').removeClass('menuitem-active');
    $(this).addClass('menuitem-active');

    $('.info:not(#info-front)').fadeOut('slow', function() {
      $('#info-front').fadeIn('slow');
    });

    /*
	setTimeout(function() {
      $('#background').fadeOut('fast', function() {
      // $('#background').css('background', 'url("/images/img_04.jpg") top center fixed no-repeat');
	  $('#background').css('background', 'url("/images/img_split.jpg") top center fixed no-repeat');
      $('#background').fadeIn('fast'); 
    })}, 

    500);
	*/
  });

  var clearMePrevious = [];

  // clear input on focus
  $('.clearMeFocus').focus(function()
  {
    if($(this).val()==$(this).attr('title'))
    {
      clearMePrevious[$(this).attr('title')] = $(this).val();
      $(this).val('');
    }
  });

  // if field is empty afterward, add text again
  $('.clearMeFocus').blur(function()
  {

    if($(this).val()=='')
    {
      $(this).val(clearMePrevious[$(this).attr('title')]);
    }
  });

  $('input[type="text"]').click(function() {

    $(this).removeClass('invalid');

  });

  var validator = new Validator($('#sign'),
  { 

    navn: {},
    // adresse: {},
    // postnummer: { rules: ["regex"], params: ["\\d{4}"] },
    // sted: {},
    // telefon: { rules: ["regex"], params: ["\\d+"] },
    // e_post: { rules: ["email"] },
    // dato: { rules: ["regex"], params: ["\\d{2}\.\\d{2}\.\\d{4}"] },
    // personnummer: { rules: ["regex"], params: ["\\d{11}"] },
    // barn: {},
    // land: {},
    // payment_method: {},
    // payment_date: {},
    // payment_recurring: {}
  }, onInvalidForm, onValidForm,  true);
  
  $('#sign-button').click(function() {

    $('#sign').submit();

  });

  $('#popup-outer-receipt, #popup-inner-receipt').click(function() {

    $('#popup-outer-receipt').fadeOut(10);

  });
  
  $('#popup-outer-error, #popup-inner-error').click(function() {

    $('#popup-outer-error').fadeOut(10);

  });

});



