// ==UserScript==
// @name          SoftPedia Common Moderation Stuff
// @author        Ciuly
// @namespace     http://www.ciuly.com
// @description   Set common functions and variable for the softpedia scripts
// @include       http://forum.softpedia.com/*
// @require       http://www.ciuly.com/greasemonkey/aa_all_common.user.js
// @require       http://www.ciuly.com/greasemonkey/aa_sp_common.user.js
// ==/UserScript==

(function(wnd){

  wnd.ciuly_sp_post_bless = function (username, forum, postContent){
	var searchr = wnd.ciuly_sp_post_res( "http://forum.softpedia.com/index.php?", 
		"forums="+forum+
		"&cat_forum=forum"+
		"&act=Search"+
		"&joinname=1"+
		"&CODE=01"+
		"&keywords="+username+
		"&submit=Search forum",
	   false
	);
	var i = searchr.indexOf(">"+username+"</a>");
	if (i>-1){
		while (searchr[i]!="\"")
			i--;
		var j=i;
		i--;
		while (searchr[i]!="\"")
			i--;
		var url=searchr.substr(i+1, j-i+1);
		i = url.indexOf("showtopic=");
		if (i>-1){
			i=i+10;
			var t="";
			while (url[i]>="0" && url[i]<="9"){
				t=t+url[i];
				i++;
			}
			return wnd.ciuly_reply(forum, t, postContent);
		}
	}
	return wnd.ciuly_newTopic(forum, username, "", postContent);
  };

  wnd.ciuly_BanUser = function(username, userid, reason){
    if (confirm("Il banez pe "+username+" cu motivul "+reason+". Continuati?"))
    ;
    else
      return true;

    // commit the ban
    wnd.ciuly_progress_log("Banning user "+username);
    var url = "http://forum.softpedia.com/index.php?&act=warn&CODE=dowarn&mid="+userid+"&t=0&st=0&type=add";
    var data =  "key="+wnd.ipb_md5_check+
		"&level=add"+
		"&mod_indef=1"+
		"&mod_value=0"+
		"&mod_unit=d"+
		"&post_indef=1"+
		"&post_value=0"+
		"&post_unit=d"+
		"&susp_value=2147483647"+
		"&susp_unit=d"+
		"&reason="+reason+"\n** Banat **"+
		"&subject="+
		"&contactmethod=pm"+
		"&ed-0_wysiwyg_used=0"+
		"&editor_ids[]=ed-0"+
		"&contact=";
        ;

    if (wnd.ciuly_sp_post(url, data, false)){
      // post in dedicated area
      if (!wnd.ciuly_sp_post_bless(username, "267", reason, false)){
        alert("Could not save to dedicated forum.");
        return false;
      }
      return true;
    }

    alert("Could not commit the ban.");
    return false;
  }

  wnd.ciuly_moveTopic = function (tid, fid){
	if (!fid) 
		fid=prompt("ForumID", "");
	if (!fid)
		return false;
	return wnd.ciuly_sp_post( "http://forum.softpedia.com/index.php?", 
		"st=0"+
		"&act=mod"+
		"&s="+
		"&f="+wnd.ipb_input_f+
		"&selectedpids="+
		"&auth_key="+wnd.ipb_md5_check+
		"&CODE=14"+
		"&tid="+tid+
		"&sf="+wnd.ipb_input_f+
		"&move_id="+fid+
		"&leave=n"+
		"&submit=Move this topic",
	    false
	);
  };

  wnd.ciuly_deleteOnePost = function(forum, topic, post, ignoreFailure){
    wnd.ciuly_get_res("http://forum.softpedia.com/index.php?"+
		"act=Mod"+
		"&CODE=04"+
		"&f="+forum+
		"&t="+topic+
		"&p="+post+
		"&auth_key="+wnd.ipb_md5_check
	, ignoreFailure
	);
  }

  wnd.ciuly_deleteOneTopic = function(forum, topic, ignoreFailure){
	return wnd.ciuly_sp_post( "http://forum.softpedia.com/index.php?", 
		"t="+topic+
		"&f="+forum+
		"&st=0"+
		"&act=Mod"+
		"&CODE=08"+
		"&auth_key="+wnd.ipb_md5_check+
		"&submit=Go"
	, ignoreFailure
	);
  }

})(window.wrappedJSObject || window);