
$(document).ready(function() {
  $('#fsubmit').click(function() {
    submitContactForm();
  });
  
  /*
  $('#contact-us-frm').submit(function() {
    alert('submitting');
    return true;
  });
  */
	$('#fphone').focusout(function() {
		if( $('#fphone').val() ) {
			if( !isValidPhoneFormat($('#fphone').val()) ) {
        alert('Please make sure your phone number is in the format XXX-XXX-XXXX.');
      }
		}
	});
  
});


function submitContactForm() {
  var errors = 0;
  var phone = 0;
  var attachment = '';
  
	// check fields
	if( $('#ftype').val() == 'resume' ) {
		if( !$('#fname').val() )
		  errors++;
		
		if( (!$('#femail').val() || !isValidEmailAddress($('#femail').val())) && !isValidPhoneFormat($('#fphone').val()) )
			errors++;
		
	  if( !($('#attachment1').val() === '') ) {
	    var ext = $('#attachment1').val().split('.').pop().toLowerCase();
	    if( $.inArray(ext,['pdf','txt','doc','docx']) == -1 )
	      attachment = ' Please make sure that you have attached a resume format of either PDF, Word document, or text file.';
	  }
		
		if( errors == 0 ) {
			$('#resume-frm').submit();
		} else {
			alert("Please make sure you have provided at least:\n your name\n a way for us to contact you.\n"+attachment);
		}
	} else if( $('#ftype').val() == 'referral' ) {
		if( !$('#fname').val() )
		  errors++;
		
		if( !$('#femail').val() || !isValidEmailAddress($('#femail').val()) )
			errors++;
		
    if( !$('#frname').val() )
      errors++;
		
		if( (!$('#fremail').val() || !isValidEmailAddress($('#fremail').val())) && !isValidPhoneFormat($('#frphone').val()) )
			errors++;
		
	  if( !($('#attachment1').val() === '') ) {
	    var ext = $('#attachment1').val().split('.').pop().toLowerCase();
	    if( $.inArray(ext,['pdf','txt','doc','docx']) == -1 )
	      attachment = ' Please make sure that you have attached a resume format of either PDF, Word document, or text file.';
	  }
		
		if( errors == 0 ) {
			$('#referal-frm').submit();
		} else {
			alert("Please make sure you have provided at least:\n your name\n your email\n your refferal's name\n a way for us to contact your refferal.\n"+attachment);
		}
	} else {
		if( $('#ftype').val() == 'contact-home' ) {
			if( !$('#ffname').val() )
			  errors++;
				
			if( !$('#flname').val() )
			  errors++;
		} else {
			if( !$('#fname').val() )
			  errors++;
		}
		
		if( !$('#femail').val() || !isValidEmailAddress($('#femail').val()) )
		  errors++;
		
		if( $('#fphone').val() && !isValidPhoneFormat($('#fphone').val()) )
		  phone++;
		
		if( !$('#fcomment').val() )
		  errors++;
			
		if( errors == 0 && phone == 0 ) {
			$('#contact-us-frm').submit();
		} else if( errors > 0 && phone > 0 ) {
		  alert('Please make sure all the required fields have been completed and that your phone number is in the format XXX-XXX-XXXX.'+attachment);
		} else if( phone > 0 ) {
		  alert('Please make sure your phone number is in the format XXX-XXX-XXXX.'+attachment);
		} else {
		  alert('Please make sure all the required fields have been completed.'+attachment);
		}
	}
}


function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
};


function isValidPhoneFormat(phone) {
  var pattern = new RegExp(/^\d{3}-\d{3}-\d{4}$/);
  return pattern.test(phone);
}
