// JavaScript Document
function initializeProfilePopup(){
	$('#profileSaveChangesButton').click(function(){
		processProfileSave();
	});
	
	$('#firstName').focus();
	
	$('#profileForm').submit(function(){
		return false;
	});
	
	// key press event...
	$('#profileForm').keypress(function(e){
		// Enter pressed submit form...
		if(e.keyCode==13) {
			processProfileSave();
		}
		$('#systemMessage').html('');
	});
	
	$('#profilePopupClose').html('x');
	
	$('#newPassword').keyup(function(){
		processNewPasswordKeyup(this);
	});
	
	$('#confirmPassword').keyup(function(){
		processPPConfirmPasswordKeyUp(this);
	});
};

function processNewPasswordKeyup(control){
	var requiredLength = 8;
	var errorMessage = '<span class="pp_redX"><img src="images/red_x.jpg" width="20"  /></span><span class="pp_passwordError">Must be at least ' + requiredLength + ' characters long.</span>';
	var okMessage = '<span class="pp_redX"><img src="images/check.png" width="20"  /></span><span class="pp_passwordError"></span>';
	var password = $(control).attr('value');
	if(password.length < requiredLength){
		$('#pp_passwordMessage').html(errorMessage);
	}
	if(password.length < 1) {
		$('#pp_passwordMessage').html('');
	}
	if(password.length >= requiredLength){
		$('#pp_passwordMessage').html(okMessage);
	}
	
	processPPConfirmPasswordKeyUp( $('#confirmPassword') );
}

function processPPConfirmPasswordKeyUp(control){
	var errorMessage = '<span class="pp_redX"><img src="images/red_x.jpg" width="20"  /></span><span class="pp_passwordError">Does not match password.</span>';
	var okMessage = '<span class="pp_redX"><img src="images/check.png" width="20"  /></span><span class="passwordError"></span>';
	var password = $('#newPassword').attr('value');
	var confirmPassword = $(control).attr('value');
	if(password != confirmPassword){
		$('#pp_confirmMessage').html(errorMessage);
	}
	else {
		$('#pp_confirmMessage').html('');
	}
	if(password.length > 0 && password == confirmPassword){
		$('#pp_confirmMessage').html(okMessage);
	}
}

function processProfileSave(){
	var parms = $('#profileForm').serialize();
	$('#profilePopup').jcPopup({
		refreshAfterClose: false,
		loadFrom: backToRoot + 'ajaxProfileMaint.php?' + parms,
		afterLoad: 'initializeProfilePopup()',
		processFlag: true
	});
}
