function jqvalidate() {
  var errmsg = '';
  var jqform = this;
  var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

  // Reset validation color schemes
  $('.req').each( function() {
    $(this).css('background', '');       
    $('label[for="'+$(this).attr('id')+'"]', jqform).css('color', '');
  });

  // Required fields without values set
  $('.req:enabled[value=""], .req:enabled:checkbox:not(:checked)', jqform).each( function() {
     $(this).css('background', '#e9e980');
     $('label[for="'+$(this).attr('id')+'"]', jqform).css('color', 'red');
     errmsg += '* Required field missing: '+$('label[for="'+$(this).attr('id')+'"]', jqform).text()+"\n";
  });

  // Validate email fields
  $('.email:enabled', jqform).each( function() {
    if (!filter.test($(this).val())) {
      $(this).css('background', '#e9e980');
      $('label[for="'+$(this).attr('id')+'"]', jqform).css('color', 'red');
      errmsg += '* Invalid email format: '+$('label[for="'+$(this).attr('id')+'"]', jqform).text()+"\n";
    }      
  });

  // Check file types are not restricted
  $('.restricted:enabled:file[value!=""]', jqform).each( function() {
    if(!check_filename($(this).val())) {
      $(this).css('background', '#e9e980');
      $('label[for="'+$(this).attr('id')+'"]', jqform).css('color', 'red');
      errmsg += '* Attachment format incorrect: '+$('label[for="'+$(this).attr('id')+'"]', jqform).text()+"\n";      
    }
  });

  // Display Error message and return success code
  if(errmsg.length>0) {
    alert(errmsg);
    return false;
  }

  return true;  
}

function check_filename(filename) {
	extension = new Array(".pdf",".doc",".docx",".txt");
	if (filename.length>0) {
	  var thisext = filename.substr(filename.lastIndexOf('.'));
	  for(var i = 0; i < extension.length; i++) {
	    if(thisext == extension[i]) {
	      return true; 
	    }
	  }      
	}
	return false;
}
