// JavaScript Document
  var HealthForm = Class.create();
  HealthForm.prototype =
  {
    initialize: function(form_id){
      $("number_of_children").value = 0;
      this.initializeDisplay();
      this.initializeEvents();
      this.lockReturnKey  = false;
      this.form_elements  = $$(".validation-advice");
    },

    /***************************************************************************************************
     *initialize the elements of the form, hide and show as directed
     */
    initializeDisplay : function(){
      showChildren($("number_of_children"));

      this.showExistingCarrier();
      this.showTakesMedications();
      this.showPreExisting();
//////////////////////////////////////////////////////////////////////////////////////
//add by qgz
      this.showAdditional();
      this.showPreExistingText();
//////////////////////////////////////////////////////////////////////////////////////
    },

    /***************************************************************************************************
     *add event listeners for submit button, return key and custom onclick/onchange
     */
    initializeEvents : function(){
      Event.observe(document, "keypress", this.validateOnReturnKey.bindAsEventListener(this));
      Event.observe("submit1", "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));
      
    //////////////////////////////////////////////////////////////////////////////////////
      //add by qgz
      Form.getElements($("pre_existing_conditions_row")).each(this.bindCheck.bind(this));      
//      Event.observe("pre_existing_conditions", "click", this.showPreExisting.bindAsEventListener(this));
      Event.observe("input_additional_1", "click", this.showAdditional.bindAsEventListener(this));
      Event.observe("input_additional_0", "click", this.showAdditional.bindAsEventListener(this));
      Event.observe("premiums100", "change", this.showExplain.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("add_spouse", "click", this.spouseValidation.bindAsEventListener(this));
      Event.observe("remove_spouse", "click", this.removeSpouseValidation.bindAsEventListener(this));
      
      Event.observe("add_child", "click", function(e){
        $("number_of_children").value = parseInt($F("number_of_children")) + 1
        showChildren($("number_of_children"));
        Event.stop(e);
      });
      Event.observe("remove_child", "click", function(e){
        $("number_of_children").value = parseInt($F("number_of_children")) - 1
        showChildren($("number_of_children"));
        Event.stop(e);
      });
    },
    
    showExplain:function(){
    	if ($('premiums100').value=="No")
    		$("has_existing_carrier_row3_1").show();
    	else
    		$("has_existing_carrier_row3_1").hide();
    },
    
    //////////////////////////////////////////////////////////////////////////////////////
    //add by qgz
    showAdditional : function(){
      if($("input_additional_1").checked){
      	$("input_additional_row").show();
      }
      else
      	$("input_additional_row").hide();
    },
    
    showPreExistingText : function(){
      var anyChecked=Form.getElements($("pre_existing_conditions_row")).any(function(em){return em.checked;});
      if (anyChecked) 
      	$("pre_existing_conditions_row2").show();
      else
      	$("pre_existing_conditions_row2").hide();
    },
    bindCheck:function(em){
	 		Event.observe(em, "click", this.showPreExistingText.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);
    },

    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;
        }
//////////////////////////////////////////////////////////////////////////////////////
//add by qgz
//        else
//        {
//        	if ($('pre_existing_details').value=='')
//        	{
//			      $("pre_existing_conditions_error_text").addClassName("validation-advice");
//			      is_valid      =false;
//			    }
//			    else
//        		$("pre_existing_conditions_error_text").removeClassName("validation-advice");
//        }
//////////////////////////////////////////////////////////////////////////////////////
      }
      
    //////////////////////////////////////////////////////////////////////////////////////
    //add by qgz
      if ($('email2').value!=$('FromEmailAddress').value)
      {
      	is_valid=false;
      	$('email1_error2').show();
      }
      else{
      	$('email1_error2').hide();
      }
    //////////////////////////////////////////////////////////////////////////////////////
      	
      
      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");
    //////////////////////////////////////////////////////////////////////////////////////
    //add by qgz
      	$("pre_existing_conditions_row2").hide();
    //////////////////////////////////////////////////////////////////////////////////////
        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("#has_existing_carrier_row",$("existing_carrier_error"),"required") : this.hideRemoveValidations("#has_existing_carrier_row",$("existing_carrier_error"),"required");
      if ($("has_existing_carrier_1").checked)
      {
      	$('has_existing_carrier_row2').show(); 
      	$('has_existing_carrier_row3').show(); 
      	$('has_existing_carrier_row4').show(); 
      	$('has_existing_carrier_row5').show(); 
      }
      else 
      {
      	$('has_existing_carrier_row2').hide();
      	$('has_existing_carrier_row3').hide();
      	$('premiums100').selectedIndex=0;
      	$('has_existing_carrier_row3_1').hide();
      	$('has_existing_carrier_row4').hide();
      	$('has_existing_carrier_row5').hide();
      }
    },

    spouseValidation : function(){
      $("spouse_row").show();
      $("gender2_error").addClassName("required");
      $("dob2_on_error").addClassName("validate-dob");
      $("insured2_height_error").addClassName("validate-height");
      $("insured2_weight_error").addClassName("validate-number");
    },
    
    removeSpouseValidation : function() {
      $("gender2").value = '';
      $("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 = parseInt($F(el));
    (num_children > 0 ) ? $("remove_child").show() : $("remove_child").hide();

    if($("help_text") != undefined){
      (num_children > 0) ? $("help_text").show() : $("help_text").hide();
    }

    for (var i = 0;i <= 4; 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-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-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("validation-advice");
    }else{
      $("pre_existing_conditions_error").removeClassName("validation-advice");
    }
    return is_valid
  }

  if (!(BrowserDetect.browser == "Explorer" && BrowserDetect.version < 6)) {
    Event.observe(window, "load", function() {
      var health_form = new HealthForm();
    });
  }
