var home = {
  cancelClick : function ()
  {
    $.prompt('Are you sure you want to cancel all future shipments?', {
      buttons: {
        Yes : 'Yes',
	      No  : 'No'
      },
      callback: function (v, m, f) {
        if (v == 'Yes') {
          $.ajax({
            url      : 'customer_portal_ajax.php?mode=cancelAutoship',
            dataType : 'json',
            success  : function (response) {
              if (response.success == false) {
                $.prompt(response.msg);
              } else {
                $.prompt(response.response, {
                  classes : 'cancel-autoship-width'  
                });
                $('.jqibuttons').remove();
                $('.jqi').width(500);
                $('.jqiclose').bind('click', function () {
                  window.location.reload();
                });
              }
            }
          });
        } else if (v == 'No') {
          $.prompt('Your autoship program has not been cancelled.', {
            buttons : {
              Close : 'Close'
            }
          });
        }
      }
    });
  },

  shipClick : function ()
  {
    $.ajax({
      url      : 'customer_portal_ajax.php?mode=shipNextAutoshipOrder',
      dataType : 'json',
      success  : function (response) {
        if (response.success == false) {
          $.prompt(response.msg);
        } else {
          $.prompt('Your next shipment is being processed for shipment.', {
            buttons : {
              Close : 'Close'
            },
            callback: function (v, m, f) {
              if (v == 'Close') {
                window.location.reload();
              }
            }
          });
        }
      }
    });
  },

  applyClick : function ()
  {
    var frequency = $('#program_frequency').val();
    if (frequency == '') {
      $.prompt('Please select the Program Frequency.', {
        callback : function (v, m, f) {
          $('#program_frequency').focus();
        }
      });
    } else {
      $.ajax({
        url      : 'customer_portal_ajax.php?mode=updateAutoshipFrequency&frequency='+frequency,
        dataType : 'json',
        success  : function (response) {
          if (response.success == false) {
            $.prompt(response.msg);
          } else {
            $.prompt('Your autoship program frequency is changed to '+frequency.replace('w', ' Weeks'), {
              buttons : {
                Close : 'Close'
              },
              callback: function (v, m, f) {
                if (v == 'Close') {
                  window.location.reload();
                }
              }
            });
          }
        }
      });
    }
  },

  trackingClick : function () {
    $.ajax({
      url      : 'customer_portal_ajax.php?mode=tracking',
      dataType : 'json',
      success  : function (response) {
        $.prompt(response.msg);
      }
    });
  }
}

$(window).load(function () {
  $('#cancel-btn').click(function () {
    home.cancelClick();
  });

  $('#ship-order-btn').click(function () {
    home.shipClick();
  });

  $('#apply-btn').click(function () {
    home.applyClick();
  });

  $('#tracking-btn').click(function () {
    home.trackingClick();
  });

  $('#login').click(function () {
    $.ajax({
      url      : 'login.php',
      success  : function (response) {
        $.prompt(response);
      },
      complete : function () {
        $('.jqibuttons').remove();
        $('#zip').focus();
      }
    });
  });

  $('#logout').click(function () {
    $.ajax({
      url      : 'customer_portal_ajax.php?mode=logout',
      success  : function (response) {
        window.location.href = response;
      }
    });
  });

  var url = window.location.href;
  if (url.search('login') != -1) {
    $('#login').click();
  }
  
  if (url.search('home') != -1) {
    $('#cont-right-part2').show();
  }
});

