jQuery(function($) {
		 //alert('get a quote in plugins start');

   $(document).ready(function() {

	   //alert('get a quote in plugins start');
	  
	  $.get(myAjax.ajaxurl, { action: "getAQuotePopulateServiceTypes", _ajax_nonce: myAjax.nonce }, function(data) {        
		 $('select[name="serviceType"]').append(data);
      });

	  $.get(myAjax.ajaxurl, { action: "getAQuotePopulateCountries", _ajax_nonce: myAjax.nonce }, function(data) {        
		 $('select[name="countryOrigin"]').append(data);
		 $('select[name="countryDestination"]').append(data);
      });

	  $('select[name="serviceType"]').change(function() {		  
		  
		if ($(this).find('option:selected').text() == 'DOMESTIC SERVICE') { 
			$('select[name="countryDestination"]').prop('disabled', true);
		} else {
			$('select[name="countryDestination"]').prop('disabled', false);
		}
	  });
	  
	  $('select[name="countryOrigin"]').change(function() {
		  
		var valOrigin = $(this).val();
		var $selectServiceType = $('select[name="serviceType"]');
		var $selectDestination = $('select[name="countryDestination"]');
		
		if ($selectServiceType.find('option:selected').text() == 'DOMESTIC SERVICE') {			
			$selectDestination.val(valOrigin);
		}		  
	  });
	  
	  $('#btnReset').click(function() {		  
		  //window.location.reload();
		  $('select, input').val('');
	  });
	  
	  $('#getAQuote').click(function() {
		  
		var serviceType = $('select[name="serviceType"]').val();
		console.log(serviceType);
		var countryOrigin = $('select[name="countryOrigin"]').val();
		var countryDestination = $('select[name="countryDestination"]').val();
		
		var actualWeight = $('input[name="weight"]').val();

		var length = $('input[name="length"]').val();
		var width = $('input[name="width"]').val();
		var height = $('input[name="height"]').val();

		var computedWeight = length * width * height / 5000;
		var weight = actualWeight;

		if (computedWeight > actualWeight) {
			weight = computedWeight;
		}

		// validate fields are not empty
		var countEmpty = 0;

		$('#msg').hide();
		$('select, input[type="number"]').removeAttr('style');

		$('select, input[type="number"]').each(function() {
			if (isEmpty($(this).val())) {
				if (!$(this).prop('disabled')) {

					++countEmpty;
					$(this).css({ 'border': '1px solid red' });
				}
			} 			
		});	
		
		if (countEmpty > 0) {

			$('#msg').html('* All fields are required.').show();
			
		} else {
			console.log(myAjax.ajaxurl);
			

			$.post(myAjax.ajaxurl, { action: "getAQuote", _ajax_nonce: myAjax.nonce, 
				serviceType: serviceType, countryOrigin: countryOrigin, countryDestination: countryDestination, weight: weight }, function(data) { 
				//$('input[name="rate"]').val(data);
				console.log(data);
				
				$('input[name="rate"]').val(data.currency + ' ' + data.rate);
				//$('#msg').text(data['msg']);

				$('#lastQuery').text(data['lastQuery']);
				//console.log(data['lastQuery']);

				$('#lastError').text(data['lastError']);
				//console.log(data['lastError']);
		
			}, 'json');		
		}
 
	  });

	  $('.isNumericInt').keypress(function(event) {
		  var charCode = event.which || event.keyCode;

		  if (($(this).val().indexOf('.') == -1 || charCode != 46) && charCode > 40 && (charCode < 48 || charCode > 57)) {
			  return false;
		  } else {
			  return true;
		  } 
	  });

	  $('.isNumericFloat').keypress(function(event) {
		  var charCode = event.which || event.keyCode;

		  if (($(this).val().indexOf('.') != -1 || charCode != 46) && charCode > 40 && (charCode < 48 || charCode > 57)) {
			  return false;
		  } else {
			  return true;
		  } 
	  });

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

   });	
});
