$(document).ready(function() {
  // selected nav item
  $('.action_'+_cur_action).addClass('selected');


  // disable client field if user role set to IMRE
  $('#role').click(function() {
    if($('#role').val() == 'IMRE') {
      $('#client_id').attr('disabled','disabled');
    } else if($('#role').val() == 'CLIENT') {
      $('#client_id').removeAttr('disabled','disabled');
    }
  });


  // hide Rapid Response definitions if not active
  $("#rapidresponse").change(function() {
    if($(this).val() == 'ACTIVE') {
        $('.rr-level-defs').removeClass("hide");
    }
    else {
        $('.rr-level-defs').addClass("hide");
    }
  });


  // display progress gif during file upload
  $('#submitVersion').click(function() {
    $('#submitVersion').addClass("hide");
    $('#cancelVersion').addClass("hide");
    $('#sending').removeClass("hide");
  });


  // expand/contract version list in project stats when file folder is selected
  $(".file-expand").click(function() {
    $("li").removeClass("selected");
    $(".version").addClass("hide");
    $('.file-contract').addClass('hide');
    $('.file-expand').removeClass('hide');
    $(this).parent().children(".file-expand").addClass("hide");
    $(this).parent().addClass("selected");
    $(this).siblings(".version").removeClass("hide");
    $(this).siblings(".file-contract").removeClass("hide");
    $("p").removeClass("selected");
    $(this).siblings("p").addClass("selected");
    return false;
  });

  $(".file-contract").click(function() {
    $(".version").addClass("hide");
    $(this).siblings(".file-expand").removeClass("hide");
    $(this).addClass("hide");
    $(this).parent().removeClass("selected");
    $(this).siblings(".version").addClass("hide");
    $(this).siblings(".file-contract").addClass("hide");
    $("p").removeClass("selected");
    return false;
  });

  // onChange submit of dropdown menu
  $('form#searchTwitter').submit(function() {
    $('.twitter-save').removeClass("hide");
  });


  $('#client-for-filter').change(function() {
    $('form#filterTwitterClientResults').submit();
  });


  $('#xml-file').change(function() {
    $('form#filterTwitterXMLResults').submit();
  });


  // clear input fields' default values
  $('input.time-date-field').focus(function() {
    var defaultText = $(this).val();
    if($(this).value == $(this).defaultText)
    {
      $(this).val('');
    }
  });

  $('.notification_msg').focus(function() {
    var defaultText = $(this).val();
    if($(this).value == $(this).defaultText)
    {
      $(this).val('');
    }
  });


  $("#loginReqd").change(function() {
    if($(this).val() == 1) {
        $('.login').removeClass("hide");
    }
    else {
        $('.login').addClass("hide");
    }
  });


  // email imre reviewers
  $('#notify-imre form').submit(function(){
    $.post('/index.php/admin/sendImreNotification', $(this).serialize(), function(){
      tb_remove();
      $('#imre_reviewers').val('');
      $('#imre_reviewers_msg').val('');
    });
    return false;
  });


  // email client reviewer
  $('#notify-client form').submit(function(){
    $.post('/index.php/admin/sendClientNotification', $(this).serialize(), function(){
      tb_remove();
      $('#client_reviewer_msg').val('');
    });
    return false;
  });


});


// RAPID RESPONSE CREATE RESPONSE FORM VALIDATION
// STEP 1
function validate_required(field, alerttxt)
{
  with(field)
  {
    if(value==null || value=='' || value=='0')
    {
      alert(alerttxt);
      return false;
    }
    else
    {
      return true;
    }
  }
}

function validate_form(thisform)
{
  with(thisform)
  {
    $('input').removeClass("error");
    if(validate_required(client_id, "Please select a client.")==false)
    {
      $('#client_id').addClass("error");
      client_id.focus();
      return false;
    }
    if(validate_required(post_name, "Please enter a name for the post.")==false)
    {
      $('#post_name').addClass("error");
      post_name.focus();
      return false;
    }
    if(validate_required(post_link, "Please enter a link to the post.")==false)
    {
      $('#post_link').addClass("error");
      post_link.focus();
      return false;
    }
    if(posted_at_time != 'h:m am' || posted_at_time != '')
    {
      if(validate_required(posted_at_date, "Please enter the date the post was posted at.")==false)
      {
        $('#posted_at_date').addClass("error");
        posted_at_date.focus();
        return false;
      }
    }
    if(post_viewed_at_time != 'h:m am' || post_viewed_at_time != '')
    {
      if(validate_required(post_viewed_at_time, "Please enter the date the post was viewed at.")==false)
      {
        $('#post_viewed_at_date').addClass("error");
        post_viewed_at_date.focus();
        return false;
      }
    }
    if(validate_required(recommended_action, "Please enter a recommended action to take.")==false)
    {
      $('#recommended_action').addClass("error");
      recommended_action.focus();
      return false;
    }
  }
}