// Google+ button
	(function() {
		var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
			po.src = 'https://apis.google.com/js/plusone.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
	})();

// Form setup-------------------------------------------------------------------------------------------------------------
	// Error messages used in form validation
		var validationErrorMessage = {};
			validationErrorMessage['email'] = 'Invalid';	
			validationErrorMessage['phone'] = 'XXX-XXX-XXXX';
			validationErrorMessage['phone2'] = 'XXX-XXX-XXXX';
			validationErrorMessage['postal_code'] = 'Invalid';
			validationErrorMessage['opt_in'] = 'Required';
	
	ieSelectWidthFix();
	
	for ( i=0, ii = required_fields.length; i<ii; i++ )
	{ $('#'+required_fields[i]).attr({validation: 'required'}); }
	
	$('#btn_Next').replaceWith('<img id="btn_Next" src="_presentation/images/btn_Next.png" alt="Next" />');
	$('#btn_Submit').replaceWith('<img id="btn_Submit" src="_presentation/images/btn_Submit.png" alt="Next" />');
	
	$('#request_form').submit(
		function () 
		{ return false; }
	);
	
	$('#btn_Submit').click(
		function () 
		{
			var validForm = validate();
		
			_gaq.push(['_trackEvent', 'Forms', 'submit']);
		
			if ( validForm ) 
			{ 
				_gaq.push(['_trackPageview','/form/filled']);
				
				$('#request_form').unbind('submit').submit(); 
			}								 
		}
	);
	
/*	Additional Button Setup:
_____________________________________________________________________________*/

		$('#stepTwo').hide();

		$('#btn_Next').click(
			function () 
			{	
				var validStepOne = validate('stepOne');
				
				_gaq.push(['_trackEvent', 'Forms', 'next']);
				
				if ( validStepOne ) 
				{
					_gaq.push(['_trackPageview','/form/continue']);
					
					$('#stepOne').hide();
					$('#stepTwo').show();
				}
			}
		);


/*	Main Navigation Dropdown
_____________________________________________________________________________*/

	$('#programs_dropdown').hide();
	
	$('.main_dropdown').click(function() {
		$('#programs_dropdown').slideDown('slow');	
		$('#programs_dropdown').mouseleave(function() {									
			$(this).slideUp('slow');										  
		});
	});
	
	
	
/*	Sub Navigation Dropdowns
_____________________________________________________________________________*/
	
	$('#associates, #bachelors, #masters').hide();
	
	$('.associates_dropdown').click(function() {
		
		$('#bachelors, #masters').slideUp();
		
		$('#associates').slideDown('slow');
		$('#associates').mouseleave(function() {
			$(this).slideUp('slow');									 
		});
	});
	
	
	$('.bachelors_dropdown').click(function() {
		
		$('#associates, #masters').slideUp();
		
		$('#bachelors').slideDown('slow');
		$('#bachelors').mouseleave(function() {
			$(this).slideUp('slow');									 
		});
	});
	
	
	$('.masters_dropdown').click(function() {
		
		$('#associates, #bachelors').slideUp();
		
		$('#masters').slideDown('slow');
		$('#masters').mouseleave(function() {
			$(this).slideUp('slow');									 
		});
	});
	
	
	
/*	Sub Navigation 2 Dropdowns
_____________________________________________________________________________*/

	$('#bachelors2').hide();
	
	$('.bachelors_dropdown2').click(function() {
		$('#bachelors2').slideDown('slow');	
		$('#bachelors2').mouseleave(function() {									
			$(this).slideUp('slow');										  
		});
	});
	
	
	
/* Sub Navigation 4 Dropdowns
_____________________________________________________________________________*/

	$('#associates4, #bachelors4').hide();
	
	$('.bachelors_dropdown4').click(function() {
		
		$('#associates4').slideUp();
		
		$('#bachelors4').slideDown('slow');
		$('#bachelors4').mouseleave(function() {
			$(this).slideUp('slow');									 
		});
	});
	
	
	$('.associates_dropdown4').click(function() {
		
		$('#bachelors4').slideUp();
		
		$('#associates4').slideDown('slow');
		$('#associates4').mouseleave(function() {
			$(this).slideUp('slow');									 
		});
	});


