// wait for the DOM to be loaded
$(document).ready(function() { 
    // bind 'myForm' and provide a simple callback function 
    $('#subscribForm').ajaxForm(function() { 
        
        var value = $('input:text[name=techstory]').val();
        var langval = $('input:hidden[name=lang]').val();
        var botval = $('input:text[name=email]').val();
        
        if(!validateEmail(value)) {
		$("#techstory").css("margin-top","2px");
		
          $("#invalid").fadeOut("fast");
          $("#invalid").fadeIn("fast");
          return;
        }
        else
          $("#invalid").fadeOut("fast");
        //http://"+window.location.host+"
        $.post("/cgi-twentyfeet/subscribe.pl", 
                  { techstory: value, lang: langval, bot: botval},
          function(data) {
            switch(data) {
              case '1': 
					var img = new Image();
					  $(img)
					    .load(function () {	
					    	$(this).hide();
							$('#imagebild').empty();
							$('#imagebild').css("height","245px");
					     	$('#imagebild').append(this);
							$('#imagebild').append("<div><h1>Your e-mail has been registered.</h1><p>Please check your e-mail account and click the attached link to confirm your registration.</p></div>")
					     	$(this).fadeIn("fast");
					    })
					    .error(function () {
					    })
					    .attr('src', 'images/bg_meldung_erfolg.png');    	
              break;
              
              default: 
              
                $("#techstory").fadeOut("fast", function(){
                  $("#status_f").fadeIn("fast");
                });
              break;
            }
          });
    });
	$("#submitSubscribForm").click(function(){
		$("#subscribForm").trigger('submit');
		return false;
	});
});

function validateEmail(email) {

  var email_regex = /^[a-zA-Z0-9\-\.\_]+\@[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,6}|[0-9]{1,3})$/;
  
  // Schreibweise der E-Mail-Adresse abfragen
  if(email.search(email_regex) == -1) {
  	return 0;
  } else {
  	return 1;
  }

}
