// ==UserScript==
// @name          Experts-Exchange Why B Grade automatica question
// @author        Ciuly  
// @namespace     http://www.ciuly.com
// @description   Posts the question "Why B Grade" and link to grading tips.
// @include       http://www.experts-exchange.com/*/Q_*.html*
// @include       http://www.experts-exchange.com/*/Q-*.html*
// ==/UserScript==

var forms, form, uid, uname, div;
var is_opera = navigator.userAgent.toLowerCase().indexOf('opera') != -1;
var wnd;

if (is_opera)
  wnd = window;
else
  wnd = unsafeWindow;

  wnd.ciuly_post = function(url, data) {
	var request = new XMLHttpRequest();
	var error = 0;
	while (error!=-1){
		request.open("POST", url, false);
		request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		request.send(data);
	
		var error = request.responseText.indexOf("Sorry, an error occurred.");
		if (error!=-1){
			w = window.open('','');
			w. document.write(request.responseText);
			w.focus();
			if (confirm("Error posting to url. Try again?"))
				; 
			else
				return false;
		}
	}
	return true;
  };

  wnd.ciuly_get = function(url) {
	var request = new XMLHttpRequest();
	var error = 0;
	while (error!=-1){
		request.open("GET", url, false);
//		request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		request.send(null);
	
		var error = request.responseText.indexOf("Sorry, an error occurred.");
		if (error!=-1){
			w = window.open('','');
			w. document.write(request.responseText);
			w.focus();
			if (confirm("Error getting url. Try again?"))
				; 
			else
				return "";
		}
	}
	return request.responseText;
  };

  wnd.ciuly_PostComment = function(message){
    // change all crlf (no matter the case) into \n
    message = message.replace(/crlf/gi,"\n");
    message = message.replace(/</gi,"&lt;");
    message = message.replace(/>/gi,"&gt;");
    qid = "";
    omnitureData = "";

    forms = document.body.getElementsByTagName("form");
    for (var j=0; j<forms.length; j++){
      form = forms.item(j);
      if ( form.className == "formFactory nameValue" ){
        for (var l=0; l<form.elements.length; l++){
          if (form.elements[l].name == "qid")
            qid = form.elements[l].value;
          else
          if (form.elements[l].name == "omnitureData")
            omnitureData = form.elements[l].value;
        }
        break;
      }
    }

	return wnd.ciuly_post( "answerQuestion.jsp", 
		"answerType=10"+
		"&text="+encodeURIComponent(message)+
		"&codeSnippet="+
		"&extraNotification=0"+
		"&qid="+qid+
		"&omnitureData="+omnitureData+
		"&Submit=1"
	);
  }

  wnd.htmlDecode = function ( text ) {
    text = text.replace(/&quot;/gi, String.fromCharCode(34));
    text = text.replace(/&amp;/gi, String.fromCharCode(38));
    text = text.replace(/&lt;/gi, String.fromCharCode(60));
    text = text.replace(/&gt;/gi, String.fromCharCode(62));
    return text;
  }

  wnd.ciuly_why_b_grade = function(){
    if (!wnd.ciuly_PostComment("Why a B grade? see grading tips here: http://www.experts-exchange.com/help.jsp#hi147")){
      alert("Could not post comment.");
    } else 
      alert("Question posted.");
    return false;
  }


var asker = "";

spans = document.getElementsByTagName("span");
for (var i=0; i<spans.length; i++){
  span = spans.item(i);
  aas = span.getElementsByTagName("a");
  if (span.className == "value" && aas.length==1){
    asker = aas.item(0).innerHTML;
    break;
  }
}

if (asker.length>0){
  var divs = document.body.getElementsByTagName("div");
  for (var i=0; i<divs.length; i++){
    div = divs.item(i);
    if (div.className == "s shF5FF sgray expGray sectionTwo nestedSection postComment"){
      td = div.getElementsByTagName("table").item(0).getElementsByTagName("td").item(1).getElementsByTagName("span").item(0);
      td.innerHTML = td.innerHTML + "&nbsp;&nbsp;&nbsp;<button class=\"button\" name=\"blacklist\" title=\"Why B Grade\" onclick=\"return ciuly_why_b_grade()\">Why B Grade?</button>";
    }
  }
}