  var HealthForm = Class.create();
  HealthForm.prototype =
  {
    initialize: function(form_id){
      this.initializeDisplay();
      this.initializeRequiredFields();
      this.initializeEvents();
      this.lockReturnKey  = false;
      this.form_elements  = $$(".error");
    },

    /***************************************************************************************************
     *initialize the elements of the form, hide and show as directed
     */
    initializeDisplay : function(){
      showChildren($('number_of_children'));
      this.showExistingCarrier();
      this.showTakesMedications();
      this.showPreExisting();
    },

    /*************************************************
     *initialize the validations for required elements
     */
    initializeRequiredFields : function(){
      $("gender1_error").addClassName("required");
      $("dob1_on_error").addClassName("validate-dob");
      $("insured1_height_error").addClassName("validate-height");
      $("insured1_weight_error").addClassName("validate-weight");
      $("reqdate_begin_error").addClassName("validate-request-date");
      $("first_name_error").addClassName("validate-alpha");
      $("last_name_error").addClassName("validate-alpha");
      $("address1_street1_error").addClassName("required");
      $("address1_city_error").addClassName("validate-alpha");
      $("address1_state_error").addClassName("validate-state");
      $("address1_zip_error").addClassName("validate-zip");
      $("phone1_error").addClassName("validate-phone");
      $("email1_error").addClassName("validate-email");
      $("privacy_policy_error").addClassName("validate-checked");
    },

    /*************************************************
     *initialize the validations for required elements
     */
    initializeRequiredFields : function(){
      $("gender1_error").addClassName("required");
      $("dob1_on_error").addClassName("validate-dob");
      $("insured1_height_error").addClassName("validate-height");
      $("insured1_weight_error").addClassName("validate-weight");
      $("reqdate_begin_error").addClassName("validate-request-date");
      $("first_name_error").addClassName("validate-alpha");
      $("last_name_error").addClassName("validate-alpha");
      $("address1_street1_error").addClassName("required");
      $("address1_city_error").addClassName("validate-alpha");
      $("address1_state_error").addClassName("validate-state");
      $("address1_zip_error").addClassName("validate-zip");
      $("phone1_error").addClassName("validate-phone");
      $("email1_error").addClassName("validate-email");
      $("privacy_policy_error").addClassName("validate-checked");
    },

    /***************************************************************************************************
     *add event listeners for submit button, return key and custom onclick/onchange
     */
    initializeEvents : function(){
      Event.observe(document, 'keypress', this.validateOnReturnKey.bindAsEventListener(this));
      Event.observe('submit', 'click', this.validateFields.bindAsEventListener(this));

      Event.observe('takes_medications_1', 'click', this.showTakesMedications.bindAsEventListener(this));
      Event.observe('takes_medications_0', 'click', this.showTakesMedications.bindAsEventListener(this));

      Event.observe('pre_existing_1', 'click', this.showPreExisting.bindAsEventListener(this));
      Event.observe('pre_existing_0', 'click', this.showPreExisting.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('gender2', 'change', this.spouseValidation.bindAsEventListener(this));
      Event.observe('dob2_mm_on', 'keydown', this.spouseValidation.bindAsEventListener(this));
      Event.observe('dob2_dd_on', 'keydown', this.spouseValidation.bindAsEventListener(this));
      Event.observe('dob2_yyyy_on', 'keydown', this.spouseValidation.bindAsEventListener(this));
      Event.observe('insured2_height_feet', 'change', this.spouseValidation.bindAsEventListener(this));
      Event.observe('insured2_height_inches', 'change', this.spouseValidation.bindAsEventListener(this));
      Event.observe('insured2_weight', 'keydown', this.spouseValidation.bindAsEventListener(this));

      Event.observe('number_of_children', 'change', function(){
        showChildren($('number_of_children'));
      });
    },

    /***************************************************************************************************
     *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);
    },

    validateFields: function(e){
      var form      = new Validator(this.form_elements);
      var is_valid  = form.isFormValid();

      if($("pre_existing_1").checked){
        if(!isAnyChecked("pre_existing_conditions_row")){
          is_valid  = false;
        }
      }
      if(!is_valid) Event.stop(e);
    },

    validateOnReturnKey: function(e){
      if(e.keyCode == Event.KEY_RETURN){
        this.validateFields(e);
        Event.stop(e);
      }
    },

    /***************************************************************************************************
     *Custom functions
     */

    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();
      }
    },

    showPreExisting : function(){
      if($("pre_existing_1").checked){
        $("pre_existing_conditions_row").show();
        $("pre_existing_conditions_error").addClassName("validate-any-checkboxes");
      }else{
        $("pre_existing_conditions_row").hide();
        $("pre_existing_conditions_error").removeClassName("validate-any-checkboxes");
        var PECs = Form.getElements($("pre_existing_conditions_row"));
        PECs.each(function(e){ return (e.checked = false); });
      }
    },

    showExistingCarrier : function(){
      ($("has_existing_carrier_1").checked) ? this.showAddValidations("#existing_carrier_row",$("existing_carrier_error"),"required") : this.hideRemoveValidations("#existing_carrier_row",$("existing_carrier_error"),"required");
    },

    spouseValidation : function(){
      if($("gender2").value != ""    || $("dob2_mm_on").value != ""   ||
        $("dob2_dd_on").value != "" || $("dob2_yyyy_on").value != "" ||
        $("insured2_height_feet").value != "" || $("insured2_height_inches").value != "" ||
        $("insured2_weight").value != ""){
        $("gender2_error").addClassName("required");
        $("dob2_on_error").addClassName("validate-dob");
        $("insured2_height_error").addClassName("validate-height");
        $("insured2_weight_error").addClassName("validate-number");
      }else{
        $("gender2_error").removeClassName("required");
        $("dob2_on_error").removeClassName("validate-dob");
        $("insured2_height_error").removeClassName("validate-height");
        $("insured2_weight_error").removeClassName("validate-number");
        clearErrorMessage("gender2_error");
        clearErrorMessage("dob2_on_error");
        clearErrorMessage("insured2_height_error");
        clearErrorMessage("insured2_weight_error");
        $("dob2_mm_on").clear();
        $("dob2_dd_on").clear();
        $("dob2_yyyy_on").clear();
        $("insured2_height_feet").clear();
        $("insured2_height_inches").clear();
        $("insured2_weight").clear();
      }
    }
  }

  function showChildren(el) {
    var num_children = $F(el);

		(num_children%2) ? switchHealthInfoStripes(true) : switchHealthInfoStripes(false);

    if($("help_text") != undefined){
      (num_children > 0) ? $("help_text").show() : $("help_text").hide();
    }

    for (var i = 0;i <= 5; i++) {
      var id      = 'child_option' + i;
      var insured = i + 3;

      if ( i < num_children ) {
        $(id).show();
        changeValidationForInsured(insured, true);
      } else {
        $(id).hide();
        $('gender' + insured).value = '';
        changeValidationForInsured(insured, false);
      }
    }
  }

  function changeValidationForInsured(insured, add_validations) {
    var id      = 'gender' + insured;
    var gender  = $F(id);

    if ( add_validations ) {
      $(id + '_error').addClassName('required');
      $('insured' + insured + '_height_error').addClassName('validate-height');
      $('insured' + insured + '_weight_error').addClassName('validate-number');
      $('dob' + insured + '_on_error').addClassName('validate-child-dob');
    } else {
      $(id).clear();
      $('insured' + insured + '_height_feet').clear();
      $('insured' + insured + '_height_inches').clear();
      $('insured' + insured + '_weight').clear();
      $('dob' + insured + '_mm_on').clear();
      $('dob' + insured + '_dd_on').clear();
      $('dob' + insured + '_yyyy_on').clear();
      $(id + '_error').removeClassName('required');
      $('insured' + insured + '_height_error').removeClassName('validate-height');
      $('insured' + insured + '_weight_error').removeClassName('validate-number');
      $('dob' + insured + '_on_error').removeClassName('validate-child-dob');
      $(id + '_error').hide();
      $('insured' + insured + '_height_error').hide();
      $('insured' + insured + '_weight_error').hide();
      $('dob' + insured + '_on_error').hide();
    }
  }

  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
  }

	function switchHealthInfoStripes(to_paint){
		if(to_paint){
			$("spacer_child").addClassName("even");
			$("has_existing_carrier_row").removeClassName("even");
			$("existing_carrier_row").removeClassName("even");
			$("reqdate_begin_row").addClassName("even");
			$("takes_medications_row").removeClassName("even");
			$("insured1_current_medications_detail_row").removeClassName("even");
			$("pre_existing_row").addClassName("even");
			$("pre_existing_conditions_row").addClassName("even");
		}else{
			$("spacer_child").removeClassName("even");
			$("has_existing_carrier_row").addClassName("even");
			$("existing_carrier_row").addClassName("even");
			$("reqdate_begin_row").removeClassName("even");
			$("takes_medications_row").addClassName("even");
			$("insured1_current_medications_detail_row").addClassName("even");
			$("pre_existing_row").removeClassName("even");
			$("pre_existing_conditions_row").removeClassName("even");
		}
	}

  if (!(BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 6)) {
		FastInit.addOnLoad(function() {
			var health_form = new HealthForm();
		});
  }