// ==UserScript==
// @name          SoftPedia Archive Personal Messages
// @author        Ciuly
// @namespace     http://www.ciuly.com
// @description   Archives all your personal messages
// @include       http://forum.softpedia.com/index.php?act=Msg&CODE=01
// @include       http://forum.softpedia.com/index.php?&act=Msg&CODE=01
// @include       http://forum.softpedia.com/index.php?act=Msg&CODE=01&*
// @include       http://forum.softpedia.com/index.php?&act=Msg&CODE=01&*
// @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_ArchivePMs = function(){
    wnd.ciuly_progress_show();
    wnd.ciuly_progress_log("I will start archiving your PMs. This will take a few minutes. Please wait and do not refresh this page until the operation finishes.");
    wnd.ciuly_ArchivePMsStep();
  }

  wnd.ciuly_ArchivePMsStep = function(){
      wnd.ciuly_progress_log("Trying to archive 50 more messages.");
      res = wnd.ciuly_sp_post_res("http://forum.softpedia.com/index.php?",
		"act=Msg"+
		"&CODE=15"+
		"&VID=all"+
		"&dateline=7"+
		"&oldnew=older"+
		"&number=50"+
		"&delete=yes"+
		"&type=xls"+
		"&submit=Process",
	       false
            );
      remaining=0;
      k=1;
      while (true){
        k=res.indexOf("VID=", k);
        if (k==-1)
          break;
        while (res.charAt(k)!=">")
	  k++;
        k++;
        total=""
        while (res.charAt(k)!="<"){
          if (res.charAt(k)==")")
	    break;
	  if (total.length>0)
	    total=total+res.charAt(k);
          if (res.charAt(k)=="("){
	    k++;
	    total=total+res.charAt(k);
	  }
	  k++;
        }
        if (total!="")
	  remaining = remaining + parseInt(total);
      };
      if (remaining<=50){
        wnd.ciuly_progress_hide();
        return;
      }
      remaining = remaining - 50;
      wnd.ciuly_progress_log("Done. "+remaining+" more messages left to archive.");
      wnd.ciuly_execLater("ciuly_ArchivePMsStepLater()", 10);
  }

  wnd.ciuly_ArchivePMsStepLater = function(){
    wnd.ciuly_ArchivePMsStep();
  }

  d = document.getElementById("ucpcontent");
  div = d.children.item(0);
  div.innerHTML = div.innerHTML + "&nbsp;<button class=\"button\" name=\"archive_all\" title=\"Archive all\" onclick=\"return ciuly_ArchivePMs()\">Archive All</button>&nbsp;";
})(window.wrappedJSObject || window);