/*
	Sign Up & Log In Functions
	PasswordStrength Class
*/

// Sign Up Event Behavior Functions

Event.addBehavior({
	
	// Sign Up Form
	
	'body div#content div#contentwithsidebar form#signup': function()
	{
		this.focusFirstElement();
	},

	// Unquie Username Check
	
	'body div#content div#contentwithsidebar form#signup input#login_username:blur': function()
	{
		if ($F('login_username').blank()) {
			$('username_check').update('Please enter a unique username.');
		} else {
			new Ajax.Request('/signup/username_check', {
				parameters:this.form.serialize()
			});
		}
	},
	
	// Passwords Confirm Match
	
	'body div#content div#contentwithsidebar form#signup input#password_confirmation:blur': function()
	{
		$('password_confirm_check').removeClassName('error');
		if ($F('login_password') !== $F('password_confirmation')) {
			$('password_confirm_check').addClassName('error');
			$('password_confirmation').select();
			$('password_confirmation').focus();
		}
	},
	
	/*
	'body div#content div#contentwithsidebar form#signup input#agent_other_addr_zip:keyup': function()
	{
		var zip = this.value;
		if (zip.length > 4 && /^[0-9]+$/.test(zip)) {
			new Ajax.Request('/signup/nv_zip_check', {
				parameters: '&zip='+zip
			});
		}
	},*/
	
	// Unquie Email Check
	
	'body div#content div#contentwithsidebar form#signup input#agent_email:blur': function()
	{
		if ($F('agent_email').blank()) {
			$('agent_email_check').update('Please enter a unique email address.');
		} else {
			new Ajax.Request('/signup/email_check', {
				parameters:this.form.serialize()
			});
		}
	},
	
	// Show NV Subscription Prices
	
	'body div#content div#contentwithsidebar form#signup select#agent_subscription_state:change': function()
	{
		if (this.value == 'NV') {
			new Effect.BlindDown('nv_prices',{duration:0.3,delay:0,queue:'end'});
		} else {
			if ($('nv_prices').style.display != 'none') {
				new Effect.BlindUp('nv_prices',{duration:0.3,delay:0,queue:'end'});
			}
		}
		
		if (this.value.strip() == '') {
			$$('body div#content div#contentwithsidebar form#signup select#agent_subscription_state option').each(function(o){
				if (o.value == 'XX') {
					o.selected = 'selected';
					throw $break;
				}
			});
			return;
		}
		
	},
	
	// Log In Form
	
	'body div#content div#contentwithsidebar form#login': function()
	{
		this.focusFirstElement();
	},
	
	'body div#content div#contentwithsidebar form#login_help input#email:focus': function()
	{
		$('recover_by_email').checked = true;
	},
	
	'body div#content div#contentwithsidebar form#login_help input#username:focus': function()
	{
		$('recover_by_username').checked = true;
	}

});
