function saveJobQuotePermission(team_id, eid) {
  if ($('#can_jobquote_'+eid).attr('checked')) {
    jobquote_permission = 1;
  } else {
    jobquote_permission = 0;
  }
  $('#status_msg').html('saving request...');
  var args = {team_id: team_id, entity_id: eid, jqpermission : jobquote_permission};
  $.ajax({
    url: "/ajax/ajax_team.php?action=set_jobquote_permission",
    data: args,
    type: "GET",
    dataType: "json",
    success: function(json) {
      if (!json.is_success) {
        // Print errors
        $('#status_msg').html(json.err_msg[0]);
      } else {
        $('#status_msg').html(json.ok_msg[0]);
      }
    }
  });
}
function removeTeamMember(team_id, eid) {
  if (confirm('Are you sure you want to remove this team member?')) {
    $('#status_msg').html('Removing team member...');
    var args = {team_id: team_id, entity_id: eid};
    $.ajax({
      url: "/ajax/ajax_team.php?action=remove_teammember",
      data: args,
      type: "GET",
      dataType: "json",
      success: function(json) {
        if (!json.is_success) {
          // Print errors
          $('#status_msg').html(json.err_msg[0]);
        } else {
          $('#div_member_removed_'+eid).html(json.data.member_removed)
          $('#status_msg').html(json.ok_msg[0]);
          $('#can_jobquote_'+eid).attr("disabled","disabled");
        }
      }
    });
  }
}
function openSendInvitation(eid, team_id) {
  var custom_msg_form = 'Custom message to include in the invitation that will be sent to the user (optional):<br>';
  custom_msg_form += '<textarea id="custom_msg" cols="40" rows="5"></textarea>';
  custom_msg_form += '<input type="hidden" id="eid" value="'+eid+'">';
  custom_msg_form += '<input type="hidden" id="team_id" value="'+team_id+'">';
  $.prompt(custom_msg_form, {
      callback: submitSendInvitation,
      buttons: { 'Send Invitation': true, 'Cancel': false }
  });
}
function submitSendInvitation(v,m) {
  if (v) addTeamMemberbyEid(m.children('#eid').val(), m.children('#team_id').val(), m.children('#custom_msg').val())
}
function addTeamMemberbyEid(eid, team_id, custom_msg) {
  $('#status_msg').html('Adding selected user to this team...');
  var args = {team_id: team_id, entity_id: eid, custom_msg : custom_msg};
  $.ajax({
    url: "/ajax/ajax_team.php?action=add_teammember",
    data: args,
    type: "GET",
    dataType: "json",
    success: function(json) {
      if (!json.is_success) {
        // Print errors
        $('#status_msg').html(json.err_msg[0]);
      } else {
        $('#members_last_row').before(json.data.new_row)
        $('#status_msg').html(json.ok_msg[0]);
      }
    }
  });
}
function openSendBatchInvitation(team_id) {
  var custom_msg_form = 'Custom message to include in the invitation that will be sent to the user (optional):<br>';
  custom_msg_form += '<textarea id="custom_msg" cols="40" rows="5"></textarea>';
  custom_msg_form += '<input type="hidden" id="team_id" value="'+team_id+'">';
  $.prompt(custom_msg_form, {
    callback: submitSendBatchInvitation,
    buttons: { 'Send Invitation': true, 'Cancel': false }
  });
}
function submitSendBatchInvitation(v,m) {
  if (v) {
    //alert("/?sp=team/home&sp_mode=invite_users&team_id_to_invite="+m.children('#team_id').val()+$('#eids_to_invite').val());
    window.location.href = "/?sp=team/home&sp_mode=invite_users&team_id_to_invite="+m.children('#team_id').val()+$('#eids_to_invite').val()+"&custom_msg="+m.children('#custom_msg').val();
  }
}
