$(document).ready(function(){
	$('.multipart_form').each(function(){
		var hgt = $(this).find('.form_part:eq(0)').outerHeight();
		$(this).find('.multipart_form_outer').css({height: hgt});
	});	
	$('input,textarea').each(function(idx,val){
		$(this).attr('tabindex',-1);									   
	});	
	$('.form_part_active input,.form_part_active textarea').each(function(idx,val){
		$(this).attr('tabindex',Number(idx)+Number(1));								 
	});
});
function nextStep(id){
	clearerrors();
	var step 	= parseInt($.trim($('#steps_'+id+' td.steps_step.step_active').text()))-1;
	var steps	= $('#steps_'+id+' .steps_step').length;
	if(validateStep(id,step)){
		if(step<steps-1){
			var wth		= Number($('#holder_form_'+id).outerWidth())+Number(20);
			$('#holder_form_'+id+' .form_part:eq('+step+')').animate({left: -wth},1000,'swing');
			$('#holder_form_'+id+' .steps_step:eq('+step+')').removeClass('step_active').addClass('step_previous').next('td').removeClass('steps_spacerlight').addClass('steps_spacerdark');
			step++;
			var hgt = $('#holder_form_'+id+' .form_part:eq('+step+')').outerHeight();
			$('#holder_form_'+id+' .form_part:eq('+step+')').animate({left: 0},1000,'swing',function(){
				$('#holder_form_'+id+' .multipart_form_outer').animate({
					height: hgt
				},1000,'swing');																				  
			});
			$('#holder_form_'+id+' .steps_step:eq('+step+')').removeClass('step_inactive').addClass('step_active');
			$('input').attr('tabindex',-1);
			$('textarea').attr('tabindex',-1);
			$('#form_part_'+step+' input,#form_part_'+step+' textarea').each(function(idx,val){
				$(this).attr('tabindex',Number(idx)+Number(1));																	  
			});
											  
			if(step==steps-1){
				$('#holder_form_'+id+' .btn_forward').addClass('dim').hide();
				$('#holder_form_'+id+' .btn_submit').show();
			}
			if(step)$('#holder_form_'+id+' .btn_back').removeClass('dim');
		}
	}
}
function previousStep(id){
	clearerrors();
	var step 	= parseInt($.trim($('#steps_'+id+' td.steps_step.step_active').text()))-1;
	var steps	= $('#steps_'+id+' .steps_step').length;
	if(step){
		var wth		= Number($('#holder_form_'+id).outerWidth())+Number(20);
		$('#holder_form_'+id+' .form_part:eq('+step+')').animate({left: wth},1000,'swing');
		$('#holder_form_'+id+' .steps_step:eq('+step+')').removeClass('step_active').addClass('step_inactive').prev('td').removeClass('steps_spacerdark').addClass('steps_spacerlight');
		step--;
		var hgt = $('#holder_form_'+id+' .form_part:eq('+step+')').outerHeight();
		$('#holder_form_'+id+' .form_part:eq('+step+')').animate({left: 0},1000,'swing',function(){
			$('#holder_form_'+id+' .multipart_form_outer').animate({
				height: hgt
			},500,'swing');																				  
		});
		$('#holder_form_'+id+' .steps_step:eq('+step+')').removeClass('step_previous').addClass('step_active');
		$('input').attr('tabindex',-1);
		$('textarea').attr('tabindex',-1);
		$('#form_part_'+step+' input,#form_part_'+step+' textarea').each(function(idx,val){
			$(this).attr('tabindex',Number(idx)+Number(1));																	  
		});
		if(step<steps-1){
			$('#holder_form_'+id+' .btn_forward').removeClass('dim').show();;
			$('#holder_form_'+id+' .btn_submit').hide();
		}
		if(!step)$('#holder_form_'+id+' .btn_back').addClass('dim');
	}	
}
function submitMultiForm(id){
	clearerrors();
	$('#holder_form_'+id+' .successdiv').remove();
	var step 	= parseInt($.trim($('#steps_'+id+' td.steps_step.step_active').text()))-1;
	var steps	= $('#steps_'+id+' .steps_step').length;
	if(validateStep(id,step)){
		if(step==steps-1){
			waitElement('submit_form_'+id,'form_wait');
			var params		= new Object();
			params.fields	= new Object();
			params.id		= id;
			$('#form_'+id+' input').each(function(idx,val){
				if($(this).attr('type')=='radio' || $(this).attr('type')=='checkbox'){
					if($(this).attr('__hypid')!='undefined'){
						if($(this).is(':checked')){
							params.fields[$(this).attr('__hypid')]={name: $(this).attr('name'),id:$(this).attr('id'),value:$(this).val()};
						}
					}
				}else{
					if($(this).attr('__hypid')!='undefined'){
						params.fields[$(this).attr('__hypid')]={name: $(this).attr('name'),id:$(this).attr('id'),value:$(this).val()};
					}
				}
			});
			$('#form_'+id+' select').each(function(idx,val){
				if($(this).attr('__hypid')!='undefined'){
					params.fields[$(this).attr('__hypid')]={name: $(this).attr('name'),id:$(this).attr('id'),value:$(this).val()}; 								 
				}
			});
			$('#form_'+id+' textarea').each(function(idx,val){
				if($(this).attr('name')!='hyp_serialize_object' && $(this).attr('__hypid')!='undefined'){
					params.fields[$(this).attr('__hypid')]={name: $(this).attr('name'),id:$(this).attr('id'),value:$(this).val()}; 								 
				}
			});
			$('#hyp_serialize_object').val($.toJSON(params));
			$('#form_'+id).ajaxSubmit({
				url:			'index.php?r=forms/saveResult',
				type:			'POST',
				dataType:		'json',
				success: function(data){
					restoreElement('submit_form_'+id);
					$('#form_'+id).after('<div class="successdiv">'+
										'<div class="success-message">'+
											data.response +
											'<div class="clearfix"></div>' +
										'</div>' +
										'<div class="success-lower"></div>' +
										'<div class="clearfix"></div>' +
									'</div>');
				clearForm(id);
				},
				error:	function(data){
					restoreElement('submit_form_'+id);
				}
			});
		}
	}	
}
function validateStep(id,step){
	clearerrors();
	errors = false;
	$('#form_'+id+' #form_part_'+step+' [input_required]').each(function(idx,val){
		if(!errors){
			if($(this).is('input') || $(this).is('textarea')){
				var ty = typeof($(this).attr('type'))!='undefined' ? $(this).attr('type').toLowerCase() : 'textarea';
				if(ty=='checkbox' && !errors){
					if(!$(this).is(':checked')){
						jpop($(this).attr('id'),'Required Field','You are required to check the '+$(this).attr('name')+' checkbox before you will be allowed to continue.','bottom',10000);	
						scrollToId($(this).attr('id'));
						errors = true;
					}
				}else if(ty=='radio'){
					checked = false;
					$('input[type=radio][name='+$(this).attr('id')+']').each(function(){
						if($(this).is(':checked'))checked = true;
					});
					if(!checked){
						jpop($(this).attr('id'),'Required Field','Must select one of these options.','bottom',10000);
						scrollToId($(this).attr('id'));
						errors = true;	
					}					
				}else{
					if($.trim($(this).val())==''){
						jpop($(this).attr('id'),'Required Field',$(this).attr('name')+' is a required field','bottom',10000);
						scrollToId($(this).attr('id'));
						$(this).focus();
						errors = true;
					}
				}
			}
			if($(this).is('select')){
				if($.trim($(this).val())==''){
					jpop($(this).attr('id'),'Required Field',$(this).attr('name')+' is a required field. Please select a value from the drop down box','bottom',10000);
					scrollToId($(this).attr('id'));
					$(this).focus();
					errors = true;	
				}
			}
		}
	});
	$('#form_'+id+' #form_part_'+step+' input[type=radio][yesrequired]').each(function(idx,val){
		if(!errors){
			if(!$('input[type=radio][name='+$(this).attr('name')+'][value=Yes]').is(':checked')){
				jpop($(this).attr('id'),'Required Answer','You must be able to answer yes to this question before you will be allowed to complete the rest of the application.','bottom',10000);	
				errors = true;
			}
		}
	});
	return !errors;
}
