function isValidEmailAddress(emailAddress) {
    var emailRe = '([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})';
    var pattern = new RegExp(emailRe,["i"]);
    return pattern.test(emailAddress);
}

function showSuccessMsg() {

    $('#newsletter').parent().append('<div class="messagepop pop">E-post registrert.<span id="close" >X</span></div>');
            
}

function showSuccessMsg2() {

    $('#newslettersub').parent().append('<div class="messagepop2 pop">E-post registrert!</div>');
            
}

$.fn.slideFadeToggle = function(easing, callback) {
    return this.animate({ opacity: 'toggle', height: 'toggle' }, "fast", easing, callback);
};

$(document).ready(function() {

  $("#close").live('click', function() {
      $(".pop").slideFadeToggle();
      return false;
  });
  
  $("#emailInput").focus(function(){
      // only select if the text has not changed
      if(this.value == this.defaultValue){
       this.select();
      }
    }
  )
  
  $("#emailInput2").focus(function(){
      // only select if the text has not changed
      if(this.value == this.defaultValue){
       this.select();
      }
    }
  )
  
  $("#subscribeBtn").click(function(){
    
    var email = $("#emailInput").val();

    if(email != 0)
    {
      if(isValidEmailAddress(email))
      {
      
        $("#emailInput").css("border", "");

        $.ajax({
          type: 'POST',
          data: $('#subscribeform').serialize(),
          url: $('#subscribeform').attr('action'),
          success: showSuccessMsg() //alert('yes')
      })

      }
      else
      {
        $("#emailInput").css("border", "2px solid red");
      }

    }
    else
    {
      $("#emailInput").css("border", "2px solid red");
    }
  });
  
  $("#subscribeBtn2").click(function(){
    
    var email = $("#emailInput2").val();

    if(email != 0)
    {
      if(isValidEmailAddress(email))
      {
      
        $("#emailInput2").css("border", "");

        $.ajax({
          type: 'POST',
          data: $('#subscribeform2').serialize(),
          url: $('#subscribeform2').attr('action'),
          success: showSuccessMsg2() //alert('yes')
      })

      }
      else
      {
        $("#emailInput2").css("border", "2px solid red");
      }

    }
    else
    {
      $("#emailInput2").css("border", "2px solid red");
    }
  });
});


