// Front-end Form Validation

// -- vars
var $j = jQuery.noConflict(); // ** b/c prototype is being used **
var numOfSteps = 3; 
var formId = "#purchaseform";

if( jQuery.browser.version == '6.0' ){
	var ie = true; 
} else { ie = false; }



$j(document).ready( function(){
	initForm();
});


// -- initForm
// initialize the form 
function initForm(){

	if (!ie){
		var validate = $j(formId).validate();	
	}
		

	// hide all the form steps, show the first form
	$j('.formstep').hide(); 
	$j('#step1').show(); 
	
	//show the "next step" buttons --  hidden by default (for users w/o js)
	$j('.nextstep').show(); 
	$j('#gobacktoStep1').show();
	$j('#gobacktoStep2').show();
	
	unsetRequired(2);
	unsetRequired(3);
	
	nextStep(2);
	nextStep(3);
	
	$j('#finalstep').click(finalStep);
	
	prevStep();			
}		


// -- unsetRequired
// form validation per-step fails b/c the user can't see required fields in future steps.
// gets all the .required classes for the specified step, changes it to '.required[stepnumber]'
function unsetRequired(stepnum){
	$j('#step'+stepnum+' .required').each( function(){
		$j(this).removeClass('required');
		$j(this).addClass('required'+stepnum);
		//$j(this).val(" ");	
	});
}

// -- resetRequired
// makes '.required[stepnumber]' into > '.required'
function resetRequired(stepnum){
	$j('#step'+stepnum+' .required'+stepnum).each( function(){
		$j(this).removeClass('required'+stepnum);
		$j(this).removeClass('error');
		$j(this).addClass('required');
	});	
}


// -- nextStep(x)
// parameter is the form # that you want to go to
function nextStep(nextForm){
		
	var prevForm = nextForm - 1; 
	var theid = 'step'+nextForm;
					
	$j("#step"+prevForm).find("input.nextstep").click( function(){	
		
		if (!ie){
			validate = $j(formId).validate();	
			if(!validate.form()){  // run the validation, don't continue if it fails
				return false;
			}
		}
					
		$j('#step'+prevForm).slideUp();
		$j('#step'+nextForm).slideDown();
		
		$j('#tabstep'+prevForm).removeClass();
		$j('#tabstep'+nextForm).addClass('active');
		
		resetRequired(nextForm);
		
		return false;
	});
	
}



// -- prevStep(goTo)
// if it doesn't have a specific form thinger to go to (parameter), it determines what the previous form to go to is.
function prevStep(goTo){
							
	$j(".prevstep").each( function(){
		$j(this).click( function(){ 
		
			//if( validate.form() ){
				var currentForm = (goTo == null) ? $j(this).parent().attr('id').split('step')[1] : goTo + 1;
				var prevForm = (goTo == null) ? currentForm - 1 : goTo;
				
				unsetRequired(currentForm);
									
				$j('#step'+currentForm).slideUp();
				$j('#step'+prevForm).slideDown();
				
				$j('#tabstep'+currentForm).removeClass();
				$j('#tabstep'+prevForm).addClass('active');

				return false;
			//}
			
		}); // click
	}); // each
	
} 	


// -- finalstep
function finalStep(){
	
	if (!ie){
		validate = $j(formId).validate();	
		if( !validate.form() ){  // run the validation, don't continue if it fails			
			return false ; 
		} else { 
			$j(formId).submit();
			return true;
		}
	}
		
	return true;

}