﻿
//  $(document).ready(function () {
//      $("#LoginForm").validate({
//          rules: {
//              Agency: { required: true },
//              Password: { required: true }
//          },
//          messages: {
//              Agency: { required: "Please enter your Username " },
//              Password: { required: "Please provide a password" }
//          }
//      });
//  });

	


//jQuery.validator.addMethod("checkOperations", function (value, element, params) {
//            var timeShare = $("#WorkersComp.Operations").val();
//            return this.optional(element) || value == 1;
//        }, "MGALive does not provide coverage for Timeshare operations."); 


jQuery.validator.addMethod("vacancyRate", function (value, element) {
     return this.optional(element) || value < 16; 
     });"Vacancy Rate greater than 15%"

jQuery.validator.addMethod("buildinglocation", function (value, element) {
    if ($("#LocationsList").val() > 0) {
        return this.optional(element) || true;
    }
    else {
        return this.optional(element) || false;
    }
}, "You must select a location to add the building to.");

jQuery.validator.addMethod("OrgType", function (value, element) {
    if ($("#OrgType").val() != "0") {
        return this.optional(element) || true;
    }
    else {
        return this.optional(element) || false;
    }
}, "You must select an Organization Type.");

jQuery.validator.addMethod("InsuranceLimits", function (value, element) {
    if ($("#HasFullLimits").is(":checked") || $("#Dic.OptionalStopLoss").val() > 0) {
      
       return true;
    }
    else {
       return false;
    }
}, "You must select either Full Limits or Optional Stop Loss.");
jQuery.validator.addMethod("DeductibleMax", function (value, element) {
    var count = 0;
    if ($("#HasDeductibleLowest").is(":checked")) {
        count++;
    }
    if ($("#HasDeductible10Pct").is(":checked")) {
        count++;
    }
    if ($("#HasDeductible15Pct").is(":checked")) {
        count++;
    }
    if ($("#HasDeductible20Pct").is(":checked")) {
        count++;
    }
    if ($("#HasDeductible25Pct").is(":checked")) {
        count++;
    }
    if (count > 2 || count == 0) {
        return false;
    }
    else {
       return true;
    }


}, "Select 1 or 2 deductible options.");
//jQuery.validator.addMethod("DeductibleMin", function (value, element) {
//    var count = 0;
//    if ($("#HasDeductibleLowest").is(":checked")) {
//        count++;
//    }
//    if ($("#HasDeductible10Pct").is(":checked")) {
//        count++;
//    }
//    if ($("#HasDeductible15Pct").is(":checked")) {
//        count++;
//    }
//    if ($("#HasDeductible20Pct").is(":checked")) {
//        count++;
//    }
//    if ($("#HasDeductible25Pct").is(":checked")) {
//        count++;
//    }
//    if (count === 0 ) {
//        return  false;
//    }
//    else {
//        return  true;
//    }
//}, "ksdjfjaskdjfk");


jQuery.validator.addMethod("OrgOther", function (value, element) {
    if ($("#OrgType").val() != "O" && $("#Insured.OrganizationOther").val() == "") {
        return this.optional(element) || false;
    }
    else {
        return this.optional(element) || true;
    }
}, "You must select an Organization Type.");
jQuery.validator.addMethod("directEmployees", function (value, element) {                
         if ($("#DirectEmployees_Yes").is(":checked")) {             
             return this.optional(element) || false;
         } else {
             return this.optional(element) || true;
         }
     }, "Direct Employees cannot be covered"); 

jQuery.validator.addMethod("otherStates", function (value, element) {      
      if ($("#OtherStates_Yes").is(":checked")) {          
          return this.optional(element) || false;
      } else {
          return this.optional(element) || true;
      }
  }, "HOA with multi-state locations cannot be covered");

