
Event.observe(window, 'load', load_captcha);
Event.observe(window, 'load', init_referral_autocompleter);
Event.observe(window, 'load', init_gym_autocompleter);




// ****************************************************************************
//							REFERRAL AUTOCOMPLETER
// ****************************************************************************
function init_referral_autocompleter()
{
    var editor = new Ajax.Autocompleter( 'referral_autocomplete_textbox', 
					   	  				 'referral_choices', 
					    	  			 '/lookup.php?method=lookup_user',
						{
							afterUpdateElement : referral_autocompleter_after_update,
							indicator: 'referral_loading',
							paramName: 'typed_chars',
							minChars: 1
						});  // end Ajax.Autocompleter.
}  // end function init_referral_autocompleter.
	
function referral_autocompleter_after_update ( text, li )
{
	var param_hash = new Hash ();
	param_hash.set ( 'method', 'get_user_name' );
	param_hash.set ( 'user_id', li.id );

       // Get the name of the location so we can display it after the user selects it.
		new Ajax.Request(absolute_link_path + 'ajax.php', 
		{
			method: "post",
			evalJSON: true, 
			sanitizeJSON: true, 
			parameters: $H(param_hash),
			onSuccess: function(transport)
			{
				ajax_require_logged_in(transport.responseJSON.status);	
				
				$('referral_autocomplete_textbox').style.display = 'none';
			    $('referral_formatted').innerHTML = transport.responseJSON.data['user_name'];
			    $('referral_formatted').style.display = 'block';

                // When the user clicks the location name, we want to display the textbox 
				// again so he can change his selection.	
			    $('referral_formatted').onclick = show_referral_autocompleter;
			    
			    // Update the hidden field with the venue_id, since we can't 
			    // save it anywhere just yet - the user hasn't been created.
			    $('referred_by').value = li.id;
			}  // end onSuccess.
		});  // end Ajax.Request.
} // end afterUpdateElement function.



function show_referral_autocompleter ()
{
	$('referral_autocomplete_textbox').style.display = 'block';
	$('referral_autocomplete_textbox').value = '';
	$('referral_autocomplete_textbox').select();
	$('referral_formatted').style.display = 'none';	
}  // end function show_referral_autocompleter.






// ****************************************************************************
//								GYM AUTOCOMPLETER
// ****************************************************************************
function init_gym_autocompleter()
{
    var editor = new Ajax.Autocompleter( 'gym_autocomplete_textbox', 
					   	  				 'gym_choices', 
					    	  			 '/lookup.php?method=lookup_gyms',
						{
							afterUpdateElement : gym_autocompleter_after_update,
							indicator: 'gym_loading',
							paramName: 'typed_chars',
							minChars: 1
						});  // end Ajax.Autocompleter.
}  // end function init_gym_autocompleter.
	
function gym_autocompleter_after_update ( text, li )
{
	var param_hash = new Hash ();
	param_hash.set ( 'method', 'get_gym_name' );
	param_hash.set ( 'gym_id', li.id );

       // Get the name of the location so we can display it after the user selects it.
		new Ajax.Request(absolute_link_path + 'ajax.php', 
		{
			method: "post",
			evalJSON: true, 
			sanitizeJSON: true, 
			parameters: $H(param_hash),
			onSuccess: function(transport)
			{
				ajax_require_logged_in(transport.responseJSON.status);	
				
				$('gym_autocomplete_textbox').style.display = 'none';
			    $('gym_formatted').innerHTML = transport.responseJSON.data['name'];
			    $('gym_formatted').style.display = 'block';

                // When the user clicks the gym name, we want to display the textbox 
				// again so he can change his selection.	
			    $('gym_formatted').onclick = show_gym_autocompleter;
			    
			    // Update the hidden field with the gym_id, since we can't 
			    // save it anywhere just yet - the user hasn't been created.
			    $('gym_id').value = li.id;
			}  // end onSuccess.
		});  // end Ajax.Request.
} // end afterUpdateElement function.



function show_gym_autocompleter ()
{
	$('gym_autocomplete_textbox').style.display = 'block';
	$('gym_autocomplete_textbox').value = '';
	$('gym_autocomplete_textbox').select();
	$('gym_formatted').style.display = 'none';	
}  // end function show_gym_autocompleter.






function check_user_type ()
{
	if ( $('user_type').value == 2 )
	{
		// If the new user is a fighter, add the gym autocompleter.
		$('join_fighter_gym_autocompleter').style.display = "block";
	}
	else
	{
		$('join_fighter_gym_autocompleter').style.display = "none";
	}
}  // end function check_user_type.


