// Page setup ----------------------------------------------------------------	


// Form setup ----------------------------------------------------------------
	for ( i=0, ii = required_fields.length; i<ii; i++ )
	{ $('#'+required_fields[i]).attr({validation: 'required'}); }
	
	$('#request_form').submit(
		function () 
		{
			var validForm = validate();
			
			if ( validForm ) 
			{ return true; }
			else
			{ return false; }
		}
	);
	
	$('#step_two').hide();
	
	$('#btn_next').click(
		function () 
		{
			var validStepOne = validate('step_one');

			if ( validStepOne ) 
			{
				$('#step_one').hide();
				$('#step_two').show();
			}
		}
	);
	
	$('#btn_back').click(
		function () 
		{
			$('#step_two').hide();
			$('#step_one').show();
		}
	);
	
// Dynamic Removal of Various States Dependent On Program Selection ----------------------//

	stateHTML = $('#state').html();

	$('#program_code').change(function() {
		$('#state').html(stateHTML);

		switch( $(this).val() ) {
			
			case "MEDELAPL":
				$('#state option').each(function () {
					switch ($(this).val()) {
						case "TN":
						case "GA":
						case "AL":
						case "KY":
						case "AR":
						case "IA":
							$(this).remove();
						break;
					}
				});
			break;
			
			case "BABCCM":
			case "BABCSM":
			case "BBAA":
			case "BBACCM":
			case "BBACF":
			case "BBAG":
			case "BBAHCM":
			case "BBASMM":
			case "BBATSM":
			case "BABCENT":
			case "BABCLDIN":
			case "BABCORG":
			case "BABCMGR":
			case "BBALDINF":
			case "BBAENT":
			case "BBAGENT":
			case "BBASECMG":
			case "BBAHCMG":
			case "BBALDORG":
			case "BBACNMGR":
			case "BBAMGR":
			case "BBAHCM2":
			case "MABCE":
			case "MABCLI":
			case "MABCLCDO":
			case "MABCPM":
			case "MBAA":
			case "MBAE":
			case "MBAF":
			case "MBAFINA":
			case "MBAFA":
			case "MBAGS":
			case "MBAGEM":
			case "MBAHCM":
			case "MBAISM":
			case "MBAITM":
			case "MBALCDO":
			case "MBANC":
			case "MBAPM":
			case "MEDELAPL":
				$('#state option').each(function () {
					switch ($(this).val()) {
						case "IA":
							$(this).remove();
						break;
					}
				});
			break;

			case "MEDAEA":
			case "MEDTK":
			case "MEDTD":
			case "MEDELA":
			case "MEDHELA":
			case "MEDECIA":
			case "MEKLDE":
			case "MEKLDS":
			case "MEDK12LI":
			case "MEDSCIA":
			case "MEDECIAT":
			case "MELLDE":
			case "MELLDS":
			case "MEDK12TL":
			case "MBASCIAT":
				$('#state option').each(function () {
					switch ($(this).val()) {
						case "KY":
						case "AR":
						case "IA":
							$(this).remove();
						break;
					}
				});
			break;
			
			
			default:
				$('#state').html(stateHTML);
			break;
		}
	});


// Validation checks ----------------------------------------------------------------
	function isRequired( formField ) 
	{
		switch ( $(formField).prop('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 == "step_one" ) 
		{ var formFields = $('#step_one :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]).prop('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 =  "Invalid"; }
	
				writeError(formFields[i], errorMessage);
	
				validForm = false;
			}
		}
	
		return validForm;
	}
	
	function writeError ( formField, message )
	{
		var fieldID = $(formField).prop('id');
		var fieldWidth = $(formField).prop('offsetWidth');
		var fieldHeight = $(formField).prop('offsetHeight');
		
		if ( typeof(_gaq) != 'undefined' )
		{ _gaq.push(['_trackEvent', 'Forms', 'error-'+fieldID]); }
		
		$(formField).addClass('errorMessage');
		
		$(formField).focus(removeError);
		$(formField).click(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);
	}
	
// IE Specific Code ----------------------------------------------------------------
	if ( $('#root_ie').length ) 
	{						
		$('body').prepend('<div id="ie_warning"><p>For the best experience using this site, please upgrade to a more modern web browser.</p></div>');
		
		$('#ie_warning').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"); 
	
		ieSelectWidthFix();
	}

	// IE select width fix
		function ieSelectWidthFix ()
		{
			if ( $('#root_ie').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');
						
						$('select').mouseleave(
							function () {
								$(this).css('width', $(this).data("originalWidth"));
								$(this).removeClass('expanded');
							}
						);
					}
				);
				
				$('select').mouseleave(
					function () {
						$(this).css('width', $(this).data("originalWidth"));
						$(this).removeClass('expanded');
					}
				);
				
				$('select').click(
					function ()
					{
						if ( $(this).prop('class') == 'expanded' ) 
						{
							$(this).unbind('mouseleave');	
						}
					}
				);
							
				$(':input').focus(
					function () 
					{
						if ( $(this).prop('class') != 'expanded' ) 
						{
							$('.expanded').each(
								function () 
								{
									$(this).css('width', $(this).data("originalWidth"));
									$(this).removeClass('expanded');
								}
							);
						}
						
						$('select').mouseleave(
							function () {
								$(this).css('width', $(this).data("originalWidth"));
								$(this).removeClass('expanded');
							}
						);
					}
				);
			}
		}
