$('head').append('<link rel="stylesheet" type="text/css" href="_presentation/screen_js.css" />');
		
$(function () {
	// IE6 Warning-------------------------------------------------------------------------------
		if ($('#rootIE6').html() != null) {						
			$('body').append('<div id="IEwarning"><p>You are using an outdated browser. For the best experience using this site, please upgrade to a modern web browser.</p></div>');
			
			$('#IEwarning').css({
				border: "1px solid #f7941d",
				borderTop: "none",
				background: "#feefda",
				textAlign: "center",
				position: "absolute",
				top: 0,
				left: 0,
				zIndex: 100,
				display: "none",
				font: "bold 12px arial, sans-serif",
				padding: "3px",
				width: $('body').width()
			}).slideDown("slow");
		}
	// END IE6 Warning---------------------------------------------------------------------------
	
	
	
	
	// Error messages used in form validation-------------------------------------------------------------------------------
		var validationErrorMessage = {};
			validationErrorMessage['email'] = 'Invalid email address';	
			validationErrorMessage['phone'] = 'XXX-XXX-XXXX';
			validationErrorMessage['phone2'] = 'XXX-XXX-XXXX';
			validationErrorMessage['postal_code'] = 'Invalid postal code';
	// END Error messages used in form validation---------------------------------------------------------------------------
	
	
	
	
	// IE select width fix--------------------------------------------------------------------------------------------------
		function ieSelectWidthFix() {
			if (jQuery.browser.msie) {
				$('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');
						});
					}
				});
			}
		}
	// END IE select width fix----------------------------------------------------------------------------------------------
	
	
	
	
	// 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 isValidPostalCode(formField) { return isPattern(formField, "^[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]{1}[0-9]{1}[A-Za-z]{1} *[0-9]{1}[A-Za-z]{1}[0-9]{1}$"); }
		
		function radioCheck(groupName) {
			var isChecked = false;
			
			for (var i=0; i<document.lead_data [groupName].length; i++) {
				if (document.lead_data [groupName][i].checked) {
					isChecked = true;
				}
			}
			
			return isChecked;
		}
	// END Validation checks----------------------------------------------------------------------------------------------------
	
	
	
	
	// 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; i < formFields.length; i++ ) {
				var validation = $(formFields[i]).attr('validation');
				var fieldID = $(formFields[i]).attr('id');
				var OK, requiredFirst = true;
				
				if ( !validation ) {
					switch ( fieldID ) {
						case "phone":
						case "phone2":
						case "email":
							if ( $(formFields[i]).val() == "" ) continue;
						break;
						
						default:
							continue;
						break;
					}
				}
				
				switch ( fieldID ) {
					case "postal_code":
						OK = isRequired(formFields[i]);
						
						if ( OK ) { 
							if ( $('#country').length ) {
								switch ( $('#country').val() ) {
									case "Canada":
										OK = isValidPostalCode(formFields[i]);
									break;
									
									case "USA":
										OK = isValidZip(formFields[i]);
									break;
								}
							} else {
								OK = isValidZip(formFields[i]);	
							}
							
							requiredFirst = false;					
						}
					break;
						
					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;
					
					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')-2;
			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+'" htmlFor="'+fieldID+'">'+message+'</label>');
			
			$(formField).data('errorMessage', message);
		}
	// END Form validation-----------------------------------------------------------------------------------------------------
	
	
	
	
	// Page setup-------------------------------------------------------------------------------------------------------------	
		$('#contentArea h2').click(function () {
			var checkElement = $(this).next();
			
			$('.expandable:visible').slideUp();
					
			if (checkElement.is(':visible')) {
				checkElement.slideUp();
			} else {
				checkElement.slideDown();
			}									 
		});
		
		$('#formArea li').hover(
			function () { if ( $(this).attr('id') != "currentPage" ) { $(this).css({backgroundImage: 'url("_presentation/images/background_nav-hover.png")'}); } },
			function () { if ( $(this).attr('id') != "currentPage" ) { $(this).css({backgroundImage: 'url("_presentation/images/background_nav.png")'}); } }
		);
		
		var currentPage = location.href;
			currentPage = currentPage.split('/');
			currentPage = currentPage[currentPage.length - 1];
			
		$('#formArea li a').each(function () {
			if ( $(this).attr('href') == currentPage ) {
				$(this).parent().attr({id: "currentPage"}).css({backgroundImage: 'url("_presentation/images/background_nav-selected.png")'});;	
			}
		});
	// END Page setup---------------------------------------------------------------------------------------------------------
	
	
	
	// Form setup-------------------------------------------------------------------------------------------------------------
		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_code').val() ) 
			{
				case "ASSOC":
					eduLevelCheck = RegExp(/^BA\//);		
				break;
				
				case "BACH":
				case "MAST":
				case "DOCT":
					eduLevelCheck = RegExp(/^(MA\/|MBA|Master)/);		
				break;
				
				default:
					eduLevelCheck = RegExp(/^AA\//);	
				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 a Program</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 a Program');
		$('#category, #program_code').attr({disabled: 'disabled'});
		
		$('#education_level_code').change(function () { qualifyCheck(); });
		
		if ( $('#education_level_code').val() != "" ) { $('#education_level_code').trigger('change'); }
		
		$('#btn_Next').replaceWith('<img id="btn_Next" src="_presentation/images/btn_Next.png" alt="Go To Step 2" title="" />');
		$('#btn_Next').click(function () {
			var validForm = validate();
				
			if ( validForm ) {
				$('#requestForm').submit();
			}									   
		});
		
		$('#btn_Submit').replaceWith('<img id="btn_Submit" src="_presentation/images/btn_Submit.gif" alt="Submit" title="" />');
		$('#btn_Submit').click(function () {
			var validForm = validate();
			
			if ( validForm ) {
				$('#requestForm').submit();
			}								 
		});
		
		ieSelectWidthFix();	
	// END Form setup---------------------------------------------------------------------------------------------------------
});