jQuery.validator.addMethod("priorLoss", function (value, element) {
    if ($("#PriorLoss_Yes").is(":checked") && $("#WorkersComp_PriorLossExplanation:blank").length) {
          return this.optional(element) || false;
      } else {
          return this.optional(element) || true;
      }
      }, "Explanation for a Prior Loss must be provided for review");

  jQuery.validator.addMethod("priorInjury", function (value, element) {    
    if ($("#PriorInjury_Yes").is(":checked") && $("#WorkersComp_PriorInjuryExplanation:blank").length) {
          return this.optional(element) || false;
      } else {
          return this.optional(element) || true;
      }
      }, "Explanation for a Prior Injury must be provided for review");

  jQuery.validator.addMethod("specialTrips", function (value, element) {     
      if ($("#Trips_Yes").is(":checked") && $("#WorkersComp_TripsExplanation:blank").length) {
          return this.optional(element) || false;
      } else {
          return this.optional(element) || true;
      }
  }, "Explanation for Special Activities must be provided for review");

//  jQuery.validator.addMethod("timeShare", function (value, element) {
//      if ($("#Operations1").is(":checked")) {
//          alert("is checked");
//          return this.optional(element) || false;
//      } else {
//          return this.optional(element) || true;
//      }
//  }, "Timeshare Operations cannot be covered");

    jQuery.validator.addMethod("PObox", function (value, element, params) {         
             var poboxPattern = /^(P\s*\.?\s*)(O\s*.?\s*)(Box)/i;
             var testResult = poboxPattern.test(value);
             var retCode;
             var msg = params.msg;
             var error = ": Post Office box cannot be used";
             var errormsg = msg + error;
             if (testResult) {
                 retCode = false;
                 var validator = this;
                 $.validator.messages.PObox = msg;             
             } else { retCode = true };
             return this.optional(element) || retCode
         } );

        


    

