// http://bf2.buzzfeed.com/bf2/_fixtures?action=setup&test=signup&path=/bf2/signup&fixture=test
BF_DID_REGISTER = false;

BF_Register = function() {
	this.highlight_field = new Array();
	this.ajax = new BF_Request();

	this.ERR_MISSING_PARAMS = 'Please complete all fields.';
	this.ERR_BAD_USERNAME = 'Your username may only contain letters and numbers.';
	this.ERR_USERNAME_LENGTH = 'Your username must be between 3 and 30 characters in length.';
	this.ERR_PW_LENGTH = 'Your password must be at least 6 characters in length.';
	this.ERR_MUST_AGGEE = 'You must agree to the Terms of Service in order to register.';
	this.ERR_PW_MUST_MATCH = 'Your passwords do not match.';
	this.ERR_DUP_USERNAME = 'Sorry, that username  is not available. Please try another.';
	this.ERR_DUP_EMAIL = 'There is already an account with that email address.';
	this.BANNED_FB_UID = 'You cannot register with that Facebook user.';
	this.ERR_BAD_EMAIL = 'You have entered an invalid email address.';
	this.ERR = 'Sorry, we are unable to process your request at this time.';
	this.ERR_ALREADY_REGISTERED = 'You have already registered for an account';
	this.highlight_field[ this.ERR_BAD_USERNAME ] = 'register-username';
	this.highlight_field[ this.ERR_USERNAME_LENGTH ] = 'register-username';
	this.highlight_field[ this.ERR_PW_LENGTH ] = 'register-pw1';
	this.highlight_field[ this.ERR_PW_MUST_MATCH ] = 'register-pw2';
	this.highlight_field[ this.ERR_MUST_AGGEE ] = 'register-agree';
	this.highlight_field[ this.ERR_DUP_USERNAME ] = 'register-username';
	this.highlight_field[ this.ERR_DUP_EMAIL ] = 'bfe';
	this.highlight_field[ this.ERR_BAD_EMAIL ] = 'bfe';
		
	this.SUCCESS1 = "Thanks for registering. We&rsquo;ve created your account and saved your post, but before we post it we need you to validate your email address. We&rsquo;ve sent you a confirmation email with instructions. If you don&rsquo;t receive the mail in a few minutes, please check your spam folder.<br /><br />Your post will appear automatically once you validate.";
	this.SUCCESS1_VALIDATED = "Thanks for registering. We&rsquo;ve validated your email address with Facebook, created your account and saved your post. ";
	this.SUCCESS2 = "Thanks for registering. We&rsquo;ve created your account, but before you can vote we need you to validate your email address. We&rsquo;ve sent you a confirmation email with instructions. If you don&rsquo;t receive the mail in a few minutes, please check your spam folder.";
	this.SUCCESS2_VALIDATED = "Thanks for registering. We&rsquo;ve validated your email address with Facebook and created your account. You now can vote.";
	this.SUCCESS3 = "<h3>Please Validate Your Email</h3><p>Thanks for registering. We&rsquo;ve created your account, but before you can log in we need you to validate your email address. We&rsquo;ve sent you a confirmation email with instructions. If you don&rsquo;t receive the mail in a few minutes, please check your spam folder.</p>";
	this.SUCCESS3_VALIDATED = "<h3>Your Email Has Been Validated</h3><p>Thanks for registering. We&rsquo;ve created your account and validated your email address with Facebook. Some of your profile data has been prepopulated with information from your Facebook profile. You can log in now.</p>";
	this.SUCCESS4 = "Thanks for registering. We&rsquo;ve created your account, but before you can post we need you to validate your email address. We&rsquo;ve sent you a confirmation email with instructions. If you don&rsquo;t receive the mail in a few minutes, please check your spam folder.<br/><br/>Once you validate <a id='register_this_page_url' href=''>click here</a> to make a post.";
	this.SUCCESS4_VALIDATED = "Thanks for registering. We&rsquo;ve created your account and validated your email address with Facebook.";
	
	this.fb_link_handler = function() {
		var handler = function( event ) {
			event.stop();
			$('facebook-login').hide();
			$('facebook-registration-form').hide();
			bf_login.switch_to_link_accounts();
		}.bind( this );
		if ($('fb-link-submit')) $('fb-link-submit').observe('click', handler);
		if ($('fb-link-submit2')) $('fb-link-submit2').observe('click', handler);
	}

	this.fb_register_handler = function(reset_form, args) {
		if (BF_Register.registered_fb_handler) return;
		BF_Register.registered_fb_handler = true;
		this.fb_link_handler();
		if ( typeof reset_form == 'undefined' ) reset_form = true;
		if ( args ) this.args = args;
		var handler = function(event) {
			event.stop();
			this.facebook_register();			
		}.bind(this);
		if ($('facebook-registration-form')) $('facebook-registration-form').stopObserving('submit', handler);
		if ($('facebook-registration-form')) $('facebook-registration-form').observe('submit', handler);
		if ($('fb-register-submig')) $('fb-register-submit').disabled = false;
		if ( reset_form && $('facebook-registration-form') ) $('facebook-registration-form').reset();
	}
	
	this.alias_register_handler = function(reset_form,args) {
		if (BF_Register.alias_registered_handler) return;
		BF_Register.alias_registered_handler = true;
		if ( typeof reset_form == 'undefined' ) reset_form = true;
		if ( args ) this.args = args;
		var handler = function(event) {
			event.stop();
			this.alias_register();			
		}.bind(this);
		BF_UserSwitcher.count_aliases( function(obj){
			if ( $('alias-sites-left') && obj.success ) {
				var user_info = new BF_User().getUserInfo();
				var remaining = parseInt(user_info.max_aliases) - parseInt(obj.count);
				$('alias-sites-left').update( remaining ); 
				if ( remaining == 1 ) $('alias-sites-left-plural').hide();
				if ( obj.unlimited == 1) $('alias-count-message').hide();
				else $('alias-count-message').show();
				if ( remaining <= 0 && user_info.max_aliases != 0 ) {
					$('alias-registration-fieldset').hide();
					$('alias-registration-hit-limit').show();
				}
			}
		});
		
		if ($('alias-registration-form')) $('alias-registration-form').stopObserving('submit', handler);
		if ($('alias-registration-form')) $('alias-registration-form').observe('submit', handler);
		if ($('alias-registration-form')) $('alias-register-submit').disabled = false;
		if ( reset_form && $('alias-registration-form') ) $('alias-registration-form').reset();
	}
	
	this.register_handler = function(reset_form,args) {
		if (BF_Register.registered_handler) return;
		BF_Register.registered_handler = true;
		if ( typeof reset_form == 'undefined' ) reset_form = true;
		if ( args ) this.args = args;
		var handler = function(event) {
			event.stop();
			this.register();			
		}.bind(this);
		
		if ($('regsitration-form')) $('regsitration-form').stopObserving('submit', handler);
		if ($('regsitration-form')) $('regsitration-form').observe('submit', handler);
		if ($('register-submit')) $('register-submit').disbaled = false;
		if ( reset_form && $('regsitration-form') ) $('regsitration-form').reset();
	}
	
	this.facebook_register = function() {
		var name = $F('fb-register-name').strip();
		var email = 'get_from_facebook@buzzfeed.com';
		try {
			email = facebook.user_data.email;
		} catch(e){ }
		var username = $F('fb-register-username').strip();
		var pw1 = $F('fb-register-pw1').strip();
		var agreed = true;
		var emailme = 0;
		var import_fb_profile = $('import_fb_profile_checkbox').checked ? 'true' : 'false';
		if ( import_fb_profile == 'false' ) name = username;
		var facebook_uid = $('fb-facebook-uid') ? $('fb-facebook-uid').value : '';
		this.username = username;
		this.password = pw1;
		if( $('fb-email-me').checked ) {
			emailme = 1;
		}
		var rr = function(resp) {
			this.response(resp);
		}.bindAsEventListener(this);
		
		var er = function(resp) {
			this.err(resp);
		}.bindAsEventListener(this);

		var params = { name: name, username: username, pw: pw1, email: email, contribute: 1, allow_email_updates: emailme, facebook_uid: facebook_uid, import_fb_profile : import_fb_profile, registered_from_url:window.location.href, reg_user_agent:navigator.userAgent } ;
		new Ajax.Request('/buzzfeed/_contribute_register', {method: 'post', parameters: params, onSuccess: rr, onFailure: er});
	}
	
	this.alias_register = function() {
		var username = $('alias-register-username').value.strip();
		if (!username.match(/^[a-zA-Z0-9]+$/)) {
			this._alias_register({success:0,message:'Site URL must consist of only alphanumeric characters'});
			return;
		}
		var display_name = $('alias-register-display_name').value;
		if ($('alias-registering-status')) $('alias-registering-status').show();
		if ( !username || !display_name ) {
			this._alias_register({success:0,message:'missing username'});
		}
		else {
			BF_UserSwitcher.create_alias( { username : username, display_name : display_name}, this._alias_register );
		}
	}
	
	this._alias_register = function( obj ) {
		if ( obj.success == 0 || obj.status != 200 ) {
			$('alias-message').show();
			if ( obj.message.match(/missing username/i ) ) {
				$('alias-message').update('please fill out all fields');
			}
			else if ( obj.message.match( /logged in/i ) ) {
				$('alias-message').update('please log in and try again');
			}
			else if ( obj.message.match( /privileges/i ) ) {
				$('alias-message').update('you don\'t have permission to create an alias');
			}
			else if ( obj.message.match( /limit/i ) ) {
				$('alias-message').update('Sorry, but you are over your alias limit');
			}
			else if ( obj.message.match( /username/i ) ) {
				$('alias-message').update('this site URL already is taken, please select another');
			}
			else {
				$('alias-message').update( obj.message );
			}
		}
		else {
			if ($('alias-registration-fieldset')) $('alias-registration-fieldset').hide();
			if ($('alias-registration-creating-site')) $('alias-registration-creating-site').show();
			//BF_UserSwitcher.switchTo(obj.userid, function(){window.location.href = BF_STATIC.web_root+'/'+$('alias-register-username').value});
			BF_UserSwitcher.switchTo(obj.userid, function(){});
		}
		if ($('alias-registering-status')) $('alias-registering-status').hide();
	}
	
	this.register = function() {
		$('registering-status').show();
		if( $('register-submit') ) $('register-submit').disabled = true;
		if( typeof this.args =='undefined' ) {
			this.args={action:'vote'};
		}
		var name = $F('register-name').strip();
		var email = $F('bfe').strip();
		var username = $F('register-username').strip();
		var pw1 = $F('register-pw1').strip();
		var pw2 = $F('register-pw2').strip();
		var agreed = false;
		var emailme = '';
		var facebook_location = '';
		var facebook_uid = '';
		var facebook_url = '';
		var facebook_session_key = '';
		var import_fb_profile = false;
		if( BF_STATIC.facebook_enabled ) {
			facebook_uid = $('facebook-uid') ? $('facebook-uid').value : '';
			facebook_session_key = $('facebook-session_key') ? $('facebook-session_key').value : '';
			import_fb_profile = $('import_fb_profile_checkbox').checked;
		}

		if($('register-agree').checked) {
			agreed = true;
		}
		if($('email-me').checked) {
			emailme = 1;
		}
		
		if(! name || ! email || ! username || ! pw1 || ! pw2) {
			if( $('register-submit') ) $('register-submit').disabled = false;
			this.update_registration_message( this.ERR_MISSING_PARAMS);
			$('facebook-registration-message').addClassName('error');
			$('facebook-registration-message').show();
			$('registration-message').addClassName('error');
			$('registration-message').show();
			$('registering-status').hide();
		} else if (! username.match(/^[a-zA-Z0-9]+$/)) {
			if( $('register-submit') ) $('register-submit').disabled = false;
			this.update_registration_message( this.ERR_BAD_USERNAME);
			$('facebook-registration-message').addClassName('error');
			$('facebook-registration-message').show();
			$('registration-message').addClassName('error');
			$('registration-message').show();
			$('registering-status').hide();

		} else if ( username.length < 3 || username.length > 30 ) {
			if( $('register-submit') ) $('register-submit').disabled = false;
			this.update_registration_message( this.ERR_USERNAME_LENGTH);
			$('facebook-registration-message').addClassName('error');
			$('facebook-registration-message').show();
			$('registration-message').addClassName('error');
			$('registration-message').show();

			$('registering-status').hide();
		} else if(pw1 != pw2) {
			this.update_registration_message( this.ERR_PW_MUST_MATCH);
			if( $('register-submit') ) $('register-submit').disabled = false;
			$('facebook-registration-message').addClassName('error');
			$('facebook-registration-message').show();
			$('registration-message').addClassName('error');
			$('registration-message').show();

			$('registering-status').hide();
		} else if(! agreed) {
			this.update_registration_message( this.ERR_MUST_AGGEE);
			if( $('register-submit') ) $('register-submit').disabled = false;
			$('facebook-registration-message').addClassName('error');
			$('facebook-registration-message').show();
			$('registration-message').addClassName('error');			
			$('registration-message').show();

			$('registering-status').hide();
		} 
		else if(typeof pw1 == 'undefined' || pw1.length < 6) {
			this.update_registration_message( this.ERR_PW_LENGTH );
			if( $('register-submit') ) $('register-submit').disabled = false;
			$('facebook-registration-message').addClassName('error');
			$('facebook-registration-message').show();
			$('registration-message').addClassName('error');
			$('registration-message').show();
			$('registering-status').hide();
		}
		else if(typeof email =='undefined' || !email.match(/^([^@]+)@([^\.]+)\.([a-zA-Z])+/)) {
			if( $('register-submit') ) $('register-submit').disabled = false;
			this.update_registration_message( this.ERR_BAD_EMAIL );
			$('facebook-registration-message').addClassName('error');
			$('facebook-registration-message').show();
			$('registration-message').addClassName('error');
			$('registration-message').show();
			$('registering-status').hide();
		}
		else {
			
			this.username = username;
			this.password = pw1;
			var rr = function(resp) {
				this.response(resp);
			}.bindAsEventListener(this);
			
			var er = function(resp) {
				this.err(resp);
			}.bindAsEventListener(this);
			
			$('register-submit').disbaled = true;
			var params = { name: name, username: username, pw: pw1, email: email, contribute: 1, allow_email_updates: emailme, facebook_uid: facebook_uid, location: facebook_location, link: facebook_url, facebook_session_key: facebook_session_key, import_fb_profile : import_fb_profile, registered_from_url:window.location.href, reg_user_agent : navigator.userAgent } ;
			new Ajax.Request('/buzzfeed/_contribute_register', {method: 'post', parameters: params, onSuccess: rr, onFailure: er});
		}
	}
	
	this.update_registration_message = function( msg ) {
		if(Prototype.Browser.IE) {
			$('registration-message').innerText=msg;
			$('facebook-registration-message').innerText=msg;
		} else {
			$('registration-message').update(msg);
			$('facebook-registration-message').update(msg);
		}
		if( this.__error_field ) {
			this.__error_field.style.border='1px solid gray';
			this.__error_field = null;
		}
		if ( this.highlight_field[ msg ] && $(this.highlight_field[msg])) {
			$(this.highlight_field[msg]).style.border='1px solid red';
			this.__error_field = $(this.highlight_field[msg]);
		}
	}

	this.response = function(resp) {
		var username = this.username;
		var password = this.password;
		$('registering-status').hide();
		try {
			$('register-submit').disbaled = false;			
			obj = eval('(' + resp.responseText + ')');
			if(! obj || ! obj.status) {
				this.err();			
			} else {
				if(obj.status == 'success') {
					__reg_ok = true;
					BF_DID_REGISTER = true;
					$('regsitration-form').reset();	
					if ( this.args && this.args.action=='vote' && vote_manager.contribution_id) {
						if( $('contribute-success') ) $('contribute-success').hide();
						var reg_success_div = $('registered-success').remove();
						$('registration-div').hide();
						var div = document.createElement('div');
						div.appendChild( reg_success_div );						
						$($('registration-div').parentNode).appendChild(div);
						$('registered-success').show();
						$('registered-success').style.marginTop = '10px';
						var show_message = this.SUCCESS2;
						if( obj.user_validated ) {
	show_message = this.SUCCESS2_VALIDATED;
	if ( $('please-validate-your-email') ) $('please-validate-your-email').update('We Have Validated Your Email');
}
						$('reg-success-message').update(show_message);
					} 
					else if( this.args && this.args.action=='signin' ) {
						if( $('contribute-success') )$('contribute-success').hide();
						if( $('registered-success') ) $('registered-success').show();
						if( $('registration-div') ) $('registration-div').hide();
						if( $('reg-success-message') ) {
							var show_message = this.SUCCESS3;
							if( obj.user_validated ) show_message = this.SUCCESS3_VALIDATED;
							$('reg-success-message').update(show_message);
							$('reg-success-message').show();
							if( obj.user_validated ) {
								var params = { username: username, password: password, contribute: 1} 
								new Ajax.Request('/buzzfeed/_www_login', {method: 'post', parameters: params, onSuccess: function(r) { bf_login.loggedIn(r); if( window.location.href.indexOf('/signin') != -1 ) window.location.href='/'+username }, onFailure: bf_login.err});
							}
						}
					}
					else if( this.args && this.args.action=='badge_vote' ) {
						if ( badge_vote_manager && badge_vote_manager.onRegister ) {
							badge_vote_manager.onRegister();
						}
						else {
							if( $('registered-vote-success') ) $('registered-vote-success').show();
							if( $('registration-div') ) $('registration-div').hide();
						}
					}
					else if( this.args && this.args.action=='contest_entry' ) {
						if ( typeof contest_manager != 'undefined' && contest_manager.onLogin ) contest_manager.onLogin();
					}
					else if( this.args && this.args.action=='micosite_submission' ) {
						if ( typeof picks_controller != 'undefined' && picks_controller.onLogin ) picks_controller.onLogin();
					}
					else if ( this.args && this.args.action == 'prediction' && typeof predictions != 'undefined' ) {
						predictions.onRegister();
					}
					else {
						if( $('contribute-success') )$('contribute-success').hide();
						if( $('registered-success') ) $('registered-success').show();
						if( $('registration-div') ) $('registration-div').hide();
						if(bf_contribute && bf_contribute.type == 'image') {
							var show_message = this.SUCCESS4;
							if( obj.user_validated ) show_message = this.SUCCESS4_VALIDATED;
							if( $('reg-success-message') )  $('reg-success-message').update(show_message);
						} else {
							var show_message = this.SUCCESS1;
							if( obj.user_validated ) {
								show_message = this.SUCCESS1_VALIDATED;
								if ( $('please-validate-your-email') ) $('please-validate-your-email').update('We Have Validated Your Email');
								var params = { username: username, password: password, contribute: 1} 
								new Ajax.Request('/buzzfeed/_www_login', {method: 'post', parameters: params, onSuccess: function(r) { bf_login.loggedIn(r,this.args); if( window.location.href.indexOf('/signin') != -1 ) window.location.href='/'+username }, onFailure: bf_login.err});
}
							if( $('reg-success-message') )  $('reg-success-message').update(show_message);							
						}
						bf_contribute.submit({session_key: obj.session_key});						
					}
					if($('register_this_page_url')) {
						var rurl = document.location.href;
						rurl = rurl.replace(/#.*/, '');
						$('register_this_page_url').href = rurl;
					}
				} else {
					if(obj.message == 'bad beta key') {
						if( $('register-submit') ) $('register-submit').disabled = false;
						this.update_registration_message(this.ERR_BETA_KEY);
						$('facebook-registration-message').addClassName('error');
						$('facebook-registration-message').show();
						$('registration-message').addClassName('error');
						$('registration-message').show();

					} else if(obj.message == 'duplicate username') {
						if( $('register-submit') ) $('register-submit').disabled = false;
						this.update_registration_message(this.ERR_DUP_USERNAME);
						$('facebook-registration-message').addClassName('error');
						$('facebook-registration-message').show();
						$('registration-message').addClassName('error');
						$('registration-message').show();

					} else if(obj.message == 'duplicate email') {
						if( $('register-submit') ) $('register-submit').disabled = false;
						if ( facebook.is_logged_in() ) {
							this.update_registration_message(this.BANNED_FB_UID );
						}
						else {
							this.update_registration_message(this.ERR_DUP_EMAIL);
						}
						$('facebook-registration-message').addClassName('error');
						$('facebook-registration-message').show();
						$('registration-message').addClassName('error');
						$('registration-message').show();
					} else {
						this.err();
					}
				}
			}
		} catch(e) {
			this.err();
		}			
	}

	this.err = function(resp) {
		$('register-submit').disabled = false;
		$('facebook-registration-message').update(this.ERR);			
		$('facebook-registration-message').addClassName('error');		
		$('facebook-registration-message').show();
		$('registration-message').update(this.ERR);			
		$('registration-message').addClassName('error');		
		$('registration-message').show();
		$('registering-status').hide();
	}
	
	this.redirect_to = function(url) {
		window.location.href = BF_STATIC.web_root + '/' + url;
	}

	/*
	 * Get form values url, username, and password if they aren't already defined
	 *
	 * @param obj 		existing form values
	 */
	this.form_values = function( obj ) {

		if( typeof obj.pw == "undefined" )
			obj.pw = $("register-password").value;
		
		if( typeof obj.username == "undefined" )
			obj.name = obj.username = $("register-uri-text").value;
		
		if( typeof obj.email == "undefined" )
			obj.email = $("register-email").value;

		return obj;
	}

	/*
	 * New user registration (signup)
	 * Gets password from form if not passed
	 *
	 * @param obj 	data dictionary of user, pass, etc
	 */
	 this.new_register = function(obj) {
		var errors = false;
		if (typeof obj.pw2=='undefined') obj.pw2 = obj.pw ;
		if (typeof obj.name=='undefined') obj.name = obj.username ;
		
		var required = {
			'username':'Please enter a URL',
			'email':'Please enter a valid e-mail address',
			'pw':'Please enter a password'
		};
		var error_msg_box = $('registration-error-message');
		var error = function(msg) {
			errors = true;
			if ( error_msg_box ) {
				error_msg_box.update( msg );
				error_msg_box.show();
			}
		}

		// Keyboard submit does not pass pw and url value correctly
		obj = this.form_values( obj );

		var strip = new Array('name','email','username','pw');
		strip.each( function(el){ if (typeof obj[el] != 'undefined') obj[el] = obj[el].strip() } );
		for ( var each in required ) {
			if ( typeof obj[each] == 'undefined' || obj[each] == '' ) {
				error(required[each]);
				break;
			}
		}
		
		// ignore if values set  are defaults...
		if(obj.username == 'url') { obj.username = '' }; // these are set as promts and can get through, so ignore
		if(obj.pw == 'password') { obj.pw = '' }; // these are set as promts and can get through, so ignore
		if(obj.email == 'your@email.com') { obj.email = '' }; // these are set as promts and can get through, so ignore

		if ( !errors && !obj.email.match(/^([^@]+)@([^\.]+)\.([a-zA-Z])+/)) {
			error('Please enter a valid email address');
		}
		if ( !errors && (obj.username.length < 3 || obj.username.length > 30)) {
			error('Please enter a URL between 3 and 30 characters');
		}
		if ( !errors && obj.pw.length < 6 ) {
			error('Your password must have at least 6 characters');
		}
		if ( !errors ) {
			bf_register.ajax.request('/buzzfeed/_contribute_register', {
				method:'post',
				parameters:obj,
				while_processing:{show:'registration-spinner',hide:'new-registration-form'},
				onSuccess:function(r){
					var json = eval('('+r.responseText+')');
					bf_register._new_register(obj,json)
				}
			});
		}
	}

	this._new_register = function( obj, json ) {
		if ( json.status == 'failed' ) bf_register._error(json);
		else bf_register._success(obj,json);
	}

	this._error = function(json) {
		var messages = {
			'duplicate email' : facebook.is_logged_in() ? bf_register.BANNED_FB_UID : bf_register.ERR_DUP_EMAIL,
			'duplicate username' : bf_register.ERR_DUP_USERNAME
		}
		var old_msg = $('registration-success-message');
		old_msg.hide();
		var msg = $('registration-error-message');
		if ( msg ) {
			msg.update( messages[json.message] || json.message );
			msg.show();
		}
	}

	this._success = function(obj,json) {
		var old_msg = $('registration-error-message');
		old_msg.hide();
		if ( json.user_validated ){
			var params = { username: obj.username, password: obj.pw, contribute: 1} 
			bf_register.ajax.request( '/buzzfeed/_www_login',{
				method:'post',
				parameters:params,
				while_processing:{show:'logging-in',hide:'new-registration-form'},
				onSuccess:function(r) {
					bf_login.loggedIn(r);
					bf_register.redirect_to('settings?firstTime=true');
				}
			});
		}
		else {
			var form = $('new-registration-form');
			var msg = $('registration-success-message');
			if (form) {
				form.hide();
			}
			if (msg) {
				msg.show();
			}
		}
	}

	this.register_alias = function(obj) {
		var errors = false;
		var error_msg_box = $('registration-error-message');
		var error = function(msg) {
			errors = true;
			if ( error_msg_box ) {
				error_msg_box.update( msg );
				error_msg_box.show();
			}
		}
		if ( obj.username.length < 3 || obj.username.length > 30 ) {
			error('Please enter a URL between 3 and 30 characters');
		}
		if ( !errors ) {
			BF_UserSwitcher.create_alias({
				username : obj.username,
				display_name : typeof obj.name == 'undefined' ? obj.username : obj.name
			}, bf_register._register_alias);
		}
	}
	
	this._register_alias = function(obj) {
		if ( obj.success == 0 || obj.status != 200 ) {
			bf_register._error({message:obj.message || 'Unable to create an alias at this time'});
		}
		else {
			BF_UserSwitcher.switchTo(obj.userid, function(){
				bf_register.redirect_to('settings?firstTime=true');
			});
		}
	}
}
