﻿
Base.Report = Class.create();

Base.Report.pop = function(e, postId, postDataId)
{
   if(Base.Report.current)
   {
      if(confirm('You already have a Report dialog open.\n\nWould you like to close the current dialog and report a different item?'))
         Base.Report.current.close();
      else
         return;
   }
   
   if(!e.hide) Sd.Proto.extend(e);
   var bounds = $id(e.id.replace('_LinkReport', '_Reply'))._bounds();
   var x = e._bounds().x + 10;
   var y = bounds.y + 10;
   var caption = e.title;
   var content = $new('div', null, 'report');
   var msg = $new('div', null, 'msg');
   var trow = $new('div', null, 'row');
   var srow = $new('div', null, 'row');
   var tlabel = $new('label');
   var slabel = $new('label');
   var ttext = $new('textarea');
   var sselect = $new('select');

   msg.innerHTML = 'This form is to be used <b>ONLY</b> for reporting objectional content, etc and <b>is not to be used</b> as a method of communicating with moderators, replying to posts, or  for other reasons. Abuse of this form will result in disciplinary action!';
   
   sselect.options[0] = new Option('Please select a Reason', '');
   sselect.options[1] = new Option('Posted SPAM', 'Posted SPAM');
   sselect.options[2] = new Option('Warez or Pirated Software', 'Warez or Pirated Software');
   sselect.options[3] = new Option('TOS Violation', 'TOS Violation');
   sselect.options[4] = new Option('Flame or Trolling', 'Flame or Trolling');
   sselect.options[5] = new Option('Excessive Language', 'Excessive Language');
   sselect.options[6] = new Option('Other (Please specify)', 'Other');

   slabel.innerHTML = 'Common Reasons:';
   
   srow.appendChild(slabel);
   srow.appendChild(sselect);
   
   tlabel.innerHTML = 'Enter other Information:';
   ttext.maxLength = 255;
   
   trow.appendChild(tlabel);
   trow.appendChild(ttext);

   content.appendChild(msg);
   content.appendChild(srow);
   content.appendChild(trow);
   
   var data = new Object();
   data.postId = postId;
   data.postDataId = (postDataId == null) ? 0 : postDataId;
     
   var fsend = Base.Report.send;
   var fforget = function(){ this.dialog.close() };
   var buttons = {sendreport: fsend, forgetit: fforget};
   
   Base.Report.current = new Base.UI.Dialog('_ReportDialog', Base.UI.Dialog.type.small, buttons , x, y, caption, content, Base.Report.onClose);
   
   Base.Report.current.data = data;
   sselect.id = Base.Report.current.id + '_DropDownReport';
   ttext.id = Base.Report.current.id + '_TextReport';
};

Base.Report.onClose = function()
{
   Base.Report.current = null;
};

Base.Report.send = function()
{
   var s = $id(Base.Report.current.id + '_DropDownReport');
   var reason = s.options[s.selectedIndex].value;
   var more = $id(Base.Report.current.id + '_TextReport').value;
   
   var err = '';
   if(reason == '') err = 'Please select a reason for your report.';
   if((!more || more.length < 4) && reason == 'Other') err += 'Please enter more information about your report (minimum 6 characters).';
   if(err && err.length > 0){ alert(err); return; };

   Base.Report.current.disable();
   ForumService.Report(Base.Report.current.data.postId, Base.Report.current.data.postDataId, reason, more, Base.Report.callback, Base.Report.error);
};

Base.Report.error = function(res)
{
   alert('An error occurred:\n\n' + res.get_message() + '\n\n' + res.get_stackTrace() + '\n\n' + res.get_statusCode() + '\n\n' + res.get_stackTrace());
};

Base.Report.callback = function(res)
{
   var msg;
   var result = (res.Code == Stardock.Web.UI.Services.ServiceResultCode.Success);
   if(result == true)
      msg = 'Your report was sent and has been entered into the queue. Please allow some time for our moderators to review it.';
   else
   {
      msg = 'An error prevented your report from being submitted. It has been logged and will be reviewed.';
   }
      
   Base.Report.current.clearContent();
   Base.Report.current.clearMask();
   Base.Report.current.clearButtons();
   Base.Report.current.addResult(result, msg);
   Base.Report.current.addButtons({close:function(){this.dialog.close();}});   
};