//----------------Workers Comp Form -------------------------------//

         $(document).ready(function () {
             $("#errorFS").hide();

             $("#WorkersCompForm").validate({

                 errorContainer: $('#errContainer'),
                 errorLabelContainer: $("ul", $('#errContainer')),
                 wrapper: "li",
                 errorClass: "err-state-error",
                 rules: {
                     'Insured.Name': { required: true },
                     'Insured.Phone': { required: true },
                     'Insured.ContactName': { required: true },
                     'Insured.State': { required: true },
                     'Insured.Address': { required: true, PObox: { msg: 'Insured Address: Post office box cannot be used'} },
                     'Insured.Address2': { PObox: { msg: 'Insured Address: Post office box cannot be used'} },
                     'Insured.Zip': { required: true },
                     'Quote.EffectiveDate': { required: true },
                     'Quote.ExpirationDate': { required: true },
                     'WorkersComp.Zip': { required: true },
                     'WorkersComp.State': { required: true },
                     'WorkersComp.VacancyRate': { vacancyRate: true },
                     'WorkersComp.FederalID': { required: true },
                     'WorkersComp.Address': { required: true, PObox: { msg: 'Location Address: Post Office box cannot be used'} },
                     'WorkersComp.Address2': { PObox: { msg: 'Location Address: Post Office box cannot be used'} },
                     'WorkersComp.Operations': { required: true },
                     'WorkersComp.DirectEmployees': { required: true },
                     'WorkersComp.OtherStates': { required: true },
                     'WorkersComp.PriorLoss': { required: true },
                     'WorkersComp.PriorInjury': { required: true },
                     'WorkersComp.Trips': { required: true },
                     'WorkersComp.SubcontractedAdditionalInsured': { required: true }

                     //                 'WorkersComp.DirectEmployees': { directEmployees: true },
                     //                                      'WorkersComp.OtherStates': { otherStates: true },
                     //                                      'WorkersComp.PriorLoss': { priorLoss: true },
                     //                                      'WorkersComp.PriorInjury': { priorInjury: true },
                     //                                      'WorkersComp.Trips': { specialTrips: true }

                 },
                 messages: {
                     'Insured.Name': { required: 'Insured Name is required' },
                     'Insured.Phone': { required: 'Insured Phone is required' },
                     'Insured.ContactName': { required: 'Insured Contact Name is required' },
                     'Insured.State': { required: 'Insured State is required' },
                     'Insured.Address': { required: 'Insurance Address: Address is required', Pobox: 'Insured Address: Post office box cannot be used' },
                     'Insured.Address2': { Pobox: 'Insured Address: Post Office box cannot be used' },
                     'Insured.Zip': { required: 'Insured Zip Code is Required' },
                     'Quote.EffectiveDate': { required: 'Effective Date is Required' },
                     'Quote.ExpirationDate': { required: 'Expiration Date is Required' },
                     'WorkersComp.FederalID': { required: 'Federal ID is required' },
                     'WorkersComp.State': { required: 'Location State is required' },
                     'WorkersComp.VacancyRate': { vacancyRate: 'Vacancy Rate must be lower than 15%' },
                     'WorkersComp.DirectEmployees': { directEmployees: 'Direct Employees cannot be covered' },
                     'WorkersComp.Address': { required: 'Location Address is required', Pobox: 'Location Address: Post Office box cannot be used' },
                     'WorkersComp.Address2': { Pobox: 'Location Address: Post Office box cannot be used' },
                     'WorkersComp.Zip': { required: 'Location Zip Code is Required' },
                     'WorkersComp.PriorLoss': { priorLoss: 'Explanation for a Prior Loss must be provided for review' },
                     'WorkersComp.Operations': { required: 'Please select a Description of Operations.' },
                     'WorkersComp.DirectEmployees': { required: 'Please answer if the Association has Direct Employees.' },
                     'WorkersComp.OtherStates': { required: 'Please answer if the HOA has locations in other states.' },
                     'WorkersComp.PriorLoss': { required: 'Please answer if the association has experienced a workers compensation loss.' },
                     'WorkersComp.PriorInjury': { required: 'Please answer if the association is aware of any injuries onsite that may result in a workers compensation loss.' },
                     'WorkersComp.Trips': { required: 'Please answer if the association holds special activities.' },
                     'WorkersComp.SubcontractedAdditionalInsured': { required: 'Please answer if there is evidence of Workers Compensation coverage.' }
                 },


                 //             submitHandler: function (form) {
                 //                 $("#errorContainer").hide();
                 //                 $("#errorFS").hide();
                 //                 $("#errorMessage").hide();
                 //                 $("#InsuredCity").removeAttr('disabled');
                 //                 $("#InsuredState").removeAttr('disabled');
                 //                 $("#LocationCity").removeAttr('disabled');
                 //                 $("#LocationState").removeAttr('disabled');
                 //                 $("input[name='Quote.ExpirationDate']").removeAttr('disabled');
                 //                 jQuery(form).ajaxSubmit({
                 //                     type: 'POST',
                 //                     success: function (result) {
                 //                         var agencycode = $('#AgencyCode').val();
                 //                         var quotecode = $('#QuoteCode').val();
                 //                         $('#QuoteStatusDiv').load('/WorkersComp/QuoteStatus', { id: agencycode, code: quotecode },
                 //                          function () {
                 //                             
                 //                              if ($('#UpdateStatus').attr('checked')) {                                 
                 //                                  $("#ReferSuccess").dialog({ height: 150, modal: true, dialogClass: 'alert', buttons:
                 //                                      [{ text: "Return to Agency Homepage", click: function () { window.location = "/Agency/Index"; return false; } },
                 //                                      { text: "Review Quote", click: function () { $(this).dialog("close"); } }
                 //                                      ], width: 450, height: 200
                 //                                  });
                 //                                  var quotestatus = "R";
                 //                                  $('#menucontainerApp').load('/WorkersComp/WcMenu', { id: quotestatus });
                 //                              }
                 //                              else {
                 //                                  $("#SaveSuccess").dialog({ height: 150, modal: true, dialogClass: 'alert', buttons:
                 //                                        [{ text: "Return to Agency Homepage", click: function () { window.location = "/Agency/Index"; return false; } },
                 //                                            { text: "Edit Quote", click: function () { $(this).dialog("close"); } }
                 //                                        ], width: 450, height: 200
                 //                                      });
                 //                                  }
                 //                              });
                 //                            }
                 //                        });                 
                 //                    },

                 onkeyup: false,
                 invalidHandler: function (e, validator) {
                     var errors = validator.numberOfInvalids();
                     if (errors) {

                         $("#errorFS").show();
                     } else {
                         $("#errorFS").hide();

                     }
                 },
                 unhighlight: function (element, errorClass) {
                     $(element).removeClass(errorClass);
                 }
             });
         });

