// ==UserScript==
// @name          SoftPedia Bless Users
// @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=*
// ==/UserScript==

var forms, form, s;

s = "0";
forms = document.body.all.tags("form");
for (var j=0; j<forms.length; j++){
  form = forms.item(j);
  if ( form.getAttribute("id") == "postingform" ){
    s = "1";
    break;
  }
}

if ( s == "0" ){
  alert("Error: Correct form was not found. Helper script is not installed.");
} 
else
{

  window.ciuly_diacritice = function(post){// ugly fix for romanian special character support
	post = encodeURIComponent(post);
	post = post.replace(/%C4%83/gi,"%E3");// a tilda
	post = post.replace(/%C3%AE/gi,"%EE");// i caciula
	post = post.replace(/%C5%9F/gi,"%BA");// sh
	post = post.replace(/%C5%A3/gi,"%FE");// tz
	post = post.replace(/%C3%A2/gi,"%E2");// a caciula
	return post;
  }

  window.ciuly_post = function(url, data) {
	var request = new XMLHttpRequest();
	request.open("POST", url, false);
	request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	request.send(data);
//	request.send(encodeURI(data));

//	w = window.open('','');
//	w. document.write(request.responseText);
//	w.focus();

//	GM_xmlhttpRequest({
//    	  method: "POST",
//    	  url: url,
//    	  headers:{'Content-type':'application/x-www-form-urlencoded'},
//    	  data:encodeURI(data)
//  	});
	alert("Done.");
  };

  window.ciuly_post_bless = function (username, forum, postContent){
	ciuly_post( "http://forum.softpedia.com/index.php?", 
		"st=0"+
		"&act=Post"+
		"&s="+
		"&f="+forum+
		"&auth_key="+ciuly_authKey+
		"&removeattachid=0"+
		"&MAX_FILE_SIZE=0"+
		"&CODE=01"+
		"&attach_post_key="+
		"&TopicTitle="+username+
		"&TopicDesc="+
		"&poll_question="+
		"&ed-0_wysiwyg_used=0"+
		"&editor_ids=ed-0"+
		"&Post="+ciuly_diacritice(postContent)+
		"&enableem=yes"+
		"&enablesig=yes"+
		"&enabletrack=0"+
		"&iconid=0"+
		"&dosubmit=Bless"
	);
  };

  window.ciuly_post_warn = function(username){
	postContent = prompt("Reason (wait until notified)", "Slightly less boring default.");
	ciuly_post_bless(username, "265", postContent);
  };

  window.ciuly_form = form;
  window.ciuly_username = form.all.tags("a").item(0).innerHTML;
  window.ciuly_WarnUser = function(wtype, rsn){
    var  reason = rsn;
    if (reason == "")
      reason = prompt("Reason", "");
    if (reason =="" || !reason) {
      alert("No reason given. No action taken.");
      return false;
    }

    // change all crlf (no matter the case) into \r
    reason = reason.replace(/crlf/gi,"\r");

    if (wtype == "suspend"){
	days = prompt("Suspend user for how many days?", "3");
    } else {
	days = "0";
    }
    if (wtype != "avertisment"){
    	for (var l=0; l<ciuly_form.elements.length; l++){
		var el = ciuly_form.elements[l];
		if (el.name == "key"){
			window.ciuly_authKey = el.value;
		}
      		if (el.name == "level"){
			if (el.value == "add")
				el.checked="checked";
			else
				el.checked="";
      		}
		if (el.name == "mod_indef"){
			if (wtype == "ban")
				el.checked="checked";
			else
				el.checked="";
		}
		if (el.name == "mod_value"){
			el.value=days;
		}
		if (el.name == "post_indef"){
			if (wtype == "ban")
				el.checked="checked";
			else
				el.checked="";
		}
		if (el.name == "post_value"){
			el.value=days;
		}
		if (el.name == "susp_value"){
			if (wtype == "ban")
				el.value="999999999999999999";
			else
				el.value=days;
		}
		if (el.name == "reason"){
			el.value = reason;
		}
		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 (el.name == "contact"){
			el.value = "Acesta este un mesaj automat de notificare: "+reason;
		}

    	}
    }

    // 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 (wtype == "avertisment")
	rsn = "Avertisment: ";
    if (reason.length>80)
      rsn = rsn+"\n";

    if (wtype == "warn" || wtype == "avertisment")
    	ciuly_post_bless(ciuly_username, "265", rsn+reason);
    else if (wtype == "suspend")
    	ciuly_post_bless(ciuly_username, "266", rsn+reason+"\n\nPerioada: "+days+" zile");
    else
    	ciuly_post_bless(ciuly_username, "267", rsn+reason);

    if (wtype == "avertisment")
	return false;

    return true;
  }

  var div = form.all.tags("div").item(2);

  div.insertAdjacentHTML("beforeEnd", 
     "&nbsp;<button class=\"button\" name=\"ban\" title=\"Ban User\" onclick=\"return ciuly_WarnUser('ban', '')\">Ban</button>&nbsp;"
  			);
  div.insertAdjacentHTML("beforeEnd", 
     "&nbsp;<button class=\"button\" name=\"suspend\" title=\"Suspend User\" onclick=\"return ciuly_WarnUser('suspend', '')\">Suspend</button>&nbsp;"
  			);
  div.insertAdjacentHTML("beforeEnd", 
     "&nbsp;<button class=\"button\" name=\"warn\" title=\"Warn User\" onclick=\"return ciuly_WarnUser('warn', '')\">Warn</button>&nbsp;"
  			);
  div.insertAdjacentHTML("beforeEnd", 
     "&nbsp;<button class=\"button\" name=\"avertisment\" title=\"Avertisment\" onclick=\"return ciuly_WarnUser('avertisment', '')\">Avertisment</button>&nbsp;"
  			);
  div.insertAdjacentHTML("beforeEnd", 
     "&nbsp;<button class=\"button\" name=\"warn2\" title=\"Warn User\" onclick=\"return ciuly_WarnUser('warn', 'regulile ariei programare si sub-ariilor ei, continute in topic-urile sticky si inchise de pe aria programare, au fost scrise ca sa fie citite iar continutul aplicat.')\">topic-urile sticky programare</button>&nbsp;"
  			);
  div.insertAdjacentHTML("beforeEnd", 
     "&nbsp;<button class=\"button\" name=\"ban2\" title=\"Ban societyonline.uv.ro\" onclick=\"return ciuly_WarnUser('ban', 'idiot 1 (reclama, spam, clone)')\">societyonline.uv.ro</button>&nbsp;"
  			);
}

