var Signup = {
	
	init: function()
	{
		$('input#subdomain').keyup(Signup.updateAccountName);
		
		if ($('input#subdomain').val() != '')
		{
			$('.lessImportant .subdomainPreview').text(Signup.convertForUrl($('input#subdomain').val()));
		}
	},
	
	updateAccountName: function()
	{
		var newAccountName = Signup.convertForUrl($(this).val());
		
		$('.lessImportant .subdomainPreview').text(newAccountName);
	},
	
	convertForUrl: function(string)
	{
		// Trim
		string = string.replace(/^\s*/, '').replace(/\s*$/, '');
		
		string = string.toLowerCase();
		
		// These need to be done first
		string = string.replace(' - ', '');
		
		// Special case (needs to be done before removal of non-alpha characters
		string = string.replace(/(&#[0-9]+;)+/g, '');
		
		// Remove all non alpha-numeric character
		string = string.replace(/[^a-zA-Z0-9\s]/g, '');
		
		// Replaces "and" and spaces
		string = string.replace(' and ', '-');
		string = string.replace(/\s/g, '-');
					
		// Special case (needs to be at the end)
		// Removes double dashes e.g. H & M -> h--m which should be h-m
		string = string.replace(/\-\-+/g, '-');
		
		return string;
	}
}

$(document).ready(Signup.init);