//----------------Add Agency Form -------------------------------//

     $(document).ready(function () {
         $.validator.setDefaults({
             highlight: function (input) {
                 $(input).addClass("ui-state-error");
             },
             unhighlight: function (input) {
                 $(input).removeClass("ui-state-error");
             }
         });

         jQuery.validator.messages.required = "";
         $("#AddAgencyForm").validate({
             rules: {
                 'Agency.AgencyCode': { required: true },
                 'Agency.AgencyName': { required: true },
                 'Agency.City': { required: true },
                 'Agency.State': { required: true },
                 'Agency.Zip': { required: true },
                 'Agency.Phone': { required: true },
                 'Agency.ContactName': { required: true },
                 'Agency.EmailAddress': { required: true },                 
                 'Agency.Password': { required: true },
                 'Agency.LicenseNumber': { required: true }
             },
             
             submitHandler: function (form) {
                 alert("Im here");
                 $("#Agency.City").removeAttr('disabled');
                 $("#Agency.State").removeAttr('disabled');
                 jQuery(form).ajaxSubmit({
                     type: 'POST',
                     success: function (result) {
                         $("#AddAgencySuccess").dialog({ height: 150, modal: true, dialogClass: 'alert', buttons:
                                        [{ text: "Ok", click: function () { $(this).dialog("close"); } }
                                        ], width: 450, height: 200
                         });
                     }
                 });
             },
             invalidHandler: function(e, validator) {
			    var errors = validator.numberOfInvalids();
			    if (errors) {				    
				    $("#errorFS").show();
			    } else {
				    $("#errorFS").hide();
			    }
		    }
//             onkeyup: false,
//             invalidHandler: function (e, validator) {
//                 var errors = validator.numberOfInvalids();
//                 if (errors) {

//                     $("#errorFS").show();
//                 } else {
//                     $("#errorFS").hide();

//                 }
//             },
//             unhighlight: function (element, errorClass) {
//                 $(element).removeClass(errorClass);
//             }
         });
     });

$(document).ready(function () {
    $.validator.setDefaults({
        highlight: function (input) {
            $(input).addClass("ui-state-error");
        },
        unhighlight: function (input) {
            $(input).removeClass("ui-state-error");
        }
    });

    jQuery.validator.messages.required = "";
    $("#UpdateAgencyForm").validate({
        rules: {
            'Agency.AgencyCode': { required: true },
            'Agency.AgencyName': { required: true },
            'Agency.City': { required: true },
            'Agency.State': { required: true },
            'Agency.Zip': { required: true },
            'Agency.Phone': { required: true },
            'Agency.ContactName': { required: true },
            'Agency.EmailAddress': { required: true },
            'Agency.Password': { required: true },
            'Agency.LicenseNumber': { required: true }
        },

        submitHandler: function (form) {
            alert("Im here");
            $("#Agency.City").removeAttr('disabled');
            $("#Agency.State").removeAttr('disabled');
            jQuery(form).ajaxSubmit({
                type: 'POST',
                success: function (result) {
                    $("#AddAgencySuccess").dialog({ height: 150, modal: true, dialogClass: 'alert', buttons:
                                        [{ text: "Ok", click: function () { $(this).dialog("close"); } }
                                        ], width: 450, height: 200
                    });
                }
            });
        },
        invalidHandler: function (e, validator) {
            var errors = validator.numberOfInvalids();
            if (errors) {
                $("#errorFS").show();
            } else {
                $("#errorFS").hide();
            }
        }
        //             onkeyup: false,
        //             invalidHandler: function (e, validator) {
        //                 var errors = validator.numberOfInvalids();
        //                 if (errors) {

        //                     $("#errorFS").show();
        //                 } else {
        //                     $("#errorFS").hide();

        //                 }
        //             },
        //             unhighlight: function (element, errorClass) {
        //                 $(element).removeClass(errorClass);
        //             }
    });
});


