$('script:first').before('<link rel="stylesheet" type="text/css" href="_Presentation/jsReady_siteStyle.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({
				borderBottom: "1px solid #f7941d",
				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");
		}
	
	
	
	//--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';
	
	
	
	//--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');
						});
					}
				});
			}
		}
	
	
	
	//--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}$"); }
	//-----------------------------------------------------------------------------------------------------END Validation checks



	//--Form validation---------------------------------------------------------------------------------------------------------
		function removeError() {
			if (!$(this).data('errorMessage')) return;
			
			$(this).removeClass('errorMessage');	
			$(this).next().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');
			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-------------------------------------------------------------------------------------------------------------	
		if ($('#index').html() != null) {
			$('#campusLinkList').append('<img id="campusThumb" src="'+thumbs[0]+'" />');
			
			$('#campusThumb').fadeIn(500);
			
			function rotateImage () {
				$('#campusThumb').fadeOut(500, function () {
					currentImage++;
					if (currentImage >= thumbs.length) {
						currentImage = 0;
					}
					$('#campusThumb').attr('src', thumbs[currentImage]);
					$('#campusThumb').fadeIn(500);
				});
	
				setTimeout(rotateImage, 5000);
			}
			
			setTimeout(rotateImage, 5000);
		}
		
		$('#program_code').attr('disabled', 'disabled');
		
		$('body').css({minHeight: $(window).height()-290});
		
		$(window).resize(function () {$('body').css({minHeight: $(window).height()-290});})
		
		$('#contentArea').css({minHeight: $(window).height()-290});
		
		$('#index, #index #contentArea').css({minHeight: 650});
		
		$('#programLink').hover(
			function () {
				$(this).children("ul:first").show();	
			},
			function () {
				$(this).children("ul:first").hide();		
			}
		);
		
		$('#campusLink').hover(
			function () {
				$(this).children("ul:first").show();	
			},
			function () {
				$(this).children("ul:first").hide();		
			}
		);
		
		$('.massageTherapy #contentArea').prepend('<img id="massageTherapyHeader" src="_Presentation/Images/massageTherapy.jpg" alt="Concorde Career Colleges&mdash;MassageTherapy" title="" />');
		$('#massageTherapyHeader').css({position: "absolute", top: 0, left: 0, zIndex: 10});
		
		$('.program #contentArea, .massageTherapy #contentArea').prepend('<div id="sideNav"></div>');
		$('.dental #sideNav').append('<img src="_Presentation/Images/dentalPrograms.jpg" alt="Concorde Career Colleges&mdash;Dental Programs" title="" />');
		$('.nursing #sideNav').append('<img src="_Presentation/Images/nursingPrograms.jpg" alt="Concorde Career Colleges&mdash;Nursing Programs" title="" />');
		$('.medical #sideNav').append('<img src="_Presentation/Images/medicalPrograms.jpg" alt="Concorde Career Colleges&mdash;Medical Programs" title="" />');
		
		$('.program #contentArea ul:first').css({background: "#dbe1eb", padding: "5px 0 15px 0"}).prependTo('#sideNav');
		$('.program #contentArea ul:first li').css({position: "relative", left: -35});
		$('.program #contentArea ul:first a').css({fontSize: "11px"});
		
		$('.program #contentArea').css({minHeight: 385});
		
		$('.program #contentArea ul:first a, #introMaterial a').click(function () {
			$('#contentArea div[id != sideNav]').hide();
			
			var selectedProgram = $(this).attr('href');
			
			$(selectedProgram).show();
			
			$('#contentArea').css({minHeight: $(window).height()-290});
			
			return false;													
		});
		
		if (window.location.hash) {
			var programSelect = window.location.hash;
			
			$('#contentArea div[id != sideNav]').hide();
			
			$(programSelect).show();
			
			$('#contentArea').css({minHeight: $(window).height()-290});
		}
		
		if ($.url.param("selection")) {
			var programSelect = $.url.param("selection");
			
			$('#contentArea div[id != sideNav]').hide();
			
			$('#'+programSelect).show();
			
			$('#contentArea').css({minHeight: $(window).height()-290});
		}
	//-----------------------------------------------------------------------------------------------------------END Page setup
	
	
	
	//--Form setup-------------------------------------------------------------------------------------------------------------
		$('#btn_Submit').replaceWith('<img id="btn_Submit" src="_Presentation/Images/btn_Submit.gif" alt="Submit" title="" />');
		$('#stepOne').append('<img id="btn_Next" src="_Presentation/Images/btn_Next.gif" alt="Next" title="" />');
		$('#stepTwo').append('<img id="btn_Back" src="_Presentation/Images/btn_Back.png" alt="Back" title="" />');
		$('#formArea').append('<img id="stepIndicator" src="_Presentation/Images/step1of2.png" alt="Step 1 of 2" title="" />');
		
		$('#program_code option:first').text('-- Select a campus first --');
		
		$('#campus_code').change(function () {			
			if ( $(this).val() == "" ) {
				$('#program_code').attr('disabled', 'disabled');
				$('#program_code option:first').text('-- Select a campus first --');
			} else {
				$('#program_code').removeClass('errorMessage');	
				$('#program_code').next().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').find('option').remove().end();
					$('#program_code').append('<option value="">-- Select a program --</option>');
					$('#select_program_code').text('-- Select a program --');
					for (var i=0; i<json.body.length; i++) {
						$('#program_code').append('<option value="'+json.body[i].ProgramCode+'">'+json.body[i].DefaultName+'</option>');
					}
					$('#program_code').removeAttr('disabled');
					
					if (window.location.hash) {
						$('#program_code').val($(programSelect).attr('class'));
					}
				});
			}
		});
		
		$('#btn_Next').click(function () {
			var validStepOne = validate('stepOne');
			
			if (validStepOne) {
				$('#stepOne').hide();
				$('#stepTwo').show();
				
				$('body').css({minHeight: $(window).height()-260});
				
				$('#stepIndicator').attr({
					src: "_Presentation/Images/step2of2.png",
					alt: "Step 2 of 2"
				});
				
				$('body, #contentArea').css({minHeight: 650});
			}
		});
		
		$('#btn_Back').click(function () {
			$('#stepTwo').hide();
			$('#stepOne').show();
			
			$('body').css({minHeight: $(window).height()-290});
			
			$('#stepIndicator').attr({
				src: "_Presentation/Images/step1of2.png",
				alt: "Step 1 of 2"
			});

			$('body, #contentArea').css({minHeight: 650});
		});
		
		$('#btn_Submit').click(function () {
			var validForm = validate();
			
			if (validForm) {
				$('#requestForm').submit();
			}								 
		});
		
		if (preselectCampus != "undefined") {
			$('#campus_code').val(preselectCampus).trigger('change');
		}
		
		ieSelectWidthFix();
	//-------------------------------------------------------------------------------------------------------------END Form setup
});