	var isFha = false;
	var currentTab = 0;
	var clicks = 0;
	$(document).ready(function(){
		var originalHref = location.href;
		if(originalHref.match("#Step-2")) {
			location.href = "#Step-1";
		}
		else if(originalHref.match("#Step-3")) {
			location.href = "#Step-1";	
		}
		if($("#HiddenLT").val() == 'P') {
			var isRefi = false;
			$(".refi").hide();
			$(".purchase").show();
		}
		else {
			var isRefi = true;
			$(".refi").show();
			$(".purchase").hide();
		}
		/*if($("#hideOn3").val() == 'true') {
			var hideOn3 = true;
		}
		else {
			var hideOn3 = false;
		}*/
		$.tools.tabs.addEffect("slideHorizontal", function(tabIndex, done) {
			if(clicks > 0) {
				if(currentTab < tabIndex) {
					this.getPanes().hide();
					$("#Step" + (currentTab+1)).show().animate({marginLeft:'-600px'},'normal', function() {
						$("#Step" + (currentTab+1)).hide('fast', function() { 
							$("#Step" + (tabIndex+1)).css('margin-left', '500px').show().animate({marginLeft:'0px'},'normal');			
						})				
					})
				}
				else {
					this.getPanes().hide();
					$("#Step" + (currentTab+1)).show().animate({marginLeft:'600px'},'normal', function() {
						$("#Step" + (currentTab+1)).hide('fast', function() { 
							$("#Step" + (tabIndex+1)).css('margin-left', '-500px').show().animate({marginLeft:'0px'},'normal');			
						})				
					})
				}
				//$("#Step" + (tabIndex+1)).show().animate({marginLeft:'0px'},'slow');
			}
			clicks++;

			if(tabIndex == 2) {
				for(divID in hideOn3) {
					$(hideOn3[divID]).hide();
				}
				for(divID in showOn3) {
					$(showOn3[divID]).show();
				}
				/*$(".YouAreSecure").show();
				if(hideOn3) {
					$(".VALoanBenefit").hide();
				}*/
				$(".lead_form_header").text("Last Step. You're Almost There!");
			}
			else {
				$(".lead_form_header").text("Get Started Here, See if You Qualify");		
			}
			done.call(); 
		});
		$("ul.lead_tabs").tabs("div.lead_panes > div",{effect:'slideHorizontal'}).history();
		$("a.history").onHash(function(event, hash) { 
			currentTab = leadFormTabs.getIndex();
		});
		var leadFormTabs = $("ul.lead_tabs").tabs();
		leadFormTabs.onBeforeClick(function(e, tabIndex) {
			if(!validate("#Step" + tabIndex)) {
				location.href = ("#Step-" + tabIndex);
				return false;
			}
		});
	  $("#LeadLoanType").change(function() {
			switch($(this).val()) {
				case "R":
					$("#LeadLoanAmountLabel").html("Your Home's Current Value:");
					$("#HiddenLT").attr("value", "R");
					isRefi = true;
					$(".refi").show();
					$(".purchase").hide();
				break;
				case "P":
					$("#LeadLoanAmountLabel").html("Approximate Loan Amount:");
					$("#HiddenLT").attr("value", "P");
					isRefi = false;
					$(".refi").hide();
					$(".purchase").show();
				break;
			}
	  });
		
		$("a.continue").click(function(e) {
			e.preventDefault();
			clicks++;
			if(leadFormTabs.click($(this).attr('href'))) {
				location.href =  $(this).attr('href');
			}
			return false;
		});
		
    $("#LeadForm").submit(function() {
			if(validate("#Step1") && validate("#Step2") && validate("#Step3")) {
			  //track('lead-form', 'complete-step-3', 'fha-home-loans');
			  return true;
			}
			return false;
		});

		function clean(id) {
		  $(id).find('.error.message').remove();
		  $(id).find('.error').removeClass('error');
		}

		function validate(id) {
			if(id != ("#Step" + (leadFormTabs.getIndex()+1))){
				return true;
			}
			var submitable = true;
			clean(id);

			$(id + " input, " + id + " select").each(function() {
			submitable &= sValidate($(this));
			});

			return submitable;
		}

		function sValidate(field) {
			error = field.attr('title');
			valid = true;

			if((isRefi && !field.parent().hasClass("purchase")) || (!isRefi && !field.parent().hasClass("refi"))) {
				if(field.hasClass('notempty')) {
				  if(field.val() == '' || field.val() == '0') {
					valid = false;
				  }
				}

				if(field.hasClass('checked')) {
				  if(!field.is(':checked')) {
					valid = false;
					field.parent("label").addClass('error');
				  }
				}

				if(field.hasClass('numeric')) {
				  if(field.val().match(/[^\d]/g)) {
					valid = false;
				  }
				}

				if(field.hasClass('email')) {
				  if(!field.val().match(/^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[a-z]{2,4}|museum|travel)$/i)) {
					valid = false;
				  }
				}

				if(field.hasClass('length')) {
				  if(field.val().length != field.attr("maxlength")) {
					valid = false;
				  }
				}

				if(!valid) {
				field.parent().children("label").addClass('error');
				field.parent().parent().children("label").addClass("error");
				field.parent().children(".hiddenError").show().css('color', '#DD0000');
				if(field == $('pNum1a')) {
					alert('yes');
				}
				field.addClass('error');
				  if(leadFormTabs.getIndex() == 0) {
					$(".step1Intro2").fadeOut(function() {
						$(this).html('To connect with a VA Specialist, please finish answering all of the questions.').css({color: "#D00"}).fadeIn('fast');
					});
				  }
				  else if(leadFormTabs.getIndex() == 1) {
					$("#step2Error").fadeOut(function() {
						$(this).html('To connect with a VA Specialist, please finish answering all of the questions.').fadeIn('fast');
					});
				  }
				  else if(leadFormTabs.getIndex() == 2) {
					$("#step3Error").fadeOut(function() {
						$(this).html('To connect with a VA Specialist, please finish answering all of the questions.').fadeIn('fast');
					});
				  }
				}
				else {
					if(leadFormTabs.getIndex() == 0) {
						$(".step1Intro2").html('It\'s easy, free and it takes less than 2 minutes.').css({color: '#009900'});
					}
					else if(leadFormTabs.getIndex() == 1) {
						$("#step2Error").html('&nbsp;');
					}
					$(".hiddenError").css('color', '#000');
				}
			}

			return valid;
		}
	});
	var phone_field_length = null;
		function TabNext(obj,event,len,next_field) {
		if (event == "down") {
			phone_field_length=obj.value.length;
			}
		else if (event == "up") {
			if (obj.value.length != phone_field_length) {
				phone_field_length=obj.value.length;
				if (phone_field_length == len) {
					next_field.focus();
					}
				}
			}
		}