$(document).ready(
  function()
  {
    $("#commentbutton").hover(
      function()
      {
        $("#commentbutton").css( "textDecoration", "underline" );
      },
      function()
      {
        $("#commentbutton").css( "textDecoration", "none" );
      }
    );

    $("#commentbutton_secondary").click(
      function()
      {
        $("#commentinputscreen").css( "display", "block" );
        var str = window.location.href;
        if ( str.lastIndexOf( "#commentstart" ) < 0 ) window.location.href = window.location.href + "#commentstart";
        else window.location.href = window.location.href;
        return false;
      }
    );

    $("#commentbutton").click(
      function()
      {
        $("#commentinputscreen").css( "display", "block" );
      }
    );

    $("#commentinputscreencancelbutton").click(
      function()
      {
        $("#commentinputscreen").css( "display", "none" );
      }
    );

    $("#commentinputscreensendbutton").click(
      function()
      {
        $("#commentinputscreen").css( "display", "none" );
      }
    );

    $("#unit_long").css( "cursor", "pointer" );
  }
);

function submitMailForm( instancename )
{
  $("#btn_submit").attr( "disabled", true );
  var senderName     = $( "#tf_from" ).val();
  var senderEmail    = $( "#tf_sender" ).val();
  var recipientEmail = $( "#tf_recipient" ).val();
  $.ajax({
    url: "/mailclip",
    type: "POST",
    data: { from : senderName , sender : senderEmail, recipient : recipientEmail, instancename : instancename },
    timeout: 6000,
    dataType: "html",
    success: function( html )
    {
      $("#btn_submit").attr( "disabled", false );
      if ( html == "Ok." ) $("#span_msg").append( "Mail sent." );
      else alert( html );
    }
  });
}

function sendCommentForm( url )
{
  var tf_author  = $("#tf_author").val();
  var tf_comment = $("#tf_comment").val();
  $.ajax({
    url: url,
    type: "POST",
    data: { tf_author : tf_author , tf_comment : tf_comment },
    timeout: 6000,
    dataType: "html",
    success: function( html )
    {
      $("#commentstable_zerothRow").after( html );
      var commentCount = parseInt( $("#commentcount").html() );
      $("#commentcount").html( ++commentCount );
      $("#tf_author").val("");
      $("#tf_comment").val("");
    },
    error: function( a, b, c )
    {
      alert( a + " " + b + " " + c )
    }
  });
}
