document.observe('dom:loaded', function(){
	// attach a function to a button when clicked
	if (Object.isElement($('signIn'))) $('signIn').observe('click', signIn);							
	if (Object.isElement($('resetPassword'))) $('resetPassword').observe('click', resetPassword);								
	if (Object.isElement($('changePassword'))) $('changePassword').observe('click', changePassword);									
	// when the enter key is pressed, proceed sign in
	if (Object.isElement($('frmSignIn'))) $('frmSignIn').observe('keypress', function(e){if (e.keyCode==13) signIn();});								
	// set focus off a button when clicked
	$$('a').each( function(el) { el.observe('click', function(){ el.blur(); }); } );			
	// set focus on the first element, if found		
	focusFirstElement();	
});

focusFirstElement = function() {
	thisForm = $$("form").find(function(el){return !(el.id.blank()) });
	if (Object.isElement(thisForm)){
		thisEl = thisForm.findFirstElement();		
		if (Object.isElement(thisEl))	thisEl.focus();
	}	
}

signIn = function() {
	if( !($F('email').blank()) && !($F('password').blank()) )
		$('frmSignIn').submit();
}

resetPassword = function() {	
	if ( !($F('email').blank()) ) {
		if (isValidEmail($F('email'))) {
			$('frmPasswordReset').submit();
		} else {
			$('errMsg').update('You entered an invalid email. Please try again.');
			showMsgBox2();			
		}			
	}
}

showMsgBox2 = function() {
	if ($('msgBox2').visible()) Effect.Shake('msgBox2'); else $('msgBox2').show();
}

changePassword = function() {
	if (Object.isElement($('password0')) && $F('password0').blank()) {
		$('errMsg').update('Please enter your old password.');
		showMsgBox2();		
		$('password0').focus();				
	}
	else if ($F('password1').blank()) {
		$('errMsg').update('Please enter your new password.');
		showMsgBox2();				
		$('password1').focus();				
	}
	else if ($F('password1').length < 8) {
		$('errMsg').update('Passwords must be at least eight (8) characters in length.');
		showMsgBox2();								
		$('password1').select();				
	}	
	else if ($F('password2').blank()) {
		$('errMsg').update('Please confirm your new password.');	
		showMsgBox2();						
		$('password2').focus();				
	}
	else if ($F('password1') != $F('password2')) {
		$('errMsg').update('The passwords entered did not match, please try again.');
		showMsgBox2();										
		$('password1').select();				
	}
	else $('frmPasswordChange').submit();
}

// Validate the Captcha code the user has entered.
function validateCode()
{
	var myAjax = new Ajax.Updater( {success: 'comment_result'}, '?fa=captcha.validate', 
	{ 
		method: 'post',
		parameters: Form.serialize($('comment_form')),
		onCreate: function() { 
			$('comment_post_button').disable();
		},
		onFailure: function() {
		    alert('An error occured validating your posting.  Please contact the webmaster to report this problem.');
		},
		onComplete: function() {
			$('comment_post_button').enable();
		},
		evalScripts: true
	});
}

isValidEmail = function(email){
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;	
	var returnValue = filter.test(email) ? true : false;
	return returnValue;
}		

// confirm delete of a user comment by an administrator
commentOffensive = function(commentID) {
	if (confirm('**** NOTICE ****\n\nYou are about to report this comment as offensive or otherwise inappropriate.  This will alert the website operator to review the material in question.\n\nIf you would like to continue with the press OK, or CANCEL to abort the report.')) {
		new Ajax.Request( '?fa=comment.complaint', { 
		  method: 'get',
		  parameters: 'commentID=' + commentID,
		  onSuccess: function() {
			  alert('Your report has been received, thank you.');
		  },
		  onFailure: function() {
			  alert('An error occured while trying to file your report.  Please contact the webmaster to report this problem.');
		  },
		  evalScripts: false
		});
	}
}