$(function(){
	$(".button").click(function () {
    login();
  });

  $("#login_form > #email, #login_form > #password").keypress(function (e) {
		if(e.which == 13) {}
      //login();
  });
});

function login()
{
  if($("#login_form > #email").val() == "")
  {
    setStatusOrig("Please enter your e-mail / user name.");
    return false;
  }

  if($("#login_form > #password").val() == "")
  {
    setStatusOrig("Please enter your password");
    return false;
  }

  var filePath = $("#login_form > #filePath").val();
  $.ajax({
    type: "POST",
    url: filePath + 'resources/validateLogin.php',
    data: $("#login_form").serialize(),
    dataType: "json",
    success: function(data)
    {
      if(data.valid)
      {
        switch(data.which) {
          case 'WSO':
          default:
            fileName = data.url;
            if(fileName == 'undefined' || fileName == '') fileName = 'index.html';
            break;
          case 'APP':
            if(filePath == '') {
              filePath = '../';
            }
            else {
              filePath = '';
            }
            //alert("FilePath: " + filePath);
            fileName = 'app.html';
            if(data.last_page_saved != null)
              fileName += '?appPage=' + data.last_page_saved;
            break;
        }
        var moveTo = filePath + fileName;
        //alert("moveTo: " + moveTo);
        parent.location = moveTo;
      }
      else
      {
        setStatusOrig("Invalid Login. Please try again or <a style='color:#fff;' href='" + filePath + "user-request.html'>Get a User Name</a>");
      }
    }
  });
  return false;
}
      /*
        //alert('Data: ' + data);
        //alert(data.substring(0,11));
        if(data.substring(0,11) == 'Valid Login') {
          //alert(data.substring(12));
          var fileName = 'index.html';
          //alert(data);
          //alert("abbv: " + data.substring(12,15));

        }
        else if(data == 'No Username') {
          setStatusOrig("Invalid Login <a style='color:#fff;' href='" + filePath + "user-request.html'>Request It</a>");
        }
        else {
          setStatusOrig(data);
        }
       */


function setStatusOrig(data) {

	$('#errorMessage').hide('slow').html(data).show('slow');
		setTimeout(function(){
			$("#errorMessage").hide('slow');
	}, 5000);
}