//     jQuery(document).ready(function () { $.validator.addMethod('validate-checkbox-oneormore', function (value) { return $('.require-one:checked').size() != 0; }, 'need a value'); jQuery('#itemForm').validate({}); }); 




             //             submitHandler: function (form) {
             //                 $("#errorFS").hide();
             //                 $("WorkersCompForm").ajaxSubmit( {
             ////                        url: "ContactHandler.ashx",
             //                        success: function(result)
             ////                          alert("Your Application has been submitted");
             //                    }) },


//     switch (value){
//            case 1: 
//            errormsg = "MGALive does not provide coverage for Timeshare operations. Please contact an MGAlive representative.";
//            break;
//            case 2: 
//            statement;
//            break;
//            "
//            "
//            default : statement;
//            }

//jQuery.validator.addMethod("greaterThanZero", function(value, element) {
//      
//      var ops = $(WorkersComp.Operations).val();
//       if (ops == 1) {
//         return true

//         $.validator.addMethod("checkOperations", function(value, element) { 
//            var isAus = $("#Country").val() === "Aus";
//                  if ( isAus && value.length < 4 ) {
//                           return false;     } 
//                           else return true;  }, "Australian Zip Code Must Be at least 4 Digits"); 


//$.validator.setDefaults({
////    submitHandler: function () { alert("submitted!"); },
//    highlight: function (input) {
//        $(input).addClass("ui-state-error");
//    },
//    unhighlight: function (input) {
//        $(input).removeClass("ui-state-error");
//    }
//});
$("#WCForm input:not(:submit)").addClass("ui-widget-content");

//$(":submit").button();

//$.validator.addMethod('regexp', function(value, element, param) {
//  return this.optional(element) || value.match(param); // Compare with regular expression
//}, 'Please enter a matching formatted value.');

//$.validator.addMethod('requiredDefault', function(value, element, param) {
//   return value && value != param; // Compare with blank and default (parameter) value
//   }, 'Please enter a value.');

//jQuery.validator.addMethod("rangeWords", function (value, element, params) {
//    return this.optional(element) || stripHtml(value).match(/\b\w+\b/g).length >= params[0] && value.match(/bw+b/g).length < params[1];
//}, jQuery.validator.format("Please enter between {0} and {1} words."));


//jQuery.validator.addMethod('regex', function (value, element, param) {
//    return this.optional(element) ||
//            value.match(typeof param == 'string' ? new RegExp(param) : param);

//jQuery.validator.addMethod(
//      "PObox",
//      function(value, element, regexp) {
//         var check = false;         
//          return this.optional(element) || regexp.test(value);
//      }, "PO Boxes are not allowed.");

//      $.validator.addMethod('PObox', function (value, element, param) {
//  return this.optional(element) || value.match(param);
//  },
// 'PO Boxes are not allowed');


