window.addEvent('domready', function(){
	if($('email') != null){
		$('email').focus();
		$('email').addEvents({
			'keydown': function(e){ if(e.key == 'enter'){ forgot(1); } }
		});
		$('answer').addEvents({
			'keydown': function(e){ if(e.key == 'enter'){ forgot(2); } }
		});
		$('password1').addEvents({
			'keydown': function(e){ if(e.key == 'enter'){ $('password2').focus(); } }
		});
		$('password2').addEvents({
			'keydown': function(e){ if(e.key == 'enter'){ forgot(3); } }
		});
	}

	if($('txt_password') != null){
		$('fake_password').addEvents({
			'focus': function(){
				$(this).hide();
				$('txt_password').show().focus();
			}
		});

		$('txt_password').addEvents({
			'blur': function(){
				if(this.value == ''){ 
					$(this).hide();
					$('fake_password').show();
				}
			}
		});	
	}

	if($('txt_username') != null){
		$('txt_username').addEvents({
			'focus': function(){ 
				if(this.value == 'Email'){ this.value = ''; }
			},
			'blur': function(){
				if(this.value == ''){ this.value = 'Email'; }
			}
		});
	}

	$('search_button').addEvents({
		'mouseenter': function(){ $(this).setStyle('cursor','pointer'); },
		'mouseleave': function(){ $(this).setStyle('cursor','default'); },
		'click': function(){ search(); }
	});

	$('txt_search').addEvents({
		'keydown': function(e){
			if(e.key == 'enter'){ search(); }
		}	
	});
	
	// Left-side menu pulldowns
	if($('about_adicia_i') != null){
		$('about_adicia_h').addEvents({
			'click': function(){
				treetoggle($(this).getElement('img'));
				$('about_adicia_i').toggle();
			}	
		});

		$('shop_adicia_h').addEvents({
			'click': function(){
				treetoggle($(this).getElement('img'));
				$('shop_adicia_i').toggle();
			}	
		});

		$('things_h').addEvents({
			'click': function(){
				treetoggle($(this).getElement('img'));
				$('things_i').toggle();
			}	
		});

		$('social_h').addEvents({
			'click': function(){
				treetoggle($(this).getElement('img'));
				$('social_i').toggle();
			}	
		});

		$('customer_service_h').addEvents({
			'click': function(){
				treetoggle($(this).getElement('img'));
				$('customer_service_i').toggle();
			}	
		});
	}

	if($('_username') != null){ $('_username').focus(); }

	var preload_plus = new Image();
	preload_plus.src = 'images/tree-plu.gif';

	var preload_minus = new Image();
	preload_minus.src = 'images/tree-min.gif';
});

function treetoggle(ele){
	if(ele != null){
		var state = (ele.getProperty('src') == 'images/tree-plu.gif' ? true : false);
		if(state){ ele.setProperty('src', 'images/tree-min.gif'); }
		else { ele.setProperty('src', 'images/tree-plu.gif'); }
	}
}

function search(s){
	if(isNaN(s)){ s = 0; }

	if($chk($('txt_search').value)){
		var htmlrequest = new Request.HTML({
			url: "search.php",
			onSuccess: function(tree, elements, html, script){
				var ele = $('div_main_content');
				if(ele == null){
					ele = $('div-jsflash-main');
				}

				ele.empty().set('html', html);
			}
		}).post({
			"q": $('txt_search').value,
			"s": s
		});
	}	
}

function forgot(s){
	if(s == 1){
		$('step1_button').hide();
		new Request.JSON({
			url: "resetpass.php",
			onSuccess: function(json, txt){
				if(json.success){
					$('email').setProperty('disabled', true);
					$$('tr.step2').show();
					$('question').empty().appendText(json.response);
					$('answer').focus();
				} else {
					$('step1_button').show();
					alert(json.response);
				}
			}
		}).post({
			email: $('email').value
		});

	} else if(s == 2){
		$('step2_button').hide();
		new Request.JSON({
			url: "resetpass.php",
			onSuccess: function(json, txt){
				if(json.success){
					$('answer').setProperty('disabled', true);
					$$('tr.step3').show();
					$('password1').focus();
				} else {
					$('step2_button').show();
					alert(json.response);
				}
			}
		}).post({
			email: $('email').value,
			answer: $('answer').value
		});

	} else if(s == 3){
		if($('password1').value != $('password2').value){
			alert('Passwords do not match.');
			return;
		} else if($('password1').value.length < 4){ 
			alert('Passwords must be at least four characters in length.');
			return;
		}	

		$('step3_button').hide();
		new Request.JSON({
			url: "resetpass.php",
			onSuccess: function(json, txt){
				if(json.success){
					$('password1').setProperty('disabled', true);
					$('password2').setProperty('disabled', true);
					alert(json.response);

					location.href = 'myaccount.php';
				} else {
					$('step3_button').show();
					alert(json.response);
				}
			}
		}).post({
			email: $('email').value,
			answer: $('answer').value,
			password: $('password1').value
		});
	}
}

function billtoship(e){
	if($(e).get('checked')){
		var fields = new Array("first","last","company","address1","address2","city","state","zip","phone");
		for(var i=0;i < fields.length; i++){
			$$('[name=shipping_'+fields[i]+']').set('value',
				$$('[name=billing_'+fields[i]+']').get('value')
			);
		}
	}	
}

function leftswitch(){
	var panels = $$('#leftpanel a');
	panels.setStyle('display', 'inline');

	var i = panels.length - 1;

	setInterval(function(){
		panels[i].fade('out');
		i = (i == 0) ? panels.length - 1 : i-1;
		panels[i].fade('in');
	}, 6000);
}

