$(document).ready(function() {
  $('a[@href*=feedback.htm?id]').click(function() {
    var thisHref = $(this).attr('href');
    var thisId = thisHref.lastIndexOf('=') + 1;
    thisId = thisHref.slice(thisId);
    var productId = "";
    if ($('#pid').length) {
      productId = '?p=' + $('#pid').val();
    };
    $('#frmSendMe').attr('action', '/contact/feedback.htm' + productId)
    .find('input[@name=hidContId]').val(thisId)
    .end()
    .submit();
    return false;
  });
});

/***************************************
   =FORM VALIDATION
   
* use the following for validation rules:
* class="required" : the *input* is a required field OR 
*                    the *div* contains checkboxes, one of which must be checked
* name="Email"     : the input value must be in proper email format
-------------------------------------- */

$(document).ready(function() {
  var stripeTable = function() {
    $('tr:visible:odd').removeClass('even').addClass('odd');
    $('tr:visible:even').removeClass('odd').addClass('even');    
  };
  
  $('#magnifyPD, #magnifyCT').hide();
  $('select[@name=ProductName]').change(function() {
    if ($(this).val() == 'other') {
      $('#magnifyPD').show().find(':input').addClass('required').focus();
    } else {
      $('#magnifyPD').hide().find(':input').removeClass('required');
    }
    stripeTable();
  });
  $('input[@name=CompanyName]').blur(function() {
    var $nextRow = $(this).parents('tr:first').next('tr');

    if ($(this).val()) {
      $nextRow.show().find(':input').focus();
    } else {
      $nextRow.hide();
    }
    stripeTable();
  });
  $('form .required').parents('form').submit(function() {
	  var errorMessage = '', errorEmail =  '';
	  var errorNumber = 0;
	  
    $('.required', this).each(function(index) {
      var $this = $(this);
      if ( ($this.is(':input') && $this.val() == '') || ($this.is('div') && !$this.find('input:checked').length) ) {
        errorMessage += "\n" + $(this).parents('td:first').siblings().text();
        errorNumber++; 
      }
    });
    $('input[@name=Email]', this).each(function() {
      var emailValid = /(.+)@(.+)(\.([a-z]{2,3})$)/.test( $(this).val() );
      if ( !emailValid && $(this).val()) {
        errorEmail = "\n" + 'Please use correct Email format';
      }      
    });
    
    
    if (errorNumber || errorEmail) {
      if (errorNumber == 1) {
        errorMessage = 'The following field is required:' + errorMessage;   
      } else if (errorNumber > 1) {
        errorMessage = 'The following ' + errorNumber + ' fields are required:' + errorMessage;
      }
      errorMessage += errorEmail;

      alert(errorMessage);
      return false;
    };  
  });
});