$(document).ready(function () {
    $("#DICForm").validate({
        errorContainer: $('#errContainer'),
        errorLabelContainer: $("ul", $('#errContainer')),
        wrapper: "li",
        errorClass: "err-state-error",
        rules: {
            'Insured.Name': { required: true },
            'Insured.Phone': { required: true },
            'Insured.ContactName': { required: true },
            'Insured.State': { required: true },
            'Insured.Address': { required: true, PObox: { msg: 'Insured Address: Post office box cannot be used'} },
            'Insured.Address2': { PObox: { msg: 'Insured Address: Post office box cannot be used'} },
            'Insured.Zip': { required: true },
            'OrgType': { OrgType: { msg: 'An Organization Type must be selected'} },
            'OrgOther': { OrgOther: { msg: 'You must enter the Organization Type when selecting Other'} },
            'Quote.EffectiveDate': { required: true },
            'Quote.ExpirationDate': { required: true }
          },
        messages: {
            'Insured.Name': { required: 'Insured Name is required' },
            'Insured.Phone': { required: 'Insured Phone is required' },
            'Insured.ContactName': { required: 'Insured Contact Name is required' },
            'Insured.State': { required: 'Insured State is required' },
            'Insured.Address': { required: 'Insurance Address: Address is required', Pobox: 'Insured Address: Post office box cannot be used' },
            'Insured.Address2': { Pobox: 'Insured Address: Post Office box cannot be used' },
            'Insured.Zip': { required: 'Insured Zip Code is Required' },
            'OrgOther': { OrgOther: { msg: 'You must enter the Organization Type when selecting Other'} },
            'Quote.EffectiveDate': { required: "Please enter a EffectiveDate " },
            'Quote.ExpirationDate': { required: "Please provide an Expiration Date" }
          },
          onkeyup: false,
          invalidHandler: function (e, validator) {
              var errors = validator.numberOfInvalids();
              if (errors) {

                  $("#errorFS").show();
              } else {
                  $("#errorFS").hide();

              }
          },
          unhighlight: function (element, errorClass) {
              $(element).removeClass(errorClass);
          }
      });
  });


  $(document).ready(function () {
      $("#DICLocationForm").validate({
          errorContainer: $('#errContainer'),
          errorLabelContainer: $("ul", $('#errContainer')),
          wrapper: "li",
          errorClass: "err-state-error",
          rules: {
              'Location.LocationAddress': { required: true },
               'Location.LocationZip': { required: true }
          },
          messages: {
              'Location.LocationAddress': { required: 'Location Address is required' },
               'Location.LocationZip': { required: 'Location Zipcode is required' }
          },
          onkeyup: false,
          invalidHandler: function (e, validator) {
              var errors = validator.numberOfInvalids();
              if (errors) {

                  $("#errorFS").show();
              } else {
                  $("#errorFS").hide();

              }
          },
          unhighlight: function (element, errorClass) {
              $(element).removeClass(errorClass);
          }
      });
  });


  $(document).ready(function () {
      $("#DICBuildingForm").validate({
          errorContainer: $('#errContainer'),
          errorLabelContainer: $("ul", $('#errContainer')),
          wrapper: "li",
          errorClass: "err-state-error",
          rules: {
              'Building.BuildingDescription': { required: true },
              'LocationsList': { buildinglocation: true }

          },
          messages: {
              'Building.BuildingDescription': { required: 'Building Description is required' },
              'LocationsList': { buildinglocation: 'You must select a location to add the buiilding to.'}
          },
          onkeyup: false,
          invalidHandler: function (e, validator) {
              var errors = validator.numberOfInvalids();
              if (errors) {

                  $("#errorFS").show();
              } else {
                  $("#errorFS").hide();

              }
          },
          unhighlight: function (element, errorClass) {
              $(element).removeClass(errorClass);
          }
      });
  });
  $(document).ready(function () {
      $("#DICInsuranceForm").validate({

          errorContainer: $('#errContainer'),
          errorLabelContainer: $("ul", $('#errContainer')),
          wrapper: "li",
          errorClass: "err-state-error",
          rules: {

              'Dic.HasDeductibleLowest': { DeductibleMax: true },
              'Dic.HasFullLimits': { InsuranceLimits: true }


          },
          messages: {


      },
      onkeyup: false,
      focusCleanup: true,
      invalidHandler: function (e, validator) {
          var errors = validator.numberOfInvalids();
          if (errors) {

              $("#errorFS").show();
          } else {
              $("#errorFS").hide();

          }
      },
      unhighlight: function (element, errorClass) {
          $(element).removeClass(errorClass);
          
      }
  });
});
