function fbconnect_register_form() {
 $fbuid    = fbconnect_get_fbuid();
 $sitename = variable_get('site_name', t('this website'));
 
 $form['reg'] = array(
   '#type' => 'fieldset',
   '#collapsible' => FALSE,
 );
 $form['reg']['username'] = array(
   '#type' => 'textfield',
   '#title' => t('Username'),
   '#required' => TRUE,
   '#default_value' => fbconnect_get_fbname($fbuid),
   '#description' => t('Spaces are allowed; punctuation is not allowed except for periods, hyphens, and underscores.'),
 );
 $form['reg']['mail'] = array(
   '#type' => 'textfield',
   '#title' => t('E-mail address'),
   '#required' => TRUE,
   '#description' => t('A valid e-mail address. All e-mails from the system will be sent to this address.')
 );
 $form['reg']['visibility'] = array(
   '#type' => 'checkbox',
   '#title' => t('Let my Facebook friends see me on @sitename', 
      array('@sitename' => $sitename)),
   '#description' => t('My Facebook friends will be able to see that I own an account on @sitename.',
      array('@sitename' => $sitename)),
   '#default_value' => 1,
 );
 
 // Get all the fields which are on normal sign up.
 $extra = _user_forms($null, NULL, NULL, 'register');

 // merge them with FbConnect SignUp Form.
 $form = array_merge($form, $extra);

 $form['submit'] = array( 
 '#type' => 'submit',
 '#value' => t('Submit'),
 '#weight' => '10',
 );
 
 return $form; 
}

/** 
 * Handle post-validation register form submission. 
 */ 
function fbconnect_register_form_submit($form, &$form_state) {
  global $user;
  $visible = $form_state['visibility'];
  $mail    = $form_state['mail'];
  
  $udata = array(
    'name' => $form_state['username'],
    'mail' => $mail,
    'init' => $mail,
    'status' => 1,
    'login' => time(),
    'pass' => user_password(),    
  );

  fbconnect_register_user($udata);
	foreach(profile_categories() as $categories) {
		$result = _profile_get_fields($categories['name'], FALSE);
		//	Fetch fields to replace values
		while ($field = db_fetch_object($result)) {
			$edit[$field->name] = $form_state[$field->name] ;
		}
		
		user_save($user, $edit, $categories['name']);
	}
  // Update user visibility, default = 1
  if (!$visible) {
    fbconnect_set_user_visibility($user, $visible);
  }
  
  if ($_SESSION['fb_reg_import'] && module_exists('fbconnect_profile')) {
    // Saving import settings.
    fbconnect_profile_insert_user_info($user->uid, $_SESSION['fb_reg_import']);
    unset($_SESSION['fb_reg_import']);
  }

  // If the user chose to be visible, and the registration feed parameter is enabled.
  // We store a value in the session variable, 
  // this variable will be read by the _fbconnect_render_js function.
  if (variable_get('fbconnect_reg_feed', 1) && $visible) {
    $_SESSION['fbconnect_feed']['type'] = 'registration';
  }
  // The user has chosen to be visible, just redirect to the friends invite page.
  if ($visible) {
    $sitename = variable_get('site_name', t('this website'));
    $welcome_msg = t('Registration successful. You may now invite friends to join you on @site',  array('@site' => $sitename));
    drupal_set_message($welcome_msg, 'status');
    drupal_goto('fbconnect/invite/friends');
  }
  else {
    drupal_set_message(t('Registration successful.'));  
    drupal_goto();
  }
}