$.fn.blank=function() {
    return $.trim($(this).val()).length==0;
};


function show_progress() 
{
	// showing
	$('#progress').show();
}

function hide_progress() 
{
	// hiding
    $('#progress').hide();
}

function isValid()
{	
    var hasError = false;

    if(true == $('#posName').blank())
	{
	  $('#posName').addClass('this_is_required');
	  hasError = true;
	}
	if(true == $('#posPhone').blank())
	{
	  $('#posPhone').addClass('this_is_required');
	  hasError = true;
	}
	if(true == $('#posCSZ').blank())
    {
	  $('#posCSZ').addClass('this_is_required');
	  hasError = true;
	}
	if(true == $('#posEmail').blank())
	{
	  $('#posEmail').addClass('this_is_required');
	  hasError = true;
	}
	
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(!filter.test($('#posEmail').val()))
	{
	  alert('Please enter a valid email address.');
	  $('#posEmail').addClass('this_is_required');
	  hasError = true;
    }
    
    if(true == $('#posText').blank())
    {
      $('#posText').addClass('this_is_required');
      if ($('#posRegarding').val() == 'JOBS') {
        alert('Please include your credentials, years of experience in coding and what type of coding proficiencies you currently have, such as inpatient, outpatient, etc.  Thank you.');
      } else {
        alert('Please explain your needs for DocuCoders\' services');
      }
	  hasError = true;
	}
	
	if  (hasError == true) {
	    $('#status').html('You are missing some required fields.');
	    return false;
	} else {
        return true;
    }
}

function submitContactForm(e, f) {
        var form = $(f);
        form.find('.this_is_required').removeClass('this_is_required');
        $('#status').empty();
        e.preventDefault();
        if (isValid() == true) {
            show_progress();
            // ajax submit
            $.ajax({
                type:     'POST',
                url:      e.target.action,
                data:     form.serialize(),
                success:  function(data) {
                              if (data.success == true) {
                                  $(':input', form).not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');
                                  form.find('.this_is_required').removeClass('this_is_required');
                              }
                              hide_progress();
                              alert(data.message);
                          },
                dataType: 'json'
            });
        }
}

$(function(){
    $('#contact_form').submit(function(e) {
        submitContactForm(e, this);
    });
    
    $("#job_form").validate({
        debug: true,
        submitHandler: function(form) {
          var f = $(form);
          var progress = $('#jobProgress');
          progress.show();
          $.ajax({
              type:     'POST',
              url:      f.attr('action'),
              data:     f.serialize(),
              success:  function(data) {
                            if (data.success == true) {
                                $(':input', f).not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');
                            }
                            progress.hide();
                            alert(data.message);
                          },
              dataType: 'json'
            });
        }
    }); 

    $('#contactSelectionArea ul li a').click(function(e) {
        e.preventDefault();
        var show_jobs = $('a#show_jobs');
        var show_contact = $('a#show_contact');
        var id = e.target.id;
        if(id == "show_jobs") {
            show_contact.parent().removeClass('active');
            show_jobs.parent().addClass('active');
            $('form#contact_form').hide();
            $('form#job_form').show();
        } else { 
            show_contact.parent().addClass('active');
            show_jobs.parent().removeClass('active');
            $('form#job_form').hide();
            $('form#contact_form').show();
        }
    });

});
