/* ------------------
	JS - localStorage
--------------------*/
$.fn.isValid = function() {
  return this[0].checkValidity();
}

function login() {
  if ($('#loginForm').isValid()) {
    var username = $('#username').val();
    var password = $('#password').val();
    $.ajax({
      url: '../../../pmarket/api/client/login',
      type: 'POST',
      dataType: "json",
      contentType: "application/json; charset=utf-8",
      data: JSON.stringify({
        "username": username,
        "password": password
      }),
      success: function(response) {
        if (response.username) {
		  localStorage.setItem('user', JSON.stringify(response));
          window.location.href = 'products.html';
        } else if (response.notice) {
          $('#alertError').html(response.notice.text);
        } else {
          $('#alertError').html(response.error.text);
        }
      }
    })
  }
}

/* ------------------
	PHP - session
--------------------*/
/*
$.fn.isValid = function() {
  return this[0].checkValidity();
}

function login() {
  if ($('#loginForm').isValid()) {
    var username = $('#username').val();
    var password = $('#password').val();
    $.ajax({
      url: '../../../pmarket/api/client/login',
      type: 'POST',
      dataType: "json",
      contentType: "application/json; charset=utf-8",
      data: JSON.stringify({
        "username": username,
        "password": password
      }),
      success: function(response) {
        if (response.username) {
          window.location.href = 'products.html';
        } else if (response.notice) {
          $('#alertError').html(response.notice.text);
        } else {
          $('#alertError').html(response.error.text);
        }
      }
    })
  }
}
*/
