  var LifeForm = Class.create();
  LifeForm.prototype =
  {
    initialize: function(form_id){
      this.name           = form_id;
			this.step						= 1;
      this.form_elements  = $$(".error");
      this.initializeDisplay();
      this.initializeEvents();
    },

    /***************************************************************************************************
     *add display states, event listeners for submit button, return key and custom onclick/onchange
     */
    initializeDisplay : function(){
			$("step2").hide();
			$("step3").hide();
			$("step1_navigation").show();
      this.showLengthOfTerm();
      this.showTakesMedications();
      this.showPreExisting();
      this.showExistingCarrier();
    },
		
    initializeEvents : function(){
      Event.observe(document, 'keypress', this.validateOnReturnKey.bindAsEventListener(this));
      Event.observe('submit', 'click', this.validateFields.bindAsEventListener(this));

      Event.observe('submit_step1', 'click', this.nextStep.bindAsEventListener(this));

      Event.observe('submit_step2', 'click', this.nextStep.bindAsEventListener(this));
      Event.observe('go_to_step1', 'click', this.previousStep.bindAsEventListener(this));

      Event.observe('go_to_step2', 'click', this.previousStep.bindAsEventListener(this));

      Event.observe('life_insurance_type', 'change', this.showLengthOfTerm.bindAsEventListener(this));

      Event.observe('has_existing_carrier_1', 'click', this.showExistingCarrier.bindAsEventListener(this));
      Event.observe('has_existing_carrier_0', 'click', this.showExistingCarrier.bindAsEventListener(this));
      Event.observe('has_pre_existing_conditions_1', 'click', this.showPreExisting.bindAsEventListener(this));
      Event.observe('has_pre_existing_conditions_0', 'click', this.showPreExisting.bindAsEventListener(this));
      Event.observe('takes_medications_1', 'click', this.showTakesMedications.bindAsEventListener(this));
      Event.observe('takes_medications_0', 'click', this.showTakesMedications.bindAsEventListener(this));
    },

    /***************************************************************************************************
     *show/add functions
     *takes hidden row, element and validation to apply/remove
     */
    showAddValidations: function(name, element, validation_name){
      var name = String(name);
      $$(name).each(function(element){ element.show(); });
      $(element.id).addClassName(validation_name);
    },

    hideRemoveValidations: function(name, element, validation_name){
      var name = String(name);
      $$(name).each(function(element){ element.hide(); });
      $(element.id).hide();
      $(element.id).removeClassName(validation_name);
    },

    previousStep: function(){
    	$('step' + this.step).hide();
			$("step" + this.step + "_navigation").hide();
      this.step--;
    	$('step' + this.step).show();
			$("step" + this.step + "_navigation").show();
    },

    nextStep: function(e){       
      var elements        = $("step" + this.step).getElementsByClassName("error");
      var form_elements1  = new Validator(elements);
      var is_valid        = form_elements1.isFormValid();

      if(isDefined("has_pre_existing_conditions_1")){
        if($("has_pre_existing_conditions_1").checked){
          if(!isAnyChecked("pre_existing_conditions_row")){
            is_valid  = false;
          }
        }
      }

      if(!is_valid){
        Event.stop(e);
        return;
      }
      
    	$("step" + this.step).hide();
      var next_step = this.step + 1;
    	$("step" + next_step).show();
    	
    	(next_step == 3) ? $("go_to_step2").show() : $("go_to_step2").hide();
				
			$("step" + this.step + "_navigation").hide();
			$("step" + next_step + "_navigation").show();
      this.step++;
    },

    validateFields: function(e){
      var form      = new Validator(this.form_elements);
      var is_valid  = form.isFormValid();

      if($("has_pre_existing_conditions_1").checked){
        if(!isAnyChecked("pre_existing_conditions_row")){
          is_valid  = false;
        }
      }
      if(!$("privacy_policy").checked){
        is_valid = false;
        $("privacy_policy_error").show();
      }else{
        $("privacy_policy_error").hide();
      }
      if(!is_valid) Event.stop(e);
    },

    validateOnReturnKey: function(e){
      if(e.keyCode == Event.KEY_RETURN){
        this.validateFields(e);
        Event.stop(e);
      }
    },

    showPreExisting : function(){
      if($("has_pre_existing_conditions_1").checked){
        $("pre_existing_conditions_row").show();
      }else{
        $("pre_existing_conditions_row").hide();
        $("pre_existing_conditions_error").removeClassName("error");
        var PECs = Form.getElements($("pre_existing_conditions_row"));
        PECs.each(function(e){ return (e.checked = false); });
      }
    },

    showExistingCarrier : function(){
      if($("has_existing_carrier_1").checked){
        $("existing_carrier_error").addClassName("required");
        $("existing_carrier_row").show();
      }else{
        $("existing_carrier_row").hide();
        $("existing_carrier_error").removeClassName("required");
        var PECs = Form.getElements($("existing_carrier_row"));
        PECs.each(function(e){ return (e.checked = false); });
      }
    },

    showTakesMedications : function(){
      if($("takes_medications_1").checked){
        this.showAddValidations("#insured1_current_medications_detail_row",$("insured1_current_medications_detail_error"),"required");
      }else{
        this.hideRemoveValidations("#insured1_current_medications_detail_row",$("insured1_current_medications_detail_error"),"required");
        $("insured1_current_medications_detail").clear();
      }
    },

    showLengthOfTerm : function(){
      (/(term)/i.test($F("life_insurance_type"))) ? $("length_of_term_row").show() : $("length_of_term_row").hide();
    }
  }

  if (!(BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 6)) {
    Event.observe(window, 'load', function() {
      var life_form = new LifeForm();
    });
  }

  function isAnyChecked(parent_id){
    var is_valid  = true;
    is_valid      =  Form.getElements($(parent_id)).any(function(e){ return (e.checked); });
    if(!is_valid){
      $("pre_existing_conditions_error").addClassName("error");
    }else{
      $("pre_existing_conditions_error").removeClassName("error");
    }
    return is_valid
  }