/*	Sub Navigation 3 Dropdowns
_____________________________________________________________________________*/
	
	$('#bachelors3, #masters3').hide();

	$('.bachelors_dropdown3').click(function() {
		
		$('#masters3').slideUp();
		
		$('#bachelors3').slideDown('slow');
		$('#bachelors3').mouseleave(function() {
			$(this).slideUp('slow');									 
		});
	});
	
	
	$('.masters_dropdown3').click(function() {
		
		$('#bachelors3').slideUp();
		
		$('#masters3').slideDown('slow');
		$('#masters3').mouseleave(function() {
			$(this).slideUp('slow');									 
		});
	});

// EXPANDABLE PROGRAM SUMMARY SETUP ---------------------------------------

	$('.expandable').hide();
	$('#content h6').prepend('<span>+ </span>');
	
	$('#content h6').click(function () {
		var checkElement = $(this).next('div');	
				
		$('.expandable:visible').slideUp();	
		$('.expandable').prev().find('span').text('+ ');
				
		if (checkElement.is(':visible')) {
			checkElement.slideUp('normal');
		} else {
			checkElement.slideDown('normal');
			checkElement.prev().find('span').text('- ');
		}
		
		return false;
	});
	
// Dynamic Removal of Various States Dependent On Program Selection ----------------------//

	// CAPTURE STATE DROPDOWN ON PAGE LOAD
	stateHTML = $('#state').html();

	$('#category').change(function() {
		//RESET STATE DROPDOWN TO DEFAULT ON EACH CHANGE OF PROGRAM CHOICE
		$('#state').html(stateHTML);

		switch( $(this).val() ) {
			
			// PROGRAMS ALLOWED IN KENTUCKY
			case "AA/Business":
			case "AA/Military Studies":
			case "AA/Organizational Management":
			case "BA/Accounting":
			case "BA/Adult Development":
			case "BA/Applied Behavioral Science":
			case "BA/Applied Linguistics":
			case "BA/Business Administration":
			case "BA/Business Economics":
			case "BA/Business Information Systems":
			case "BA/Business Leadership":
			case "BA/Child Development":
			case "BA/Cognitive Studies":
			case "BA/Communication Studies":
			case "BA/Complementary and Alternative Health":
			case "BA/Consumer and Family Financial Services":
			case "BA/Cultural Anthropology":
			case "BA/eMarketing":
			case "BA/English":
			case "BA/English Language Learner Studies":
			case "BA/Entrepreneurship":
			case "BA/Environmental Studies":
			case "BA/Finance":
			case "BA/Gerontology":
			case "BA/Health and Human Services":
			case "BA/Health and Marketing Communication":
			case "BA/Health and Wellness":
			case "BA/Health Care Administration":
			case "BA/Health Care Studies":
			case "BA/Health Informatics":
			case "BA/History":
			case "BA/Homeland Security and Emergency Management":
			case "BA/Human Resources Management":
			case "BA/Instructional Design":
			case "BA/International Business":
			case "BA/Journalism and Mass Communication":
			case "BA/Law Enforcement Administration":
			case "BA/Liberal Arts":
			case "BA/Library Science and Media":
			case "BA/Military Studies":
			case "BA/Operations Management and Analysis":
			case "BA/Organizational Management":
			case "BA/Political Science and Government":
			case "BA/Project Management":
			case "BA/Psychology":
			case "BA/Public Administration":
			case "BA/Public Relations and Marketing":
			case "BA/Real Estate Studies":
			case "BA/Service Management":
			case "BA/Social and Criminal Justice":
			case "BA/Social Science":
			case "BA/Sociology":
			case "BA/Sports and Recreation Management":
			case "BA/Supply Chain Management":
			case "BA/Sustainable Enterprise Management":
			
				// REWRITE STATE DISCLAIMER TO REMOVE KENTUCKY
				$('.state_disclaimer').text('** Ashford University does not currently accept students in Arkansas, Minnesota, or Mississippi.');
				
				// REMOVE ARKANSAS, MINNESOTA, AND MISSISSIPPI FROM STATE DROPDOWN
				$('#state option').each(function () {
					switch ($(this).val()) {
						case "AR":
						case "MN":
						case "MS":
							$(this).remove();
						break;
					}
				});
			break;
			
			// PROGRAMS ALLOWED IN MINNESOTA
			case "MA/Education":
			case "MA/Teaching and Learning with Technology":
			
				// REWRITE STATE DISCLAIMER TO REMOVE MINNESOTA
				$('.state_disclaimer').text('** Ashford University does not currently accept students in Arkansas, Kentucky, or Mississippi.');
			
				// REMOVE ARKANSAS, KENTUCKY, AND MISSISSIPPI FROM STATE DROPDOWN
				$('#state option').each(function () {
					switch ($(this).val()) {
						case "AR":
						case "KY":
						case "MS":
							$(this).remove();
						break;
					}
				});
			break;
			
			// PROGRAMS ALLOWED IN ARKANSAS **AND** MINNESOTA
			case "MA/Organizational Management":
			case "Master of Public Administration":
			
				// REWRITE STATE DISCLAIMER TO REMOVE ARKANSAS AND MINNESOTA
				$('.state_disclaimer').text('** Ashford University does not currently accept students in Kentucky or Mississippi.');
				
				// REMOVE KENTUCKY AND MISSISSIPPI FROM STATE DROPDOWN
				$('#state option').each(function () {
					switch ($(this).val()) {
						case "KY":
						case "MS":
							$(this).remove();
						break;
					}
				});
			break;
			
			// PROGRAMS ALLOWED IN ARKANSAS **AND** MINNESOTA **AND** KENTUCKY
			case "MA/Health Care Administration":
			case "MBA":
			
				// REWRITE STATE DISCLAIMER TO REMOVE ARKANSAS, MINNESOTA, AND KENTUCKY
				$('.state_disclaimer').text('** Ashford University does not currently accept students in Mississippi.');
				
				// REMOVE MISSISSIPI FROM STATE DROPDOWN
				$('#state option').each(function () {
					if ($(this).val() == 'MS') {
						$(this).remove();
					}
				}); 
			break;
			
			default:
				// RESET STATE DROPDOWN TO DEFAULT
				$('#state').html(stateHTML);
				
				// REWRITE STATE DISCLAIMER TO DEFAULT TEXT
				$('.state_disclaimer').text('** Ashford University does not currently accept students in Arkansas, Kentucky, Minnesota, or Mississippi.');
				
				// REMOVE ARKANSAS, KENTUCKY, MINNESOTA, AND MISSISSIPPI FROM STATE DROPDOWN
				$('#state option').each(function () {
					switch ($(this).val()) {
						case "AR":
						case "KY":
						case "MN":
						case "MS":
							$(this).remove();
						break;
					}
				});
			break;
		}
	});

	
