// disabling all but the username instructions. "too slow"
document.observe("dom:loaded", function() {
	//Event.observe('user_login', 'blur', checkUsername);
	//Event.observe('user_login', 'blur', hideUsernameInstructions);
	//Event.observe('user_login', 'blur', hideUsernameFeedback);
	Event.observe('user_login', 'focus', showUsernameInstructions);
	
	//Event.observe('u_email', 'blur', checkEmail);
	//Event.observe('user_email', 'blur', hideEmailFeedback);
	
	//Event.observe('user_password', 'blur', checkPassword);
	//Event.observe('user_password', 'blur', hidePasswordFeedback);
	
	//Event.observe('user_password_confirmation', 'blur', checkPasswordConfirmation);
	//Event.observe('user_password_confirmation', 'blur', hidePasswordConfirmationFeedback);
});

// LOGIN
function checkUsername(e){
	var login = $F(this);
	var field = this;
	
	if (login.length != 0) {
	  new Ajax.Request('/users/check_user_params_inline?login=' + login,
	  {
	    method:'post',
		parameters:login,
	    onSuccess: function(transport){
			if (transport.responseText != "OK") {
				cleanup('login_x','login_ok','username_error');
				new Insertion.After(field, "<img src=\"/assets/images/chrome/forms/icon_x_error.gif\" width=\"14\" height=\"14\" style=\"padding-left:6px\" id=\"login_x\" />");
				new Insertion.After('user_row', "<div class=\"formrow error_messages\" id=\"username_error\">" + transport.responseText + "</div>");
				field.addClassName("inputWithErrors");
				$('reg_user_label').addClassName("error_messages");
			}else{
				cleanup('login_x','login_ok','username_error');
				new Insertion.After(field, "<img src=\"/assets/images/chrome/forms/icon_ok.gif\" width=\"21\" height=\"13\" style=\"padding-left:6px\" id=\"login_ok\" />");
				field.removeClassName("inputWithErrors");
				$('reg_user_label').removeClassName("error_messages");
			}
	    },
	    onFailure: function(){
			console.log('bad');
			alert("We're sorry. An unexpected error occurred. Please try again.")
		}
		
	  });
	}
}
function showUsernameInstructions(e){
	if(! $('username_instructions')){
		new Insertion.After('user_row', "<div class=\"formrow instructions\" id=\"username_instructions\">Your username cannot contain any spaces.</div>");
	}
}
function hideUsernameInstructions(e){
	if($('username_instructions')){
		$('username_instructions').remove();
	}
}
function hideUsernameFeedback(e){
	if($('username_error')){$('username_error').remove();}
	if($('login_x')){$('login_x').remove();}
}

// EMAIL
function checkEmail(e){
	var email = $F(this);
	var field = this;

	if (email.length != 0) {
	  new Ajax.Request('/users/check_user_params_inline?email=' + email,
	  {
	    method:'post',
		parameters:email,
	    onSuccess: function(transport){
			if (transport.responseText != "OK") {
				cleanup('email_x','email_ok','email_error');
				new Insertion.After(field, "<img src=\"/assets/images/chrome/forms/icon_x_error.gif\" width=\"14\" height=\"14\" style=\"padding-left:6px\" id=\"email_x\" />");
				new Insertion.After('email_row', "<div class=\"formrow error_messages\" id=\"email_error\">" + transport.responseText + "</div>");
				field.addClassName("inputWithErrors");
				$('reg_email_label').addClassName("error_messages");
			}else{
				cleanup('email_x','email_ok','email_error');
				new Insertion.After(field, "<img src=\"/assets/images/chrome/forms/icon_ok.gif\" width=\"21\" height=\"13\" style=\"padding-left:6px\" id=\"email_ok\" />");
				field.removeClassName("inputWithErrors");
				$('reg_email_label').removeClassName("error_messages");
			}
	    },
	    onFailure: function(){
			console.log('bad');
			alert("We're sorry. An unexpected error occurred. Please try again.")
		}
	   });
	}
}
function hideEmailFeedback(e){
	
}
function checkPassword(e){
	var password = $F(this);
	var field = this;

	if (field.value.length != 0) {
	  new Ajax.Request('/users/check_user_params_inline?password=' + password,
	  {
	    method:'post',
		parameters:password,
	    onSuccess: function(transport){
			if (transport.responseText != "OK") {
				cleanup('pw_x','pw_ok','password_error');
				new Insertion.After(field, "<img src=\"/assets/images/chrome/forms/icon_x_error.gif\" width=\"14\" height=\"14\" style=\"padding-left:6px\" id=\"pw_x\" />");
				new Insertion.After('password_row', "<div class=\"formrow error_messages\" id=\"password_error\">" + transport.responseText + "</div>");
				field.addClassName("inputWithErrors");
				$('reg_pass_label').addClassName("error_messages");
			}else{
				cleanup('pw_x','pw_ok','password_error');
				new Insertion.After(field, "<img src=\"/assets/images/chrome/forms/icon_ok.gif\" width=\"21\" height=\"13\" style=\"padding-left:6px\" id=\"pw_ok\" />");
				field.removeClassName("inputWithErrors");
				$('reg_pass_label').removeClassName("error_messages");
			}
	    },
	    onFailure: function(){
			console.log('bad');
			alert("We're sorry. An unexpected error occurred. Please try again.")
		}
	  });
	}
}
function hidePasswordFeedback(e){
	
}
function checkPasswordConfirmation(e){
	var password_confirmation = $F(this);
	var field = this;	
	var password = $('user_password').value;

	if (field.value.length != 0) {
	  new Ajax.Request('/users/check_user_params_inline?password=' + password + '&password_confirmation=' + password_confirmation,
	  {
	    method:'post',
		parameters:password_confirmation,
	    onSuccess: function(transport){
			if (transport.responseText != "OK") {
				cleanup('pwc_x','pwc_ok','password_confirmation_error');
				new Insertion.After(field, "<img src=\"/assets/images/chrome/forms/icon_x_error.gif\" width=\"14\" height=\"14\" style=\"padding-left:6px\" id=\"pwc_x\" />");
				new Insertion.After('password_confirm_row', "<div class=\"formrow error_messages\" id=\"password_confirmation_error\">" + transport.responseText + "</div>");
				field.addClassName("inputWithErrors");
				$('reg_passconf_label').addClassName("error_messages");
			}else{
				cleanup('pwc_x','pwc_ok','password_confirmation_error');
				new Insertion.After(field, "<img src=\"/assets/images/chrome/forms/icon_ok.gif\" width=\"21\" height=\"13\" style=\"padding-left:6px\" id=\"pwc_ok\" />");
				field.removeClassName("inputWithErrors");
				$('reg_passconf_label').removeClassName("error_messages");
			}
	    },
	    onFailure: function(){
			console.log('bad');
			alert("We're sorry. An unexpected error occurred. Please try again.")
		}
	  });
	}
}

function hidePasswordConfirmationFeedback(e){
	
}

function cleanup(x_label,ok_label,error_label){
	if($(error_label)){$(error_label).remove();}
	if($(x_label)){$(x_label).remove();}
	if($(ok_label)){$(ok_label).remove();}
}