  $(document).ready(function() {
 


 // =========== TRACK ORDER FORM ==========================
$('form#trackOrderForm').on("submit", function(e) {
  e.preventDefault();
  var trackOrderID = $(this).find("input#trackOrderID").val().toLowerCase();
  var Button = $(this).find("button");
 
 
 if(!trackOrderID.length > 0) {
  swal("Error", "Tracking number cannot be empty", "error");
 }else{
  Button.attr("disabled", "disabled");
  window.location = "/home/view/"+trackOrderID;
  Button.text("Hold on..");
 }
  
  
  });
   // =========== TRACK ORDER FORM ==========================
$('a#trackHome').on("click", function(e) {
  e.preventDefault();
  var trackOrderID = $("input#trackNumberHome").val().toLowerCase();
 
 
 if(!trackOrderID.length > 0) {
  swal("Error", "Tracking number cannot be empty", "error");
 }else{
  window.location = "/home/view/"+trackOrderID;
  $(this).text("Hold on..");
 }
  
  
  });

   // =========== QUOTE ORDER FORM ==========================
$('form#requestQuote').on("submit", function(e) {
  e.preventDefault();
 
  swal("Request received", "We have received your request, and we will get back to you shortly through the mail you provided", "success");
 
  });



//==================== SEND PARCEL SCRIPT ===========================
$("button#SendParcel").on("click", function() {
  $('form#sendParcelForm').submit();
});
$('form#sendParcelForm').on("submit", function(e) {
  e.preventDefault();
  var sendButton = $(this).find('button#SendParcel');
  sendButton.text('Sending PARCEL..');
  $('#addedParcel').html('');
  var itemName = $(this).find('input#itemName').val();
  var TrackingCode = $(this).find('input#TrackingCode').val();
  var senderName = $(this).find('input#senderName').val();
  var receiverName = $(this).find('input#receiverName').val();
  var receiverEmail = $(this).find('input#receiverEmail').val();
  var parcelStat = $(this).find('input#parcelStat').val();
  var currLocation = $(this).find('input#currLocation').val();
  var arrivalDate = $(this).find('input#arrivalDate').val();
  var shippingType = $(this).find('input#shippingType').val();
  var taxFee = $(this).find('input#taxFee').val();
  var Pieces = $(this).find('input#Pieces').val();
  var serviceType = $(this).find('input#serviceType').val();
  var Phone = $(this).find('input#receiverPhone').val();
  var destinationAddr = $(this).find('input#destinationAddr').val();
  var itemDesc = $(this).find('textarea#itemDesc').val();

  var InsurFee = $(this).find('input#InsurFee').val();
  var Vat = $(this).find('input#Vat').val();
  var TotalFee = $(this).find('input#totalFee').val();
  var Map = $(this).find('input#map').val();
  var TotalPaid = $(this).find('input#totalPaid').val();

   $.post('/home/admin/adminScript/sendParcel.php',{
    itemName:itemName, TrackingCode:TrackingCode, 
    senderName:senderName, receiverName:receiverName, Phone:Phone, 
    parcelStat:parcelStat, currLocation:currLocation, 
    arrivalDate:arrivalDate, shippingType:shippingType, 
    taxFee:taxFee, Pieces:Pieces, serviceType:serviceType, 
    receiverEmail:receiverEmail,
    destinationAddr:destinationAddr, itemDesc:itemDesc,
    TotalFee:TotalFee, Vat:Vat, InsurFee:InsurFee, Map:Map, TotalPaid:TotalPaid}, function(data) {
      $('#addedParcel').html(data);
      sendButton.text('Send PARCEL');
      if (data == "<p style='text-align: center; color: green;'> PARCEL successfully sent.</p>") {
        window.location.href="/home/admin/";
      }

      });

});
//========================== DELETE PARCEL =========================================
$('button#deleteParcel').click(function(e) {
  e.preventDefault();
  if (confirm('Are you sure you want to delete this PARCEL ?')) {
    var parcelID = $(this).data('id');
    $.post('/home/admin/adminScript/deleteParcel.php',{parcelID:parcelID}, function(data, status) {
      $('#addedParcel').html(data);
      if (data == "deleted") {
        alert("You've successfully deleted that PARCEL");
        window.location.href="/home/admin/";
      }else {
        alert('Could not delete this PARCEL, please try again.');
      }

     }); 
  }
});
//========================= UPDATE PARCEL =========================================
$('button#UpdateParcelButtonPopup').click(function(e) {
  e.preventDefault();

  var updateModal = $(this).closest('td').find('.updateParcel');
  updateModal.fadeIn(600, function() {
    updateModal.find('.updateParcelBody').removeClass('show wow fadeOutUp animated');
    updateModal.find('.updateParcelBody').addClass('show wow fadeInUp animated');
    $('body').addClass('noScroll');
  });

$('button.closeUpdateModal').click(function() {
  $('.updateParcel').fadeOut(600, function() {
    updateModal.find('.updateParcelBody').removeClass('show wow fadeInUp animated');
    updateModal.find('.updateParcelBody').addClass('show wow fadeOutUp animated');
    $('body').removeClass('noScroll');
  });
});

$('button#UpdateParcel').click(function(e) {
  $('button#UpdateParcel').text('Updating..');
  $('#updateParcelSucess').html('');
  var updateModalTrack = $(this).closest('td').find('.updateParcel');
  var parcelID = $(this).data('id');
  
  var itemName = updateModalTrack.find('input#UpdateitemName').val();
  var TrackingCode = updateModalTrack.find('input#UpdateTrackingCode').val();
  var senderName = updateModalTrack.find('input#UpdatesenderName').val();
  var receiverName = updateModalTrack.find('input#UpdatereceiverName').val();
  var receiverEmail = updateModalTrack.find('input#UpdatereceiverEmail').val();
  var Phone = updateModalTrack.find('input#UpdatereceiverPhone').val();
  var parcelStat = updateModalTrack.find('input#UpdateparcelStat').val();
  var currLocation = updateModalTrack.find('input#UpdatecurrLocation').val();
  var arrivalDate = updateModalTrack.find('input#UpdatearrivalDate').val();
  var shippingType = updateModalTrack.find('input#UpdateshippingType').val();
  var taxFee = updateModalTrack.find('input#UpdatetaxFee').val();
  var Pieces = updateModalTrack.find('input#UpdatePieces').val();
  var serviceType = updateModalTrack.find('input#UpdateserviceType').val();
  var destinationAddr = updateModalTrack.find('input#UpdatedestinationAddr').val();
  var itemDesc = updateModalTrack.find('textarea#UpdateitemDesc').val();

  var InsurFee = updateModalTrack.find('input#InsurFee').val();
  var Vat = updateModalTrack.find('input#Vat').val();
  var TotalFee = updateModalTrack.find('input#totalFee').val();

  var Map = updateModalTrack.find('input#map').val();
  var TotalPaid = updateModalTrack.find('input#totalPaid').val();

     $.post('/home/admin/adminScript/updateParcel.php',{
    parcelID:parcelID,
    itemName:itemName, TrackingCode:TrackingCode, 
    senderName:senderName, receiverName:receiverName, Phone:Phone, 
    parcelStat:parcelStat, currLocation:currLocation, 
    arrivalDate:arrivalDate, shippingType:shippingType, 
    taxFee:taxFee, Pieces:Pieces, serviceType:serviceType, receiverEmail:receiverEmail,
    destinationAddr:destinationAddr, itemDesc:itemDesc,
    TotalFee:TotalFee, Vat:Vat, InsurFee:InsurFee, Map:Map, TotalPaid:TotalPaid}, function(data) {
      $('#updateParcelSucess').html(data);
      $('button#UpdateParcel').text('update PARCEL');
      if (data == "<p style='text-align: center; color: green;'> PARCEL successfully updated.</p>") {
        window.location.href="/home/admin/";
      }

      });




});

});

$('#closeCookie').click(function() {

  var close = 1;
  $.post('/home/closeCookie.php',{close:close}, function(data, status) {
    $('.cloudfrontCookie').html(data);
    $('#cookieAlert').addClass('wow slideOutUp animated close');

    });

});


//========================= LOGIN FORM PROCESSS ========================================
     $('button#AdminLogin').click(function(e) {
      e.preventDefault();
      $(this).text('Authenticating...').fadeIn(300);
      $(this).attr('disabled', 'disable');
      //=================== PROCESS ======================================================
      var AdminEmail = $('#body_container .loginBody').find('input#AdminEmail').val();
      var AdminPassword = $('#body_container .loginBody').find('input#AdminPassword').val();

       $.post('/home/admin/adminScript/AdminLogin.php', {AdminEmail:AdminEmail, AdminPassword:AdminPassword}, function(data, status) {
              $('.data').html(data);
              $('button#AdminLogin').text("Login");
              $('button#AdminLogin').removeAttr('disabled');
              if (data == 'invalidEmail') {
                $('.data').html('');
                 $('.errorEmail').html('Invalid field *');
                 $('button#AdminLogin').text("Login");
                $('button#AdminLogin').removeAttr('disabled');
              }
              if (data == 'emptyPassword') {
                $('.data').html('');
                 $('.errorPassword').html('Required field *');
                 $('button#AdminLogin').text("Login");
                $('button#AdminLogin').removeAttr('disabled');

              }
              });



     });











  });