// 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" src="_presentation/images/btn_Submit.png" alt="Submit" />');
	
	$('#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();
				}
			}
		);
	
		$('#btn_Back').click(
			function () 
			{
				// Show step one
				// Hide step two
			}
		);


/*	Program Information Switch Setup:
_____________________________________________________________________________*/

	$('#employers').hide();
	
	$('#switchB').click(
			function () 
			{
				$('#overview').hide();
				$('#switchA').css({color: "#afafaf"});
				$('#employers').show();
				$('#switchB').css({color: "#ffffff"});
			}
		);
	
		$('#switchA').click(
			function () 
			{
				$('#overview').show();
				$('#switchA').css({color: "#ffffff"});
				$('#employers').hide();
				$('#switchB').css({color: "#afafaf"});
			}
		);

// 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 validateCheckBox() {
		var program_selected = false;
		
		var program_code = $('input[name="program_code[]"]:checked').length;
		
		if ( program_code != 0 ) { program_selected = true; } 
	
		return program_selected;
	}
	
// 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;
	
					case "program_AT":
					case "program_AMT":
					case "program_NT":
					case "program_QC":
					case "program_PP":
					case "program_BSAMT":
						OK = validateCheckBox();
						if ( OK ) { 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');
		
		switch ( fieldID ) {
			case "program_AT":
			case "program_AMT":
			case "program_NT":
			case "program_QC":
			case "program_PP":
			case "program_BSAMT":
				fieldWidth = 135;
				fieldHeight = 20;
				message = "Select a program.";
				
				_gaq.push(['_trackEvent', 'Forms', 'error-program_code']);
			break;
			
			default:
				_gaq.push(['_trackEvent', 'Forms', 'error-'+fieldID]);
			break;
		}
		
		$(formField).addClass('errorMessage');
		$(formField).parent().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);
	}
	
// Event Tracking-------------------------------------------------------------------------------------------------------------	
	$(':input').each(
		function ()
		{
			switch ( $(this).attr('name') )
			{
				case "program_code[]":
					$(this).bind('click', function () { _gaq.push(['_trackEvent', 'Forms', 'program_code']); });
				break;
				
				case "grad_year":
					$(this).bind('change', function () { _gaq.push(['_trackEvent', 'Forms', 'grad_year']); });
				break;
				
				case "first_name":
					$(this).bind('focus', function () { _gaq.push(['_trackEvent', 'Forms', 'first_name']); });
				break;
				
				case "last_name":
					$(this).bind('focus', function () { _gaq.push(['_trackEvent', 'Forms', 'last_name']); });
				break;
				
				case "email":
					$(this).bind('focus', function () { _gaq.push(['_trackEvent', 'Forms', 'email']); });
				break;
				
				case "phone":
					$(this).bind('focus', function () { _gaq.push(['_trackEvent', 'Forms', 'phone']); });
				break;
				
				case "age":
					$(this).bind('focus', function () { _gaq.push(['_trackEvent', 'Forms', 'age']); });
				break;
				
				case "address":
					$(this).bind('focus', function () { _gaq.push(['_trackEvent', 'Forms', 'address']); });
				break;
				
				case "city":
					$(this).bind('focus', function () { _gaq.push(['_trackEvent', 'Forms', 'city']); });
				break;
				
				case "state":
					$(this).bind('change', function () { _gaq.push(['_trackEvent', 'Forms', 'state']); });
				break;
				
				case "postal_code":
					$(this).bind('focus', function () { _gaq.push(['_trackEvent', 'Forms', 'postal_code']); });
				break;
				
				case "country":
					$(this).bind('change', function () { _gaq.push(['_trackEvent', 'Forms', 'country']); });
				break;
				
				case "citizenship":
					$(this).bind('change', function () { _gaq.push(['_trackEvent', 'Forms', 'citizenship']); });
				break;
				
				case "relocation":
					$(this).bind('change', function () { _gaq.push(['_trackEvent', 'Forms', 'relocation']); });
				break;
			}
		}
	);
