  var HealthForm = Class.create({
    initialize: function() {
      this.children              = 0;
      this.max_children          = 5;
      
      if ($("step1")) {
        this.step = 1;
        this.initializeStep1();
        this.initializeEvents();
      } else {
        this.step = 3;
        this.initializeSeniorHealthToHealthForm();
      }

      /* Handle window closing */
      if ($('exit-message')) window.onbeforeunload = this.onUnload;
    },

    /***************************************************************************
     * initialize the elements of the form, hide and show as directed
     */
    initializeStep1 : function() {
      $("step2").hide();
      $("step1_button").show();
      
      /* Hide certain form elements */
      this.onChangeChildren();
      this.onChangeCurrentlyInsured();
      this.onChangeTakesMedications();
      this.onChangePreExistingConditions();
    },

    /***************************************************************************
     * add event listeners for submit button, return key and custom onclick/onchange
     */
    initializeEvents : function() {
      /* Submits */
      Event.observe(document, 'keypress', this.onInterceptReturns.bindAsEventListener(this));
      Event.observe('submit_step1', 'click', this.onDoneWithStep1.bindAsEventListener(this));
      Event.observe('go_to_step1', 'click', this.onBackToStep1.bindAsEventListener(this));
      Event.observe('submit', 'click', this.onRequestMyQuote.bindAsEventListener(this));

      /* Step 1 Events */
      Event.observe('lead_number_of_children', 'change', this.onChangeChildren.bindAsEventListener(this));
      Event.observe('lead_gender2', 'change', this.onChangeSpouse.bindAsEventListener(this));
      
      Event.observe('lead_has_existing_carrier_0', 'click', this.onChangeCurrentlyInsured.bindAsEventListener(this));
      Event.observe('lead_has_existing_carrier_1', 'click', this.onChangeCurrentlyInsured.bindAsEventListener(this));
      
      Event.observe('lead_takes_medications_0', 'click', this.onChangeTakesMedications.bindAsEventListener(this));
      Event.observe('lead_takes_medications_1', 'click', this.onChangeTakesMedications.bindAsEventListener(this));
      
      Event.observe('lead_pre_existing_0', 'click', this.onChangePreExistingConditions.bindAsEventListener(this));
      Event.observe('lead_pre_existing_1', 'click', this.onChangePreExistingConditions.bindAsEventListener(this));
    },

    initializeSeniorHealthToHealthForm : function() {
      this.onChangeCurrentlyInsured();
      this.onChangeTakesMedications();
      this.onChangePreExistingConditions();
      /* Submits */
      Event.observe(document, 'keypress', this.onInterceptReturns.bindAsEventListener(this));
      Event.observe('submit', 'click', this.onDoneWithStep3.bindAsEventListener(this));

      /* Step 1 Events */
      
      Event.observe('lead_has_existing_carrier_0', 'click', this.onChangeCurrentlyInsured.bindAsEventListener(this));
      Event.observe('lead_has_existing_carrier_1', 'click', this.onChangeCurrentlyInsured.bindAsEventListener(this));
      
      Event.observe('lead_takes_medications_0', 'click', this.onChangeTakesMedications.bindAsEventListener(this));
      Event.observe('lead_takes_medications_1', 'click', this.onChangeTakesMedications.bindAsEventListener(this));
      
      Event.observe('lead_pre_existing_0', 'click', this.onChangePreExistingConditions.bindAsEventListener(this));
      Event.observe('lead_pre_existing_1', 'click', this.onChangePreExistingConditions.bindAsEventListener(this));
    },

    onUnload : function() {
      // What?! jQuery from within Prototype? This is crazy I know.
      jQuery("#exit-message").trigger('click');
      // Re-position popup...
      jQuery("#colorbox").css({ top:jQuery(window).scrollTop() + 30 });

      window.onbeforeunload = null;
      return "Hit CANCEL to get access to rate quotes from leading providers right now.";
    },

    /* Event Handlers */
    onInterceptReturns : function(e) {
      if (e.keyCode == Event.KEY_RETURN) {
        if (this.step == 1) {
          this.onDoneWithStep1(e);
        } else if (this.step == 2) {
          this.onRequestMyQuote(e);
        } else if (this.step == 3) {
          this.onDoneWithStep3(e);
        }
        
        if (e) Event.stop(e);
      }
    },
    
    /* Step 1 */
    onChangeChildren : function() {
      this.children = parseInt( $F('lead_number_of_children') );

      if ($("help_text") != undefined) {
        (this.children > 0) ? $("help_text").show() : $("help_text").hide();
      }

      for (var i = 0; i <= this.max_children; i++) {
        var insured = i + 3;
        var id      = 'child_option' + insured;

        if ( i < this.children ) {
          $(id).show();
        } else {
          $(id).hide();
          this.resetInsured(insured);
        }
      }
    },

    onChangeSpouse : function() {
      if ( $('lead_gender2').selectedIndex == 0) {
        this.resetInsured(2);
      }
    },

    onChangeCurrentlyInsured : function() {
      if ( $('lead_has_existing_carrier_1').checked ) {
        $('existing_carrier_row').show()
      } else {
        $('existing_carrier_row').hide();
        $('lead_existing_carrier').selectedIndex = 0;
      }
    },
    
    onChangeTakesMedications : function() {
      if ( $('lead_takes_medications_1').checked ) {
        $('insured1_current_medications_detail_row').show();
      } else {
        $('insured1_current_medications_detail_row').hide();
        $('lead_insured1_current_medications_detail').clear();
      }
    },
    
    onChangePreExistingConditions : function() {
      if ( $('lead_pre_existing_1').checked ) {
        $('pre_existing_conditions_row').show();
      } else {
        $('pre_existing_conditions_row').hide();
        var conditions = $('pre_existing_conditions_row').getElementsByTagName('INPUT');
 		    $A(conditions).each(function(n) { n.checked = false; });
      }
    },
    
    onDoneWithStep1 : function(e) {
      var valid = this.validateStep1();
      //console.log(valid);

      if (valid) {
      	$('step' + this.step).hide();
      	$('go_to_step' + this.step).show();
      	$('step' + ++this.step).show();
      }
      
      if (e) Event.stop(e);
    },

    onBackToStep1: function(e){
    	$('step' + this.step ).hide();
    	$('step' + --this.step ).show();
      if (e) Event.stop(e);
    },
    
    onRequestMyQuote : function(e) {
      var is_valid = this.validateStep2();

      if (is_valid) {
        window.onbeforeunload = null;
      } else {
        Event.stop(e);
      }
    },

    onDoneWithStep3 : function(e) {
      var is_valid = this.validateStep3();
      if (is_valid) {
        window.onbeforeunload = null;
      } else {
        Event.stop(e);
      }
    },

    resetInsured : function(insured) {
      $('lead_gender' + insured).selectedIndex = 0;
      $('lead_insured' + insured + '_height_feet').clear();
      $('lead_insured' + insured + '_height_inches').clear();

      $('lead_insured' + insured + '_weight').clear();

      $('lead_dob' + insured + '_mm_on').clear();
      $('lead_dob' + insured + '_dd_on').clear();
      $('lead_dob' + insured + '_yyyy_on').clear();

      $('lead_insured' + insured + '_height_error').hide();
      $('lead_insured' + insured + '_weight_error').hide();
      $('lead_dob' + insured + '_on_error').hide();
    },

    /* Validations */
    validateStep1 : function() {
      var valid_inputs = [
        this.validateFamily(this.children),
        this.validateCurrentlyInsured(),
        this.validateCoverageStartDate(),
        this.validateMedications(),
        this.validatePreExistingConditions()
      ];
      
      var valid = $A(valid_inputs).all(function(n) { return n; });
      return valid;
    },
    
    validateStep2 : function() {
      return this.validateShortContactInfo.apply(this, arguments);
    },
    
    validateStep3 : function() {
      var valid_inputs = [
        this.validateCurrentlyInsured(),
        this.validateMedications(),
        this.validatePreExistingConditions()
      ];
      
      var valid = $A(valid_inputs).all(function(n) { return n; });
      return valid;
    }
    
  });
  
  HealthForm.addMethods(ValidationHelper);

  /* Validate the form */
  if (!(BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 6)) {
    FastInit.addOnLoad(function() {
      var health_form = new HealthForm();
    });
  }
