// Orkut ScrapEasy 0.5
// Author: Karthik Veeramani (karthikveeramani@gmail.com)
// Orkut ScrapEasy adds a couple of convenient links next to every scrap message on your orkut scrapbook.
// Using these, you can reply to the person directly without going to his/her scrapbook. You can also 
// launch the person's scrapbook directly without going through his profile.
// Read the usage at http://www.employees.org/~vkarthik/downloads/scrapeasy/scrapeasy.html
// 
// ==UserScript==
// @name          Orkut ScrapEasy
// @namespace     http://www.employees.org/~vkarthik/downloads/scrapeasy/
// @description   
// @include  http*://*orkut.com/Scrapbook.*
// ==/UserScript==

function sendForm(e) {
  buttonid = e.target.id;
  suf = buttonid.substring(buttonid.indexOf("Action.submit") + 13);
  uid = buttonid.substring(buttonid.indexOf("Action.submit") + 13, buttonid.indexOf("_"));
	  var url = "/Scrapbook.aspx?uid=" + uid;
	  sig = escape(document.getElementById("signature" + uid).value );
	  sig = sig.replace(/\+/g,"%2B");
	  postToken = escape(document.getElementById("POST_TOKEN" + uid).value);
	  scrap = escape(document.getElementById("scrapText" + suf).value);
	  scrap = scrap.replace(/\+/g,"%2B");
     	  str = "POST_TOKEN=" + postToken
  	  	  + "&signature=" + sig
  		  + "&Action.submit=" + document.getElementById("Action.submit" + suf).value
  		  + "&scrapText=" + scrap;
	  //alert(str);
	  var xmlReq = new XMLHttpRequest();
	  xmlReq.open("POST",url,true);
	  xmlReq.onreadystatechange = function() {
	    if(xmlReq.readyState == 4) {
	    	if(xmlReq.status != 200)
           	  alert("Encountered an error. Try scrapping the regular way. Error code is " + xmlReq.status + ".");
           	else 
           	  alert("Message posted.");
  	    }
	  };
          xmlReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
          xmlReq.setRequestHeader("Content-length", str.length);
          xmlReq.send(str);
}

function init() {
    var allElements, el;
    //myUid = "-1";

    var home = 0;
    if(document.evaluate(
	    "//a[contains(@href, 'Home.aspx')]",
	    document,
	    null,
	    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
	    null).snapshotLength == 2) { home = 1; }
	    

    allElements = document.evaluate(
	    "//a[@href | @class]",
	    document,
	    null,
	    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
	    null);

    postToken = document.evaluate(
	    "//input[@name=\'POST_TOKEN\']",
	    document,
	    null,
	    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
	    null);
    
    if(postToken.snapshotLength > 0) { postToken = postToken.snapshotItem(0).value; }


    signature = document.evaluate(
	    "//input[@name=\'signature\']",
	    document,
	    null,
	    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
	    null);

    if(signature.snapshotLength > 0) { signature = signature.snapshotItem(0).value; }	    

    for (var i = 0; i < allElements.snapshotLength-1; i++) {

	    el = allElements.snapshotItem(i);
	    href = el.href;
	    if(href.indexOf("Profile.aspx?uid=")) {

	    	var uid = href.substring(href.indexOf("uid=") + 4);
	    	//if(myUid == "-1") { myUid = uid; }
	    	var nxt = allElements.snapshotItem(i+1);

	    	if(home == 1 && nxt.innerHTML == "delete") {
	    	//if((el.innerHTML.indexOf("img src") >= 0) && (allElements.snapshotItem(i+1).href.indexOf("Profile.aspx?uid=") >= 0)) {

		    var replyLink = document.createElement("div");
		    var sb = document.createElement("div");
		    sb.id = "scrap_" + uid + "" + i;
      	            sb.style.width = "100%";
      		    sb.style.display = "none";
      		    sb.style.position = "relative";
      		    sb.style.float = "left";
      		    sb.style.padding = "10px";
       		    sb.style.align = "left";
       		    
       		    suf = uid + "_" + i;
       		    //alert(suf);
      		    sb.innerHTML = "<form id=\"daForm" + suf + "\" method=\"post\" action=\"javascript:void(0);\" autocomplete=\"off\">" + 
      			"<textarea id=\"scrapText" + suf + "\" name=\"scrapText\" cols=\"42\" rows=\"5\" onKeyUp=\"counterUpdate(\'scrapText\', \'countBody\', 1024);	\"></textarea>" +
      			"<input type=\"hidden\" name=\"POST_TOKEN\" id=\"POST_TOKEN" + uid + "\" value=\"" + postToken + "\" />" + 
      			"<input type=\"hidden\" name=\"signature\" id=\"signature" + uid + "\" value=\"" + signature + "\" />" + 
      			"<input type=\"image\" name=\"Action.submit\" id=\"Action.submit" + suf + "\" alt=\"submit\" title=\"submit\" border=\"0\"  src=\"http://images3.orkut.com/img/en-US/nb_submit.gif\" onmouseover=\"this.src=\'http://images3.orkut.com/img/en-US/ob_submit.gif\';\" onmouseout=\"this.src=\'http://images3.orkut.com/img/en-US/nb_submit.gif\';\" onload=\"var obtn_submit=new Image(); obtn_submit.src=\'http://images3.orkut.com/img/en-US/ob_submit.gif\';\"></form>";
      			
		    el.parentNode.parentNode.parentNode.insertBefore(sb, el.parentNode.parentNode.nextSibling);
      		    var elmLink = document.getElementById('Action.submit' + suf);
		    elmLink.addEventListener('click', sendForm, true);
		    idStr = "scrap_" + uid + "" + i;
		    id = document.getElementById(idStr);
		    str = "if(document.getElementById('" + idStr + "').style.display == 'none') { document.getElementById('" + idStr + "').style.display='inline'; } else { document.getElementById('" + idStr + "').style.display='none'; }";
		    
      		    replyLink.innerHTML = '<p><div id=reply' + uid + '><p><a href=\"javascript:void(0);\" onclick=\"' + str + '\">Reply</a></p>' + 
            			   '<p><a href=\"/Scrapbook.aspx?uid=' + uid + '\">Scrapbook</a></p></div></p>';
		
	            nxt.parentNode.insertBefore(replyLink, nxt);
      		    i++;
    		}
    	    }
    }
}

init();
