(function( $ ) {	
	
	$(document).ready(function(){
		
		$(window).keydown(function(e) {

			if(e.keyCode == 13) {
				e.preventDefault();
				return false;
			}

		});

		accp_validate_requried_admin_wizard_fields();		
		
		$(document).on('keyup', '.accp-wizard-input-field-required', function(){

			if ( 'text' === $(this).attr('type') ) {

				accp_validate_requried_admin_wizard_fields();

			}

		});

		$(document).on('change', '.accp-wizard-input-field-required', function(){			

			if ( true === $(this).is('select') ) {				

				accp_validate_requried_admin_wizard_fields();

			}

		});

		$('.accp-wizard-continue-button.accp-wizard-add-company-step-button a').on('click', function(e){

			accp_wizard_add_new_company(e);

		});

		$('.accp-wizard-continue-button.accp-wizard-add-company-home-page-step-button a').on('click', function(e){

			accp_wizard_add_company_home_page(e);

		});

		accp_hide_show_client_page_select();

		$(document).on('change', '#accp-enable-use-existing-client-page', function(){

			accp_hide_show_client_page_select();

		});

		accp_hide_show_create_new_primary_user_section();

		$(document).on('change', '#accp-wizard-create-new-user', function(){

			accp_hide_show_create_new_primary_user_section();

			accp_validate_requried_admin_wizard_fields();

		});

		accp_wizard_assign_existing_primary_user();

		accp_wizard_create_and_assign_new_primary_user();

		accp_wizard_generate_password_on_click();

		accp_generate_sample_content_on_click();

	}); // End document ready.


	function accp_validate_requried_admin_wizard_fields() {		

		var required_elem = $('.accp-wizard-input-field-required');

		if ( 0 === required_elem.length ) {
			return;
		}

		var continue_button   = $('.accp-wizard-continue-button a');

		if ( 0 === continue_button.length ) {
			return;
		}

		var all_required_filled = true;

		for ( i = 0; i < required_elem.length; i++ ) {

			var required_elem_val = $(required_elem[i]).val();

			if ( 0 === required_elem_val.length ) {

				var all_required_filled = false;
				break;

			}

		}

		if ( false === all_required_filled ) {

			$(continue_button).prop('disabled', true);
			$(continue_button).attr('disabled', 'disabled');

		} else {

			$(continue_button).prop('disabled', false);
			$(continue_button).removeAttr('disabled');

		}		
	}


	function accp_wizard_add_new_company(e){

		e.preventDefault();

		var target_elem      = e.target;
		var button_class_raw = $(target_elem).parent().attr('class');
		var button_class     = '.' + button_class_raw.replace(/[ ]/g, '.') + ' a';
		var spinner          = accp_get_spinner_html();							
		var company_name     = $('#accp-wizard-company-name-input').val();

		if ( 0 === company_name.length ) {
			return;
		}

		var nonce = $('input[name="accp_wizard_form_nonce"]').val();

		if ( 0 === nonce.length ) {
			return;
		}

		var company_status = '';

		if ( 0 !== $('#accp-wizard-company-status-select').length ) {

			var company_status = $('#accp-wizard-company-status-select').val();

		}

		$(target_elem).after(spinner);		
		$(target_elem).prop('disabled', true);
		$(target_elem).attr('disabled', 'disabled');		
		$('.accp-wizard-message').remove();
		$('.accp-wizard-error').remove();	
		
		$.ajax({
			url: ajaxurl,
			type: 'POST',
			//async: false,						
			data: {
				action: 'wizard_add_company_via_ajax',
				company_name: company_name,
				button_class: button_class,
				company_status: company_status,
				nonce: nonce
			},
			success: function(data){																	

				var parsed_data     = JSON.parse(data);
				var post_id         = parsed_data.post_id;
				var messages        = parsed_data.messages;
				var errors          = parsed_data.errors;
				var button_selector = parsed_data.button_selector;

				$(button_selector).prop('disabled', false);
				$(button_selector).removeAttr('disabled');
				$('.accp-wizard-spinner.spinner').remove();

				if ( 0 !== messages.length ) {

					for ( i = 0; i < messages.length; i++ ) {					

						var message_item = '<p class="accp-wizard-message">Message: ' + parsed_data.messages[i] + '</p>';

						$('.accp-wizard-button-container.accp-wizard-skip-setup-button-container').after(message_item);
					}

				}

				if ( 0 === errors.length ) {

					if ( 0 !== post_id ) {

						var href_raw = $(button_selector).attr('href');
						var new_href     = href_raw + '&company_id=' + post_id;

						$(button_selector).attr('href', new_href);

						window.location.href = new_href;

					} else {										

						var error_item = '<p class="accp-wizard-error">Error: The post ID was invalid please check your site and try again.</p>';

						$('.accp-wizard-button-container.accp-wizard-skip-setup-button-container').after(error_item);					

					}
				
				} else {

					for ( i = 0; i < errors.length; i++ ) {					

						var error_item = '<p class="accp-wizard-error">Error: ' + parsed_data.errors[i] + '</p>';

						$('.accp-wizard-button-container.accp-wizard-skip-setup-button-container').after(error_item);
					}

				}

			},
			error: function(jqXHR, textStatus, errorThrown){

				$('.accp-wizard-spinner.spinner').remove();
				$('.accp-wizard-continue-button.accp-wizard-add-company-step-button a').prop('disabled', false);
				$('.accp-wizard-continue-button.accp-wizard-add-company-step-button a').removeAttr('disabled');
			

				console.log(textStatus, errorThrown);
				console.log(jqXHR);
			},

		});
	}


	function accp_wizard_add_company_home_page(e){

		e.preventDefault();

		var target_elem      = e.target;
		var button_class_raw = $(target_elem).parent().attr('class');
		var button_class     = '.' + button_class_raw.replace(/[ ]/g, '.') + ' a';
		var spinner          = accp_get_spinner_html();							
		var company_id       = $(target_elem).attr('data-company-id');

		if ( 0 === company_id.length ) {

			var message_item = '<p class="accp-wizard-error">No company ID was found.  Please use your browser\'s back button to add a company, or skip this step to proceed without adding a company.</p>';

			$('.accp-wizard-button-container.accp-wizard-skip-setup-button-container').after(message_item);

			return;
		}

		var use_existing = false;

		if ( $('#accp-enable-use-existing-client-page').is(':checked') ) {
			
			var use_existing            = true;
			var use_existing_select_val = $('#accp_home_page').val();			

			if ( 0 !== use_existing_select_val.length ) {				

				var selected_option_selector = '#accp_home_page option[value="' + use_existing_select_val + '"]';
				var option_assignment        = $(selected_option_selector).attr('data-assignment');
				
				if ( 'assigned' === option_assignment ) {

					if (! confirm(
						'You have selected a page that is already assigned to another company.  If you proceed, you will change the company assignement of that page to the current company.  Would you like to continue?'
					) ) {

						$('.accp-wizard-spinner.spinner').remove();

						return;
					};

				}

			}

		}

		var page_title = $('#accp-client-page-title').val();

		if ( false === use_existing && 0 === page_title.length ) {

			var message_item = '<p class="accp-wizard-error">Please enter a name for the page.</p>';

			$('.accp-wizard-button-container.accp-wizard-skip-setup-button-container').after(message_item);

			return;

		}

		var existing_page_id = $('#accp_home_page').val();

		if ( true === use_existing && 0 === existing_page_id.length ) {

			var message_item = '<p class="accp-wizard-error">Please select a page to use as the home page.</p>';

			$('.accp-wizard-button-container.accp-wizard-skip-setup-button-container').after(message_item);

			return;

		}

		var make_global = '';

		if ( 0 !== $('#accp-enable-make-client-page-global').length ) {

			if ( $('#accp-enable-make-client-page-global').is(':checked') ) {

				var make_global = $('#accp-enable-make-client-page-global').val();

			}
		}

		var nonce = $('input[name="accp_wizard_form_nonce"]').val();

		if ( 0 === nonce.length ) {
			return;
		}

		$(target_elem).after(spinner);
		$(target_elem).prop('disabled', true);
		$(target_elem).attr('disabled', 'disabled');		
		$('.accp-wizard-message').remove();
		$('.accp-wizard-error').remove();
		
		$.ajax({
			url: ajaxurl,
			type: 'POST',
			//async: false,						
			data: {
				action: 'wizard_add_company_home_page_via_ajax',
				use_existing: use_existing,
				page_title: page_title,
				existing_page_id: existing_page_id,
				button_class: button_class,
				company_id: company_id,
				make_global: make_global,
				nonce: nonce
			},
			success: function(data){

				var parsed_data     = JSON.parse(data);
				var company_id      = parsed_data.company_id;
				var messages        = parsed_data.messages;
				var errors          = parsed_data.errors;
				var button_selector = parsed_data.button_selector;
				var page_id         = parsed_data.page_id;

				$(button_selector).prop('disabled', false);
				$(button_selector).removeAttr('disabled');
				$('.accp-wizard-spinner.spinner').remove();

				if ( 0 !== messages.length ) {

					for ( i = 0; i < messages.length; i++ ) {					

						var message_item = '<p class="accp-wizard-message">Message: ' + parsed_data.messages[i] + '</p>';

						$('.accp-wizard-button-container.accp-wizard-skip-setup-button-container').after(message_item);
					}

				}

				if ( 0 === errors.length ) {

					if ( 0 !== company_id ) {

						var href_raw    = $(button_selector).attr('href');
						var new_href    = href_raw + '&company_id=' + company_id + '&page_id=' + page_id;
						var skip_button = $('.accp-wizard-close-wizard-button a');

						$(button_selector).attr('href', new_href);
						$(skip_button).attr('href', href_raw + '&company_id=' + company_id );

						window.location.href = new_href;

					} else {										

						var error_item = '<p class="accp-wizard-error">Error: The company ID was invalid please check your site and try again.</p>';

						$('.accp-wizard-button-container.accp-wizard-skip-setup-button-container').after(error_item);					

					}
				
				} else {

					for ( i = 0; i < errors.length; i++ ) {					

						var error_item = '<p class="accp-wizard-error">Error: ' + parsed_data.errors[i] + '</p>';

						$('.accp-wizard-button-container.accp-wizard-skip-setup-button-container').after(error_item);
					}

				}

			},
			error: function(jqXHR, textStatus, errorThrown){

				$('.accp-wizard-spinner.spinner').remove();
				$('.accp-wizard-continue-button.accp-wizard-add-company-step-button a').prop('disabled', false);
				$('.accp-wizard-continue-button.accp-wizard-add-company-step-button a').removeAttr('disabled');
			

				console.log(textStatus, errorThrown);
				console.log(jqXHR);
			},

		});
	}


	function accp_wizard_assign_existing_primary_user() {

		$( '.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a' ).click( function (e) {
			
			e.preventDefault();

			if ( $('#accp-wizard-create-new-user').is(':checked') ) {
				return;
			}
			
			if ( 0 === $('#accp-wizard-company-primary-user-field').val().length ) {
				return;
			}

			var user_id = $('#accp-wizard-company-primary-user-field').val();
			var nonce = $('input[name="accp_wizard_form_nonce"]').val();

			if ( 0 === nonce.length ) {
				return;
			}
			
			var target_elem      = e.target;
			var button_class_raw = $(target_elem).parent().attr('class');
			var button_class     = '.' + button_class_raw.replace(/[ ]/g, '.') + ' a';
			var spinner          = accp_get_spinner_html();

			$(target_elem).after(spinner);
			$(target_elem).prop('disabled', true);
			$(target_elem).attr('disabled', 'disabled');		
			$('.accp-wizard-message').remove();
			$('.accp-wizard-error').remove();
								
			var company_id = $(target_elem).attr('data-company-id');

			if ( 0 === company_id.length ) {

				var message_item = '<p class="accp-wizard-error">No company ID was found.  Please use your browser\'s back button to add a company, or skip this step to proceed without adding a company.</p>';

				$('.accp-wizard-button-container.accp-wizard-skip-setup-button-container').after(message_item);

				return;
			}	

			$.ajax({
				type: 'POST',				
				url: ajaxurl,
				cache: false,
				headers: {
					'cache-control': 'no-cache',					
				},				
				data: { 
					action:'accp_assign_existing_primary_user',
					company_id: company_id,
					user_id: user_id,
					nonce: nonce																							
				},
				success: function(data){					
									
					var parsed_data   = JSON.parse(data);
					var user_id       = parsed_data.user_id;
					var username      = parsed_data.username;
					var company_id    = $('.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a').attr('data-company-id');
					var page_id       = '';
					var page_id_param = '';

					if ( 0 !==  $('.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a').attr('data-page-id').length ) {

						var page_id = $('.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a').attr('data-page-id');

					}

					if ( 0 !== page_id.length ) {

						var page_id_param = '&page_id=' + page_id;

					}

					$('.accp-wizard-spinner.spinner').remove();
					$('.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a').prop('disabled', false);
					$('.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a').removeAttr('disabled');

					var message_item = '<p class="accp-wizard-message">Message: The user was successfully assigned.</p>';

					$('.accp-wizard-button-container.accp-wizard-skip-setup-button-container').after(message_item);

					if ( 0 !== company_id.length ) {

						var href_raw    = $('.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a').attr('href');
						var new_href    = href_raw + '&company_id=' + company_id + page_id_param;
						var skip_button = $('.accp-wizard-close-wizard-button a');

						$('.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a').attr('href', new_href);
						$(skip_button).attr('href', href_raw + '&company_id=' + company_id + page_id_param);

						window.location.href = new_href;

					}									
					
				},
				error: function(jqXHR, textStatus, errorThrown){

					console.log(textStatus, errorThrown);
					console.log(jqXHR);

					$('.accp-wizard-spinner.spinner').remove();
					$('.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a').prop('disabled', false);
					$('.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a').removeAttr('disabled');

				},			
								
			});	
						
		});

	}


	function accp_wizard_create_and_assign_new_primary_user(){

		$( '.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a' ).click( function (e) {		
			
			e.preventDefault();

			if ( ! $('#accp-wizard-create-new-user').is(':checked') ) {
				return;
			}

			var nonce = $('input[name="accp_wizard_form_nonce"]').val();

			if ( 0 === nonce.length ) {
				return;
			}
			
			if( $('#accp-new-user-username').val() && $('#accp-new-user-email').val() && $('#accp-new-user-password').val() ){

				var username = $('#accp-new-user-username').val();
				var role = $('#accp-new-user-role').val();
				var email = $('#accp-new-user-email').val();
				var password = $('#accp-new-user-password').val();
				var firstname = $('#accp-new-user-firstname').val();
				var lastname = $('#accp-new-user-lastname').val();
				var send_email = $('#accp-send-user-notification').val();
				var company_id = $(this).attr('data-company-id');

				var target_elem      = e.target;
				var button_class_raw = $(target_elem).parent().attr('class');
				var button_class     = '.' + button_class_raw.replace(/[ ]/g, '.') + ' a';
				var spinner          = accp_get_spinner_html();

				$(target_elem).after(spinner);
				$(target_elem).prop('disabled', true);
				$(target_elem).attr('disabled', 'disabled');		
				$('.accp-wizard-message').remove();
				$('.accp-wizard-error').remove();

				if($('#accp-send-user-notification').is(':checked')){
					var send_email = 'send';
				}
				
				$.ajax({
					type: 'POST',				
					url: ajaxurl,
					cache: false,
					headers: {
						'cache-control': 'no-cache',					
					},				
					data: { 
						action:'accp_create_and_assign_primary_user',
						nonce: nonce,
						username: username,
						role: role,
						email: email,
						password: password,
						firstname: firstname,
						lastname: lastname,
						send_email: send_email,
						company_id: company_id

					},
					success: function(data){

						var parsed_data     = JSON.parse(data);
						var user_id         = parsed_data.user_id;
						var username        = parsed_data.username;
						var company_id      = $('.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a').attr('data-company-id');
						var status          = parsed_data.status;
						var error_messages  = parsed_data.error_message;
						var page_id       = '';
						var page_id_param = '';

						if ( 0 !==  $('.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a').attr('data-page-id').length ) {

							var page_id = $('.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a').attr('data-page-id');

						}

						if ( 0 !== page_id.length ) {

							var page_id_param = '&page_id=' + page_id;

						}

						if ( 'error' === status ) {

							if ( 0 !== error_messages.length ) {

								for ( i = 0; i < error_messages.length; i++ ) {

									var message_item = '<p class="accp-wizard-error">Error: ' + error_messages[i] + '</p>';

									$('.accp-wizard-button-container.accp-wizard-skip-setup-button-container').after(message_item);

								}

							} else {

								var message_item = '<p class="accp-wizard-error">Error: There was an error in your request.  Please check your data, refresh the page and try again.</p>';

								$('.accp-wizard-button-container.accp-wizard-skip-setup-button-container').after(message_item);

							}

							$('.accp-wizard-spinner.spinner').remove();
							$('.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a').prop('disabled', false);
							$('.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a').removeAttr('disabled');

							return;

						}


						$('.accp-wizard-spinner.spinner').remove();
						$('.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a').prop('disabled', false);
						$('.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a').removeAttr('disabled');

						var message_item = '<p class="accp-wizard-message">Message: The user was successfully assigned.</p>';

						$('.accp-wizard-button-container.accp-wizard-skip-setup-button-container').after(message_item);

						if ( 0 !== company_id.length ) {

							var href_raw = $('.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a').attr('href');
							var new_href     = href_raw + '&company_id=' + company_id + page_id_param;

							$('.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a').attr('href', new_href);
							$('.accp-wizard-close-wizard-button a').attr('href', new_href);							

							window.location.href = new_href;

						}						
						
					},
					error: function(jqXHR, textStatus, errorThrown){
						
						console.log(textStatus, errorThrown);
						console.log(jqXHR);					

						$('.accp-wizard-spinner.spinner').remove();
						$('.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a').prop('disabled', false);
						$('.accp-wizard-continue-button.accp-wizard-add-company-primary-user-step-button a').removeAttr('disabled');

					},			
									
				});
			
			}else{

				$('.accp-generate-user-message').text('Please fill out all required(*) fields.');

				var message_item = '<p class="accp-wizard-error">Please fill out all required(*) fields.</p>';

				$('.accp-wizard-button-container.accp-wizard-skip-setup-button-container').after(message_item);

			}
			
		});

	}


	function accp_wizard_generate_password_on_click(){

		$('#accp-wizard-autogenerate-password').click(function (e) {
			
			e.preventDefault();
			
			var nonce = $('input[name="accp_wizard_form_nonce"]').val();

			if ( 0 === nonce.length ) {
				return;
			}

			var spinner_elem = '<span class="accp-generate-pw-spinner spinner" style="float: none;"></span>';			
			
			$(spinner_elem).insertAfter(this);
			$('.accp-generate-pw-spinner.spinner').css('visibility', 'visible');
			$(this).css('pointer-events', 'none');

			$.ajax({
				type: 'POST',				
				url: ajaxurl,
				cache: false,
				headers: {
					'cache-control': 'no-cache',					
				},				
				data: { 
					action:'accp_generate_user_password',
					nonce: nonce																							
				},
				success: function(data){
					
					$('#accp-new-user-password').val(data);
					$('#accp-new-user-password').trigger('keyup');
					$('#accp-wizard-autogenerate-password').css('pointer-events', 'all');
					$('.accp-generate-pw-spinner.spinner').remove();					
					
				},
				error: function(jqXHR, textStatus, errorThrown){
					console.log(textStatus, errorThrown);
					console.log(jqXHR);
				},			
								
			});		
			
		});

	}


	function accp_generate_sample_content_on_click(){

		$('#accp-wizard-generate-default-content-button').click(function (e) {
			
			e.preventDefault();
			
			var nonce = $(this).attr('data-nonce');

			if ( 0 === nonce.length ) {
				return;
			}

			var spinner_elem = '<span class="accp-generate-pw-spinner spinner" style="float: none;"></span>';			
			
			$(spinner_elem).insertAfter(this);
			$('.accp-generate-pw-spinner.spinner').css('visibility', 'visible');
			$(this).css('pointer-events', 'none');

			$.ajax({
				type: 'POST',				
				url: ajaxurl,
				cache: false,
				headers: {
					'cache-control': 'no-cache',					
				},				
				data: { 
					action:'accp_generate_sample_content_ajax',
					nonce: nonce																							
				},
				success: function(data){

					var parsed_data     = JSON.parse(data);					
					var company_id      = parsed_data.company_id;
					var status          = parsed_data.status;
					var error_messages  = parsed_data.error_message;
					var page_id       = '';
					var page_id_param = '';

					$('.accp-generate-pw-spinner.spinner').remove();					

					if ( 'error' === status ) {

						if ( 0 !== error_messages.length ) {

							for ( i = 0; i < error_messages.length; i++ ) {

								var message_item = '<p class="accp-wizard-error">Error: ' + error_messages[i] + '</p>';

								$('#accp-wizard-generate-default-content-button').after(message_item);

							}

						} else {

							var message_item = '<p class="accp-wizard-error">Error: There was an error in your request.  Please check your data, refresh the page and try again.</p>';

							$('#accp-wizard-generate-default-content-button').after(message_item);

						}				

						return;

					}

					var message_item = '<p class="accp-wizard-message">Message: The content was successfully generated.</p>';

					$('#accp-wizard-generate-default-content-button').after(message_item);				
				},
				error: function(jqXHR, textStatus, errorThrown){

					$('.accp-generate-pw-spinner.spinner').remove();

					console.log(textStatus, errorThrown);
					console.log(jqXHR);
				},			
								
			});		
			
		});

	}


	function accp_get_spinner_html() {

		var spinner_elem = '<span class="accp-wizard-spinner spinner" style="float: none; visibility: visible;"></span>';

		return spinner_elem;
	}


	function accp_hide_show_client_page_select() {

		if ( 0 === $('#accp-enable-use-existing-client-page').length || 0 === $('#accp_home_page').length ) {
			return;
		}

		var enable_existing_client_page = $('#accp-enable-use-existing-client-page');
		var home_page_select            = $('#accp-wizard-home-page-select-container');

		if ( $( enable_existing_client_page ).is(':checked') ) {

			$(home_page_select).show('slow');
			$('#accp-client-page-title').attr('disabled', 'disabled');
			$('#accp-client-page-title').prop('disabled', true);
			accp_remove_requred_attribute('#accp-client-page-title');
			accp_add_requred_attribute('#accp_home_page');
			$('#accp-client-page-title').removeClass('accp-wizard-input-field-required');
			$('#accp_home_page').addClass('accp-wizard-input-field-required');

		} else {

			$(home_page_select).hide();
			$('#accp-client-page-title').removeAttr('disabled');
			$('#accp-client-page-title').prop('disabled', false);
			$('#accp_home_page').val(null).trigger('change');
			accp_add_requred_attribute('#accp-client-page-title');
			accp_remove_requred_attribute('#accp_home_page');
			$('#accp-client-page-title').addClass('accp-wizard-input-field-required');
			$('#accp_home_page').removeClass('accp-wizard-input-field-required');

		}

	}


	function accp_hide_show_create_new_primary_user_section() {

		if ( 0 === $('#accp-wizard-create-new-user').length || 0 === $('#accp-wizard-create-new-user-container').length ) {
			return;
		}

		var enable_existing_client_page = $('#accp-wizard-create-new-user');
		var new_user_section            = $('#accp-wizard-create-new-user-container');

		if ( $( enable_existing_client_page ).is(':checked') ) {

			$(new_user_section).show('slow');

			$('#accp-wizard-company-primary-user-field').attr('disabled', 'disabled');
			$('#accp-wizard-company-primary-user-field').prop('disabled', true);

			accp_remove_requred_attribute('#accp-wizard-company-primary-user-field');
			accp_add_requred_attribute('.accp-wizard-create-new-user-section-required');			

			$('#accp-wizard-company-primary-user-field').removeClass('accp-wizard-input-field-required');
			$('.accp-wizard-create-new-user-section-required').addClass('accp-wizard-input-field-required');

		} else {

			$(new_user_section).hide();

			$('#accp-wizard-company-primary-user-field').removeAttr('disabled');
			$('#accp-wizard-company-primary-user-field').prop('disabled', false);

			accp_remove_requred_attribute('.accp-wizard-create-new-user-section-required');
			accp_add_requred_attribute('#accp-wizard-company-primary-user-field');			

			$('#accp-wizard-company-primary-user-field').addClass('accp-wizard-input-field-required');
			$('.accp-wizard-create-new-user-section-required').removeClass('accp-wizard-input-field-required');

		}

	}


	function accp_remove_requred_attribute(selector) {

		if ( ! selector || 0 === selector.length ) {
			return;
		}
		
		$(selector).removeAttr('required');

	}


	function accp_add_requred_attribute(selector) {

		if ( ! selector || 0 === selector.length ) {
			return;
		}
		
		$(selector).attr('required', 'required');

	}

})( jQuery );
