function getQualifiedName(formElement) {

	var formElementName;
	
	if (formElement.name) {
		formElementName = formElement.name;
	} 
	else {
		formElementName = formElement.id;
	}
   
	return 'document.forms["' + formElement.form.id + '"].elements["' + formElementName + '"]';
}

function validateField(formElement, required, validationRule, args) {
	// assume successful validation
	validationError = false;

	// first check if we have a required field
	if (required == 1) {
		if ($(formElement).val().length < 1) {
			validationError = true;
		}
	}
	
	// if not then check the additional rule
	switch (validationRule) {
		
		case 'selectedOption':
			if (!$(formElement).val()) {
				validationError = true;
			}
		break;
			
		case 'selectedDate':
			if (!$(formElement).val()) {
				validationError = true;
			}
		break;
			
		case 'maxLength':
			if ($(formElement).val().length > args) {
				validationError = true;
			}
		break;
			
		case 'minLength':
			if ($(formElement).val().length < args && args == 1) {
				validationError = true;
			}
		break;
			
		case 'minMaxLength':
			var argsArray = args.split(","); 
			if (($(formElement).val().length < parseInt(argsArray[0])) || 
			    ($(formElement).val().length > parseInt(argsArray[1]))) {
				validationError = true;
			}
		break;
			
		case 'password':
			// allow for no change of password
			if ($(formElement).val().length < 1) {
				break;
			}
			if (($(formElement).val().length < 6) || 
			    ($(formElement).val().length > 16)) {
				validationError = true;
			}
		break;
			
		case 'compare':
			if ($(formElement).val() != $('#' + args).val()) {
				validationError = true;
			}			
		break;
			
		default:
		break;
	}
	
	if (validationError) {
		toggleValidationError(formElement, true);
	} 
	else {
		toggleValidationError(formElement, false);
	}
}

function toggleValidationError(formElement, showError) {
	var id = $(formElement).attr('id');
	var errorDiv = id + 'Error';
	var iconDiv  = id + 'Required';
	
	if($(formElement).attr('id').lastIndexOf('_') != -1) {
		errorDiv = id.substring(0, id.length-2) + 'Error';
		iconDiv = id.substring(0, id.length-2) + 'Required';
	}

	if (showError == true) {
		$('#' + errorDiv).removeClass('displayNone').addClass('activeError');
		if ($('#' + iconDiv)) {
			$('#' + iconDiv).addClass('highlightRequired');
		}
	}
	else {
		if (showError == false) {
			$('#' + errorDiv).addClass('displayNone').removeClass('activeError');
			if ($('#' + iconDiv)) {
				$('#' + iconDiv).removeClass('highlightRequired');
			}
		}
	}
}

$(document).ready(function() {
	// hide comment box if comment is empty
	if ($('#attitudeReason').val() == '') {
		$('#attitudeReasonBox').addClass('displayNone');
	}
	
	$('#next').click(function(e) {
		var form = $('form.registerform');
		var errors = $('.activeError');
		if (errors.length) {
			//console.log(errors.eq(0).prev().attr('id'));
			errors.eq(0).prev().focus();
			if (e && e.preventDefault()) {
				e.preventDefault();
			}
			return false;
		}
		else {
			form.submit;
		}
	});
});