/**
 * @author msandow
 */

$(document).ready(function(){
	
	var currentStep = 1;
	
	function numeralsOnly(evt)	{
		evt = (evt) ? evt : event;
		var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
		if (charCode > 31 && (charCode < 48 || charCode > 57))	{
			alert ('Enter Numerals Only in this field. ');
			return false;
			}
		return true;	
	}
	function lettersOnly(evt)	{
		evt = (evt) ? evt : event;
		var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
		if (charCode > 31 && (charCode < 65 || charCode > 90) && (charCode < 97 || charCode > 122))	{
			alert ('Enter Letters Only in this field. ');
			return false;
			}
		return true;	
	}
	
	$.fn.testphone=function() {
		var string = $(this).val();
		if(string.length == 3)
			$(this).val(string + '-');
		if(string.length == 7)
			$(this).val(string + '-');
	}		
	
	$.fn.allFields=function(){
		var totals = $(this).find('input[type=text]').length + $(this).find('select').length;
		var running = 0;
		$(this).find('input[type=text], select').each(function(){
			if($(this).val() != ''){
				running++;
			}
		});
		if (running == totals){
			return true;
		}else{
			return false;
		}
		
	}
	
	$.nextStep=function(){
		currentStep++;
		if (currentStep == 2) {
			//middle step
			$('.clunkers_holder .tabs').css('background-position','0px -63px');
			$('#clunkStep1').fadeOut(400,function(){
				$('#clunkStep2').fadeIn(400);
			});
			$('#clunkNext').show();
			$('#clunkPrev').show();
			$('#secondInv').show();
			$('#clunkInv').hide();
			$('#clunkIntro').show();
		}else if(currentStep == 3){
			//last step
			$('.clunkers_holder .tabs').css('background-position','0px -126px');
			$('#clunkStep2').fadeOut(400);
			$('#clunkNext').hide();
			$('#clunkPrev').hide();
			$('#secondInv').hide();
			$('#clunkInv').show();
			$('#clunkIntro').hide();
		}
	}
	
	$.prevStep=function(){
		currentStep--;
		if (currentStep == 2) {
			//middle step
			$('.clunkers_holder .tabs').css('background-position','0px -63px');
			$('#clunkInv').fadeOut(400,function(){
				$('#clunkStep2').fadeIn(400);
			});
			$('#clunkNext').show();
			$('#clunkPrev').show();
			$('#clunkInv').hide();
			$('#clunkIntro').show();
		}else{
			//first step
			$('.clunkers_holder .tabs').css('background-position','0px 0px');
			$('#clunkStep2').fadeOut(400,function(){
				$('#clunkStep1').fadeIn(400);
			});
			$('#clunkNext').show();
			$('#clunkPrev').hide();
			$('#secondInv').hide();
			$('#clunkInv').hide();
			$('#clunkIntro').show();
		}
	}
	
	$.fn.submitForm=function(){
		$.ajax({  
		  type: "POST",  
		  url: $(this).attr('action'),  
		  data: $(this).serialize(),  
		  complete: function() {  

		  }  
		});  
	}
	
	$('.clunkers_holder form').submit(function(){
		return false;
	});
	
	$('.onlyNumbers').bind('keypress',function(k){
		return numeralsOnly(k);
	});
	
	$('.phones').bind('keyup',function(){
		$(this).testphone();
	});
	
	$('.cashContent form tr').each(function(){
		$(this).find('td:first').css('width','220px');
		$(this).find('td:last').css('width','166px');
	});
	
	$('#clunkNext').bind('click',function(){
		if(currentStep == 1){
			if($('#clunkStep1').allFields()){
				$('#clunkStep1').submitForm();
				$('#clunkStep2 input[name=fn]').attr('value',$('#clunkStep1 input[name=fn]').val());
				$('#clunkStep2 input[name=ln]').attr('value',$('#clunkStep1 input[name=ln]').val());
				$('#clunkStep2 input[name=ea]').attr('value',$('#clunkStep1 input[name=ea]').val());
				$('#clunkStep2 input[name=p]').attr('value',$('#clunkStep1 input[name=p]').val());
				$.nextStep();
			}else{
				alert('Please fill in all the fields');
			}
		}else{
			if($('#clunkStep2').allFields()){
				$('#clunkStep2').submitForm();
				$.nextStep();
			}else{
				alert('Please fill in all the fields');
			}
		}
	});
	
	$('#clunkPrev').bind('click',function(){
		$.prevStep();
	});
});