function load_captcha()
{
	Recaptcha.create(
	"6LeZEwcAAAAAAH9hia9wmoNvwBKAEPk7RbQ0R8xx",
	"recaptcha_div",
	{
   		theme: "red",
   		callback: Recaptcha.focus_response_field
	});
}

function create_user()
{
	submit_button_processing_on ( 'submit_create_form', 'Creating account...' );
	
	var user_type = '';
	var handle = '';
	var email = '';
	var pwd = '';
	var tos = '';
	var referral = '';

	// First, let's clear out all error messages.
	callback_hide_general_errors ( 'error_general' );
	callback_hide_error ( 'error_tos' );
	callback_hide_error ( 'error_pwd' );
	callback_hide_error ( 'error_email' );
	callback_hide_error ( 'error_handle' );
	callback_hide_error ( 'error_recaptcha_response_field' );
	callback_hide_error ( 'referral_error' );

	user_type = $F('user_type');
	handle = $F('handle');
	email = $F('email');
	pwd = $F('pwd');
	pwd_verify = $F('pwd');
	tos = $F('tos');
	referred_by = $F('referred_by');

	new Ajax.Request
	(
		"/ajax.php", 
		{ 
			method:'post',
			evalJSON: true, sanitizeJSON: true, parameters: 
				{
						recaptcha_challenge_field: $('recaptcha_challenge_field').value,
						recaptcha_response_field:  $('recaptcha_response_field').value,
						method: 'create_user',
						user_type: user_type, 
						handle: handle, 
						email: email,
						pwd: pwd,
						pwd_verify: pwd,
						tos: tos,
						referred_by: referred_by
				}, 
  			
			onSuccess: create_user_callback, 
			onFailure: create_user_errback
  		}
	);
} // end login_user


function create_user_callback(oReq, json)
{	
	// This will give us a variable named oReq.responseJSON.
	// This variable is passed back from ajax.php
	// Now the account creation should be successful.
	if (oReq.responseJSON.status == 'success') 
	{
		// If the user was a fighter and he entered a gym, save that now.
		save_gym_fighter ( oReq.responseJSON.data['user_id'] );
		
		window.location = absolute_link_path + 'confirmaccount/' + oReq.responseJSON.data['url_encoded_email'];
		return 0;
	}
	else 
	{
		callback_show_error('error_tos', oReq.responseJSON.data_errors['tos']);
		callback_show_error('error_pwd', oReq.responseJSON.data_errors['pwd']);
		callback_show_error('error_email', oReq.responseJSON.data_errors['email']);
		callback_show_error('error_handle', oReq.responseJSON.data_errors['handle']);
		callback_show_general_errors ('error_general', oReq );
				
		if (oReq.responseJSON.data_errors['recaptcha_response_field'] != undefined) 
		{
			$('error_recaptcha_response_field').innerHTML = oReq.responseJSON.data_errors['recaptcha_response_field'];
			$('error_recaptcha_response_field').style.display = 'block';
			
			// If there's an error, load the new captcha.  For some reason, the second time
			// the captcha is processed, it comes up incorrect.  Might need to look into this.
			load_captcha();
		}
		
		submit_button_processing_off('submit_create_form', 'Create account');
	}  // endif status == 'success'.			
}  // end function create_user_callback.

function create_user_errback(oReq, oJSN)
{
	alert('There was an error when we tried to create your account. Please call Customer Service and they will get you taken care.');
	// window.location ="join?result=somethingfarkedup";
}


function save_gym_fighter ( arg_fighter_id )
{
	// It's possible that the user selected fighter, chose a gym, then switched the
	// user type to something else, so let's just check here to make sure this guy
	// is still a fighter.
	if ($('user_type').value == 2) 
	{
		var param_hash = new Hash ();
		param_hash.set ( 'method', 'save_gym_fighter_join' );
		param_hash.set ( 'fighter_id', arg_fighter_id );
		param_hash.set ( 'gym_id', $('gym_id').value );
	
       // Get the name of the location so we can display it after the user selects it.
		new Ajax.Request(absolute_link_path + 'ajax.php', 
		{
			method: "post",
			evalJSON: true, 
			sanitizeJSON: true, 
			parameters: $H(param_hash)
		});  // end Ajax.Request.	
	}  // endif the user_type == fighter.
	
} // end function save_gym_fighter.




