jQuery.validator.setDefaults({
    ignore: ':hidden',
    showErrors: function(errorMap, errorList)
    {
      this.defaultShowErrors();
      jQuery('.errorWrapper').each(function()
        {
          if ( jQuery(this).find('label.error:visible').length == 0 )
          {
            jQuery(this).remove();
          }
        });
    },
    errorPlacement: function(error, element)
    {
      element.parent().append(error)
    },
    wrapper: 'p class="errorWrapper"'
  });

jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, "");
  	return this.optional(element) || (phone_number.length > 9 && phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/));
}, "Please specify a valid phone number");

jQuery.validator.addMethod("alphanum", function(text, element) {
return this.optional(element) || text.match(/^[a-zA-Z0-9 \s  \.]+$/);
 }, "Please enter only alphanumeric characters (A-Z a-z 0-9).");
jQuery.validator.classRuleSettings.phoneUS = {phoneUS: true};
jQuery.validator.classRuleSettings.alphanum = {alphanum: true};

jQuery(document).ready(function()
{
  /* step 1 */
  jQuery('#account_name').focus();
  /* step 3 */
  if(jQuery('#step3_rms_module_brand_other').val())
  {
    jQuery('#step3_rms_csi_brand_other').show();
  }
  if (jQuery('#step3_gms_module_brand_other').val())
  {
    jQuery('#step3_gms_thinfilm_brand_other').show();
    jQuery('#step3_gms_csi_brand_other').show();
  }
  /* step 4 */
  jQuery('#roofing_information_c').change(function()
    {
    jQuery('p.roofing_').hide();
    jQuery('#' + jQuery(this).val()).show();
    });
  jQuery('#Single_Ply').change(function()
    {
    jQuery('#attachment').show();
    });

  jQuery('#attachment').change(function()
    {
      jQuery('#manufacturer').show();
    });

  jQuery('#roofing_information_c').trigger('change');

  jQuery('#request_a_quote_step1').validate({
    rules:
    {
      'step1[returning_password2]': { equalTo: '#returning_password1' }
    },
    messages:
    {
      'step1[returning_password2]': {
        equalTo: 'Passwords do not match'
      }
    }
  });/**/

  jQuery('#referred_by_c').change(function()
  {
    jQuery('.referred_by_c').hide().find('input');
    if ( jQuery(this).find(':selected').hasClass('expander') )
    {
      jQuery('#' + jQuery(this).val()).show();
    }
  });
  jQuery('#referred_by_c').trigger('change');

  jQuery('.request_a_quote_form').validate();

  jQuery('#step2_quote_fork').click(function()
    {
    if ( jQuery("#step2_state option:selected").hasClass("province") )
    {
      jQuery("#us_can").val('can');
    }
    else
    {
      jQuery("#us_can").val('us');
    }
    });

  jQuery('.toggle_module_type').click(function()
  {
    jQuery('.module_type').hide();
    jQuery('.' + jQuery(this).val()).show();
  });

  jQuery('.toggle_other').change(function()
  {
    if ( jQuery(this).val() == 'Other' )
    {
      jQuery(this).parents('p').siblings('.other').show()
    }
    else
    {
      jQuery(this).parents('p').siblings('.other').hide()
    }
  });
  jQuery('.toggle_other').trigger('change');
  // establish initial value
  jQuery('#product_address_state').change();/**/
  
  // Activate validation on white-paper form & InterSolar form
  jQuery('#white-paper').validate();
  jQuery('#intersolar').validate();
  
});

