function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}

$(document).ready(function()
{
	$("#mail").blur(function()
	{
		//remove all the class add the messagebox classes and start fading
		$("#mailbox").removeClass().addClass('messagebox').text('Loading...').fadeIn(1000);
		//check the username exists or not from ajax
		$.post("avaible_mails.php",{ email:$(this).val() } ,function(data)
        {
		  if(data=='no') //if username not avaiable
		  {
		  	$("#mailbox").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('E-posten finns redan.').addClass('messageboxerror').fadeTo(900,1);
			});		
          }
		  else if(data=='yes') //if username avaiable
		  {
			$("#mailbox").fadeTo(200,0.1,function() //start fading the messagebox
			{
			//add message and change the class of the box and start fading
			$(this).html('Ok!').addClass('messageboxok').fadeTo(900,1);
			});
		  }
		  else if(isValidEmailAddress(data))
		  {
		  	$("#mailbox").fadeTo(200,0.1,function()  //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Ej korrekt e-post').addClass('messageboxerror').fadeTo(900,1);	
			});
		  }
		  else
		  {
		  	$("#mailbox").fadeTo(200,0.1,function()  //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Kan ej l\xe4mnas tomt.').addClass('messageboxerror').fadeTo(900,1);	
			});
		  }
		  
				
        });
 
	});
});