// ==UserScript==
// @name          SoftPedia Delete with no confirmation and notification
// @author        Ciuly  
// @namespace     http://www.ciuly.com
// @description   modifies the delete topic and delete post moderation options to not ask for confirmation and to send PM notifications
// @include       http://forum.softpedia.com/index.php?showtopic=*
// @require       http://www.ciuly.com/greasemonkey/aa_all_common.user.js
// @require       http://www.ciuly.com/greasemonkey/aa_sp_common.user.js
// @require       http://www.ciuly.com/greasemonkey/aa_sp_mod_common.user.js
// ==/UserScript==

(function(wnd){

  var forms, form;

  form = document.getElementsByName("modform2")[0];
  if ( !form ) 
    return;

  wnd.ciuly_form = form;

  findObject = function(list, text){
    for (var i=0; i<list.length; i++){
      if (list[i].innerHTML.indexOf(text)>-1)
        return list[i];
    }
    return null;
  }

  wnd.ciuly_deleteNotify = function(istopic, usr){
    if (istopic){
      for (var l=0; l<wnd.ciuly_form.elements.length; l++){
        if (wnd.ciuly_form.elements[l].name == "CODE"){
          wnd.ciuly_form.elements[l].checked="checked";
          options = wnd.ciuly_form.elements[l].getElementsByTagName("option");
          for (var i=0; i<options.length; i++)
            if (options[i]){
              if (options[i].value == "03"){
                options[i].value="08";
              }
              if (options[i].value == "08"){
	        options[i].selected = "1";
                break;
              }
            }
          break;
        }
      } 
    }

    var reason = prompt("Reason (no reason=no pm)", "");
    if (reason =="" || !reason) {
//      alert("No reason given. No PM sent.");
      if (confirm("Notification cancelled. Continue deleting post?"))
        return true;
      else
        return false;
    }

    // change all crlf (no matter the case) into \n
    reason = reason.replace(/crlf/gi,"\n");

    // send PM
    var what;
    if (istopic) {
      what="Topic";
      reason = "Topicul cu titlul '"+wnd.ciuly_topictitle+"' a fost sters: "+reason;
    } else {
      what="Post";
      reason = "Un post din topicul cu titlul '"+wnd.ciuly_topictitle+"' a fost sters: "+reason;
    }

    if (!wnd.ciuly_send_pm(usr, what+" sters", reason)){
	alert("Did not send PM. Aborting.");
	return false;
    }

    alert("Sent PM. Now will delete...");
    if (wnd.is_ie)
      wnd.ciuly_form.submit();
    return true;
  }
  
  wnd.ciuly_deletePost = function(url, usr){
    if (wnd.ciuly_deleteNotify(false, usr))
      wnd.location=url;
  }

  var div;
  div=document.getElementsByClassName("maintitle")[1];
  div=div.getElementsByTagName("div")[0];
  var b;
  b=div.getElementsByTagName("b")[0];

  wnd.ciuly_topictitle = b.innerHTML;

  var tables=document.getElementsByClassName("ipbtable");
  for (var i=0; i<tables.length; i++){
    var table=tables[i];
    if (table.getAttribute("cellspacing")=="0")
      continue;
    var aaa=table.getElementsByTagName("a");
    if (aaa.length == 0){
      continue;
    }

    uname = aaa[1].innerHTML;
    div=table;
    break;
  }

  if (wnd.is_ie){
    div.getElementsByClassName = document.getElementsByClassName;
  }
  a=div.getElementsByClassName("postdetails")[1];
  var as=a.getElementsByTagName("a");
  for (var i=0; i<as.length; i++){
    a=as[i];
    if (a.innerHTML.substring(0, 1)=="#")
      break;
  }

  if (a.innerHTML == "#1"){  
    form.innerHTML = form.innerHTML + 
       "&nbsp;<button class=\"button\" name=\"ciuly_delete_topic\" title=\"Delete Topic Notify User\" onclick=\"return ciuly_deleteNotify(true, '"+uname+"')\">Delete Topic & Notify</button>&nbsp;"
    ; 
  } else
    ;//alert("First post not found. Cannot determin username. Go to first page and try again.");

  var tables=document.getElementsByClassName("ipbtable");
  for (var i=0; i<tables.length; i++){
    var table=tables[i];
    if (table.getAttribute("cellspacing")=="0")
      continue;
    var tr= findObject(table.getElementsByTagName("tr"), "delete_post");
    if (tr){
      var aaa = table.getElementsByTagName("a");
      if (aaa.length == 0){
        continue;
      }
      
      text = aaa[1].getAttribute("href");
      var j = text.indexOf("showuser=");
      if (j>-1){
//        uid = text.substr(j+9, text.length);
        uname = aaa[1].innerHTML;

        var divs = tr.getElementsByTagName("td")[1].getElementsByTagName("div");
        var a = divs[1].getElementsByTagName("a");
        if (a.length<1)
          continue;
        var url=a[0].getAttribute("href");
        if (url.indexOf("delete_post")==-1)
          continue;
        url=url.substring(24, url.length-2);
        url="javascript:ciuly_deletePost('"+url+"', '"+uname+"')";
        a[0].setAttribute("href", url);
      }
    }
  }


})(window.wrappedJSObject || window);