
    function isset(varname){
      return(typeof(window[varname])!='undefined');
    }
    
    function track(x, y, z) {
      if(isset("gwoTracker")) {
        gwoTracker._trackEvent(x, y, z);
      }
    }
    
    jQuery(function($) {
		$(".refi").show();
		$(".purchase").hide();
     // $("#FHAHomeLeadForm .clear-me").each(function() { $(this).val(""); });
      $("#LeadLoanType").change(function() {
        switch($(this).val()) {
          case "R":
            $("#LeadLoanAmountLabel").html("Your Home's Current Value:");
            //$("#FHAHomeLeadForm").attr("action", "http://www.mortgageloanplace.com/submit.php");
            //$("#HiddenEFL").attr("value", "no");
            $("#HiddenLT").attr("value", "R");
            isRefi = true;
            isFha = false;
            $(".refi").show();
            $(".purchase").hide();
          break;
          case "P":
            $("#LeadLoanAmountLabel").html("Approx Loan Amount:");
            //$("#FHAHomeLeadForm").attr("action", "http://www.mortgageloanplace.com/submit.php");
            //$("#HiddenEFL").attr("value", "no");
            $("#HiddenLT").attr("value", "P");
            isRefi = false;
            isFha = false;
            $(".refi").hide();
            $(".purchase").show();
          break;
        }
      });
      $("#FlyoutChartTrigger").hover(function() {
        $("#FlyoutChart").css({top: $(this).offset().top + 20, left: $(this).offset().left - 450}).show();
      }, function() {
        $("#FlyoutChart").hide();
      }).click(function() { return false; });

      $("a.continue").click(function(e) {
		e.preventDefault();
        stepNum = $(this).attr('rel');
        stepId = "#Step" + stepNum;
        if(validate(stepId)) {
          $(stepId + ', ' + $(this).attr('href')).toggle();
          /*if(stepNum == '1') {
            if(isRefi) {
              var s = isFha ? "fha" : "reg";
              $(".purchase").remove();
              $(".refi").show();
              if(data["fha"][$("#LeadLoanState").val()] == "qsr-link") {
                $("html").remove();
                track('lead-form', 'redirect', 'qsr');
                window.location = "http://www.mortgageloanplace.com/qs_apply.html?url=http://v.vifils.com/cgi/r?;n=203;c=365812;s=6003;x=7936;f=200802131411400;u=j;z=" + (new Date().getTime() / 1000) + ";c_border_color=1F5647&c_backgound=ffffff&typeOfLoan=REFI";
              } else if (data["fha"][$("#LeadLoanState").val()] == "lp-refi-link") {
                $("html").remove();
                track('lead-form', 'redirect', 'lp-refi');
                window.location = "http://www.mortgageloanplace.com/lp_apply.php";
              }
            } else {
              $(".refi").remove();
              $(".purchase").show();
              track('lead-form', 'complete-step-1', 'fha-home-loans');
            }
          } else */if(stepNum == '2') {
            track('lead-form', 'complete-step-2', 'fha-home-loans');
            $("#FormHeader").html("Last Step.  You're Almost There!");
			$("#YouAreSecure").show();
			if(hideOn3) {
				$("#UseBenefit").hide();
			}
          }
        }
        return false;
      });

      $("#FHAHomeLeadForm").submit(function() {
        if(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) {
      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() == '') {
            valid = false;
          }
        }

        if(field.hasClass('checked')) {
          if(!field.is(':checked')) {
            valid = false;
          }
        }

        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().addClass('error');
          if(error != '') {
            if(!field.next().hasClass("message")) {
              field.parent().append($('<div class="error message">' + error + '</div>'));
			  field.parent().parent().children("label").addClass("error");
            }
          }
        }
      }

      return valid;
    }