var user_hash = null;
var checking_url = false;
var do_submit = false;

var urls = {};
urls['orchant_login'] = 'https://orchant.awayfind.com/login.php';
urls['prod_login'] = 'https://awayfind.com/ext_login.php';

urls['orchant_forgot'] = 'https://orchant.awayfind.com/forgot_password.php';
urls['prod_forgot'] = 'https://awayfind.com/ext_forgot_password.php';

function showForgot() {
	$('forgot_password').show();
	$('forgot_link').hide();
	
	if ($('login_email').value.indexOf('@') != -1) {
		$('reset_email').value = $('login_email').value;
	}
	$('login_button').addClassName('active_button');
	$('login_button').disable();
}

function checkYes() {
	$('yesCheckbox').addClassName('checked');
	$('noCheckbox').removeClassName('checked');
	$('have_an_account').value = "TRUE";
}

function checkNo() {
	$('yesCheckbox').removeClassName('checked');
	$('noCheckbox').addClassName('checked');
	$('have_an_account').value = "FALSE";
}

function get_login_url() {
	return get_site('login_email', 'login_form', 'login');
}

function get_forgot_url() {
	return get_site('reset_email', 'forgot_password', 'forgot');
}

function get_site(field_name, form_name, url_suffix) {
	checking_url = true;
	new Ajax.Request( 'check.php', {
		method : 'get',
		parameters : {
			'username' : $(field_name).value
		},
		onSuccess : function(transport) {
			var _url = urls[transport.responseText + '_' + url_suffix];
			$(form_name).action = _url;
			checking_url = false;
			if (do_submit) {
				$(form_name).submit();
			}
		},
		onFailure : function(transports) {
			checking_url = false;
		}
	});
}

function check_login() {
	$('login_button').addClassName('active_button');
	return is_checking('login_form', 'check', get_login_url);
}

function check_forgot() {
	$('reset_username').value = $('reset_email').value;
	$('reset_password').addClassName('active_button');
	return is_checking('forgot_password', 'check', get_forgot_url);
}

function is_checking(form_name, invalid_action, callback) {

	if ($(form_name).action.indexOf(invalid_action) != -1) {
		do_submit = true;
		if (!checking_url) {
			callback();
		}
		return false;
	} else {
		return true;
	}
	return false;
}

function add_user_request() {
	$('request_invite').hide();
	$('loader1').show();
	$('email_input').disable();
	new Ajax.Request( 'ajax.php?url=/ajax/add_user_request/', {
		method : 'post',
		parameters : {
			'email' : $('email_input').value
		},
		onSuccess : function(transport) {
			$('email_input').enable();
			$('form_area').removeClassName('errorbox');
			var json = transport.responseText.evalJSON();
			if (json.errors == null) {
				pageTracker._trackPageview('/ajax/register_user_login');
				if (json.result == null) {
					$('orchant_form').hide();
					$('successbox').innerHTML = "Found you! You're an existing<br />AwayFind user, so when we contact<br />you, we'll copy your data over first!";
					$('successbox').show();
				} else {
					user_hash = json.result;
					$('request_invite').hide();
					$('loader1').hide();
					$('orchant_error1').hide();
					$('orchant_form_2').show();
				}
			} else { 
				// Hide loading logo, show input so user can retry, show error message
				$('loader1').hide();
				$('request_invite').show();
				$('form_area').addClassName('errorbox');
				// We'll only display the first error
				if (typeof json.errors.email != 'undefined' && json.errors.email == 'email') {
					//Invalid email error
					$('orchant_error1').innerHTML = "<b>Oops!</b> That email address appears to be invalid.";
					$('orchant_error1').show();
				} else if (typeof json.errors.email != 'undefined' && json.errors.email.error == '_email_is_unique') {
					//User already registered error
					$('orchant_error1').innerHTML = "The email address you entered is already registered, we will be in touch soon!";
					$('orchant_error1').show();
				} else {
					//Added to catch other errors
					$('orchant_error1').innerHTML = "<b>Oops!</b> That email address appears to be invalid.";
					$('orchant_error1').show();
				}
			}
		},
		onFailure : function(transport) {
			//Other errors
			$('loader1').hide();
			$('request_invite').show();
			$('form_area').addClassName('errorbox');
			$('orchant_error1').show();
			$('orchant_error1').innerHTML = "Yikes! We're having some technical troubles. Please try again later.";
		}
	});
	// Don't submit the form
	return false;
}

function update_user_request() {
	$('request_update').hide();
	$('loader2').show();
	$('email_input').disable();
	new Ajax.Request( 'ajax.php?url=/ajax/update_user_request/', {
		method : 'post',
		parameters : {
			'is_existing_user' : $('have_an_account').value,
			'hash' : user_hash
		},
		onSuccess : function() {
			$('email_input').enable();
			$('form_area').removeClassName('errorbox');
			if ($('have_an_account').value == "FALSE") {
				$('orchant_form_2').hide();
				$('successbox').innerHTML = "Thanks for requesting an invitation!<br />We'll be in touch";
				$('successbox').show();
			}
			else {
				$('orchant_form_2').hide();
				$('successbox').innerHTML = "Thanks for your continued support!<br />We'll be in touch";
				$('successbox').show();
			}
		},
		onFailure : function() {
			$('loader2').hide();
			$('request_update').show();
			$('form_area').addClassName('errorbox');
			$('orchant_error2').innerHTML = "Yikes! We're having some technical troubles. Please try again later.";
			$('orchant_error2').show();
		}
	});
	// Don't submit the form
	return false;
}
