
function validationReturn(data, fieldSelector)
{
   if (false == data.isvalid)
   {
      $(fieldSelector + "-error").html(data.errormessage).show();
   }
   else
   {
      $(fieldSelector + "-error").hide();
      $(fieldSelector).val(data.outputscreen);
   }
}

function addValidation(fieldSelector, dataType, isRequired, customError) {
   // isRequired is an optional argument set to TRUE if not supplied
   if (undefined === isRequired)
   {
      var isRequired = true;
   }
   // customError is an optional argument set to an empty string if not supplied
   if ( ! customError)
   {
      var customError = "";
   }

   $(fieldSelector).blur(function() {
                     if ( ! isRequired && 0 == $(this).val().length)
                     {
                        // Field is not required and has no value, so don't post for validation
                        // In case there was previously an error and now no data, remove error
                        $(fieldSelector + "-error").hide();
                     }
                     else
                     {
                        // Field is required or it is not required (optional) but has data
                        $.post("clientside-form-validation.php",
                        { type: dataType, value: $(this).val(), isrequired: isRequired, customerror: customError },
                        function(data) {
                           validationReturn(data, fieldSelector);
                        },
                        "json");
                     }

   });
}

function addDependentValidation(fieldSelector, dataType, dependentFieldSelector, dependentWhenValueIs, isRequired, customError) {
   // isRequired is an optional argument set to TRUE if not supplied
   if (undefined === isRequired)
   {
      var isRequired = true;
   }
   // customError is an optional argument set to an empty string if not supplied
   if ( ! customError)
   {
      var customError = "";
   }

   $(fieldSelector).blur(function() {
                     if ( ! isRequired && 0 == $(this).val().length)
                     {
                        // Field is not required and has no value, so don't post for validation
                        // In case there was previously an error and now no data, remove error
                        $(fieldSelector + '-error').hide();
                     }
                     else
                     {
                        // Now, only check this value if the dependent field has the correct value
                        if (dependentWhenValueIs == $(dependentFieldSelector).val())
                        {
                           // Field is required or it is not required (optional) but has data
                           $.post("clientside-form-validation.php",
                           { type: dataType, value: $(this).val(), isrequired: isRequired, customerror: customError },
                           function(data) {
                              validationReturn(data, fieldSelector);
                           },
                           "json");
                        }
                     }

   });
}

$(document).ready(function() {

   // If JavaScript is enabled, this code will show the FORM which is currently hidden
   $("form.quote").show();

   // Add DatePickers to the date fields
   $("#currenthomepolicyexpiration").datepicker({ changeYear: true, changeMonth: true, yearRange: "-2:+2" });
   $("#homeownerdateofbirth").datepicker({ changeYear: true, changeMonth: true, yearRange: "-100:+0" });
   $("#homeowner2dateofbirth").datepicker({ changeYear: true, changeMonth: true, yearRange: "-100:+0" });

   // Enable client-side validation on the following fields:
   addValidation("#email", "email");
   addValidation("#phone", "phone");
   addValidation("#firstname", "name", true, 'A valid First Name is required');
   addValidation("#lastname", "name", true, 'A valid Last Name is required');
   addValidation("#insurancetype", "notNull", true, 'Please select the type of insurance to request a quote');

   // Add validation of values entered but values are not required
   addValidation("#zip", "zipCode", false);
   addValidation("#bodilyinjuryliability", "wholeDollars", false);
   addValidation("#propertydamageliability", "wholeDollars", false);
   addValidation("#medicalpayments", "wholeDollars", false);
   addValidation("#uninsuredmotoristliability", "wholeDollars", false);
   addValidation("#uninsuredmotoristproperty", "wholeDollars", false);
   addValidation("#underinsuredmotoristliability", "wholeDollars", false);
   addValidation("#underinsuredmotoristproperty", "wholeDollars", false);
   addValidation("#comprehensivedeductible", "wholeDollars", false);
   addValidation("#collisiondeductible", "wholeDollars", false);

   // Copy the addvalidation() code (don't call it directly) and then inside the BLUR event's fuction
   // have the IF check (if ID has VALUE) then do the post
   addDependentValidation("#yearconstructed", "year", "#insurancetype option:selected", "home");
   addDependentValidation("#estreplacementcost", "wholeDollars", "#insurancetype option:selected", "home");

   $("#insurancetype").change(function(event)
   {
      var $insType = $("#insurancetype option:selected").val();

      // Hide all, then only display the chosen one
      $("#auto-fields").slideUp(1000);
      $("#home-fields").slideUp(1000);

      if ("home" == $insType)
      {
         $("#home-fields").slideDown(1000);
      }
      else if ("auto" == $insType)
      {
         $("#auto-fields").slideDown(1000);
      }
   });

   $("#pastclaims").change(function(event)
   {
      if ("Yes" == $("#pastclaims option:selected").val())
      {
         $("#number-of-claims-question").show();
      }
      else
      {
         $("#number-of-claims-question").hide();
         $("#claim-info-area").empty();
      }
   });

   $("#numberofclaims").keyup(function(event)
   {
      $("#claim-info-area").empty();

      val = $("#numberofclaims").val();
      if (val == parseInt(val,10))
      {
         val = parseInt(val,10);
         if (val > 0)
         {
            $("#numberofclaims-error").hide();

            for (i = 1; i <= val; i++)
            {
               $clonedData = $("#claim-info-clone").clone();
               $clonedData.find("legend").append(i);
               $clonedData.appendTo("#claim-info-area").show();
            }

         }
         else
         {
            $("#numberofclaims-error").show().html("Number of claims must be 1 or more");
         }
      }
      else
      {
         $("#numberofclaims-error").show().html("Please use digits to for the number of claims");
      }
   });

   $("#addauto-button").click(function(event) {
                                 $clonedData = $("#auto-info-clone").clone();
                                 $clonedData.appendTo("#auto-info-area").show();
                              });

   $("#adddriver-button").click(function(event) {
                                 $clonedData = $("#driver-info-clone").clone();
                                 $clonedData.appendTo("#driver-info-area").show();
                              });

   $("#addaccident-button").click(function(event) {
                                 $clonedData = $("#accident-info-clone").clone();
                                 $clonedData.appendTo("#accident-info-area").show();
                              });

   $("#bodilyinjuryliability1").change(function(event)
   {
      //alert($("#bodilyinjuryliability1 option:selected").val());
      if ("$25,000/$50,000" == $("#bodilyinjuryliability1 option:selected").val() ||
          "$50,000/$100,000" == $("#bodilyinjuryliability1 option:selected").val())
      {
         $("#liability-fine-print").show();
      }
      else
      {
         $("#liability-fine-print").hide();
      }
	  
   });
   
   $("#underinsuredmotoristliability1").change(function(event)
   {
      if ("$25,000/$50,000" == $("#underinsuredmotoristliability1 option:selected").val() ||
          "$50,000/$100,000" == $("#underinsuredmotoristliability1 option:selected").val())
      {
         $("#liability-fine-print2").show();
      }
      else
      {
         $("#liability-fine-print2").hide();
      }
	  
   });
   
   $("#uninsuredmotoristliability1").change(function(event)
   {
      if ("$25,000/$50,000" == $("#uninsuredmotoristliability1 option:selected").val() ||
          "$50,000/$100,000" == $("#uninsuredmotoristliability1 option:selected").val())
      {
         $("#liability-fine-print3").show();
      }
      else
      {
         $("#liability-fine-print3").hide();
      }
	  
   });


});

