$(document).ready(function() {

  $('#home #Quote').submit(function() {
    var $form = $(this),
        $odometer = $form.find('#odometer'),
        mileage = parseInt($odometer.val(),10) || 0,
        make = $form.find('#make_model').val().toLowerCase();

    var threesix = /chevrolet|pontiac|saturn|gmc/.test(make);
     
    var err = false;
    var msg = 'The form you are trying to submit has the following errors:' + "\n";

    if ( !(/\d{4}/).test($('#year').val())) {
      msg += 'You must select the year of your vehicle' + "\n";
      err = true;
    }
    if (!$('#make_model').val() ) {
      msg += 'You must enter your vehicle\'s make and model' + "\n";
      err = true;
    }
    if ( !$('#odometer').val() ) {
      msg += 'You must indicate the estimated vehicle mileage' + "\n";
      err = true;
    } 

    if (err == true){
      alert(msg);
      return false;
    } else if ((mileage > 50000) || (mileage > 36000 && threesix)) {
      $form.html('<p class="note"><strong>Your odometer reading indicates that your new-car warranty from the factory has expired.</strong><br><br>Fortunately, your vehicle still qualifies for complete protection through one of our A+ superior affiliate companies.<br><br>Call <strong>1-866-255-5546</strong> now for your free price quote.</p>').find('p').slideDown();
      return false;
    }
    

  });


// convert odometer to number 

  $('#odometer').blur(function() {
    var $this = $(this);
    var thisVal = $(this).val();
    if ( thisVal) {
      var pattern = /\D/g;
      thisVal = thisVal.replace(pattern, '');
      $this.val(thisVal);
    }
  
  });

//bind quote2 page validation to submit  
  $('#Major_Gard').submit(function() {
    var errorMessage = '';
    var errorCount = 0;
    $('td.required').each(function(index) {
      var thisValue = $(this).children(':first-child').val();
      var thisLabel = $(this).prev().text();
      thisLabel = thisLabel.slice(0,thisLabel.lastIndexOf(':'));

      if ($.trim(thisValue) == ''||thisValue=="Choose State") {
        errorMessage += thisLabel + "\n";
        errorCount++;
      }
    });
    if (errorCount > 0) {
      var errorPre = 'Before proceeding, you must enter information in the following ' + errorCount + ' fields:' + "\n";
     alert(errorPre + errorMessage);
     return false;
    }

  });


});
