	var hist = new Array();

	function changeStep(fromStep, toStep) {
		if(fromStep == '1') {
			toggleCompany();
			$("#SecureForm").show();
			$("#WhyUs").hide();
		}
		valid = true;
		for(i = 1; i <= parseInt(toStep) - 1 && valid; i++) {
			valid = valid && validate(".Step" + i, i);
			if(!valid) {
				toStep = i;
			}
		}
		window.location = '#Step' + toStep;
		if(valid) {
			_gaq.push(['_trackEvent', 'lead-form', 'complete-step-' + fromStep, 'Surety Bonds - Get a Surety Bond Quote at Surety Bonds.com']);
		} else {
			$(".Step" + fromStep + " h5").fadeOut(function() {
				$(this).html("<div style='width: 242px;  background-color: #fff; z-index: 10; margin-left: 28px; padding: 10px; border: 3px solid #000; margin-top: -25px;'>Please finish answering all of the questions so we can help you with your bond.</div>").css({color: "#D00"}).fadeIn();
			});
		}
		if(toStep != fromStep) {
			$(".Step" + fromStep + ' , .Step' + toStep).toggle();
		}
		return valid;
	}

	function toggleCompany() {
			$(".Step2.new").hide();
			$(".Step2.existing").show();
			$("#BondCompanyName").val("");
			$("#BondCompanyAddress").val("");
			$("#BondCompanyCity").val("");
			$("#BondCompanyZip").val("");
	}

	jQuery(function($) {
		//Autotabbing
		$("#BondSPhone1").autotab({ target: 'BondSPhone2', format: 'numeric' });
		$("#BondSPhone2").autotab({ target: 'BondSPhone3', format: 'numeric', previous: 'BondSPhone1' });
		$("#BondSPhone3").autotab({ format: 'numeric', previous: 'BondSPhone2' });

		//Tooltips
		$("#HelpMeChoose").css({position: 'absolute' , right: -25, top: 10, margin: 0, display: 'block'});
		$("#NotSureAmount").css({position: 'absolute', right: -25, top: 10, margin: 0, display: 'block'});
		$("#HelpMeChoose").bind("mouseover mouseout", function() {
			of = $(this).offset();
			$("#HelpMeChooseTooltip").css({top: of.top - 3, left: of.left + $("#HelpMeChoose").innerWidth() + 10}).toggle();
		}).click(function() { return false; });
		$("#NotSureAmount").bind("mouseover mouseout", function() {
			of = $(this).offset();
			$("#NotSureAmountTooltip").css({top: of.top - 3, left: of.left + $("#NotSureAmount").innerWidth() + 10}).toggle();
		}).click(function() { return false; });

		//Bold Options
		$('#BondType option[value="Bond Not Listed"], #BondType option[value="Other..."], #BondType option[value="Not sure"]').css({fontWeight: 'bold'});

		//Click / back
		$(".steps .step").click(function() {
			s = $(this).attr('rel').split('-');
			fromStep = s[0];
			toStep	 = s[1];
			changeStep(fromStep, toStep);
		});

		$("a.continue").click(function(e) {
			return changeStep($(this).attr('rel'), $(this).attr('href').charAt(5));
		});

		$("#BondLeadForm").submit(function(e) {
			if(validate('.Step3', 3)) {
				if($("#BondToggle").val() == "0") {
					$(".Step2 .existing").empty();
				} else {
					$(".Step2 .new").empty();
				}
				return true;
			}
			return false;
		});
	});

	//History
	setInterval(function() {
		prev = hist.pop();
		if(prev != window.location.href) {
			hist.push(prev);
			hist.push(window.location.href);
		} else {
			hashIdx = prev.lastIndexOf("#");
			id = prev.substr(hashIdx, prev.length - 1);
			if($(id).css('display') == 'none') {
				$(".Step1, .Step2, .Step3").hide();
				$(id).show();
			}
		}
	}, 100);

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

	function validate(id, step) {
		submitable = true;
		prefix = ' ';
		if(step == "2") {
			//prefix = $("#BondToggle").val() == 0 ? ' .new ' : ' .existing ';
		}
		clean(id);
		//alert(id + prefix + 'input, ' + id + prefix + 'select');
		$(id + prefix + 'input, ' + id + prefix + 'select').each(function() {
			//alert($(this).attr('name'));
			submitable = sValidate($(this)) && submitable;
		});
		return submitable;
	}

	function sValidate(field) {
		if(field.hasClass('notempty')) {
			if(field.val() == '') {
				error = field.attr('title');
				field.parent().addClass('error');
				if(error != '') {
					field.parent().after($('<div style="position: absolute; text-align: right; width: 200px; margin-left: -200px; margin-top: -20px; padding: 5px; background-color: #fff; border: 1px solid #000; padding-top: 4px; color: red;">' + error + '</span>'));
				}
				return false;
			}
		}
		if (field.hasClass('numeric')) {
			if(field.val().match(/[^\d]/g)) {
				error = field.attr('title');
				field.parent().addClass('error');
				if(error != '') {
					field.parent().after($('<div style="position: absolute; text-align: right; width: 200px; margin-left: -200px; margin-top: -20px; padding: 5px; background-color: #fff; border: 1px solid #000; padding-top: 4px; color: red;">' + error + '</span>'));
				}
				return 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)) {
				field.parent().addClass('error');
				field.parent().after($('<div style="position: absolute; text-align: right; width: 200px; margin-left: -200px; margin-top: -20px; padding: 5px; background-color: #fff; border: 1px solid #000; padding-top: 4px; color: red;">' + error + '</span>'));
				return false;
			}
		}
		return true;
	}

