var multistepFormComplete = false;

$j(document).ready(function(){
	$j('ol#multistepForm li').not(':first').each(function(){
		hideStep($j(this));
	});
	$j('.nextStepButton').show();
	$j('.subOptions').hide();
	$j('div.formStepHeader').click(function(c){
		gotoStep($j(this).parents('li:first'));
		return false;
	});
	$j('.nextStepButton').show().children('a').click(function(c){
		nextStep($j(this).parents('li:first'));
		return false;
	});
	$j('div.formStepHeader').click(function(c){
		gotoStep($j(this).parents('li:first'));
		return false;
	});
	
	$j('#progressbar > div').contents('a').each(function(){
		$j(this).click(function(a){
			$j("form[id^='scheduleTourStep']").attr('action', $j(this).href)
			$j("form[id^='scheduleTourStep']").trigger('submit');
			return false;
		});
	});
	
	$j('ol#multistepForm > li').find(':input').each(function(){
		$j(this).focus(function(){
			$j(this).parents('li').each(function(){
				if($j(this).parent().attr('id') == 'multistepForm') gotoStep($j(this));
			});
		});
	});
	
	$j('label.error[generated="true"]').each(function(){
		$j(':input[name=' + $j(this).attr('for') + ']').parents('p:first').addClass('invalid');
	});
	
	if($j('label.error[generated="true"]:first').parents('div.formStepContents').parents('li:first').length > 0) {
		gotoStep($j('label.error[generated="true"]:first').parents('div.formStepContents').parents('li:first'));
	}
	
	// Step 1
	if($j('form#scheduleTourStep1').length > 0){
		$j('form#scheduleTourStep1 :submit').hide();
		$j('input#touDate').datepicker({ changeMonth: false, changeYear: false, minDate: 0 });
		$j('form#scheduleTourStep1').validate({
			highlight: function( element, errorClass ) {
				$j( element ).addClass( errorClass ).parent('p').addClass('invalid');
			},
			unhighlight: function( element, errorClass ) {
				$j( element ).removeClass( errorClass ).parent('p').removeClass('invalid');
			},
			rules: {
				touReason: "required",
				touReasonExtended: {
					required: "#touReasonOther:checked",
					maxlength: 200
				},
				touDate: {
					required: true,
					date: true
				},
				touDayOfWeek: "required",
				touTimeSunday: {required: "#touDayOfWeekSunday:checked"},
				touTimeMonday: {required: "#touDayOfWeekMonday:checked"},
				touTimeTuesday: {required: "#touDayOfWeekTuesday:checked"},
				touTimeWednesday: {required: "#touDayOfWeekWednesday:checked"},
				touTimeThursday: {required: "#touDayOfWeekThursday:checked"},
				touContactMethod: "required",
				touContactTime: "required"
			}
		});
	
		$j('input[name="touDayOfWeek"]').change(function(){
			toggleSubOptions($j(this));
		});
		$j('input[name="touDayOfWeek"]').change();
	}
	
	// Step 2
	if($j('form#scheduleTourStep2').length > 0){
		$j('form#scheduleTourStep2 :submit').hide();
		$j('form#scheduleTourStep2').validate({
			highlight: function( element, errorClass ) {
				$j( element ).addClass( errorClass ).parent('p').addClass('invalid');
			},
			unhighlight: function( element, errorClass ) {
				$j( element ).removeClass( errorClass ).parent('p').removeClass('invalid');
			},
			rules: {
				touContactPrefix: "required",
				touContactFName: {
					required: true,
					maxlength: 100
				},
				touContactLName: {
					required: true,
					maxlength: 100
				},
				touAltContactFName: {
					maxlength: 100
				},
				touAltContactLName: {
					maxlength: 100
				},
				touContactAddress: {
					required: true,
					maxlength: 250
				},
				touContactCity: {
					required: true,
					maxlength: 50
				},
				touContactState: "required",
				touContactZip: {
					required: true,
					zipcode: true
				},
				touContactEmail: {
					required: true,
					maxlength: 300,
					email: true
				},
				touContactHomePhone: {
					required: true,
					phone: true
				}, 
				touContactMobilePhone: {
					phone: true
				}
			}
		});
		$j('input#touContactZip').setMask('99999-9999');
		$j('input#touContactHomePhone,input#touContactMobilePhone').setMask('(999) 999-9999');

	}
	
	// Step 3
	if($j('form#scheduleTourStep3').length > 0){
		$j('form#scheduleTourStep3 :submit').hide();
		$j('form#scheduleTourStep3').validate({
			highlight: function( element, errorClass ) {
				$j( element ).addClass( errorClass ).parent('p').addClass('invalid');
			},
			unhighlight: function( element, errorClass ) {
				$j( element ).removeClass( errorClass ).parent('p').removeClass('invalid');
			},
			rules: {
				touInterests: "required",
				touGolfType: {
					required: "#touInterestsGolf:checked"
				},
				touTennisType: {
					required: "#touInterestsTennis:checked"
				}
			}
			
		});
		$j('#touInterestsGolf,#touInterestsTennis').change(function(){
			toggleSubOptions($j(this));
		});
		$j('#touInterestsGolf,#touInterestsTennis').change();
		
	}
});

function stepValid(step){
	var stepIsValid = true;
	$j(step).find(':input').each(function(){
		//alert($j(this).attr('name') + ' = ' + $j('form#scheduleTourStep1').validate().element($j(this)));
		if($j(this).parents('form:first').validate().element($j(this)) == false) stepIsValid = false;
	});
	if(stepIsValid) $j(step).children('div.formStepHeader').addClass('success').removeClass('fail')
		else $j(step).children('div.formStepHeader').addClass('fail').removeClass('success');
	return stepIsValid;
}

function gotoStep(step, force){
	force = (typeof force == "undefined") ? false : force;
	currentStep = $j('ol#multistepForm li div.formStepHeader:hidden').parents('li:first');
	if (force || currentStep.length < 1 || stepValid(currentStep)){
		$j(step).siblings('li').each(function(){hideStep($j(this))});
		showStep(step);
	} else {
		showNotification('Please complete the current step before proceeding.', 'fail');
	}
}

function showStep(step){
	$j(step).children('div.formStepContents').slideDown('slow').addClass('gainlayout');
	$j(step).children('div.formStepHeader').hide('fast');
}

function hideStep(step){
	$j(step).children('div.formStepContents').removeClass('gainlayout').slideUp('slow');
	$j(step).children('div.formStepHeader').show('fast');
}

function nextStep(step) {
	if(stepValid(step)) {
		if(!multistepFormComplete && $j(step).next('li').length > 0){
			hideStep(step);
			showStep($j(step).next('li'));
		} else {
			multistepFormComplete = true;
			checkMultistepForm();
		}
	}
}

function checkMultistepForm() {
	var firstInvalid = null;
	$j('ol#multistepForm li').each(function(){
		//alert($j(this).attr('id') + ' = ' + stepValid($j(this)));
		if(!stepValid($j(this))){
			$j(this).children('div.formStepHeader').addClass('fail').removeClass('success');
			if(firstInvalid == null) firstInvalid = $j(this);
		}
	});
	if(firstInvalid == null) $j('ol#multistepForm').parents('form:first').submit()
		else gotoStep(firstInvalid, true);
	return false;
}

function toggleSubOptions(field){
	if($j(field).filter(':checked').length > 0) $j(field).parents('div:first').next('div.subOptions').show().addClass('gainlayout')
		else $j(field).parents('div:first').next('div.subOptions').hide().removeClass('gainlayout');
}