/*===============================================================================
	members.js
	 Created 08/28/2008 by Greg Spry
		Member Registration & Profile Javascript

===============================================================================*/
// Constants

/*===============================================================================*/
window.addEvent('domready', function() {

});

/*===============================================================================*/
function registerMember() {
  checkMemberRegistrationKey();
}
function checkMemberRegistrationKey() {
    regKey = $('key').value;
	if(regKey != '') {
    new Ajax(ROOT_PATH + 'ajaxAction.php', {
		  method: 'post',
		  data: 'ajaxAction=checkMemberRegistrationKey&key=' + regKey,
		  onComplete: function() {
        checkMemberUsername(this.response.text);
      },
		  evalScripts: true
    }).request();
	}
	else {
    checkMemberUsername('');
	}
}
function checkMemberUsername(keyResponse) {
  if(keyResponse == 'invalid') {
		alert('Invalid registration key.');
		$('registerMemberForm').key.focus();
  }
  else {
	  username = document.registerMemberForm.username.value;
	  new Ajax(ROOT_PATH + 'ajaxAction.php', {
		  method: 'post',
		  data: 'ajaxAction=checkMemberName&memberName=' + username,
		  onComplete: function() {
    	  continueMemberRegistration(this.response.text, keyResponse);
  	  },
		  evalScripts: true
	  }).request();
	}
}
function continueMemberRegistration(usernameResponse) {
	theForm = $('registerMemberForm');
	with(theForm){
		if (username.value == ''){
			alert('Please enter a username.');
			username.focus();
		}
		else if(usernameResponse == 'exists'){
			alert('Please select a different username.');
			username.focus();
		}
		else if(password.value == '') {
			alert('Please enter a password.');
			password.focus();
		}
		else if(confirmPassword.value == '') {
			alert('Please confirm your password.');
			confirmPassword.focus();
		}
		else if(password.value != confirmPassword.value){
			alert('Password and confirm password fields do not match.');
			password.focus();
		}
		else if(firstName.value == ''){
			alert('Please enter your first name.');
			firstName.focus();
		}
		else if(lastName.value == ''){
			alert('Please enter your last name.');
			lastName.focus();
		}
		else if(email.value == ''){
			alert('Please enter your email address.');
			email.focus();
		}
		else if(!isValidEmailAddress(email.value)){
			alert('Please enter a valid email address.');
			email.focus();
		}
		else{
      // Load Member Agreement Form
      showLoadingMessage('Loading Agreement...');
      new Ajax(ROOT_PATH + 'ajaxAction.php?ajaxAction=loadMemberAgreementForm', {
				method: 'post',
				data: theForm,
				update: $('agreementMemberDiv'),
				evalScripts: false,
				onComplete: function() {
					cancelMemberRegistration();
				}
			}).request();
		}
	}
}

/*===============================================================================*/
function registerMemberFromAgreement() {
  showLoadingMessage('Registering...');
  new Ajax(ROOT_PATH + 'ajaxAction.php?ajaxAction=registerMember', {
    method: 'post',
		data: theForm,
		update: $('agreementMemberDiv'),
		evalScripts: false,
		onComplete: function() {
		  cancelMemberRegistration();
		  ajaxResponse = this.response.text;
		  if(ajaxResponse.match("system")) {
        showLoadingMessage('Logging in...');
        window.location.href = ROOT_PATH + 'index.php';
      }
		}
  }).request();
}

/*===============================================================================*/
function returnToRegistrationPage() {
  if(confirm('Are you sure you want to cancel your registration?')) {
    window.location.href = 'index.php';
  }
}

/*===============================================================================*/
function cancelMemberRegistration() {
	hideLoadingMessage();
}

/*===============================================================================*/
function updateMemberProfile() {
	username = $('username').value;
	ID = $('MemberID').value;
	new Ajax(ROOT_PATH + 'ajaxAction.php', {
		method: 'post',
		data: 'ajaxAction=checkMemberName&memberName=' + username +
                                    '&MemberID=' + ID +
                                    '&isMemberEditor=true',
		onComplete: function() {
    	continueToUpdateMemberProfile(this.response.text);
  	},
		evalScripts: true
	}).request();
}
function continueToUpdateMemberProfile(usernameResponse) {
	theForm = $('memberProfileForm');
	theForm.ajaxAction.value = 'updateMemberProfile';

	with(theForm){
		if (username.value == ''){
			alert('Please enter a username.');
		}
		else if(usernameResponse == 'exists'){
			alert('Please select a different username.');
		}
		else if((password.value != '') && (password.value != confirmPassword.value)){
			alert('Password and confirm password fields do not match.');
		}
		else if(firstName.value == ''){
			alert('Please enter your first name.');
		}
		else if(lastName.value == ''){
			alert('Please enter your last name.');
		}
		else if(email.value == ''){
			alert('Please enter your email address.');
			email.focus();
		}
		else if(!isValidEmailAddress(email.value)){
			alert('Please enter a valid email address.');
			email.focus();
		}
		else{
			showLoadingMessage('Updating...');
			new Ajax(ROOT_PATH + 'ajaxAction.php', {
				method: 'post',
				data: theForm,
				update: $('updateMessage'),
				evalScripts: false,
				onComplete: function() {
					cancelMemberProfileUpdating();
				}
			}).request();
		}
	}
}

/*===============================================================================*/
function cancelMemberProfileUpdating() {
	hideLoadingMessage();
}

/*===============================================================================*/
