function checkEmail(str){
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str)) return true;
else return false;
}

$(function() {
		$("input:submit, a", ".demo").button();
		
		$("a", ".demo").click(function() { return false; });
	});

$(document).ready(function () {
		$("#forlag").show('blind', options = {},500);
		$("#tidsskrift").show('blind', options = {},500);
	//$("#nyhetsbrev :input:visible:enabled:first").focus();
	
        // Username validation logic
        var validateUsername = $('#validateUsername');
        this.lastValue = "";
       
        $('#epost').keyup(function () {
             $('#knapp').remove();
             this.lastValue = "";
            var t = this; 
            if(checkEmail(this.value)){
            	     $('#knapp').remove();
            if (this.value != this.lastValue) {
            	validateUsername.html('');
  
                if (this.timer) clearTimeout(this.timer);
                validateUsername.removeClass('error').html('<img src="images/ajax-loader.gif" height="16" width="16" />');
                this.timer = setTimeout(function () {
           		$.ajax({
           				url: 'inc/check_email.php',
           				data: 'action=check_username&username=' + t.value,
           				dataType: 'json',
           				type: 'post',	
           				success: function (j) {
           					if(j.msg=='1'){
           						$('#knapp').remove();
           						$('#validateUsername').before('<input type="submit" value="Slett" id="knapp" name="submit" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only">');
           						validateUsername.html('');
           					} else{
           						$('#validateUsername').before('<input type="submit" value="Lagre" id="knapp" name="submit" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only">');
           						validateUsername.html('');
           					}
           				}
           		});
           	}, 200);	
           	this.lastValue = this.value;
            }
            }
        });
        		$("#dialog-avbryt-nyhetsbrev").dialog({position: ['center','center']}); 
		$( "#dialog-avbryt-nyhetsbrev" ).dialog({ buttons: { "Nei": function(){$(this).dialog("close");$("#svart").css("display","none");window.location = '?visning=hjem';}, "Ja": function() { $(this).dialog("close");
		var t = $('#epost');
		$.ajax({
           				url: 'inc/check_email.php',
           				data: 'action=remove_username&username=' + t.value,
           				dataType: 'json',
           				type: 'post',	
           				success: function(){$("#svart").css("display","none");window.location = '?visning=hjem';}
           	}); }} });
		$("#dialog-registrert-ok").dialog({position: ['center','center']}); 
		$( "#dialog-registrert-ok" ).dialog({ buttons: { "Ok": function(){$(this).dialog("close");$("#svart").css("display","none"); window.location = '?visning=hjem';}} });
});

$(document).ready(function () {
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
		//$("#dialog").dialog("destroy");
	
		$("#nybruker").dialog({
			resizable: false,
			height:600,
			show: 'fold',
			hide: 'fold',
			modal: true,
			buttons: {
				'Send': function() {
					$(this).dialog('close');
					
				},
				'Avbryt': function() {
					$(this).dialog('close');
					window.location = '?visning=hjem';
				}
			}
		});
	});

