// Page setup-------------------------------------------------------------------------------------------------------------	

	var page = $('body').attr('id');
	
	$('#main_navigation option.' + page).attr('selected', 'selected');

	$('#main_navigation').change(function() {
		switch ($(this).val()) {
			case "Master of Business Administration":
				document.location.href = "master_business_administration.php";
			break;
			case "MBA - Health Care Management Track":
				document.location.href = "mba_healthcare_management.php";
			break;
			case "Master of Science in Interactive Media":
				document.location.href = "interactive_media.php";
			break;
			case "Master of Science in Information Technology":
				document.location.href = "information_technology.php";
			break;
			case "MS in Organizational Leadership":
				document.location.href = "organizational_leadership.php";
			break;
			case "MSOL – Human Resource Leadership Track":
				document.location.href = "human_resources.php";
			break;
			case "MSOL – Public Service / Nonprofit Leadership Track":
				document.location.href = "public_service.php";
			break;
			case "MSOL – Strategic Leadership Track":
				document.location.href = "strategic_leadership.php";
			break;
			case "Post Prof. Master in Occupational Therapy":
				document.location.href = "occupational_therapy.php";
			break;
			case "MS in Teacher Leadership":
				document.location.href = "teacher_leadership.php";
			break;
		}
		$('#main_navigation').val(this);
	});

// 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';
	
	ieSelectWidthFix();
	
	for ( i=0, ii = required_fields.length; i<ii; i++ )
	{ $('#'+required_fields[i]).attr({validation: 'required'}); }
	
	$('#btn_Submit').replaceWith('<img id="btn_Submit" alt="Submit" src="_presentation/images/btn_Submit.png" />');
	
	$('#request_form').submit(
		function () 
		{ return false; }
	);
	
	$('#btn_Submit').click(
		function () 
		{
			var validForm = validate();
		
			if ( validForm ) 
			{ 
				_gaq.push(['_trackEvent', 'Forms', 'submit']);
				_gaq.push(['_trackPageview','/form/filled']);
				$('#request_form').unbind('submit').submit(); 
			}								 
		}
	);
	
/*	Additional Button Setup:
_____________________________________________________________________________*/
		$('#stepTwo').hide();
		
		$('#btn_Next').click(
			function () 
			{
				var validStepOne = validate('stepOne');
	
				if ( validStepOne ) 
				{
					_gaq.push(['_trackEvent', 'Forms', 'next']);
					_gaq.push(['_trackPageview','/form/continue']);
					$('#stepOne').hide();
					$('#stepTwo').show();
				}
			}
		);
	
	
/*  Narrow programs by campus selection
_____________________________________________________________________________
	$('#program_code').empty();
	$('#program_code').append('<option value="">-- Select a campus first --</option>');
	
	$('#campus_code').change(
		function () 
		{
			if ( $(this).val() == "" ) 
			{
				$('#program_code').empty();
				$('#program_code').append('<option value="">-- Select a campus first --</option>');
			} 
			else
			{
				$('#program_code option:first').text('. . .loading. . .');
			
				$('#program_code').removeClass('errorMessage');	
				$('#program_code').next('label').remove();
				$('#program_code').removeData('errorMessage');
			
				$.getJSON(
					'/global/cdm-server.php?path=http://ulm.datamark.com/cdm/campuses/' + $(this).val() + '/programs?order_asc=ProgramName', 
					function (json) 
					{
						$('#program_code').empty();
						$('#program_code').append('<option value="">-- Select a program --</option>');
			
						for (var i=0; i<json.body.length; i++) 
						{ $('#program_code').append('<option value="'+json.body[i].ProgramCode+'">'+json.body[i].DefaultName+'</option>'); }
					}
				);							   
			}
		}
	);
*/

// 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})?$"); }
	
// Form validation---------------------------------------------------------------------------------------------------------
	function removeError () 
	{
		if ( !$(this).data('errorMessage') ) return;
	
		$(this).removeClass('errorMessage');	
		$(this).parent().find('label.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;
				
				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');
		
		$(formField).addClass('errorMessage');
		
		$(formField).focus(removeError);
		
		if ( $(formField).data('errorMessage') ) return;
		
		$(formField).parent().append('<label style="width:'+fieldWidth+'px; height: '+fieldHeight+'px;" class="errorMessage" for="'+fieldID+'">'+message+'</label>');
		
		$(formField).data('errorMessage', message);
	}
	
	
	
	
	//filtering script
		var allOptions = $('#program_code').html();
		var currentPrograms;
		
		$('#gpa, #program_code').attr({disabled:"disabled"});	
			
		$('#education_level_code').change(
			function ()
			{
				$('#program_code').html(allOptions);
				$('#gpa').val("");
				$('#gpa, #program_code').attr({disabled:"disabled"});
				
				switch ( $(this).val() )
				{
					case "":
						$('#program_code').html(allOptions);
						$('#gpa').val("");
						$('#gpa, #program_code').attr({disabled:"disabled"});
					break;
				
					case "HS":
					case "SC1":
					case "SC2":
						$('#gpa').val("");
						$('#program_code').empty();
						$('#program_code').append('<option value="">No available programs</option>');
						$('#program_code, #gpa').removeAttr('disabled');
					break;
					
					case "BACHOT":
						$('#program_code').empty();
						$('#program_code').append('<option value="">-- select --</option><option value="PPMOT">Post Professional Master in Occupational Therapy</option>');
						$('#gpa').removeAttr('disabled');
					break;
					
					default:
						$('#gpa').val("");
						$('#gpa').removeAttr('disabled');
					break;
				}
				
				currentPrograms = $('#program_code').html();
			}
		);
			
		$('#gpa').change(
			function ()
			{
				switch ( $(this).val() )
				{
					case "":
						$('#program_code').html(currentPrograms);
						$('#program_code').attr({disabled:"disabled"});
					break;
					
					case "Below 2.49":
					case "2.5 to 2.67":
						$('#program_code').empty();
						$('#program_code').append('<option value="">No available programs</option>');
						$('#program_code').removeAttr('disabled');
					break;
					
					case "2.68 to 2.74":
						$('#program_code').html(currentPrograms);
						$('#program_code option').each(
							function ()
							{
								switch ( $(this).val() )
								{
									case "MBA":
									case "MBAHCM":
										$(this).remove();
									break;
									
									default:
										// Leave programs as they meet requirements
									break;
								}
							}
						);
						$('#program_code').removeAttr('disabled');
					break;
					
					case "2.75 to 2.99":
					case "3.0 or higher":
						$('#program_code').html(currentPrograms);
						$('#program_code').removeAttr('disabled');
					break;
				}
			}
		);
