// ==UserScript==
// @name          SoftPedia Warn Helper
// @author        Ciuly  
// @namespace     http://www.ciuly.com
// @description   Posts the warn message in the dedicated area when warning
// @include       http://forum.softpedia.com/index.php?act=warn&type=add&mid=*
// @include       http://forum.softpedia.com/index.php?s=*&act=warn&type=add&mid=*
// @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;

  wnd.ciuly_use_richedit = true;// assume true, but only for this script
//wnd.IPS_editor && wnd.IPS_editor['ed-0'] && wnd.IPS_editor['ed-0'].is_rte==1; this results in undefined instead of false
  if (wnd.IPS_editor)
    if (wnd.IPS_editor['ed-0'])
      if (wnd.IPS_editor['ed-0'].is_rte==0)
        wnd.ciuly_use_richedit = false;

form = document.getElementById("postingform");
if ( form ) {

  wnd.ciuly_form = form;
  wnd.ciuly_username = form.getElementsByTagName("a").item(0).innerHTML;
  wnd.ciuly_userurl = form.getElementsByTagName("a").item(0).getAttribute("href");
  wnd.ciuly_doWarnUser = function(wtype, rsn, expl){
    wnd.ciuly_progress_show();
    var  reason = rsn;
    if (reason == "")
      reason = prompt("Reason", "");
    if (reason =="" || !reason) {
      alert("No reason given. No action taken.");
      wnd.ciuly_progress_hide();
      return false;
    }

    // change all crlf (no matter the case) into \n
    reason = reason.replace(/crlf/gi,"\n");
    expl = expl.replace(/crlf/gi,"\n");

    if (wtype == "suspend"){
	days = prompt("Suspend user for how many days?", "3");
    } else {
	days = "0";
    }
    var subj="";
    var pm="";
    for (var l=0; l<wnd.ciuly_form.elements.length; l++){
	var el = wnd.ciuly_form.elements[l];
	if (el.name == "key"){// ignore
	} else
      	if (el.name == "level"){
		if (el.value == "add")
			el.checked="checked";
		else
			el.checked="";
      	} else
	if (el.name == "mod_indef"){
		if (wtype == "ban")
			el.checked="checked";
		else
			el.checked="";
	} else
	if (el.name == "mod_value"){
		if (wtype != "warn")
			el.value=days;
	} else
	if (el.name == "post_indef"){
		if (wtype == "ban")
			el.checked="checked";
		else
			el.checked="";
	} else
	if (el.name == "post_value"){
		if (wtype != "warn")
			el.value=days;
	} else
	if (el.name == "susp_value"){
		if (wtype == "ban")
			el.value="999999999999999999";
		else
			if (wtype == "suspend")
				el.value=days;
	} else
	if (el.name == "reason"){
		if (wtype=="suspend")
			el.value = reason+"\n** Suspendat pt "+days+" zile **";
		else
		if (wtype=="ban")
			el.value = reason+"\n** Banat **";
		else
			el.value = reason;
	} else
	if (el.name == "subject"){
		if (wtype == "ban")
			el.value = "Ai fost banat definitiv.";
		else {
			if (wtype == "suspend")
				el.value = "Ai fost suspendat pentru "+days+" zile";
			else
				el.value = "Ai primit un warn.";
		}
		if (wnd.ciuly_use_richedit){
			subj = el.value;
			el.value = "";
		}
	} else
	if (el.name == "contact"){
		el.value = "Acesta este un mesaj automat de notificare: "+reason+"\n\n"+expl;
		if (wnd.ciuly_use_richedit){
			pm = el.value;
			el.value = "";
		}
	} else
	if (el.name == "mod_unit"){
		if (wtype != "warn")
			el.value = "d";// days
	} else
	if (el.name == "post_unit"){
		if (wtype != "warn")
			el.value = "d";// days
	} else
	if (el.name == "susp_unit"){
		if (wtype != "warn")
			el.value = "d";// days
	} else
	if (el.name == "contactmethod"){
		el.value = "pm";
	} else
	if (el.name == "ed-0_wysiwyg_used"){// ignore
//		el.value = "1";
	} else
	if (el.name == "editor_ids[]"){// ignore
	} else
	if (el.name == ""){// ignore
	} else
	if (el.name.indexOf("ciuly_")>-1){// ignore our buttons
	} else
		alert(el.name+" not implemented. If you get this message, it means something has changed and this script needs attention. Contact author.");
    }

    // remove quotes from reason. if the user has said something ugly which is added as the reason, we don't want to post it publicly
    var tmp = "";
    while (tmp != reason){
      tmp = reason;
      reason = reason.replace(/\[quote.*?\[\/quote\]/i,"");
    }

    var rsn = "Motiv: ";
    if (reason.length>80)
      rsn = rsn+"\n";

    if (wnd.ciuly_use_richedit){
      wnd.ciuly_progress_log("sending PM...");
      if (!wnd.ciuly_send_pm(wnd.ciuly_username, subj, pm))
        alert("Could not send PM.");
    }

    rrr=false;
    wnd.ciuly_progress_log("Blessing user...");
    if (wtype == "warn")
    	rrr = wnd.ciuly_sp_post_bless(wnd.ciuly_username, "265", rsn+reason);
    else if (wtype == "suspend")
    	rrr = wnd.ciuly_sp_post_bless(wnd.ciuly_username, "266", rsn+reason+"\n\nPerioada: "+days+" zile");
    else
    	rrr = wnd.ciuly_sp_post_bless(wnd.ciuly_username, "267", rsn+reason);

    wnd.ciuly_progress_hide();
    return rrr;
  }

  wnd.ciuly_WarnUser = function(wtype, rsn, expl){
    var r = wnd.ciuly_doWarnUser(wtype, rsn, expl);
    if (r)
      setTimeout("ciuly_the_form.submit()", 3050);
    return false;
  }

  wnd.ciuly_BanSpammer = function(rsn, expl){
    rrr = wnd.ciuly_doWarnUser('ban', rsn, expl);
    if (rrr){
      wnd.ciuly_progress_show();
      wnd.ciuly_execLater("ciuly_BanSpammer_Later", 25);
    }
    return false;
  }

  wnd.ciuly_BanSpammer_Later = function(){
    wnd.ciuly_progress_log("Posting to A52...");
    rrr = wnd.ciuly_reply(373, 325605, wnd.ciuly_userurl);
    wnd.ciuly_progress_hide();
    if (rrr)
      setTimeout("ciuly_the_form.submit()", 3050);
  }

  var div = form.getElementsByTagName("div").item(2);
  wnd.ciuly_the_form = form;

  div.innerHTML = div.innerHTML + 
     "&nbsp;<button class=\"button\" name=\"ciuly_ban\" title=\"Ban User\" onclick=\"return ciuly_WarnUser('ban', '', '')\">Ban</button>&nbsp;"+
     "&nbsp;<button class=\"button\" name=\"ciuly_ban_spammer\" title=\"Ban spammer &amp; post on A52\" onclick=\"return ciuly_BanSpammer('', '')\">Ban Spammer &amp; post on A52</button>&nbsp;"+
     "&nbsp;<button class=\"button\" name=\"ciuly_suspend\" title=\"Suspend User\" onclick=\"return ciuly_WarnUser('suspend', '', '')\">Suspend</button>&nbsp;"+
     "&nbsp;<button class=\"button\" name=\"ciuly_warn\" title=\"Warn User\" onclick=\"return ciuly_WarnUser('warn', '', '')\">Warn</button>&nbsp;"
  ;

} else {
  if (!wnd.ciuly_use_richedit)
    alert("Error 1: Correct form was not found. Helper script is not installed.");
}// end if form

})(window.wrappedJSObject || window);