/*	Form Conditional Functions
_____________________________________________________________________________*/
	
	function qualifyCheck () 
		{
			var eduLevelCheck;
			
			$('#category').find('option').remove().end();
			$('#category').append('<option value="">Loading&hellip;</option>').attr({disabled: 'disabled'});
			
			$('#program_code').find('option').remove().end();
			$('#program_code').append('<option value="">Choose a Program</option>');
			$('#program_code').attr({disabled: 'disabled'});
			
			switch ( education_level = $('#education_level_code').val() ) 
			{
				case "HSGED":
					eduLevelCheck = RegExp(/^(AA\/|BA\/)/);
				break;
				
				case "SC30":
					eduLevelCheck = RegExp(/^(AA\/|BA\/)/);
				break; 
				
				case "SC60":
					eduLevelCheck = RegExp(/^(AA\/|BA\/)/);
				break; 
				
				case "SCASSOC":
					eduLevelCheck = RegExp(/^(BA\/)/);
				break; 
				
				case "BACH":
					eduLevelCheck = RegExp(/^(MA\/|MBA|Master)/);
				break; 
				
				case "MAST":
				case "DOCT":
					eduLevelCheck = RegExp(/^(MA\/|MBA|Master)/);		
				break;
			}
			
			$.getJSON('/global/cdm-server.php?path=http://ulm.datamark.com/cdm/clients/BRI0006/programs?order_asc=ProgramName', function (json) {
					for (var i=0, ii = json.body.length; i<ii; i++) 
					{
						var currentProgram = json.body[i].Name;
					
						if ( currentProgram.search(eduLevelCheck) == 0 && ($('#category option[value='+currentProgram+']').length == 0) ) 
						{ $('#category').append('<option value="'+currentProgram+'">'+currentProgram+'</option>'); }
					}
					
					$('#category option:first').text('-- Select --');
					$('#category').removeAttr('disabled');
			});
			
			$('#category').change( function () {				
				if ($('#category').val() == "") 
				{
					$('#program_code').find('option').remove().end();
					$('#program_code').append('<option value="">Choose an Area of Interest</option>');
					$('#program_code').attr({disabled: 'disabled'});
				} 
				else 
				{
					$('#program_code').find('option').remove().end();
					$('#program_code').append('<option value="">Loading&hellip;</option>');
					$('#program_code').attr({disabled: 'disabled'});
				
					programCheck = RegExp($('#category').val());
				
					$.getJSON('/global/cdm-server.php?path=http://ulm.datamark.com/cdm/clients/BRI0006/programs?order_asc=Emphasis', function (json) {
						for (var i=0, ii=json.body.length ; i<ii; i++) 
						{
							var currentProgram = {
								'Name': json.body[i].Name,
								'ProgramCode': json.body[i].ProgramCode,
								'Emphasis': json.body[i].Emphasis
							};
				
							if ( (currentProgram['Name'].search(programCheck) == 0) && ($('#program_code option[value='+currentProgram['ProgramCode']+']').length == 0) ) 
							{
								if ( currentProgram['Emphasis'] ) 
								{ $('#program_code').append('<option value="'+currentProgram['ProgramCode']+'">'+currentProgram['Emphasis']+'</option>'); }	
								else 
								{ 
									var message = "";
									var programName = currentProgram['Name'];
									
									switch ( true )
									{
										case programName.search(/^AA\//) != -1:
										case programName.search(/^BA\//) != -1:
											message = "No Concentration";
										break;
										
										case programName.search(/^(MA\/|MBA|Master)/) != -1:
											message = "No Specialization";
										break;
									}
																																				
									$('#program_code').append('<option value="'+currentProgram['ProgramCode']+'" selected="selected">'+message+'</option>'); 
								}
				
								$('#program_code option:first').text('-- Select --');
								$('#program_code').removeAttr('disabled').attr({validation: 'required'});
							}
						}
					});
				}
			});
		}
			
					
		$('#category option:first').html('Choose an Education Level');
		$('#program_code option:first').html('Choose an Area of Interest');
		$('#category, #program_code').attr({disabled: 'disabled'});
		
		$('#education_level_code').change(function () { qualifyCheck(); });
		
		if ( $('#education_level_code').val() != "" ) { $('#education_level_code').trigger('change'); }
	
	

// IE6 Warning----------------------------------------------------------------------------------------------------------
	if ( $('#rootIE6').length ) 
	{						
		$('body').prepend('<div id="IE6warning"><p>For the best experience using this site, please upgrade to a more modern web browser.</p></div>');
		
		$('#IE6warning').css({
			background: "#feefda",
			border: "1px solid #f7941d",
			borderTop: "none",
			display: "none",
			font: "bold 12px arial, sans-serif",
			padding: "5px",
			textAlign: "center",
			width: '100%'
		}).slideDown("slow"); 
	}

// IE select width fix--------------------------------------------------------------------------------------------------
	function ieSelectWidthFix ()
	{
		if ( $('#rootIE').length )
		{
			$('select').each(
				function () 
				{ $(this).data("originalWidth", $(this).css('width')); }
			);
			
			$('select').mouseenter(
				function () 
				{
					$(this).css('width', "auto");
					$(this).data("resizedWidth", $(this).attr('offsetWidth'));
			
					if ( parseInt($(this).data("resizedWidth")) < parseInt($(this).data("originalWidth").replace(/px/, "")) ) 
					{ $(this).css('width', $(this).data("originalWidth")); } 
					else 
					{ $(this).addClass('expanded'); }
				}
			);
			
			$('select').change(
				function () {
					$(this).css('width', $(this).data("originalWidth"));
					$(this).removeClass('expanded');
				}
			);
			
			$(':input').focus(
				function () 
				{
					if ( $(this).attr('class') != 'expanded' ) 
					{
						$('.expanded').each(
							function () 
							{
								$(this).css('width', $(this).data("originalWidth"));
								$(this).removeClass('expanded');
							}
						);
					}
				}
			);
		}
	}

// Validation checks--------------------------------------------------------------------------------------------------------  
	function isRequired( formField ) 
	{
		switch ( $(formField).attr('type') ) 
		{
			case 'text':
			case 'textarea':
			case 'select-one':
				if ( $(formField).val() ) 
				{ return true; }
			return false;
		}
	}
	
	function isPattern ( formField, pattern ) 
	{
		var regExp = new RegExp("^" + pattern + "$");
		var correct = regExp.test($(formField).val());
	
		return correct;
	}
	
	function isValidEmail ( formField ) 
	{ return isPattern(formField, "[a-zA-Z0-9._+%-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}"); }
	
	function isValidPhone ( formField )
	{ return isPattern(formField, "[0-9]{3}[ .-]?[0-9]{3}[ .-]?[0-9]{4}"); }
	
	function isValidZip ( formField )
	{ return isPattern(formField, "[0-9]{5}(-[0-9]{4})?"); }
	
	function isChecked(formField) {
		return $(formField).attr('checked');
	}
	
// Form validation---------------------------------------------------------------------------------------------------------
	function removeError() 
	{
		if ( !$(this).data('errorMessage') ) return;
	
		$(this).removeClass('errorMessage');	
		$(this).parent().find('label.errorMessage, span.errorMessage').remove();
		$(this).removeData('errorMessage');
	}
	
	function validate ( step ) 
	{
		var validForm = true;
	
		if ( step == "stepOne" ) 
		{ var formFields = $('#stepOne :input'); } 
		else 
		{ var formFields = $(':input'); }
	
		for ( var i = 0, ii = formFields.length; i < ii; i++ ) 
		{
			var validation = $(formFields[i]).attr('validation');
			var fieldID = $(formFields[i]).attr('id');
			var OK, requiredFirst = true;
	
			if ( !validation ) 
			{
				switch ( fieldID ) 
				{
					case "email":
					case "phone":
					case "phone2":
					case "postal_code":
						if ( $(formFields[i]).val() == "" ) continue;
					break;
	
					default: 
						continue; 
					break;
				}
			}
	
			switch ( fieldID ) 
			{
				case "email":
					OK = isRequired(formFields[i]);
				
					if ( OK ) 
					{ 
						OK = isValidEmail(formFields[i]);
						requiredFirst = false;
					}
				break;
				
				case "phone":
				case "phone2":
					OK = isRequired(formFields[i]);
					
					if ( OK ) 
					{ 
						OK = isValidPhone(formFields[i]);
						requiredFirst = false;
					}
				break;
				
				case "postal_code":
					OK = isRequired(formFields[i]);
	
					if ( OK ) 
					{
						OK = isValidZip(formFields[i]);	
						requiredFirst = false;					
					}
				break;
				
				case "opt_in":
					OK = isChecked(formFields[i]);
					
					requiredFirst = false;
				break;
				
				default:
					OK = isRequired(formFields[i]);
				break;
			}
	
			if ( !OK ) 
			{
				var errorMessage = "Required"; 
	
				if ( !requiredFirst ) 
				{ errorMessage =  validationErrorMessage[fieldID] || ""; }
	
				writeError(formFields[i], errorMessage);
	
				validForm = false;
			}
		}
	
		return validForm;
	}
	
	function writeError ( formField, message )
	{
		var fieldID = $(formField).attr('id');
		var fieldWidth = $(formField).attr('offsetWidth');
		var fieldHeight = $(formField).attr('offsetHeight');
		
		_gaq.push(['_trackEvent', 'Forms', 'error-'+fieldID]);
		
		if(fieldID == 'opt_in') {
			fieldWidth = '303';
			fieldHeight = '20';
			
			if ( $('span.errorMessage', $(formField).parent()).length == 0 )
			{
				$(formField).parent().prepend('<span style="width:'+fieldWidth+'px; height: '+fieldHeight+'px;" class="errorMessage">'+message+'</span>');
			}
			
			$(formField).attr('checked', 'unchecked');
			$(formField).attr('checked', false);
		}
		else
		{ $(formField).parent().append('<label style="width:'+fieldWidth+'px; height: '+fieldHeight+'px;" class="errorMessage" for="'+fieldID+'">'+message+'</label>'); }
		
		$(formField).addClass('errorMessage');
		
		$(formField).click(removeError);
		$(formField).focus(removeError);
		
		if ( $(formField).data('errorMessage') ) return;
		
		$(formField).data('errorMessage', message);
	}


/* Combobox
---------------------*/
(function( $ ) {
	$.widget( "ui.combobox", {
		_create: function() {
			var self = this,
				select = this.element.hide(),
				selected = select.children( ":selected" ),
				value = selected.val() ? selected.text() : "";
			var input = this.input = $( "<input>" )
				.insertAfter( select )
				.val( value )
				.autocomplete({
					delay: 0,
					minLength: 0,
					source: function( request, response ) {
						var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
						response( select.children( "option" ).map(function() {
							var text = $( this ).text();
							if ( this.value && ( !request.term || matcher.test(text) ) )
								return {
									label: text.replace(
										new RegExp(
											"(?![^&;]+;)(?!<[^<>]*)(" +
											$.ui.autocomplete.escapeRegex(request.term) +
											")(?![^<>]*>)(?![^&;]+;)", "gi"
										), "<strong>$1</strong>" ),
									value: text,
									option: this
								};
						}) );
					},
					select: function( event, ui ) {
						ui.item.option.selected = true;
						self._trigger( "selected", event, {
							item: ui.item.option
						});
					},
					change: function( event, ui ) {
						if ( !ui.item ) {
							var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
								valid = false;
							select.children( "option" ).each(function() {
								if ( $( this ).text().match( matcher ) ) {
									this.selected = valid = true;
									return false;
								}
								
							
							});
							if ( !valid ) {
								// remove invalid value, as it didn't match anything
								//$( this ).val( "" );
								select.append('<option selected="selected" id="not_in_list" value="'+$(this).val()+'">'+$(this).val()+'</option>');
								//input.data( "autocomplete" ).term = "";
								//return false;
							}
						}
					}
				})
				.addClass( "ui-widget ui-widget-content ui-corner-left" );

			input.data( "autocomplete" )._renderItem = function( ul, item ) {
				return $( "<li></li>" )
					.data( "item.autocomplete", item )
					.append( "<a>" + item.label + "</a>" )
					.appendTo( ul );
			};

			this.button = $( "<button type='button'>&nbsp;</button>" )
				.attr( "tabIndex", -1 )
				.attr( "title", "Show All Items" )
				.insertAfter( input )
				.button({
					icons: {
						primary: "ui-icon-triangle-1-s"
					},
					text: false
				})
				.removeClass( "ui-corner-all" )
				.addClass( "ui-corner-right ui-button-icon" )
				.click(function() {
					// close if already visible
					if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
						input.autocomplete( "close" );
						return;
					}

					// work around a bug (likely same cause as #5265)
					$( this ).blur();

					// pass empty string as value to search for, displaying all results
					input.autocomplete( "search", "" );
					input.focus();
				});
		},

		destroy: function() {
			this.input.remove();
			this.button.remove();
			this.element.show();
			$.Widget.prototype.destroy.call( this );
		}
	});
})( jQuery );

$(function() {
	$( "#employer" ).combobox();
});
