jQuery(function($) {
   $(document).ready(function() {
	  //alert('contact us in plugins start');

	  $('#isSent').hide(); // NOTE

	  $.get(myAjax.ajaxurl, { action: "populateOffices", _ajax_nonce: myAjax.nonce }, function(data) {  
		  //$('select[name="office"]').html(data);
		  //$('select[name="office"]').prop('selectedIndex', 0);
		  //populateDepts($('select[name="office"]').val());

		  $('select[name="office"]').append(data);
      });

	  $('select[name="office"]').change(function() {
		  populateDepts($(this).val());
	  });

	  $('select[name="dept"]').change(function() {
		  populateOfficeDetails($('select[name="office"]').val(), $(this).val());
	  });

	  function populateDepts(officeId) {
		  $.get(myAjax.ajaxurl, { action: "populateDepts", _ajax_nonce: myAjax.nonce, officeId: officeId }, function(data) {
			  //$('select[name="dept"]').html(data);
			  //$('select[name="dept"]').prop('selectedIndex', 0);

			  $('select[name="dept"]').empty();
			  $('select[name="dept"]').append('<option value="">Select a Department</option>');
			  $('select[name="dept"]').append(data);
			  populateOfficeDetails(officeId, $('select[name="dept"]').val());
		  });
	  }
	  
	  function populateOfficeDetails(officeId, officeDeptId) {
		 //console.log('populateOfficeDetails: officeId=' + officeId + ': officeDeptId=' + officeDeptId);

		 $.getJSON(myAjax.ajaxurl, { action: "getOfficeDetails", _ajax_nonce: myAjax.nonce, officeId: officeId, officeDeptId: officeDeptId }, function(data) {  
			//console.log(data);
			//console.log(data[0], data[1], data[2], data[3], data[4], data[5]);
			
			$('#officeName').html(data[0]);
			$('#officeAddr').html(data[1]);
			$('#officeTel').html(data[2]);
			$('#officeDept').html(data[3]);
			$('#officeDeptEmail').html(data[4]);
			$('#officeHours').html(data[5]);
			
			// NOTE
			populateRecepientEmail();			
			$('#officeDeptEmail').hide();
		 });
	  }

	  function populateRecepientEmail() {
	     $('#recepientEmail').html($('#officeDeptEmail').text());
	  }

	  function isEmpty(val) {
		  var trimmed = val.trim();
		  if (trimmed || trimmed.length > 0) {
			  return false;
		  } 
		  return true;
	  }

	  function isValidEmail(email) {
		  var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		  return regex.test(email.trim());
	  }

	  $('#sendEmail').click(function() {
		  
		 //alert('sending email1...');
		 var recepientEmail = $('#recepientEmail').text();
		 var senderName = $('input[name="senderName"]').val();
		 var senderEmail = $('input[name="senderEmail"]').val();
		 var msgBody = $('textarea[name="msgBody"]').val();

		 $('#isSent').show();

		 if (isEmpty(senderName) || isEmpty(senderEmail) || isEmpty(msgBody)) {
			 $('#isSent').html('All fields are required.');

		 } else if (!isValidEmail(senderEmail)) {
			 $('#isSent').html('Email is invalid.');
		     
		 } else {
			 $('#isSent').html('Sending...');

			 //alert('sending email2...');
			 $.post(myAjax.ajaxurl, { action: "contactUs", _ajax_nonce: myAjax.nonce, recepientEmail: recepientEmail, 
					senderName: senderName, senderEmail: senderEmail, msgBody: msgBody }, function(data) {
				 
				 console.log(data);
				 $('#isSent').html(data);
				 $('input, textarea').val(''); // reset form
			 });		 
		 }		 		  
	  });

   });	
});
